From 4e4028523f9fbe25c739b28f3403e05bdbedc023 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 1 Feb 2024 18:42:49 +0530 Subject: [PATCH 001/319] provide central init function, global config, global container --- zchain/init.go | 231 +++++++++++++++++++++++++++++++++++++++++ zcncore/init.go | 16 +++ zcncore/wallet_base.go | 14 +-- 3 files changed, 251 insertions(+), 10 deletions(-) create mode 100644 zchain/init.go create mode 100644 zcncore/init.go diff --git a/zchain/init.go b/zchain/init.go new file mode 100644 index 000000000..eb1edb4f6 --- /dev/null +++ b/zchain/init.go @@ -0,0 +1,231 @@ +package zchain + +import ( + "context" + "encoding/json" + "errors" + "math" + "net/http" + "sync" + "time" + + "github.com/0chain/gosdk/core/logger" + "github.com/0chain/gosdk/core/node" + "github.com/0chain/gosdk/core/util" + "go.uber.org/zap" +) + +type ChainConfig struct { + ChainID string + BlockWorker string + Miners []string + Sharders []string + SignatureScheme string + MinSubmit int + MinConfirmation int + ConfirmationChainLength int + EthNode string + SharderConsensous int + MaxTxnQuery int + QuerySleepTime int +} + +// Maintains SDK configuration. +// Initialized through [InitZChain] function. +type Config struct { + chain *ChainConfig + logLvl int +} + +func (c *Config) getMinMinersSubmit() int { + return util.MaxInt(1, int(math.Ceil(float64(c.chain.MinSubmit) * float64(len(c.chain.Miners)) / 100))) +} + +// Container to hold global data. +// Initialized through [InitZChain] function. +type GlobalContainer struct { + stableMiners []string + sharders *node.NodeHolder + config *Config + + mguard sync.RWMutex +} + +func (gc *GlobalContainer) GetStableMiners() []string { + gc.mguard.RLock() + defer gc.mguard.Unlock() + return gc.stableMiners +} + +func (gc *GlobalContainer) ResetStableMiners() { + gc.mguard.Lock() + defer gc.mguard.Unlock() + gc.stableMiners = util.GetRandom(gc.config.chain.Miners, gc.config.getMinMinersSubmit()) +} + +var ( + Gcontainer *GlobalContainer + logging logger.Logger +) + +type SignScheme string + +const ( + ED25519 SignScheme = "ed25519" + BLS0CHAIN SignScheme = "bls0chain" +) + +type OptionKey int + +const ( + ChainId OptionKey = iota + MinSubmit + MinConfirmation + ConfirmationChainLength + EthNode + SharderConsensous + + LoggingLevel +) + +// default options value +const ( + defaultMinSubmit = int(10) + defaultMinConfirmation = int(10) + defaultConfirmationChainLength = int(3) + defaultMaxTxnQuery = int(5) + defaultQuerySleepTime = int(5) + defaultSharderConsensous = int(3) + defaultLogLevel = logger.DEBUG +) + +func init() { + logging.Init(logger.DEBUG, "0chain-config") +} + +func InitZChain(ctx context.Context, blockWorker string, signscheme SignScheme, options map[OptionKey]interface{}) error { + // get miners, sharders + miners, sharders, err := getNetworkDetails(ctx, blockWorker) + if err != nil { + logging.Error("Failed to get network details ", zap.Error(err)) + return err + } + + // init config + config := &Config{ + chain: &ChainConfig{ + BlockWorker: blockWorker, + SignatureScheme: string(signscheme), + Miners: miners, + Sharders: sharders, + MinSubmit: defaultMinSubmit, + MinConfirmation: defaultMinConfirmation, + ConfirmationChainLength: defaultConfirmationChainLength, + MaxTxnQuery: defaultMaxTxnQuery, + QuerySleepTime: defaultQuerySleepTime, + SharderConsensous: util.MinInt(defaultSharderConsensous, len(sharders)), + }, + logLvl: defaultLogLevel, + } + + // override default values + for optionKey, optionValue := range options { + switch optionKey { + case ChainId: + chainId, isTypeString := optionValue.(string) + if !isTypeString { + return errors.New("option ChainId is not of string type") + } + config.chain.ChainID = chainId + case MinSubmit: + minSubmit, isTypeInt := optionValue.(int) + if !isTypeInt { + return errors.New("option MinSubmit is not of int type") + } + config.chain.MinSubmit = minSubmit + case MinConfirmation: + minConfirmation, isTypeInt := optionValue.(int) + if !isTypeInt { + return errors.New("option MinConfirmation is not of int type") + } + config.chain.MinConfirmation = minConfirmation + case ConfirmationChainLength: + confirmationChainLength, isTypeInt := optionValue.(int) + if !isTypeInt { + return errors.New("option ConfirmationChainLength is not of int type") + } + config.chain.ConfirmationChainLength = confirmationChainLength + case EthNode: + ethNode, isTypeString := optionValue.(string) + if !isTypeString { + return errors.New("option EthNode is not of string type") + } + config.chain.EthNode = ethNode + case SharderConsensous: + sharderConsensous, isTypeInt := optionValue.(int) + if !isTypeInt { + return errors.New("option SharderConsensous is not of int type") + } + config.chain.SharderConsensous = sharderConsensous + case LoggingLevel: + loggingLevel, isTypeInt := optionValue.(int) + if !isTypeInt { + return errors.New("option LoggingLevel is not of int type") + } + logging.SetLevel(loggingLevel) + } + } + + // init GlobalContainer + Gcontainer = &GlobalContainer { + stableMiners: util.GetRandom(miners, config.getMinMinersSubmit()), + sharders: node.NewHolder(config.chain.Sharders, config.chain.SharderConsensous), + config: config, + } + + // update miners, sharders periodically + go func() { + ticker := time.NewTicker(time.Duration(1) * time.Hour) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + + } + } + }() + + // init modules + node.InitCache(Gcontainer.sharders) + return nil +} + +func getNetworkDetails(ctx context.Context, blockWorker string) ([]string, []string, error) { + networkUrl := blockWorker + "/network" + networkGetCtx, networkGetCancelCtx := context.WithTimeoutCause(ctx, 60 * time.Second, errors.New("timeout connecting network: " + networkUrl)) + defer networkGetCancelCtx() + req, err := util.NewHTTPGetRequestContext(networkGetCtx, networkUrl) + if err != nil { + return nil, nil, errors.New("Unable to create new http request with error: " + err.Error()) + } + res, err := req.Get() + if err != nil { + return nil, nil, errors.New("Unable to get http request with error: " + err.Error()) + } + if res.StatusCode != http.StatusOK { + return nil, nil, errors.New("Unable to get http request with status Ok: " + res.Status) + } + type responseBody struct { + Miners []string `json:"miners"` + Sharders []string `json:"sharders"` + } + var respBody responseBody + err = json.Unmarshal([]byte(res.Body), &respBody) + if err != nil { + return nil, nil, errors.New("Error unmarshaling response :" + res.Body) + } + return respBody.Miners, respBody.Sharders, nil + +} \ No newline at end of file diff --git a/zcncore/init.go b/zcncore/init.go new file mode 100644 index 000000000..081c0779e --- /dev/null +++ b/zcncore/init.go @@ -0,0 +1,16 @@ +package zcncore + +import ( + "sync" + + "github.com/0chain/gosdk/core/logger" +) + +// Singleton +// TODO: Remove these variable and Use zchain.Gcontainer +var ( + _config localConfig + logging logger.Logger + stableMiners []string + mGuard sync.Mutex +) \ No newline at end of file diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index afb85aa98..aa6423a5e 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -132,7 +132,6 @@ const ( ) var defaultLogLevel = logger.DEBUG -var logging logger.Logger func GetLogger() *logger.Logger { return &logging @@ -222,11 +221,6 @@ type AuthCallback interface { OnSetupComplete(status int, err string) } -// Singleton -var _config localConfig -var miners []string -var mGuard sync.Mutex - func init() { logging.Init(defaultLogLevel, "0chain-core-sdk") } @@ -234,16 +228,16 @@ func init() { func GetStableMiners() []string { mGuard.Lock() defer mGuard.Unlock() - if len(miners) == 0 { - miners = util.GetRandom(_config.chain.Miners, getMinMinersSubmit()) + if len(stableMiners) == 0 { + stableMiners = util.GetRandom(_config.chain.Miners, getMinMinersSubmit()) } - return miners + return stableMiners } func ResetStableMiners() { mGuard.Lock() defer mGuard.Unlock() - miners = util.GetRandom(_config.chain.Miners, getMinMinersSubmit()) + stableMiners = util.GetRandom(_config.chain.Miners, getMinMinersSubmit()) } func checkSdkInit() error { From 2c666493a650f4d295da8b3cae3769ba9443a3aa Mon Sep 17 00:00:00 2001 From: storybehind Date: Thu, 8 Feb 2024 16:57:06 +0530 Subject: [PATCH 002/319] refactor zcncore package to use states in core package --- constants/signscheme.go | 13 ++ core/client/init_node.go | 226 ++++++++++++++++++++++++ core/client/set.go | 79 +++++++++ core/conf/config.go | 5 + core/conf/network.go | 41 ++++- core/conf/vars.go | 56 +++--- core/node/cache.go | 7 + winsdk/sdk.go | 45 ++--- zboxcore/sdk/networkworker.go | 9 +- zchain/init.go | 231 ------------------------- zcnbridge/http/rest.go | 7 +- zcncore/ethwallet_base.go | 16 +- zcncore/ethwallet_base_test.go | 93 ++++++++-- zcncore/init.go | 16 -- zcncore/mswallet.go | 7 +- zcncore/mswallet_base.go | 18 +- zcncore/networkworker.go | 296 ++++++++++++++++---------------- zcncore/sample/snapshot_test.go | 6 +- zcncore/transaction.go | 184 +++++++++++++------- zcncore/transaction_base.go | 198 ++++++++++++--------- zcncore/transaction_query.go | 25 ++- zcncore/transactionauth_base.go | 21 ++- zcncore/wallet.go | 10 +- zcncore/wallet_base.go | 261 ++++++++++++---------------- zmagmacore/http/sc-api.go | 10 +- zmagmacore/wallet/setup.go | 9 +- 26 files changed, 1076 insertions(+), 813 deletions(-) create mode 100644 constants/signscheme.go create mode 100644 core/client/init_node.go create mode 100644 core/client/set.go delete mode 100644 zchain/init.go delete mode 100644 zcncore/init.go diff --git a/constants/signscheme.go b/constants/signscheme.go new file mode 100644 index 000000000..f8d918751 --- /dev/null +++ b/constants/signscheme.go @@ -0,0 +1,13 @@ +package constants + +type SignScheme string + +const ( + ED25519 SignScheme = "ed25519" + BLS0CHAIN SignScheme = "bls0chain" +) + +func (s SignScheme) String() string { + return string(s) +} + diff --git a/core/client/init_node.go b/core/client/init_node.go new file mode 100644 index 000000000..73d9e6fd9 --- /dev/null +++ b/core/client/init_node.go @@ -0,0 +1,226 @@ +package client + +import ( + "context" + "encoding/json" + "errors" + "math" + "net/http" + "reflect" + "sync" + "time" + + "github.com/0chain/gosdk/core/conf" + "github.com/0chain/gosdk/core/logger" + "github.com/0chain/gosdk/core/node" + "github.com/0chain/gosdk/core/util" + "go.uber.org/zap" +) + +var ( + logging logger.Logger + nodeClient *Node +) + +func init() { + logging.Init(logger.DEBUG, "0chain-core") +} + +// Container to hold global state. +// Initialized through [Init] function. +type Node struct { + stableMiners []string + sharders *node.NodeHolder + // config *conf.Config + network *conf.Network + // nonceCache *node.NonceCache + clientCtx context.Context + + networkGuard sync.RWMutex +} + +func (n *Node) GetStableMiners() []string { + n.networkGuard.RLock() + defer n.networkGuard.RUnlock() + return n.stableMiners +} + +func (n *Node) ResetStableMiners() { + n.networkGuard.Lock() + defer n.networkGuard.Unlock() + cfg, _ := conf.GetClientConfig() + reqMiners := util.MaxInt(1, int(math.Ceil(float64(cfg.MinSubmit)*float64(len(n.network.Miners()))/100))) + n.stableMiners = util.GetRandom(n.network.Miners(), reqMiners) +} + +func (n *Node) GetMinShardersVerify() int { + n.networkGuard.RLock() + defer n.networkGuard.RUnlock() + cfg, _ := conf.GetClientConfig() + minSharders := util.MaxInt(1, int(math.Ceil(float64(cfg.MinConfirmation)*float64(len(n.sharders.Healthy()))/100))) + logging.Info("Minimum sharders used for verify :", minSharders) + return minSharders +} + +func (n *Node) Sharders() *node.NodeHolder { + n.networkGuard.RLock() + defer n.networkGuard.RUnlock() + return n.sharders +} + +// func (n *NodeClient) Config() *conf.Config { +// return n.config +// } + +func (n *Node) Network() *conf.Network { + n.networkGuard.RLock() + defer n.networkGuard.RUnlock() + return n.network +} + +func (n *Node) ShouldUpdateNetwork() (bool, *conf.Network, error) { + cfg, _ := conf.GetClientConfig() + network, err := getNetwork(n.clientCtx, cfg.BlockWorker) + if err != nil { + logging.Error("Failed to get network details ", zap.Error(err)) + return false, nil, err + } + n.networkGuard.RLock() + defer n.networkGuard.RUnlock() + if reflect.DeepEqual(network, n.network) { + return false, network, nil + } + return true, network, nil +} + +func (n *Node) UpdateNetwork(network *conf.Network) error { + n.networkGuard.Lock() + defer n.networkGuard.Unlock() + cfg, _ := conf.GetClientConfig() + n.network = network + n.sharders = node.NewHolder(n.network.Sharders(), util.MinInt(len(n.network.Sharders()), util.MaxInt(cfg.SharderConsensous, conf.DefaultSharderConsensous))) + node.InitCache(n.sharders) + return nil +} + +// func (n *Node) NonceCache() *node.NonceCache { +// n.networkGuard.RLock() +// defer n.networkGuard.RUnlock() +// return n.nonceCache +// } + +func Init(ctx context.Context, cfg conf.Config) error { + // validate + err := validate(&cfg) + if err != nil { + return err + } + + // set default value for options if unset + setOptionsDefaultValue(&cfg) + + network, err := getNetwork(ctx, cfg.BlockWorker) + if err != nil { + logging.Error("Failed to get network details ", zap.Error(err)) + return err + } + + reqMiners := util.MaxInt(1, int(math.Ceil(float64(cfg.MinSubmit)*float64(len(network.Miners()))/100))) + sharders := node.NewHolder(network.Sharders(), util.MinInt(len(network.Sharders()), util.MaxInt(cfg.SharderConsensous, conf.DefaultSharderConsensous))) + nodeClient = &Node{ + stableMiners: util.GetRandom(network.Miners(), reqMiners), + sharders: sharders, + // config: &cfg, + network: network, + // nonceCache: node.NewNonceCache(sharders), + clientCtx: ctx, + } + + //init packages + conf.InitClientConfig(&cfg) + + // update Network periodically + go func() { + ticker := time.NewTicker(time.Duration(1) * time.Hour) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + shouldUpdate, network, err := nodeClient.ShouldUpdateNetwork() + if err != nil { + logging.Error("error on ShouldUpdateNetwork check: ", err) + continue + } + if shouldUpdate { + nodeClient.UpdateNetwork(network) + } + } + } + }() + + return nil +} + +func GetNode() (*Node, error) { + if nodeClient != nil { + return nodeClient, nil + } + return nil, errors.New("0chain-sdk is not initialized") +} + +func getNetwork(ctx context.Context, blockWorker string) (*conf.Network, error) { + networkUrl := blockWorker + "/network" + networkGetCtx, networkGetCancelCtx := context.WithTimeout(ctx, 60*time.Second) + defer networkGetCancelCtx() + req, err := util.NewHTTPGetRequestContext(networkGetCtx, networkUrl) + if err != nil { + return nil, errors.New("Unable to create new http request with error: " + err.Error()) + } + res, err := req.Get() + if err != nil { + return nil, errors.New("Unable to get http request with error: " + err.Error()) + } + if res.StatusCode != http.StatusOK { + return nil, errors.New("Unable to get http request with status Ok: " + res.Status) + } + type network struct { + Miners []string `json:"miners"` + Sharders []string `json:"sharders"` + } + var n network + err = json.Unmarshal([]byte(res.Body), &n) + if err != nil { + return nil, errors.New("Error unmarshaling response :" + res.Body) + } + return conf.NewNetwork(n.Miners, n.Sharders) +} + +func validate(cfg *conf.Config) error { + if cfg.BlockWorker == "" { + return errors.New("chain BlockWorker can't be empty") + } + return nil +} + +func setOptionsDefaultValue(cfg *conf.Config) { + if cfg.MinSubmit <= 0 { + cfg.MinSubmit = conf.DefaultMinSubmit + } + if cfg.MinConfirmation <= 0 { + cfg.MinConfirmation = conf.DefaultMinConfirmation + } + if cfg.ConfirmationChainLength <= 0 { + cfg.ConfirmationChainLength = conf.DefaultConfirmationChainLength + } + if cfg.MaxTxnQuery <= 0 { + cfg.MaxTxnQuery = conf.DefaultMaxTxnQuery + } + if cfg.QuerySleepTime <= 0 { + cfg.QuerySleepTime = conf.DefaultMaxTxnQuery + } + if cfg.SharderConsensous <= 0 { + cfg.SharderConsensous = conf.DefaultSharderConsensous + } +} diff --git a/core/client/set.go b/core/client/set.go new file mode 100644 index 000000000..0451420a4 --- /dev/null +++ b/core/client/set.go @@ -0,0 +1,79 @@ +package client + +import ( + "errors" + "strings" + + "github.com/0chain/gosdk/constants" + "github.com/0chain/gosdk/core/conf" + "github.com/0chain/gosdk/core/zcncrypto" +) + +var ( + wallet *zcncrypto.Wallet + splitKeyWallet bool + authUrl string + nonce int64 + fee uint64 +) + +func SetWallet(w zcncrypto.Wallet) error { + wallet = &w + return nil +} + +func SetSplitKeyWallet(isSplitKeyWallet bool) error { + cfg, err := conf.GetClientConfig() + if err != nil { + return err + } + if cfg.SignatureScheme == constants.BLS0CHAIN.String() { + splitKeyWallet = isSplitKeyWallet + } + return nil +} + +func SetAuthUrl(url string) error { + if !splitKeyWallet { + return errors.New("wallet type is not split key") + } + if url == "" { + return errors.New("invalid auth url") + } + authUrl = strings.TrimRight(url, "/") + return nil +} + +func SetNonce(n int64) error { + nonce = n + return nil +} + +func SetFee(f uint64) error { + fee = f + return nil +} + +func Wallet() *zcncrypto.Wallet { + return wallet +} + +func SplitKeyWallet() bool { + return splitKeyWallet +} + +func AuthUrl() string { + return authUrl +} + +func Nonce() int64 { + return nonce +} + +func Fee() uint64 { + return fee +} + +func IsWalletSet() bool { + return wallet == nil || wallet.ClientID != "" +} diff --git a/core/conf/config.go b/core/conf/config.go index 5b770768e..387fd388d 100644 --- a/core/conf/config.go +++ b/core/conf/config.go @@ -74,6 +74,11 @@ type Config struct { ZboxAppType string `json:"zbox_app_type"` // SharderConsensous is consensous for when quering for SCRestAPI calls SharderConsensous int `json:"sharder_consensous"` + + // Wallet zcncrypto.Wallet `json:"wallet"` + // SplitKeyWallet bool `json:"split_key_wallet"` + // //the url of zAuth server + // AuthUrl string `json:"auth_url"` } // LoadConfigFile load and parse Config from file diff --git a/core/conf/network.go b/core/conf/network.go index c2862c2bb..bddcc060c 100644 --- a/core/conf/network.go +++ b/core/conf/network.go @@ -3,6 +3,7 @@ package conf import ( "errors" "os" + "strings" thrown "github.com/0chain/errors" "github.com/0chain/gosdk/core/sys" @@ -12,14 +13,44 @@ import ( // Network settings from ~/.zcn/network.yaml type Network struct { // Sharders sharder list of blockchain - Sharders []string + sharders []string // Miners miner list of blockchain - Miners []string + miners []string +} + +func NewNetwork(miners, sharders []string) (*Network, error) { + n := &Network{ + miners: miners, + sharders: sharders, + } + if !n.IsValid() { + return nil, errors.New("network has no miners/sharders") + } + n.NormalizeURLs() + return n, nil } // IsValid check network if it has miners and sharders func (n *Network) IsValid() bool { - return n != nil && len(n.Miners) > 0 && len(n.Sharders) > 0 + return n != nil && len(n.miners) > 0 && len(n.sharders) > 0 +} + +func (n *Network) Miners() []string { + return n.miners +} + +func (n *Network) Sharders() []string { + return n.sharders +} + +func (n *Network) NormalizeURLs() { + for i := 0; i < len(n.miners); i++ { + n.miners[i] = strings.TrimSuffix(n.miners[i], "/") + } + + for i := 0; i < len(n.sharders); i++ { + n.sharders[i] = strings.TrimSuffix(n.sharders[i], "/") + } } // LoadNetworkFile load and parse Network from file @@ -51,7 +82,7 @@ func LoadNetworkFile(file string) (Network, error) { // LoadNetwork load and parse network func LoadNetwork(v Reader) Network { return Network{ - Sharders: v.GetStringSlice("sharders"), - Miners: v.GetStringSlice("miners"), + sharders: v.GetStringSlice("sharders"), + miners: v.GetStringSlice("miners"), } } diff --git a/core/conf/vars.go b/core/conf/vars.go index 508247e99..077f0ea7c 100644 --- a/core/conf/vars.go +++ b/core/conf/vars.go @@ -2,7 +2,6 @@ package conf import ( "errors" - "strings" "sync" ) @@ -11,7 +10,7 @@ var ( cfg *Config onceCfg sync.Once // global sharders and miners - network *Network + // network *Network ) var ( @@ -41,42 +40,37 @@ func GetClientConfig() (*Config, error) { // InitClientConfig set global client config func InitClientConfig(c *Config) { onceCfg.Do(func() { - sharderConsensous := c.SharderConsensous - if sharderConsensous < 1 { - sharderConsensous = DefaultSharderConsensous - } cfg = c - cfg.SharderConsensous = sharderConsensous }) } -// InitChainNetwork set global chain network -func InitChainNetwork(n *Network) { - if n == nil { - return - } +// // InitChainNetwork set global chain network +// func InitChainNetwork(n *Network) { +// if n == nil { +// return +// } - normalizeURLs(n) +// normalizeURLs(n) - if network == nil { - network = n - return - } +// if network == nil { +// network = n +// return +// } - network.Sharders = n.Sharders - network.Miners = n.Miners -} +// network.Sharders = n.Sharders +// network.Miners = n.Miners +// } -func normalizeURLs(network *Network) { - if network == nil { - return - } +// func normalizeURLs(network *Network) { +// if network == nil { +// return +// } - for i := 0; i < len(network.Miners); i++ { - network.Miners[i] = strings.TrimSuffix(network.Miners[i], "/") - } +// for i := 0; i < len(network.Miners); i++ { +// network.Miners[i] = strings.TrimSuffix(network.Miners[i], "/") +// } - for i := 0; i < len(network.Sharders); i++ { - network.Sharders[i] = strings.TrimSuffix(network.Sharders[i], "/") - } -} +// for i := 0; i < len(network.Sharders); i++ { +// network.Sharders[i] = strings.TrimSuffix(network.Sharders[i], "/") +// } +// } diff --git a/core/node/cache.go b/core/node/cache.go index 3e19d424d..4ed8e229f 100644 --- a/core/node/cache.go +++ b/core/node/cache.go @@ -25,6 +25,13 @@ func init() { }) } +// func NewNonceCache(sharders *NodeHolder) *NonceCache { +// return &NonceCache{ +// cache: make(map[string]int64), +// sharders: sharders, +// } +// } + func (nc *NonceCache) GetNextNonce(clientId string) int64 { nc.guard.Lock() defer nc.guard.Unlock() diff --git a/winsdk/sdk.go b/winsdk/sdk.go index 667da05a3..47f586487 100644 --- a/winsdk/sdk.go +++ b/winsdk/sdk.go @@ -8,17 +8,19 @@ import ( ) import ( + "context" "encoding/json" "errors" "os" "github.com/0chain/gosdk/zboxapi" - "github.com/0chain/gosdk/zboxcore/client" + // "github.com/0chain/gosdk/zboxcore/client" l "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/0chain/gosdk/zcncore" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/logger" @@ -105,18 +107,8 @@ func InitSDKs(configJson *C.char) *C.char { return WithJSON(false, err) } - err = zcncore.InitZCNSDK(configObj.BlockWorker, configObj.SignatureScheme, func(cc *zcncore.ChainConfig) error { - cc.BlockWorker = configObj.BlockWorker - cc.ChainID = configObj.ChainID - cc.ConfirmationChainLength = configObj.ConfirmationChainLength - cc.MinConfirmation = configObj.MinConfirmation - cc.EthNode = configObj.EthereumNode - cc.MinSubmit = configObj.MinSubmit - cc.SharderConsensous = configObj.SharderConsensous - cc.SignatureScheme = configObj.SignatureScheme - - return nil - }) + err = client.Init(context.Background(), *configObj) + if err != nil { l.Logger.Error(err, configJs) return WithJSON(false, err) @@ -161,32 +153,21 @@ func InitWallet(clientJson *C.char) *C.char { log.Error("win: crash ", r) } }() - l.Logger.Info("Start InitStorageSDK") + l.Logger.Info("Start InitWallet") clientJs := C.GoString(clientJson) - - configObj, err := conf.GetClientConfig() + + var w zcncrypto.Wallet + err := json.Unmarshal([]byte(clientJs), &w) if err != nil { l.Logger.Error(err) return WithJSON(false, err) } + client.SetWallet(w) - err = sdk.InitStorageSDK(clientJs, configObj.BlockWorker, configObj.ChainID, configObj.SignatureScheme, configObj.PreferredBlobbers, 0) - if err != nil { - l.Logger.Error(err, clientJs) - return WithJSON(false, err) - } - l.Logger.Info("InitStorageSDK success") - - c := client.GetClient() - if c != nil { - zboxApiClient.SetWallet(client.GetClientID(), client.GetClientPrivateKey(), client.GetClientPublicKey()) - l.Logger.Info("InitZboxApiClient success") - } else { - l.Logger.Info("InitZboxApiClient skipped") - } - - l.Logger.Info("InitSDKs successful") + l.Logger.Info("InitWallet success") + zboxApiClient.SetWallet(client.Wallet().ClientID, client.Wallet().Keys[0].PrivateKey, client.Wallet().ClientKey) + l.Logger.Info("InitZboxApiClient success") return WithJSON(true, nil) } diff --git a/zboxcore/sdk/networkworker.go b/zboxcore/sdk/networkworker.go index 276fca806..44d3c188f 100644 --- a/zboxcore/sdk/networkworker.go +++ b/zboxcore/sdk/networkworker.go @@ -14,7 +14,6 @@ import ( "go.uber.org/zap" "github.com/0chain/errors" - "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/zboxcore/blockchain" "github.com/0chain/gosdk/zboxcore/zboxutil" ) @@ -74,10 +73,10 @@ func forceUpdateNetworkDetails(networkDetails *Network) { blockchain.SetMiners(networkDetails.Miners) blockchain.SetSharders(networkDetails.Sharders) node.InitCache(blockchain.Sharders) - conf.InitChainNetwork(&conf.Network{ - Sharders: networkDetails.Sharders, - Miners: networkDetails.Miners, - }) + // conf.InitChainNetwork(&conf.Network{ + // Sharders: networkDetails.Sharders, + // Miners: networkDetails.Miners, + // }) sdkInitialized = true } diff --git a/zchain/init.go b/zchain/init.go deleted file mode 100644 index eb1edb4f6..000000000 --- a/zchain/init.go +++ /dev/null @@ -1,231 +0,0 @@ -package zchain - -import ( - "context" - "encoding/json" - "errors" - "math" - "net/http" - "sync" - "time" - - "github.com/0chain/gosdk/core/logger" - "github.com/0chain/gosdk/core/node" - "github.com/0chain/gosdk/core/util" - "go.uber.org/zap" -) - -type ChainConfig struct { - ChainID string - BlockWorker string - Miners []string - Sharders []string - SignatureScheme string - MinSubmit int - MinConfirmation int - ConfirmationChainLength int - EthNode string - SharderConsensous int - MaxTxnQuery int - QuerySleepTime int -} - -// Maintains SDK configuration. -// Initialized through [InitZChain] function. -type Config struct { - chain *ChainConfig - logLvl int -} - -func (c *Config) getMinMinersSubmit() int { - return util.MaxInt(1, int(math.Ceil(float64(c.chain.MinSubmit) * float64(len(c.chain.Miners)) / 100))) -} - -// Container to hold global data. -// Initialized through [InitZChain] function. -type GlobalContainer struct { - stableMiners []string - sharders *node.NodeHolder - config *Config - - mguard sync.RWMutex -} - -func (gc *GlobalContainer) GetStableMiners() []string { - gc.mguard.RLock() - defer gc.mguard.Unlock() - return gc.stableMiners -} - -func (gc *GlobalContainer) ResetStableMiners() { - gc.mguard.Lock() - defer gc.mguard.Unlock() - gc.stableMiners = util.GetRandom(gc.config.chain.Miners, gc.config.getMinMinersSubmit()) -} - -var ( - Gcontainer *GlobalContainer - logging logger.Logger -) - -type SignScheme string - -const ( - ED25519 SignScheme = "ed25519" - BLS0CHAIN SignScheme = "bls0chain" -) - -type OptionKey int - -const ( - ChainId OptionKey = iota - MinSubmit - MinConfirmation - ConfirmationChainLength - EthNode - SharderConsensous - - LoggingLevel -) - -// default options value -const ( - defaultMinSubmit = int(10) - defaultMinConfirmation = int(10) - defaultConfirmationChainLength = int(3) - defaultMaxTxnQuery = int(5) - defaultQuerySleepTime = int(5) - defaultSharderConsensous = int(3) - defaultLogLevel = logger.DEBUG -) - -func init() { - logging.Init(logger.DEBUG, "0chain-config") -} - -func InitZChain(ctx context.Context, blockWorker string, signscheme SignScheme, options map[OptionKey]interface{}) error { - // get miners, sharders - miners, sharders, err := getNetworkDetails(ctx, blockWorker) - if err != nil { - logging.Error("Failed to get network details ", zap.Error(err)) - return err - } - - // init config - config := &Config{ - chain: &ChainConfig{ - BlockWorker: blockWorker, - SignatureScheme: string(signscheme), - Miners: miners, - Sharders: sharders, - MinSubmit: defaultMinSubmit, - MinConfirmation: defaultMinConfirmation, - ConfirmationChainLength: defaultConfirmationChainLength, - MaxTxnQuery: defaultMaxTxnQuery, - QuerySleepTime: defaultQuerySleepTime, - SharderConsensous: util.MinInt(defaultSharderConsensous, len(sharders)), - }, - logLvl: defaultLogLevel, - } - - // override default values - for optionKey, optionValue := range options { - switch optionKey { - case ChainId: - chainId, isTypeString := optionValue.(string) - if !isTypeString { - return errors.New("option ChainId is not of string type") - } - config.chain.ChainID = chainId - case MinSubmit: - minSubmit, isTypeInt := optionValue.(int) - if !isTypeInt { - return errors.New("option MinSubmit is not of int type") - } - config.chain.MinSubmit = minSubmit - case MinConfirmation: - minConfirmation, isTypeInt := optionValue.(int) - if !isTypeInt { - return errors.New("option MinConfirmation is not of int type") - } - config.chain.MinConfirmation = minConfirmation - case ConfirmationChainLength: - confirmationChainLength, isTypeInt := optionValue.(int) - if !isTypeInt { - return errors.New("option ConfirmationChainLength is not of int type") - } - config.chain.ConfirmationChainLength = confirmationChainLength - case EthNode: - ethNode, isTypeString := optionValue.(string) - if !isTypeString { - return errors.New("option EthNode is not of string type") - } - config.chain.EthNode = ethNode - case SharderConsensous: - sharderConsensous, isTypeInt := optionValue.(int) - if !isTypeInt { - return errors.New("option SharderConsensous is not of int type") - } - config.chain.SharderConsensous = sharderConsensous - case LoggingLevel: - loggingLevel, isTypeInt := optionValue.(int) - if !isTypeInt { - return errors.New("option LoggingLevel is not of int type") - } - logging.SetLevel(loggingLevel) - } - } - - // init GlobalContainer - Gcontainer = &GlobalContainer { - stableMiners: util.GetRandom(miners, config.getMinMinersSubmit()), - sharders: node.NewHolder(config.chain.Sharders, config.chain.SharderConsensous), - config: config, - } - - // update miners, sharders periodically - go func() { - ticker := time.NewTicker(time.Duration(1) * time.Hour) - defer ticker.Stop() - for { - select { - case <-ctx.Done(): - return - case <-ticker.C: - - } - } - }() - - // init modules - node.InitCache(Gcontainer.sharders) - return nil -} - -func getNetworkDetails(ctx context.Context, blockWorker string) ([]string, []string, error) { - networkUrl := blockWorker + "/network" - networkGetCtx, networkGetCancelCtx := context.WithTimeoutCause(ctx, 60 * time.Second, errors.New("timeout connecting network: " + networkUrl)) - defer networkGetCancelCtx() - req, err := util.NewHTTPGetRequestContext(networkGetCtx, networkUrl) - if err != nil { - return nil, nil, errors.New("Unable to create new http request with error: " + err.Error()) - } - res, err := req.Get() - if err != nil { - return nil, nil, errors.New("Unable to get http request with error: " + err.Error()) - } - if res.StatusCode != http.StatusOK { - return nil, nil, errors.New("Unable to get http request with status Ok: " + res.Status) - } - type responseBody struct { - Miners []string `json:"miners"` - Sharders []string `json:"sharders"` - } - var respBody responseBody - err = json.Unmarshal([]byte(res.Body), &respBody) - if err != nil { - return nil, nil, errors.New("Error unmarshaling response :" + res.Body) - } - return respBody.Miners, respBody.Sharders, nil - -} \ No newline at end of file diff --git a/zcnbridge/http/rest.go b/zcnbridge/http/rest.go index 87bfee4f9..091fb3222 100644 --- a/zcnbridge/http/rest.go +++ b/zcnbridge/http/rest.go @@ -13,6 +13,7 @@ import ( "go.uber.org/zap" "gopkg.in/natefinch/lumberjack.v2" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/core/util" "github.com/0chain/gosdk/zcnbridge/errors" @@ -168,7 +169,11 @@ func hashAndBytesOfReader(r io.Reader) (string, []byte, error) { // extractSharders returns string slice of randomly ordered sharders existing in the current network. func extractSharders() []string { - sharders := zcncore.Sharders.Healthy() + nodeClient, err := client.GetNode() + if err != nil { + panic(err) + } + sharders := nodeClient.Network().Sharders() return util.GetRandom(sharders, len(sharders)) } diff --git a/zcncore/ethwallet_base.go b/zcncore/ethwallet_base.go index f53aaa9c5..e0cdac21a 100644 --- a/zcncore/ethwallet_base.go +++ b/zcncore/ethwallet_base.go @@ -11,6 +11,7 @@ import ( "regexp" "sync" + "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/tokenrate" "github.com/0chain/gosdk/core/zcncrypto" hdwallet "github.com/0chain/gosdk/zcncore/ethhdwallet" @@ -35,14 +36,18 @@ var ethClient *ethclient.Client var getEthClient = func() (*ethclient.Client, error) { var err error + cfg, err := conf.GetClientConfig() + if err != nil { + return nil, err + } once.Do(func() { - if len(_config.chain.EthNode) == 0 { + if len(cfg.EthereumNode) == 0 { err = fmt.Errorf("eth node SDK not initialized") return } - logging.Info("requesting from ", _config.chain.EthNode) - ethClient, err = ethclient.Dial(_config.chain.EthNode) + logging.Info("requesting from ", cfg.EthereumNode) + ethClient, err = ethclient.Dial(cfg.EthereumNode) }) return ethClient, err @@ -158,11 +163,12 @@ func isValidEthAddress(ethAddr string, client *ethclient.Client) (bool, error) { // CreateWalletFromEthMnemonic - creating new wallet from Eth mnemonics func CreateWalletFromEthMnemonic(mnemonic, password string, statusCb WalletCallback) error { - if len(_config.chain.Miners) < 1 || len(_config.chain.Sharders) < 1 { + cfg, err := conf.GetClientConfig() + if err != nil { return fmt.Errorf("SDK not initialized") } go func() { - sigScheme := zcncrypto.NewSignatureScheme(_config.chain.SignatureScheme) + sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) _, err := sigScheme.GenerateKeysWithEth(mnemonic, password) if err != nil { statusCb.OnWalletCreateComplete(StatusError, "", err.Error()) diff --git a/zcncore/ethwallet_base_test.go b/zcncore/ethwallet_base_test.go index 7c5925607..ebb4c01e4 100644 --- a/zcncore/ethwallet_base_test.go +++ b/zcncore/ethwallet_base_test.go @@ -8,7 +8,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus/ethash" - "github.com/ethereum/go-ethereum/core" + ethCore "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" @@ -50,7 +50,7 @@ func TestTokensConversion(t *testing.T) { func TestValidEthAddress(t *testing.T) { t.Run("Valid Eth wallet, but no balance", func(t *testing.T) { - _config.chain.EthNode = "test" + // _config.chain.EthNode = "test" backend, _ := newTestBackend(t) client, _ := backend.Attach() defer backend.Close() @@ -60,6 +60,13 @@ func TestValidEthAddress(t *testing.T) { getEthClient = func() (*ethclient.Client, error) { return realClient, nil } + // getZcnNode = func() (*ZcnNode, error) { + // zcnnode := &ZcnNode{ + // NodeClient: &core.NodeClient{}, + // } + // zcnnode.Config().EthereumNode = "test" + // return zcnnode, nil + // } res, err := IsValidEthAddress("0x531f9349ed2Fe5c526B47fa7841D35c90482e6cF") require.Nil(t, err, "") @@ -67,7 +74,7 @@ func TestValidEthAddress(t *testing.T) { }) t.Run("Valid Eth wallet", func(t *testing.T) { - _config.chain.EthNode = "test" + // _config.chain.EthNode = "test" backend, _ := newTestBackend(t) client, _ := backend.Attach() defer backend.Close() @@ -80,6 +87,13 @@ func TestValidEthAddress(t *testing.T) { getEthClient = func() (*ethclient.Client, error) { return realClient, nil } + // getZcnNode = func() (*ZcnNode, error) { + // zcnnode := &ZcnNode{ + // NodeClient: &core.NodeClient{}, + // } + // zcnnode.Config().EthereumNode = "test" + // return zcnnode, nil + // } res, err := IsValidEthAddress(testAddr.String()) require.Nil(t, err, "") @@ -87,7 +101,7 @@ func TestValidEthAddress(t *testing.T) { }) t.Run("Invalid Eth wallet", func(t *testing.T) { - _config.chain.EthNode = "test" + // _config.chain.EthNode = "test" backend, _ := newTestBackend(t) client, _ := backend.Attach() defer backend.Close() @@ -97,6 +111,13 @@ func TestValidEthAddress(t *testing.T) { getEthClient = func() (*ethclient.Client, error) { return realClient, nil } + // getZcnNode = func() (*ZcnNode, error) { + // zcnnode := &ZcnNode{ + // NodeClient: &core.NodeClient{}, + // } + // zcnnode.Config().EthereumNode = "test" + // return zcnnode, nil + // } res, err := IsValidEthAddress("testAddr.String()") require.NotNil(t, err, "") @@ -106,7 +127,14 @@ func TestValidEthAddress(t *testing.T) { func TestGetWalletAddrFromEthMnemonic(t *testing.T) { t.Run("Success", func(t *testing.T) { - _config.chain.EthNode = "test" + // _config.chain.EthNode = "test" + // getZcnNode = func() (*ZcnNode, error) { + // zcnnode := &ZcnNode{ + // NodeClient: &core.NodeClient{}, + // } + // zcnnode.Config().EthereumNode = "test" + // return zcnnode, nil + // } mnemonic := "expect domain water near beauty bag pond clap chronic chronic length leisure" res, err := GetWalletAddrFromEthMnemonic(mnemonic) require.Nil(t, err, "") @@ -114,7 +142,14 @@ func TestGetWalletAddrFromEthMnemonic(t *testing.T) { }) t.Run("Wrong", func(t *testing.T) { - _config.chain.EthNode = "test" + // _config.chain.EthNode = "test" + // getZcnNode = func() (*ZcnNode, error) { + // zcnnode := &ZcnNode{ + // NodeClient: &core.NodeClient{}, + // } + // zcnnode.Config().EthereumNode = "test" + // return zcnnode, nil + // } mnemonic := "this is wrong mnemonic" _, err := GetWalletAddrFromEthMnemonic(mnemonic) require.NotNil(t, err, "") @@ -123,7 +158,7 @@ func TestGetWalletAddrFromEthMnemonic(t *testing.T) { func TestGetEthBalance(t *testing.T) { t.Run("Success", func(t *testing.T) { - _config.chain.EthNode = "test" + // _config.chain.EthNode = "test" backend, _ := newTestBackend(t) client, _ := backend.Attach() defer backend.Close() @@ -136,6 +171,13 @@ func TestGetEthBalance(t *testing.T) { getEthClient = func() (*ethclient.Client, error) { return realClient, nil } + // getZcnNode = func() (*ZcnNode, error) { + // zcnnode := &ZcnNode{ + // NodeClient: &core.NodeClient{}, + // } + // zcnnode.Config().EthereumNode = "test" + // return zcnnode, nil + // } tcb := &MockBalanceCallback{} tcb.wg = &sync.WaitGroup{} @@ -155,7 +197,7 @@ func TestGetEthBalance(t *testing.T) { func TestCheckEthHashStatus(t *testing.T) { t.Run("Pending transaction", func(t *testing.T) { - _config.chain.EthNode = "test" + // _config.chain.EthNode = "test" backend, _ := newTestBackend(t) client, _ := backend.Attach() defer backend.Close() @@ -168,6 +210,13 @@ func TestCheckEthHashStatus(t *testing.T) { getEthClient = func() (*ethclient.Client, error) { return realClient, nil } + // getZcnNode = func() (*ZcnNode, error) { + // zcnnode := &ZcnNode{ + // NodeClient: &core.NodeClient{}, + // } + // zcnnode.Config().EthereumNode = "test" + // return zcnnode, nil + // } result := CheckEthHashStatus("0x05aa8890d4778e292f837dd36b59a50931c175f4648c3d8157525f5454475cf7") require.True(t, result < 0, "") }) @@ -175,7 +224,7 @@ func TestCheckEthHashStatus(t *testing.T) { func TestSuggestEthGasPrice(t *testing.T) { t.Run("suggest gas price success", func(t *testing.T) { - _config.chain.EthNode = "test" + // _config.chain.EthNode = "test" backend, _ := newTestBackend(t) client, _ := backend.Attach() defer backend.Close() @@ -185,6 +234,13 @@ func TestSuggestEthGasPrice(t *testing.T) { getEthClient = func() (*ethclient.Client, error) { return realClient, nil } + // getZcnNode = func() (*ZcnNode, error) { + // zcnnode := &ZcnNode{ + // NodeClient: &core.NodeClient{}, + // } + // zcnnode.Config().EthereumNode = "test" + // return zcnnode, nil + // } gas, err := SuggestEthGasPrice() require.Nil(t, err) require.True(t, gas > 0) @@ -193,7 +249,7 @@ func TestSuggestEthGasPrice(t *testing.T) { func TestTransferEthTokens(t *testing.T) { t.Run("success transfer", func(t *testing.T) { - _config.chain.EthNode = "test" + // _config.chain.EthNode = "test" backend, _ := newTestBackend(t) client, _ := backend.Attach() defer backend.Close() @@ -203,6 +259,13 @@ func TestTransferEthTokens(t *testing.T) { getEthClient = func() (*ethclient.Client, error) { return realClient, nil } + // getZcnNode = func() (*ZcnNode, error) { + // zcnnode := &ZcnNode{ + // NodeClient: &core.NodeClient{}, + // } + // zcnnode.Config().EthereumNode = "test" + // return zcnnode, nil + // } hash, err := TransferEthTokens("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291", 1000000000000, 10000) require.Nil(t, err) @@ -250,24 +313,24 @@ func newTestBackend(t *testing.T) (*node.Node, []*types.Block) { return n, blocks } -func generateTestChain() (*core.Genesis, []*types.Block) { +func generateTestChain() (*ethCore.Genesis, []*types.Block) { db := rawdb.NewMemoryDatabase() config := params.AllEthashProtocolChanges - genesis := &core.Genesis{ + genesis := ðCore.Genesis{ Config: config, - Alloc: core.GenesisAlloc{testAddr: {Balance: testBalance}}, + Alloc: ethCore.GenesisAlloc{testAddr: {Balance: testBalance}}, ExtraData: []byte("test genesis"), Timestamp: 9000, } // BaseFee: big.NewInt(params.InitialBaseFee), - generate := func(i int, g *core.BlockGen) { + generate := func(i int, g *ethCore.BlockGen) { g.OffsetTime(5) g.SetExtra([]byte("test")) } gblock := genesis.ToBlock() genesis.Commit(db) //nolint: errcheck engine := ethash.NewFaker() - blocks, _ := core.GenerateChain(config, gblock, engine, db, 1, generate) + blocks, _ := ethCore.GenerateChain(config, gblock, engine, db, 1, generate) blocks = append([]*types.Block{gblock}, blocks...) return genesis, blocks } diff --git a/zcncore/init.go b/zcncore/init.go deleted file mode 100644 index 081c0779e..000000000 --- a/zcncore/init.go +++ /dev/null @@ -1,16 +0,0 @@ -package zcncore - -import ( - "sync" - - "github.com/0chain/gosdk/core/logger" -) - -// Singleton -// TODO: Remove these variable and Use zchain.Gcontainer -var ( - _config localConfig - logging logger.Logger - stableMiners []string - mGuard sync.Mutex -) \ No newline at end of file diff --git a/zcncore/mswallet.go b/zcncore/mswallet.go index df6f3553e..1b40f8971 100644 --- a/zcncore/mswallet.go +++ b/zcncore/mswallet.go @@ -8,6 +8,7 @@ import ( "fmt" "github.com/0chain/errors" + "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/zcncrypto" ) @@ -182,7 +183,11 @@ func CreateMSVote(proposal, grpClientID, signerWalletstr, toClientID string, tok buff, _ := json.Marshal(transfer) hash := encryption.Hash(buff) - sigScheme := zcncrypto.NewSignatureScheme(_config.chain.SignatureScheme) + cfg, err := conf.GetClientConfig() + if err != nil { + return "", err + } + sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) if err := sigScheme.SetPrivateKey(signerWallet.Keys[0].PrivateKey); err != nil { return "", err } diff --git a/zcncore/mswallet_base.go b/zcncore/mswallet_base.go index c1762cd92..f0a5fde6a 100644 --- a/zcncore/mswallet_base.go +++ b/zcncore/mswallet_base.go @@ -9,6 +9,9 @@ import ( "time" "github.com/0chain/errors" + "github.com/0chain/gosdk/constants" + "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/zcncrypto" ) @@ -23,14 +26,19 @@ func CreateMSWallet(t, n int) (string, string, []string, error) { if t < 1 || t > n { return "", "", nil, errors.New("bls0_generate_threshold_key_shares", fmt.Sprintf("Given threshold (%d) is less than 1 or greater than numsigners (%d)", t, n)) } + cfg, err := conf.GetClientConfig() + if err != nil { + return "", "", nil, err + } id := 0 - if _config.chain.SignatureScheme != "bls0chain" { + if cfg.SignatureScheme != constants.BLS0CHAIN.String() { return "", "", nil, errors.New("", "encryption scheme for this blockchain is not bls0chain") } - groupKey := zcncrypto.NewSignatureScheme(_config.chain.SignatureScheme) + signScheme := cfg.SignatureScheme + groupKey := zcncrypto.NewSignatureScheme(string(signScheme)) wallet, err := groupKey.GenerateKeys() if err != nil { return "", "", nil, err @@ -52,7 +60,7 @@ func CreateMSWallet(t, n int) (string, string, []string, error) { msw := MSWallet{ Id: id, - SignatureScheme: _config.chain.SignatureScheme, + SignatureScheme: string(signScheme), GroupClientID: groupClientID, GroupKey: groupKey, SignerClientIDs: signerClientIDs, @@ -124,9 +132,9 @@ func GetClientID(pkey string) string { } func GetClientWalletKey() string { - return _config.wallet.ClientKey + return client.Wallet().ClientKey } func GetClientWalletID() string { - return _config.wallet.ClientID + return client.Wallet().ClientID } diff --git a/zcncore/networkworker.go b/zcncore/networkworker.go index e267c5f4a..b9c38f6bf 100644 --- a/zcncore/networkworker.go +++ b/zcncore/networkworker.go @@ -1,151 +1,151 @@ -//go:build !mobile -// +build !mobile +// //go:build !mobile +// // +build !mobile package zcncore -import ( - "context" - "encoding/json" - "net/http" - "reflect" - "time" - - "github.com/0chain/errors" - "github.com/0chain/gosdk/core/conf" - "github.com/0chain/gosdk/core/node" - "github.com/0chain/gosdk/core/util" - "go.uber.org/zap" -) - -const NETWORK_ENDPOINT = "/network" - -var networkWorkerTimerInHours = 1 - -type Network struct { - Miners []string `json:"miners"` - Sharders []string `json:"sharders"` -} - -func updateNetworkDetailsWorker(ctx context.Context) { - ticker := time.NewTicker(time.Duration(networkWorkerTimerInHours) * time.Hour) - for { - select { - case <-ctx.Done(): - logging.Info("Network stopped by user") - return - case <-ticker.C: - err := UpdateNetworkDetails() - if err != nil { - logging.Error("Update network detail worker fail", zap.Error(err)) - return - } - logging.Info("Successfully updated network details") - return - } - } -} - -func UpdateNetworkDetails() error { - networkDetails, err := GetNetworkDetails() - if err != nil { - logging.Error("Failed to update network details ", zap.Error(err)) - return err - } - - shouldUpdate := UpdateRequired(networkDetails) - if shouldUpdate { - _config.isConfigured = false - _config.chain.Miners = networkDetails.Miners - _config.chain.Sharders = networkDetails.Sharders - consensus := _config.chain.SharderConsensous - if consensus < conf.DefaultSharderConsensous { - consensus = conf.DefaultSharderConsensous - } - if len(networkDetails.Sharders) < consensus { - consensus = len(networkDetails.Sharders) - } - - Sharders = node.NewHolder(networkDetails.Sharders, consensus) - node.InitCache(Sharders) - conf.InitChainNetwork(&conf.Network{ - Sharders: networkDetails.Sharders, - Miners: networkDetails.Miners, - }) - _config.isConfigured = true - } - return nil -} - -func UpdateRequired(networkDetails *Network) bool { - miners := _config.chain.Miners - sharders := _config.chain.Sharders - if len(miners) == 0 || len(sharders) == 0 { - return true - } - - minerSame := reflect.DeepEqual(miners, networkDetails.Miners) - sharderSame := reflect.DeepEqual(sharders, networkDetails.Sharders) - - if minerSame && sharderSame { - return false - } - return true -} - -func GetNetworkDetails() (*Network, error) { - req, err := util.NewHTTPGetRequest(_config.chain.BlockWorker + NETWORK_ENDPOINT) - if err != nil { - return nil, errors.New("get_network_details_error", "Unable to create new http request with error "+err.Error()) - } - - res, err := req.Get() - if err != nil { - return nil, errors.New("get_network_details_error", "Unable to get http request with error "+err.Error()) - } - - if res.StatusCode != http.StatusOK { - return nil, errors.New("get_network_details_error", "Unable to get http request with "+res.Status) - - } - var networkResponse Network - err = json.Unmarshal([]byte(res.Body), &networkResponse) - if err != nil { - return nil, errors.Wrap(err, "Error unmarshaling response :"+res.Body) - } - return &networkResponse, nil - -} - -func GetNetwork() *Network { - return &Network{ - Miners: _config.chain.Miners, - Sharders: _config.chain.Sharders, - } -} - -func SetNetwork(miners []string, sharders []string) { - _config.chain.Miners = miners - _config.chain.Sharders = sharders - - consensus := _config.chain.SharderConsensous - if consensus < conf.DefaultSharderConsensous { - consensus = conf.DefaultSharderConsensous - } - if len(sharders) < consensus { - consensus = len(sharders) - } - - Sharders = node.NewHolder(sharders, consensus) - node.InitCache(Sharders) - - conf.InitChainNetwork(&conf.Network{ - Miners: miners, - Sharders: sharders, - }) -} - -func GetNetworkJSON() string { - network := GetNetwork() - networkBytes, _ := json.Marshal(network) - return string(networkBytes) -} +// import ( +// "context" +// "encoding/json" +// "net/http" +// "reflect" +// "time" + +// "github.com/0chain/errors" +// "github.com/0chain/gosdk/core/conf" +// "github.com/0chain/gosdk/core/node" +// "github.com/0chain/gosdk/core/util" +// "go.uber.org/zap" +// ) + +// const NETWORK_ENDPOINT = "/network" + +// var networkWorkerTimerInHours = 1 + +// type Network struct { +// Miners []string `json:"miners"` +// Sharders []string `json:"sharders"` +// } + +// func updateNetworkDetailsWorker(ctx context.Context) { +// ticker := time.NewTicker(time.Duration(networkWorkerTimerInHours) * time.Hour) +// for { +// select { +// case <-ctx.Done(): +// logging.Info("Network stopped by user") +// return +// case <-ticker.C: +// err := UpdateNetworkDetails() +// if err != nil { +// logging.Error("Update network detail worker fail", zap.Error(err)) +// return +// } +// logging.Info("Successfully updated network details") +// return +// } +// } +// } + +// func UpdateNetworkDetails() error { +// networkDetails, err := GetNetworkDetails() +// if err != nil { +// logging.Error("Failed to update network details ", zap.Error(err)) +// return err +// } + +// shouldUpdate := UpdateRequired(networkDetails) +// if shouldUpdate { +// _config.isConfigured = false +// _config.chain.Miners = networkDetails.Miners +// _config.chain.Sharders = networkDetails.Sharders +// consensus := _config.chain.SharderConsensous +// if consensus < conf.DefaultSharderConsensous { +// consensus = conf.DefaultSharderConsensous +// } +// if len(networkDetails.Sharders) < consensus { +// consensus = len(networkDetails.Sharders) +// } + +// Sharders = node.NewHolder(networkDetails.Sharders, consensus) +// node.InitCache(Sharders) +// conf.InitChainNetwork(&conf.Network{ +// Sharders: networkDetails.Sharders, +// Miners: networkDetails.Miners, +// }) +// _config.isConfigured = true +// } +// return nil +// } + +// func UpdateRequired(networkDetails *Network) bool { +// miners := _config.chain.Miners +// sharders := _config.chain.Sharders +// if len(miners) == 0 || len(sharders) == 0 { +// return true +// } + +// minerSame := reflect.DeepEqual(miners, networkDetails.Miners) +// sharderSame := reflect.DeepEqual(sharders, networkDetails.Sharders) + +// if minerSame && sharderSame { +// return false +// } +// return true +// } + +// func GetNetworkDetails() (*Network, error) { +// req, err := util.NewHTTPGetRequest(_config.chain.BlockWorker + NETWORK_ENDPOINT) +// if err != nil { +// return nil, errors.New("get_network_details_error", "Unable to create new http request with error "+err.Error()) +// } + +// res, err := req.Get() +// if err != nil { +// return nil, errors.New("get_network_details_error", "Unable to get http request with error "+err.Error()) +// } + +// if res.StatusCode != http.StatusOK { +// return nil, errors.New("get_network_details_error", "Unable to get http request with "+res.Status) + +// } +// var networkResponse Network +// err = json.Unmarshal([]byte(res.Body), &networkResponse) +// if err != nil { +// return nil, errors.Wrap(err, "Error unmarshaling response :"+res.Body) +// } +// return &networkResponse, nil + +// } + +// func GetNetwork() *Network { +// return &Network{ +// Miners: _config.chain.Miners, +// Sharders: _config.chain.Sharders, +// } +// } + +// func SetNetwork(miners []string, sharders []string) { +// _config.chain.Miners = miners +// _config.chain.Sharders = sharders + +// consensus := _config.chain.SharderConsensous +// if consensus < conf.DefaultSharderConsensous { +// consensus = conf.DefaultSharderConsensous +// } +// if len(sharders) < consensus { +// consensus = len(sharders) +// } + +// Sharders = node.NewHolder(sharders, consensus) +// node.InitCache(Sharders) + +// conf.InitChainNetwork(&conf.Network{ +// Miners: miners, +// Sharders: sharders, +// }) +// } + +// func GetNetworkJSON() string { +// network := GetNetwork() +// networkBytes, _ := json.Marshal(network) +// return string(networkBytes) +// } diff --git a/zcncore/sample/snapshot_test.go b/zcncore/sample/snapshot_test.go index 84176319a..53cf727d1 100644 --- a/zcncore/sample/snapshot_test.go +++ b/zcncore/sample/snapshot_test.go @@ -1,10 +1,13 @@ package sample import ( + "context" "fmt" "sync" "testing" + "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/zcnbridge/wallet" "github.com/0chain/gosdk/zcncore" ) @@ -44,7 +47,8 @@ const ChainConfig = ` func TestGetAggregates(t *testing.T) { t.Skip("learning test") - err := zcncore.Init(ChainConfig) + cfg := conf.Config{} + err := client.Init(context.Background(), cfg) if err != nil { fmt.Println("Init failed") return diff --git a/zcncore/transaction.go b/zcncore/transaction.go index 715cf5f6f..7293798e6 100644 --- a/zcncore/transaction.go +++ b/zcncore/transaction.go @@ -14,7 +14,9 @@ import ( "github.com/0chain/errors" "github.com/0chain/gosdk/core/block" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" + "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/node" "github.com/0chain/gosdk/core/transaction" @@ -258,8 +260,8 @@ func NewTransaction(cb TransactionCallback, txnFee uint64, nonce int64) (Transac if err != nil { return nil, err } - if _config.isSplitWallet { - if _config.authUrl == "" { + if client.SplitKeyWallet() { + if client.AuthUrl() == "" { return nil, errors.New("", "auth url not set") } logging.Info("New transaction interface with auth") @@ -285,13 +287,17 @@ func (t *Transaction) Send(toClientID string, val uint64, desc string) error { if err != nil { return errors.New("", "Could not serialize description to transaction_data") } + clientNode, err := client.GetNode() + if err != nil { + return err + } t.txn.TransactionType = transaction.TxnTypeSend t.txn.ToClientID = toClientID t.txn.Value = val t.txn.TransactionData = string(txnData) if t.txn.TransactionFee == 0 { - fee, err := transaction.EstimateFee(t.txn, _config.chain.Miners, 0.2) + fee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners(), 0.2) if err != nil { return err } @@ -309,6 +315,10 @@ func (t *Transaction) SendWithSignatureHash(toClientID string, val uint64, desc if err != nil { return errors.New("", "Could not serialize description to transaction_data") } + clientNode, err := client.GetNode() + if err != nil { + return err + } t.txn.TransactionType = transaction.TxnTypeSend t.txn.ToClientID = toClientID t.txn.Value = val @@ -317,7 +327,7 @@ func (t *Transaction) SendWithSignatureHash(toClientID string, val uint64, desc t.txn.Signature = sig t.txn.CreationDate = CreationDate if t.txn.TransactionFee == 0 { - fee, err := transaction.EstimateFee(t.txn, _config.chain.Miners, 0.2) + fee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners(), 0.2) if err != nil { return err } @@ -795,6 +805,12 @@ func (t *Transaction) RegisterMultiSig(walletstr string, mswallet string) error if err != nil { return errors.Wrap(err, "execute multisig register failed due to invalid data.") } + + clientNode, err := client.GetNode() + if err != nil { + return err + } + go func() { t.txn.TransactionType = transaction.TxnTypeSmartContract t.txn.ToClientID = MultiSigSmartContractAddress @@ -809,7 +825,7 @@ func (t *Transaction) RegisterMultiSig(walletstr string, mswallet string) error t.txn.TransactionNonce = nonce if t.txn.TransactionFee == 0 { - fee, err := transaction.EstimateFee(t.txn, _config.chain.Miners, 0.2) + fee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners(), 0.2) if err != nil { return } @@ -832,8 +848,9 @@ func NewMSTransaction(walletstr string, cb TransactionCallback) (*Transaction, e fmt.Printf("Error while parsing the wallet. %v", err) return nil, err } + cfg, err := conf.GetClientConfig() t := &Transaction{} - t.txn = transaction.NewTransactionEntity(w.ClientID, _config.chain.ChainID, w.ClientKey, w.Nonce) + t.txn = transaction.NewTransactionEntity(w.ClientID, cfg.ChainID, w.ClientKey, w.Nonce) t.txnStatus, t.verifyStatus = StatusUnknown, StatusUnknown t.txnCb = cb return t, nil @@ -859,6 +876,10 @@ func (t *Transaction) RegisterVote(signerwalletstr string, msvstr string) error if err != nil { return errors.Wrap(err, "execute multisig vote failed due to invalid data.") } + clientNode, err := client.GetNode() + if err != nil { + return err + } go func() { t.txn.TransactionType = transaction.TxnTypeSmartContract t.txn.ToClientID = MultiSigSmartContractAddress @@ -873,7 +894,7 @@ func (t *Transaction) RegisterVote(signerwalletstr string, msvstr string) error t.txn.TransactionNonce = nonce if t.txn.TransactionFee == 0 { - fee, err := transaction.EstimateFee(t.txn, _config.chain.Miners, 0.2) + fee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners(), 0.2) if err != nil { return } @@ -963,6 +984,10 @@ func (t *Transaction) ZCNSCUpdateAuthorizerConfig(ip *AuthorizerNode) (err error } func (t *Transaction) Verify() error { + clientNode, err := client.GetNode() + if err != nil { + return err + } if t.txnHash == "" && t.txnStatus == StatusUnknown { return errors.New("", "invalid transaction. cannot be verified.") } @@ -978,7 +1003,7 @@ func (t *Transaction) Verify() error { t.txn.CreationDate = int64(common.Now()) } - tq, err := NewTransactionQuery(Sharders.Healthy(), _config.chain.Miners) + tq, err := NewTransactionQuery(clientNode.Sharders().Healthy(), clientNode.Network().Miners()) if err != nil { logging.Error(err) return err @@ -1003,8 +1028,8 @@ func (t *Transaction) Verify() error { // transaction is done or expired. it means random sharder might be outdated, try to query it from s/S sharders to confirm it if util.MaxInt64(lfbBlockHeader.getCreationDate(now), now) >= (t.txn.CreationDate + int64(defaultTxnExpirationSeconds)) { - logging.Info("falling back to ", getMinShardersVerify(), " of ", len(_config.chain.Sharders), " Sharders") - confirmBlockHeader, confirmationBlock, lfbBlockHeader, err = tq.getConsensusConfirmation(context.TODO(), getMinShardersVerify(), t.txnHash) + logging.Info("falling back to ", clientNode.GetMinShardersVerify(), " of ", len(clientNode.Network().Sharders()), " Sharders") + confirmBlockHeader, confirmationBlock, lfbBlockHeader, err = tq.getConsensusConfirmation(context.TODO(), clientNode.GetMinShardersVerify(), t.txnHash) } // txn not found in fast confirmation/consensus confirmation @@ -1069,11 +1094,15 @@ func ConvertToValue(token float64) uint64 { } func GetLatestFinalized(ctx context.Context, numSharders int) (b *block.Header, err error) { + clientNode, err := client.GetNode() + if err != nil { + return nil, err + } var result = make(chan *util.GetResponse, numSharders) defer close(result) - numSharders = len(Sharders.Healthy()) // overwrite, use all - Sharders.QueryFromShardersContext(ctx, numSharders, GET_LATEST_FINALIZED, result) + numSharders = len(clientNode.Sharders().Healthy()) // overwrite, use all + clientNode.Sharders().QueryFromShardersContext(ctx, numSharders, GET_LATEST_FINALIZED, result) var ( maxConsensus int @@ -1114,11 +1143,15 @@ func GetLatestFinalized(ctx context.Context, numSharders int) (b *block.Header, } func GetLatestFinalizedMagicBlock(ctx context.Context, numSharders int) (m *block.MagicBlock, err error) { + clientNode, err := client.GetNode() + if err != nil { + return nil, err + } var result = make(chan *util.GetResponse, numSharders) defer close(result) - numSharders = len(Sharders.Healthy()) // overwrite, use all - Sharders.QueryFromShardersContext(ctx, numSharders, GET_LATEST_FINALIZED_MAGIC_BLOCK, result) + numSharders = len(clientNode.Sharders().Healthy()) // overwrite, use all + clientNode.Sharders().QueryFromShardersContext(ctx, numSharders, GET_LATEST_FINALIZED_MAGIC_BLOCK, result) var ( maxConsensus int @@ -1165,11 +1198,16 @@ func GetLatestFinalizedMagicBlock(ctx context.Context, numSharders int) (m *bloc } func GetChainStats(ctx context.Context) (b *block.ChainStats, err error) { + clientNode, err := client.GetNode() + if err != nil { + return nil, err + } + var result = make(chan *util.GetResponse, 1) defer close(result) - var numSharders = len(Sharders.Healthy()) // overwrite, use all - Sharders.QueryFromShardersContext(ctx, numSharders, GET_CHAIN_STATS, result) + var numSharders = len(clientNode.Sharders().Healthy()) // overwrite, use all + clientNode.Sharders().QueryFromShardersContext(ctx, numSharders, GET_CHAIN_STATS, result) var rsp *util.GetResponse for i := 0; i < numSharders; i++ { var x = <-result @@ -1194,11 +1232,14 @@ func GetChainStats(ctx context.Context) (b *block.ChainStats, err error) { } func GetFeeStats(ctx context.Context) (b *block.FeeStats, err error) { - + clientNode, err := client.GetNode() + if err != nil { + return nil, err + } var numMiners = 4 - if numMiners > len(_config.chain.Miners) { - numMiners = len(_config.chain.Miners) + if numMiners > len(clientNode.Network().Miners()) { + numMiners = len(clientNode.Network().Miners()) } var result = make(chan *util.GetResponse, numMiners) @@ -1232,20 +1273,31 @@ loop: } func GetBlockByRound(ctx context.Context, numSharders int, round int64) (b *block.Block, err error) { - return Sharders.GetBlockByRound(ctx, numSharders, round) + clientNode, err := client.GetNode() + if err != nil { + return nil, err + } + return clientNode.Sharders().GetBlockByRound(ctx, numSharders, round) } func GetRoundFromSharders() (int64, error) { - return Sharders.GetRoundFromSharders() + clientNode, err := client.GetNode() + if err != nil { + return 0, err + } + return clientNode.Sharders().GetRoundFromSharders() } func GetMagicBlockByNumber(ctx context.Context, numSharders int, number int64) (m *block.MagicBlock, err error) { - + clientNode, err := client.GetNode() + if err != nil { + return nil, err + } var result = make(chan *util.GetResponse, numSharders) defer close(result) - numSharders = len(Sharders.Healthy()) // overwrite, use all - Sharders.QueryFromShardersContext(ctx, numSharders, + numSharders = len(clientNode.Sharders().Healthy()) // overwrite, use all + clientNode.Sharders().QueryFromShardersContext(ctx, numSharders, fmt.Sprintf("%smagic_block_number=%d", GET_MAGIC_BLOCK_INFO, number), result) @@ -1342,47 +1394,47 @@ func (nc *NonceCache) Evict(clientId string) { delete(nc.cache, clientId) } -func WithEthereumNode(uri string) func(c *ChainConfig) error { - return func(c *ChainConfig) error { - c.EthNode = uri - return nil - } -} - -func WithChainID(id string) func(c *ChainConfig) error { - return func(c *ChainConfig) error { - c.ChainID = id - return nil - } -} - -func WithMinSubmit(m int) func(c *ChainConfig) error { - return func(c *ChainConfig) error { - c.MinSubmit = m - return nil - } -} - -func WithMinConfirmation(m int) func(c *ChainConfig) error { - return func(c *ChainConfig) error { - c.MinConfirmation = m - return nil - } -} - -func WithConfirmationChainLength(m int) func(c *ChainConfig) error { - return func(c *ChainConfig) error { - c.ConfirmationChainLength = m - return nil - } -} - -func WithSharderConsensous(m int) func(c *ChainConfig) error { - return func(c *ChainConfig) error { - c.SharderConsensous = m - return nil - } -} +// func WithEthereumNode(uri string) func(c *ChainConfig) error { +// return func(c *ChainConfig) error { +// c.EthNode = uri +// return nil +// } +// } + +// func WithChainID(id string) func(c *ChainConfig) error { +// return func(c *ChainConfig) error { +// c.ChainID = id +// return nil +// } +// } + +// func WithMinSubmit(m int) func(c *ChainConfig) error { +// return func(c *ChainConfig) error { +// c.MinSubmit = m +// return nil +// } +// } + +// func WithMinConfirmation(m int) func(c *ChainConfig) error { +// return func(c *ChainConfig) error { +// c.MinConfirmation = m +// return nil +// } +// } + +// func WithConfirmationChainLength(m int) func(c *ChainConfig) error { +// return func(c *ChainConfig) error { +// c.ConfirmationChainLength = m +// return nil +// } +// } + +// func WithSharderConsensous(m int) func(c *ChainConfig) error { +// return func(c *ChainConfig) error { +// c.SharderConsensous = m +// return nil +// } +// } // UpdateValidatorSettings update settings of a validator. func (t *Transaction) UpdateValidatorSettings(v *Validator) (err error) { @@ -1406,7 +1458,7 @@ func GetVestingClientList(clientID string, cb GetInfoCallback) (err error) { return } if clientID == "" { - clientID = _config.wallet.ClientID // if not blank + clientID = client.Wallet().ClientID // if not blank } go GetInfoFromSharders(WithParams(GET_VESTING_CLIENT_POOLS, Params{ "client_id": clientID, diff --git a/zcncore/transaction_base.go b/zcncore/transaction_base.go index ac260553b..974583ea3 100644 --- a/zcncore/transaction_base.go +++ b/zcncore/transaction_base.go @@ -10,19 +10,18 @@ import ( "time" "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" + "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/node" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/zboxcore/logger" "go.uber.org/zap" - "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/transaction" "github.com/0chain/gosdk/core/util" - "github.com/0chain/gosdk/core/version" "github.com/0chain/gosdk/core/zcncrypto" - "github.com/0chain/gosdk/zboxcore/blockchain" "github.com/0chain/gosdk/zboxcore/sdk" ) @@ -92,71 +91,71 @@ type TransactionCallback interface { OnAuthComplete(t *Transaction, status int) } -type localConfig struct { - chain ChainConfig - wallet zcncrypto.Wallet - authUrl string - isConfigured bool - isValidWallet bool - isSplitWallet bool -} - -type ChainConfig struct { - ChainID string `json:"chain_id,omitempty"` - BlockWorker string `json:"block_worker"` - Miners []string `json:"miners"` - Sharders []string `json:"sharders"` - SignatureScheme string `json:"signature_scheme"` - MinSubmit int `json:"min_submit"` - MinConfirmation int `json:"min_confirmation"` - ConfirmationChainLength int `json:"confirmation_chain_length"` - EthNode string `json:"eth_node"` - SharderConsensous int `json:"sharder_consensous"` -} - -var Sharders *node.NodeHolder +// type localConfig struct { +// chain ChainConfig +// wallet zcncrypto.Wallet +// authUrl string +// isConfigured bool +// isValidWallet bool +// isSplitWallet bool +// } + +// type ChainConfig struct { +// ChainID string `json:"chain_id,omitempty"` +// BlockWorker string `json:"block_worker"` +// Miners []string `json:"miners"` +// Sharders []string `json:"sharders"` +// SignatureScheme string `json:"signature_scheme"` +// MinSubmit int `json:"min_submit"` +// MinConfirmation int `json:"min_confirmation"` +// ConfirmationChainLength int `json:"confirmation_chain_length"` +// EthNode string `json:"eth_node"` +// SharderConsensous int `json:"sharder_consensous"` +// } + +// var Sharders *node.NodeHolder // InitZCNSDK initializes the SDK with miner, sharder and signature scheme provided. -func InitZCNSDK(blockWorker string, signscheme string, configs ...func(*ChainConfig) error) error { - if signscheme != "ed25519" && signscheme != "bls0chain" { - return errors.New("", "invalid/unsupported signature scheme") - } - _config.chain.BlockWorker = blockWorker - _config.chain.SignatureScheme = signscheme - - err := UpdateNetworkDetails() - if err != nil { - fmt.Println("UpdateNetworkDetails:", err) - return err - } - - go updateNetworkDetailsWorker(context.Background()) - - for _, conf := range configs { - err := conf(&_config.chain) - if err != nil { - return errors.Wrap(err, "invalid/unsupported options.") - } - } - assertConfig() - _config.isConfigured = true - logging.Info("******* Wallet SDK Version:", version.VERSIONSTR, " ******* (InitZCNSDK)") - - cfg := &conf.Config{ - BlockWorker: _config.chain.BlockWorker, - MinSubmit: _config.chain.MinSubmit, - MinConfirmation: _config.chain.MinConfirmation, - ConfirmationChainLength: _config.chain.ConfirmationChainLength, - SignatureScheme: _config.chain.SignatureScheme, - ChainID: _config.chain.ChainID, - EthereumNode: _config.chain.EthNode, - SharderConsensous: _config.chain.SharderConsensous, - } - - conf.InitClientConfig(cfg) - - return nil -} +// func InitZCNSDK(blockWorker string, signscheme string, configs ...func(*ChainConfig) error) error { +// if signscheme != "ed25519" && signscheme != "bls0chain" { +// return errors.New("", "invalid/unsupported signature scheme") +// } +// _config.chain.BlockWorker = blockWorker +// _config.chain.SignatureScheme = signscheme + +// err := UpdateNetworkDetails() +// if err != nil { +// fmt.Println("UpdateNetworkDetails:", err) +// return err +// } + +// go updateNetworkDetailsWorker(context.Background()) + +// for _, conf := range configs { +// err := conf(&_config.chain) +// if err != nil { +// return errors.Wrap(err, "invalid/unsupported options.") +// } +// } +// assertConfig() +// _config.isConfigured = true +// logging.Info("******* Wallet SDK Version:", version.VERSIONSTR, " ******* (InitZCNSDK)") + +// cfg := &conf.Config{ +// BlockWorker: _config.chain.BlockWorker, +// MinSubmit: _config.chain.MinSubmit, +// MinConfirmation: _config.chain.MinConfirmation, +// ConfirmationChainLength: _config.chain.ConfirmationChainLength, +// SignatureScheme: _config.chain.SignatureScheme, +// ChainID: _config.chain.ChainID, +// EthereumNode: _config.chain.EthNode, +// SharderConsensous: _config.chain.SharderConsensous, +// } + +// conf.InitClientConfig(cfg) + +// return nil +// } /*Confirmation - a data structure that provides the confirmation that a transaction is included into the block chain */ type confirmation struct { @@ -217,8 +216,12 @@ type SendTxnData struct { } func Sign(hash string) (string, error) { - sigScheme := zcncrypto.NewSignatureScheme(_config.chain.SignatureScheme) - err := sigScheme.SetPrivateKey(_config.wallet.Keys[0].PrivateKey) + cfg, err := conf.GetClientConfig() + if err != nil { + return "", err + } + sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) + err = sigScheme.SetPrivateKey(client.Wallet().Keys[0].PrivateKey) if err != nil { return "", err } @@ -226,8 +229,12 @@ func Sign(hash string) (string, error) { } var SignFn = func(hash string) (string, error) { - sigScheme := zcncrypto.NewSignatureScheme(_config.chain.SignatureScheme) - err := sigScheme.SetPrivateKey(_config.wallet.Keys[0].PrivateKey) + cfg, err := conf.GetClientConfig() + if err != nil { + return "", err + } + sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) + err = sigScheme.SetPrivateKey(client.Wallet().Keys[0].PrivateKey) if err != nil { return "", err } @@ -241,8 +248,12 @@ func signWithWallet(hash string, wi interface{}) (string, error) { fmt.Printf("Error in casting to wallet") return "", errors.New("", "error in casting to wallet") } - sigScheme := zcncrypto.NewSignatureScheme(_config.chain.SignatureScheme) - err := sigScheme.SetPrivateKey(w.Keys[0].PrivateKey) + cfg, err := conf.GetClientConfig() + if err != nil { + return "", err + } + sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) + err = sigScheme.SetPrivateKey(w.Keys[0].PrivateKey) if err != nil { return "", err } @@ -404,8 +415,13 @@ func (t *Transaction) submitTxn() { } func newTransaction(cb TransactionCallback, txnFee uint64, nonce int64) (*Transaction, error) { + cfg, err := conf.GetClientConfig() + if err != nil { + return nil, err + } + t := &Transaction{} - t.txn = transaction.NewTransactionEntity(_config.wallet.ClientID, _config.chain.ChainID, _config.wallet.ClientKey, nonce) + t.txn = transaction.NewTransactionEntity(client.Wallet().ClientID, cfg.ChainID, client.Wallet().ClientKey, nonce) t.txnStatus, t.verifyStatus = StatusUnknown, StatusUnknown t.txnCb = cb t.txn.TransactionNonce = nonce @@ -480,8 +496,13 @@ func (t *Transaction) createSmartContractTxn(address, methodName string, input i return nil } + clientNode, err := client.GetNode() + if err != nil { + return err + } + // TODO: check if transaction is exempt to avoid unnecessary fee estimation - minFee, err := transaction.EstimateFee(t.txn, _config.chain.Miners, 0.2) + minFee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners(), 0.2) if err != nil { logger.Logger.Error("failed estimate txn fee", zap.Any("txn", t.txn.Hash), @@ -564,8 +585,11 @@ func (t *Transaction) GetTransactionHash() string { } func queryFromMinersContext(ctx context.Context, numMiners int, query string, result chan *util.GetResponse) { - - randomMiners := util.Shuffle(_config.chain.Miners)[:numMiners] + nodeClient, err := client.GetNode() + if err != nil { + panic(err) + } + randomMiners := util.Shuffle(nodeClient.Network().Miners())[:numMiners] for _, miner := range randomMiners { go func(minerurl string) { logging.Info("Query from ", minerurl+query) @@ -627,9 +651,13 @@ func getBlockHeaderFromTransactionConfirmation(txnHash string, cfmBlock map[stri } func getBlockInfoByRound(round int64, content string) (*blockHeader, error) { - numSharders := len(Sharders.Healthy()) // overwrite, use all + nodeClient, err := client.GetNode() + if err != nil { + return nil, err + } + numSharders := len(nodeClient.Sharders().Healthy()) // overwrite, use all resultC := make(chan *util.GetResponse, numSharders) - Sharders.QueryFromSharders(numSharders, fmt.Sprintf("%vround=%v&content=%v", GET_BLOCK_INFO, round, content), resultC) + nodeClient.Sharders().QueryFromSharders(numSharders, fmt.Sprintf("%vround=%v&content=%v", GET_BLOCK_INFO, round, content), resultC) var ( maxConsensus int roundConsensus = make(map[string]int) @@ -700,10 +728,12 @@ func validateChain(confirmBlock *blockHeader) bool { logging.Debug("Confirmation round: ", confirmRound) currentBlockHash := confirmBlock.Hash round := confirmRound + 1 + cfg, _ := conf.GetClientConfig() + nodeClient, _ := client.GetNode() for { nextBlock, err := getBlockInfoByRound(round, "header") if err != nil { - logging.Info(err, " after a second falling thru to ", getMinShardersVerify(), "of ", len(_config.chain.Sharders), "Sharders", len(Sharders.Healthy()), "Healthy sharders") + logging.Info(err, " after a second falling thru to ", nodeClient.GetMinShardersVerify(), "of ", len(nodeClient.Network().Sharders()), "Sharders", len(nodeClient.Sharders().Healthy()), "Healthy sharders") sys.Sleep(1 * time.Second) nextBlock, err = getBlockInfoByRound(round, "header") if err != nil { @@ -716,7 +746,7 @@ func validateChain(confirmBlock *blockHeader) bool { currentBlockHash = nextBlock.Hash round++ } - if (round > confirmRound) && (round-confirmRound < getMinRequiredChainLength()) { + if (round > confirmRound) && (round-confirmRound < int64(cfg.ConfirmationChainLength)) { continue } if round < confirmRound { @@ -844,13 +874,17 @@ type MinerSCUnlock struct { } func VerifyContentHash(metaTxnDataJSON string) (bool, error) { + nodeClient, err := client.GetNode() + if err != nil { + return false, err + } var metaTxnData sdk.CommitMetaResponse - err := json.Unmarshal([]byte(metaTxnDataJSON), &metaTxnData) + err = json.Unmarshal([]byte(metaTxnDataJSON), &metaTxnData) if err != nil { return false, errors.New("metaTxnData_decode_error", "Unable to decode metaTxnData json") } - t, err := transaction.VerifyTransaction(metaTxnData.TxnID, blockchain.GetSharders()) + t, err := transaction.VerifyTransaction(metaTxnData.TxnID, nodeClient.Network().Sharders()) if err != nil { return false, errors.New("fetch_txm_details", "Unable to fetch txn details") } diff --git a/zcncore/transaction_query.go b/zcncore/transaction_query.go index 797d36d6a..4f5a26bcc 100644 --- a/zcncore/transaction_query.go +++ b/zcncore/transaction_query.go @@ -15,6 +15,7 @@ import ( "time" thrown "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/resty" "github.com/0chain/gosdk/core/util" ) @@ -230,7 +231,7 @@ func (tq *TransactionQuery) getRandomSharderWithHealthcheck(ctx context.Context) return "", ErrNoOnlineSharders } -//getRandomMiner returns a random miner +// getRandomMiner returns a random miner func (tq *TransactionQuery) getRandomMiner(ctx context.Context) (string, error) { if tq.miners == nil || len(tq.miners) == 0 { @@ -526,8 +527,12 @@ func (tq *TransactionQuery) getFastConfirmation(ctx context.Context, txnHash str } func GetInfoFromSharders(urlSuffix string, op int, cb GetInfoCallback) { - - tq, err := NewTransactionQuery(util.Shuffle(Sharders.Healthy()), []string{}) + clientNode, err := client.GetNode() + if err != nil { + cb.OnInfoAvailable(op, StatusError, "", err.Error()) + return + } + tq, err := NewTransactionQuery(util.Shuffle(clientNode.Sharders().Healthy()), []string{}) if err != nil { cb.OnInfoAvailable(op, StatusError, "", err.Error()) return @@ -543,8 +548,13 @@ func GetInfoFromSharders(urlSuffix string, op int, cb GetInfoCallback) { } func GetInfoFromAnySharder(urlSuffix string, op int, cb GetInfoCallback) { + clientNode, err := client.GetNode() + if err != nil { + cb.OnInfoAvailable(op, StatusError, "", err.Error()) + return + } - tq, err := NewTransactionQuery(util.Shuffle(Sharders.Healthy()), []string{}) + tq, err := NewTransactionQuery(util.Shuffle(clientNode.Sharders().Healthy()), []string{}) if err != nil { cb.OnInfoAvailable(op, StatusError, "", err.Error()) return @@ -560,8 +570,13 @@ func GetInfoFromAnySharder(urlSuffix string, op int, cb GetInfoCallback) { } func GetInfoFromAnyMiner(urlSuffix string, op int, cb getInfoCallback) { + clientNode, err := client.GetNode() + if err != nil { + cb.OnInfoAvailable(op, StatusError, "", err.Error()) + return + } - tq, err := NewTransactionQuery([]string{}, util.Shuffle(_config.chain.Miners)) + tq, err := NewTransactionQuery([]string{}, util.Shuffle(clientNode.Network().Miners())) if err != nil { cb.OnInfoAvailable(op, StatusError, "", err.Error()) diff --git a/zcncore/transactionauth_base.go b/zcncore/transactionauth_base.go index 8fa166a10..8f96b7ed4 100644 --- a/zcncore/transactionauth_base.go +++ b/zcncore/transactionauth_base.go @@ -4,6 +4,8 @@ import ( "encoding/json" "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/node" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/core/transaction" @@ -30,7 +32,7 @@ func newTransactionWithAuth(cb TransactionCallback, txnFee uint64, nonce int64) } func (ta *TransactionWithAuth) getAuthorize() (*transaction.Transaction, error) { - ta.t.txn.PublicKey = _config.wallet.ClientKey + ta.t.txn.PublicKey = client.Wallet().ClientKey err := ta.t.txn.ComputeHashAndSign(SignFn) if err != nil { return nil, errors.Wrap(err, "signing error.") @@ -86,8 +88,12 @@ func (ta *TransactionWithAuth) completeTxn(status int, out string, err error) { } func verifyFn(signature, msgHash, publicKey string) (bool, error) { - v := zcncrypto.NewSignatureScheme(_config.chain.SignatureScheme) - err := v.SetPublicKey(publicKey) + cfg, err := conf.GetClientConfig() + if err != nil { + return false, err + } + v := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) + err = v.SetPublicKey(publicKey) if err != nil { return false, err } @@ -100,11 +106,14 @@ func verifyFn(signature, msgHash, publicKey string) (bool, error) { } func (ta *TransactionWithAuth) sign(otherSig string) error { + cfg, err := conf.GetClientConfig() + if err != nil { + return err + } ta.t.txn.ComputeHashData() - sig := zcncrypto.NewSignatureScheme(_config.chain.SignatureScheme) + sig := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) - var err error - err = sig.SetPrivateKey(_config.wallet.Keys[0].PrivateKey) + err = sig.SetPrivateKey(client.Wallet().Keys[0].PrivateKey) if err != nil { return err } diff --git a/zcncore/wallet.go b/zcncore/wallet.go index 7775c5f47..d15bf6881 100644 --- a/zcncore/wallet.go +++ b/zcncore/wallet.go @@ -5,6 +5,7 @@ package zcncore import ( "github.com/0chain/gosdk/core/common" + "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/zcncrypto" ) @@ -17,9 +18,14 @@ func GetWalletBalance(clientId string) (common.Balance, error) { return getWalletBalance(clientId) } +// Deprecated: use Sign() method in zcncrypto.Wallet func SignWith0Wallet(hash string, w *zcncrypto.Wallet) (string, error) { - sigScheme := zcncrypto.NewSignatureScheme(_config.chain.SignatureScheme) - err := sigScheme.SetPrivateKey(w.Keys[0].PrivateKey) + cfg, err := conf.GetClientConfig() + if err != nil { + return "", err + } + sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) + err = sigScheme.SetPrivateKey(w.Keys[0].PrivateKey) if err != nil { return "", err } diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index aa6423a5e..76e34e131 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -5,7 +5,6 @@ import ( "encoding/hex" "encoding/json" "fmt" - "math" "net/http" "net/url" "strconv" @@ -13,9 +12,11 @@ import ( "sync" "time" - stdErrors "errors" + "errors" - "github.com/0chain/errors" + // "github.com/0chain/errors" + "github.com/0chain/gosdk/constants" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/logger" @@ -221,38 +222,47 @@ type AuthCallback interface { OnSetupComplete(status int, err string) } +var ( + logging logger.Logger +) + func init() { logging.Init(defaultLogLevel, "0chain-core-sdk") } +// Deprecated: Use core.GetNode().GetStableMiners() func GetStableMiners() []string { - mGuard.Lock() - defer mGuard.Unlock() - if len(stableMiners) == 0 { - stableMiners = util.GetRandom(_config.chain.Miners, getMinMinersSubmit()) + clientNode, err := client.GetNode() + if err != nil { + panic(err) } - - return stableMiners + return clientNode.GetStableMiners() } + +// Deprecated: Use core.GetNode().ResetStableMiners() func ResetStableMiners() { - mGuard.Lock() - defer mGuard.Unlock() - stableMiners = util.GetRandom(_config.chain.Miners, getMinMinersSubmit()) + clientNode, err := client.GetNode() + if err != nil { + panic(err) + } + clientNode.ResetStableMiners() } func checkSdkInit() error { - if !_config.isConfigured || len(_config.chain.Miners) < 1 || len(_config.chain.Sharders) < 1 { - return errors.New("", "SDK not initialized") + _, err := client.GetNode() + if err != nil { + return err } return nil } + func checkWalletConfig() error { - if !_config.isValidWallet || _config.wallet.ClientID == "" { - logging.Error("wallet info not found. returning error.") - return errors.New("", "wallet info not found. set wallet info") + if !client.IsWalletSet() { + return errors.New("wallet info not found. set wallet info") } return nil } + func CheckConfig() error { err := checkSdkInit() if err != nil { @@ -265,38 +275,13 @@ func CheckConfig() error { return nil } -func assertConfig() { - if _config.chain.MinSubmit <= 0 { - _config.chain.MinSubmit = defaultMinSubmit - } - if _config.chain.MinConfirmation <= 0 { - _config.chain.MinConfirmation = defaultMinConfirmation - } - if _config.chain.ConfirmationChainLength <= 0 { - _config.chain.ConfirmationChainLength = defaultConfirmationChainLength - } -} -func getMinMinersSubmit() int { - minMiners := util.MaxInt(calculateMinRequired(float64(_config.chain.MinSubmit), float64(len(_config.chain.Miners))/100), 1) - logging.Info("Minimum miners used for submit :", minMiners) - return minMiners -} - +// Deprecated: Use core.GetNode().GetMinShardersVerify() after core's Init call func GetMinShardersVerify() int { - return getMinShardersVerify() -} - -func getMinShardersVerify() int { - minSharders := util.MaxInt(calculateMinRequired(float64(_config.chain.MinConfirmation), float64(len(Sharders.Healthy()))/100), 1) - logging.Info("Minimum sharders used for verify :", minSharders) - return minSharders -} -func getMinRequiredChainLength() int64 { - return int64(_config.chain.ConfirmationChainLength) -} - -func calculateMinRequired(minRequired, percent float64) int { - return int(math.Ceil(minRequired * percent)) + clientNode, err := client.GetNode() + if err != nil { + panic(err) + } + return clientNode.GetMinShardersVerify() } // GetVersion - returns version string @@ -325,75 +310,28 @@ func SetLogFile(logFile string, verbose bool) { logging.Info("******* Wallet SDK Version:", version.VERSIONSTR, " ******* (SetLogFile)") } -// Init initialize the SDK with miner, sharder and signature scheme provided in configuration provided in JSON format -// # Inputs -// - chainConfigJSON: json format of zcn config -// { -// "block_worker": "https://dev.0chain.net/dns", -// "signature_scheme": "bls0chain", -// "min_submit": 50, -// "min_confirmation": 50, -// "confirmation_chain_length": 3, -// "max_txn_query": 5, -// "query_sleep_time": 5, -// "preferred_blobbers": ["https://dev.0chain.net/blobber02","https://dev.0chain.net/blobber03"], -// "chain_id":"0afc093ffb509f059c55478bc1a60351cef7b4e9c008a53a6cc8241ca8617dfe", -// "ethereum_node":"https://ropsten.infura.io/v3/xxxxxxxxxxxxxxx", -// "zbox_host":"https://0box.dev.0chain.net", -// "zbox_app_type":"vult", -// "sharder_consensous": 2, -// } -func Init(chainConfigJSON string) error { - err := json.Unmarshal([]byte(chainConfigJSON), &_config.chain) - if err == nil { - // Check signature scheme is supported - if _config.chain.SignatureScheme != "ed25519" && _config.chain.SignatureScheme != "bls0chain" { - return errors.New("", "invalid/unsupported signature scheme") - } - - err = UpdateNetworkDetails() - if err != nil { - return err - } - - go updateNetworkDetailsWorker(context.Background()) - - assertConfig() - _config.isConfigured = true - - cfg := &conf.Config{ - BlockWorker: _config.chain.BlockWorker, - MinSubmit: _config.chain.MinSubmit, - MinConfirmation: _config.chain.MinConfirmation, - ConfirmationChainLength: _config.chain.ConfirmationChainLength, - SignatureScheme: _config.chain.SignatureScheme, - ChainID: _config.chain.ChainID, - EthereumNode: _config.chain.EthNode, - SharderConsensous: _config.chain.SharderConsensous, - } - - conf.InitClientConfig(cfg) - } - logging.Info("0chain: test logging") - logging.Info("******* Wallet SDK Version:", version.VERSIONSTR, " ******* (Init) Test") - return err -} - // InitSignatureScheme initializes signature scheme only. func InitSignatureScheme(scheme string) { - _config.chain.SignatureScheme = scheme + cfg := conf.Config{ + SignatureScheme: scheme, + } + conf.InitClientConfig(&cfg) } // CreateWalletOffline creates the wallet for the config signature scheme. func CreateWalletOffline() (string, error) { - sigScheme := zcncrypto.NewSignatureScheme(_config.chain.SignatureScheme) + cfg, err := conf.GetClientConfig() + if err != nil { + return "", err + } + sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) wallet, err := sigScheme.GenerateKeys() if err != nil { - return "", errors.Wrap(err, "failed to generate keys") + return "", errors.New("failed to generate keys: " + err.Error()) } w, err := wallet.Marshal() if err != nil { - return "", errors.Wrap(err, "wallet encoding failed") + return "", errors.New("wallet encoding failed: " + err.Error()) } return w, nil } @@ -401,10 +339,14 @@ func CreateWalletOffline() (string, error) { // RecoverOfflineWallet recovers the previously generated wallet using the mnemonic. func RecoverOfflineWallet(mnemonic string) (string, error) { if !zcncrypto.IsMnemonicValid(mnemonic) { - return "", errors.New("", "Invalid mnemonic") + return "", errors.New("Invalid mnemonic") } - sigScheme := zcncrypto.NewSignatureScheme(_config.chain.SignatureScheme) + cfg, err := conf.GetClientConfig() + if err != nil { + return "", err + } + sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) wallet, err := sigScheme.RecoverKeys(mnemonic) if err != nil { return "", err @@ -422,11 +364,15 @@ func RecoverOfflineWallet(mnemonic string) (string, error) { // It also registers the wallet again to block chain. func RecoverWallet(mnemonic string, statusCb WalletCallback) error { if !zcncrypto.IsMnemonicValid(mnemonic) { - return errors.New("", "Invalid mnemonic") + return errors.New("Invalid mnemonic") + } + cfg, err := conf.GetClientConfig() + if err != nil { + return err } go func() { - sigScheme := zcncrypto.NewSignatureScheme(_config.chain.SignatureScheme) - _, err := sigScheme.RecoverKeys(mnemonic) + sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) + _, err = sigScheme.RecoverKeys(mnemonic) if err != nil { statusCb.OnWalletCreateComplete(StatusError, "", err.Error()) return @@ -437,21 +383,25 @@ func RecoverWallet(mnemonic string, statusCb WalletCallback) error { // Split keys from the primary master key func SplitKeys(privateKey string, numSplits int) (string, error) { - if _config.chain.SignatureScheme != "bls0chain" { - return "", errors.New("", "signature key doesn't support split key") + cfg, err := conf.GetClientConfig() + if err != nil { + return "", err } - sigScheme := zcncrypto.NewSignatureScheme(_config.chain.SignatureScheme) - err := sigScheme.SetPrivateKey(privateKey) + if cfg.SignatureScheme != constants.BLS0CHAIN.String() { + return "", errors.New("signature key doesn't support split key") + } + sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) + err = sigScheme.SetPrivateKey(privateKey) if err != nil { - return "", errors.Wrap(err, "set private key failed") + return "", errors.New("set private key failed." + err.Error()) } w, err := sigScheme.SplitKeys(numSplits) if err != nil { - return "", errors.Wrap(err, "split key failed.") + return "", errors.New("split key failed." + err.Error()) } wStr, err := w.Marshal() if err != nil { - return "", errors.Wrap(err, "wallet encoding failed.") + return "", errors.New("wallet encoding failed." + err.Error()) } return wStr, nil } @@ -464,7 +414,11 @@ type GetClientResponse struct { } func GetClientDetails(clientID string) (*GetClientResponse, error) { - minerurl := util.GetRandom(_config.chain.Miners, 1)[0] + clientNode, err := client.GetNode() + if err != nil { + panic(err) + } + minerurl := util.GetRandom(clientNode.Network().Miners(), 1)[0] url := minerurl + GET_CLIENT url = fmt.Sprintf("%v?id=%v", url, clientID) req, err := util.NewHTTPGetRequest(url) @@ -487,6 +441,7 @@ func GetClientDetails(clientID string) (*GetClientResponse, error) { return &clientDetails, nil } +// Deprecated: Use zcncrypto.IsMnemonicValid() // IsMnemonicValid is an utility function to check the mnemonic valid // // # Inputs @@ -495,21 +450,24 @@ func IsMnemonicValid(mnemonic string) bool { return zcncrypto.IsMnemonicValid(mnemonic) } +// Deprecated: Use client.SetWallet() and client.SetSplitKeyWallet() to set wallet and splitKeyWallet respectively // SetWallet should be set before any transaction or client specific APIs // splitKeyWallet parameter is valid only if SignatureScheme is "BLS0Chain" func SetWallet(w zcncrypto.Wallet, splitKeyWallet bool) error { - _config.wallet = w - - if _config.chain.SignatureScheme == "bls0chain" { - _config.isSplitWallet = splitKeyWallet + err := client.SetWallet(w) + if err != nil { + return err + } + err = client.SetSplitKeyWallet(splitKeyWallet) + if err != nil { + return err } - _config.isValidWallet = true - return nil } +// Deprecated: Use client.Wallet() in core package func GetWalletRaw() zcncrypto.Wallet { - return _config.wallet + return *client.Wallet() } // SetWalletInfo should be set before any transaction or client specific APIs @@ -531,28 +489,19 @@ func GetWalletRaw() zcncrypto.Wallet { // // - splitKeyWallet: if wallet keys is split func SetWalletInfo(jsonWallet string, splitKeyWallet bool) error { - err := json.Unmarshal([]byte(jsonWallet), &_config.wallet) - if err == nil { - if _config.chain.SignatureScheme == "bls0chain" { - _config.isSplitWallet = splitKeyWallet - } - _config.isValidWallet = true + wallet := zcncrypto.Wallet{} + err := json.Unmarshal([]byte(jsonWallet), &wallet) + if err != nil { + return errors.New("invalid jsonWallet: " + err.Error()) } - return err + return SetWallet(wallet, splitKeyWallet) } // SetAuthUrl will be called by app to set zauth URL to SDK. // # Inputs // - url: the url of zAuth server func SetAuthUrl(url string) error { - if !_config.isSplitWallet { - return errors.New("", "wallet type is not split key") - } - if url == "" { - return errors.New("", "invalid auth url") - } - _config.authUrl = strings.TrimRight(url, "/") - return nil + return client.SetAuthUrl(url) } func getWalletBalance(clientId string) (common.Balance, error) { @@ -589,7 +538,7 @@ func GetBalance(cb GetBalanceCallback) error { return err } go func() { - value, info, err := getBalanceFromSharders(_config.wallet.ClientID) + value, info, err := getBalanceFromSharders(client.Wallet().ClientID) if err != nil { logging.Error(err) cb.OnBalanceAvailable(StatusError, 0, info) @@ -608,7 +557,7 @@ func GetMintNonce(cb GetInfoCallback) error { } go GetInfoFromSharders(withParams(GET_MINT_NONCE, Params{ - "client_id": _config.wallet.ClientID, + "client_id": client.Wallet().ClientID, }), OpGetMintNonce, cb) return nil } @@ -640,7 +589,7 @@ func GetNonce(cb GetNonceCallback) error { } go func() { - value, info, err := getNonceFromSharders(_config.wallet.ClientID) + value, info, err := getNonceFromSharders(client.Wallet().ClientID) if err != nil { logging.Error(err) cb.OnNonceAvailable(StatusError, 0, info) @@ -680,7 +629,7 @@ func GetWalletNonce(clientID string) (int64, error) { return cb.nonce, nil } - return 0, stdErrors.New(cb.info) + return 0, errors.New(cb.info) } // GetBalanceWallet retreives wallet balance from sharders @@ -704,11 +653,19 @@ func GetBalanceWallet(walletStr string, cb GetBalanceCallback) error { } func getBalanceFromSharders(clientID string) (int64, string, error) { - return Sharders.GetBalanceFieldFromSharders(clientID, "balance") + clientNode, err := client.GetNode() + if err != nil { + return 0, "", err + } + return clientNode.Sharders().GetBalanceFieldFromSharders(clientID, "balance") } func getNonceFromSharders(clientID string) (int64, string, error) { - return Sharders.GetBalanceFieldFromSharders(clientID, "nonce") + clientNode, err := client.GetNode() + if err != nil { + return 0, "", err + } + return clientNode.Sharders().GetBalanceFieldFromSharders(clientID, "nonce") } // ConvertToToken converts the SAS tokens to ZCN tokens @@ -905,7 +862,7 @@ func GetMinerSCNodePool(id string, cb GetInfoCallback) (err error) { } go GetInfoFromSharders(withParams(GET_MINERSC_POOL, Params{ "id": id, - "pool_id": _config.wallet.ClientID, + "pool_id": client.Wallet().ClientID, }), 0, cb) return @@ -920,7 +877,7 @@ func GetMinerSCUserInfo(clientID string, cb GetInfoCallback) (err error) { return } if clientID == "" { - clientID = _config.wallet.ClientID + clientID = client.Wallet().ClientID } go GetInfoFromSharders(withParams(GET_MINERSC_USER, Params{ "client_id": clientID, @@ -988,7 +945,7 @@ func GetAllocations(clientID string, cb GetInfoCallback) (err error) { return } if clientID == "" { - clientID = _config.wallet.ClientID + clientID = client.Wallet().ClientID } var url = withParams(STORAGESC_GET_ALLOCATIONS, Params{ "client": clientID, @@ -1100,7 +1057,7 @@ func GetReadPoolInfo(clientID string, cb GetInfoCallback) (err error) { return } if clientID == "" { - clientID = _config.wallet.ClientID + clientID = client.Wallet().ClientID } var url = withParams(STORAGESC_GET_READ_POOL_INFO, Params{ "client_id": clientID, @@ -1131,7 +1088,7 @@ func GetStakePoolUserInfo(clientID string, offset, limit int, cb GetInfoCallback return } if clientID == "" { - clientID = _config.wallet.ClientID + clientID = client.Wallet().ClientID } var url = withParams(STORAGESC_GET_STAKE_POOL_USER_INFO, Params{ diff --git a/zmagmacore/http/sc-api.go b/zmagmacore/http/sc-api.go index c97ff8629..4180bcff6 100644 --- a/zmagmacore/http/sc-api.go +++ b/zmagmacore/http/sc-api.go @@ -9,8 +9,8 @@ import ( "net/http" "net/url" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/util" - "github.com/0chain/gosdk/zcncore" "github.com/0chain/gosdk/zmagmacore/errors" ) @@ -79,8 +79,12 @@ func hashAndBytesOfReader(r io.Reader) (hash string, reader []byte, err error) { // extractSharders returns string slice of randomly ordered sharders existing in the current network. func extractSharders() []string { - network := zcncore.GetNetwork() - return util.GetRandom(network.Sharders, len(network.Sharders)) + nodeClient, err := client.GetNode() + if err != nil { + panic(err) + } + sharders := nodeClient.Network().Sharders() + return util.GetRandom(sharders, len(sharders)) } const ( diff --git a/zmagmacore/wallet/setup.go b/zmagmacore/wallet/setup.go index 24cbf845e..de4847d21 100644 --- a/zmagmacore/wallet/setup.go +++ b/zmagmacore/wallet/setup.go @@ -1,6 +1,10 @@ package wallet import ( + "context" + + "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/zcncore" ) @@ -14,7 +18,10 @@ func SetupZCNSDK(cfg Config) error { var logName = cfg.LogDir() + "/zsdk.log" zcncore.SetLogFile(logName, false) zcncore.SetLogLevel(logLevelFromStr(cfg.LogLvl())) - return zcncore.InitZCNSDK(cfg.BlockWorker(), cfg.SignatureScheme()) + return client.Init(context.Background(), conf.Config{ + BlockWorker: cfg.BlockWorker(), + SignatureScheme: cfg.SignatureScheme(), + }) } // logLevelFromStr converts string log level to gosdk logger level int value. From 2adec95da50a579217a0f2475745ddc62aefbb5b Mon Sep 17 00:00:00 2001 From: storybehind Date: Thu, 8 Feb 2024 17:08:48 +0530 Subject: [PATCH 003/319] fix issues after tests run --- core/client/set.go | 4 ++ zcncore/networkworker.go | 151 --------------------------------------- 2 files changed, 4 insertions(+), 151 deletions(-) delete mode 100644 zcncore/networkworker.go diff --git a/core/client/set.go b/core/client/set.go index 0451420a4..4a8a1bb49 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -17,6 +17,10 @@ var ( fee uint64 ) +func init() { + wallet = &zcncrypto.Wallet{} +} + func SetWallet(w zcncrypto.Wallet) error { wallet = &w return nil diff --git a/zcncore/networkworker.go b/zcncore/networkworker.go deleted file mode 100644 index b9c38f6bf..000000000 --- a/zcncore/networkworker.go +++ /dev/null @@ -1,151 +0,0 @@ -// //go:build !mobile -// // +build !mobile - -package zcncore - -// import ( -// "context" -// "encoding/json" -// "net/http" -// "reflect" -// "time" - -// "github.com/0chain/errors" -// "github.com/0chain/gosdk/core/conf" -// "github.com/0chain/gosdk/core/node" -// "github.com/0chain/gosdk/core/util" -// "go.uber.org/zap" -// ) - -// const NETWORK_ENDPOINT = "/network" - -// var networkWorkerTimerInHours = 1 - -// type Network struct { -// Miners []string `json:"miners"` -// Sharders []string `json:"sharders"` -// } - -// func updateNetworkDetailsWorker(ctx context.Context) { -// ticker := time.NewTicker(time.Duration(networkWorkerTimerInHours) * time.Hour) -// for { -// select { -// case <-ctx.Done(): -// logging.Info("Network stopped by user") -// return -// case <-ticker.C: -// err := UpdateNetworkDetails() -// if err != nil { -// logging.Error("Update network detail worker fail", zap.Error(err)) -// return -// } -// logging.Info("Successfully updated network details") -// return -// } -// } -// } - -// func UpdateNetworkDetails() error { -// networkDetails, err := GetNetworkDetails() -// if err != nil { -// logging.Error("Failed to update network details ", zap.Error(err)) -// return err -// } - -// shouldUpdate := UpdateRequired(networkDetails) -// if shouldUpdate { -// _config.isConfigured = false -// _config.chain.Miners = networkDetails.Miners -// _config.chain.Sharders = networkDetails.Sharders -// consensus := _config.chain.SharderConsensous -// if consensus < conf.DefaultSharderConsensous { -// consensus = conf.DefaultSharderConsensous -// } -// if len(networkDetails.Sharders) < consensus { -// consensus = len(networkDetails.Sharders) -// } - -// Sharders = node.NewHolder(networkDetails.Sharders, consensus) -// node.InitCache(Sharders) -// conf.InitChainNetwork(&conf.Network{ -// Sharders: networkDetails.Sharders, -// Miners: networkDetails.Miners, -// }) -// _config.isConfigured = true -// } -// return nil -// } - -// func UpdateRequired(networkDetails *Network) bool { -// miners := _config.chain.Miners -// sharders := _config.chain.Sharders -// if len(miners) == 0 || len(sharders) == 0 { -// return true -// } - -// minerSame := reflect.DeepEqual(miners, networkDetails.Miners) -// sharderSame := reflect.DeepEqual(sharders, networkDetails.Sharders) - -// if minerSame && sharderSame { -// return false -// } -// return true -// } - -// func GetNetworkDetails() (*Network, error) { -// req, err := util.NewHTTPGetRequest(_config.chain.BlockWorker + NETWORK_ENDPOINT) -// if err != nil { -// return nil, errors.New("get_network_details_error", "Unable to create new http request with error "+err.Error()) -// } - -// res, err := req.Get() -// if err != nil { -// return nil, errors.New("get_network_details_error", "Unable to get http request with error "+err.Error()) -// } - -// if res.StatusCode != http.StatusOK { -// return nil, errors.New("get_network_details_error", "Unable to get http request with "+res.Status) - -// } -// var networkResponse Network -// err = json.Unmarshal([]byte(res.Body), &networkResponse) -// if err != nil { -// return nil, errors.Wrap(err, "Error unmarshaling response :"+res.Body) -// } -// return &networkResponse, nil - -// } - -// func GetNetwork() *Network { -// return &Network{ -// Miners: _config.chain.Miners, -// Sharders: _config.chain.Sharders, -// } -// } - -// func SetNetwork(miners []string, sharders []string) { -// _config.chain.Miners = miners -// _config.chain.Sharders = sharders - -// consensus := _config.chain.SharderConsensous -// if consensus < conf.DefaultSharderConsensous { -// consensus = conf.DefaultSharderConsensous -// } -// if len(sharders) < consensus { -// consensus = len(sharders) -// } - -// Sharders = node.NewHolder(sharders, consensus) -// node.InitCache(Sharders) - -// conf.InitChainNetwork(&conf.Network{ -// Miners: miners, -// Sharders: sharders, -// }) -// } - -// func GetNetworkJSON() string { -// network := GetNetwork() -// networkBytes, _ := json.Marshal(network) -// return string(networkBytes) -// } From ef95a972d3222d610ba356dfaee7e5d7d7f14b69 Mon Sep 17 00:00:00 2001 From: storybehind Date: Sat, 10 Feb 2024 11:05:51 +0530 Subject: [PATCH 004/319] fix sdk builds --- core/client/init_node.go | 14 +- core/conf/vars.go | 4 +- wasmsdk/sdk.go | 18 +- winsdk/sdk.go | 6 +- zboxcore/sdk/networkworker.go | 8 +- zcncore/mswallet_mobile.go | 8 +- zcncore/networkworker_mobile.go | 318 ++++++++++++++-------------- zcncore/transaction.go | 91 ++++---- zcncore/transaction_base.go | 93 ++++---- zcncore/transaction_mobile.go | 80 +++++-- zcncore/transaction_query_mobile.go | 15 +- zcncore/wallet_base.go | 29 +++ zcncore/wallet_mobile.go | 9 +- 13 files changed, 395 insertions(+), 298 deletions(-) diff --git a/core/client/init_node.go b/core/client/init_node.go index 73d9e6fd9..4c5bf6c8a 100644 --- a/core/client/init_node.go +++ b/core/client/init_node.go @@ -79,7 +79,10 @@ func (n *Node) Network() *conf.Network { } func (n *Node) ShouldUpdateNetwork() (bool, *conf.Network, error) { - cfg, _ := conf.GetClientConfig() + cfg, err := conf.GetClientConfig() + if err != nil { + return false, nil, err + } network, err := getNetwork(n.clientCtx, cfg.BlockWorker) if err != nil { logging.Error("Failed to get network details ", zap.Error(err)) @@ -96,7 +99,10 @@ func (n *Node) ShouldUpdateNetwork() (bool, *conf.Network, error) { func (n *Node) UpdateNetwork(network *conf.Network) error { n.networkGuard.Lock() defer n.networkGuard.Unlock() - cfg, _ := conf.GetClientConfig() + cfg, err := conf.GetClientConfig() + if err != nil { + return err + } n.network = network n.sharders = node.NewHolder(n.network.Sharders(), util.MinInt(len(n.network.Sharders()), util.MaxInt(cfg.SharderConsensous, conf.DefaultSharderConsensous))) node.InitCache(n.sharders) @@ -154,7 +160,9 @@ func Init(ctx context.Context, cfg conf.Config) error { continue } if shouldUpdate { - nodeClient.UpdateNetwork(network) + if err = nodeClient.UpdateNetwork(network); err != nil { + logging.Error("error on updating network: ", err) + } } } } diff --git a/core/conf/vars.go b/core/conf/vars.go index 077f0ea7c..3a1fa0376 100644 --- a/core/conf/vars.go +++ b/core/conf/vars.go @@ -57,8 +57,8 @@ func InitClientConfig(c *Config) { // return // } -// network.Sharders = n.Sharders -// network.Miners = n.Miners +// network.sharders = n.Sharders +// network.miners = n.Miners // } // func normalizeURLs(network *Network) { diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index b616d48ee..b89040aeb 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -4,6 +4,7 @@ package main import ( + "context" "encoding/hex" "encoding/json" "errors" @@ -12,6 +13,8 @@ import ( "os" "sync" + "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/imageutil" "github.com/0chain/gosdk/core/logger" @@ -34,12 +37,15 @@ func initSDKs(chainID, blockWorker, signatureScheme string, return err } - err = zcncore.InitZCNSDK(blockWorker, signatureScheme, - zcncore.WithChainID(chainID), - zcncore.WithMinConfirmation(minConfirmation), - zcncore.WithMinSubmit(minSubmit), - zcncore.WithConfirmationChainLength(confirmationChainLength), - zcncore.WithSharderConsensous(sharderconsensous)) + err = client.Init(context.Background(), conf.Config{ + BlockWorker: blockWorker, + SignatureScheme: signatureScheme, + ChainID: chainID, + MinConfirmation: minConfirmation, + MinSubmit: minSubmit, + ConfirmationChainLength: confirmationChainLength, + SharderConsensous: sharderconsensous, + }) if err != nil { fmt.Println("wasm: InitZCNSDK ", err) diff --git a/winsdk/sdk.go b/winsdk/sdk.go index 47f586487..c0252e15b 100644 --- a/winsdk/sdk.go +++ b/winsdk/sdk.go @@ -163,7 +163,11 @@ func InitWallet(clientJson *C.char) *C.char { l.Logger.Error(err) return WithJSON(false, err) } - client.SetWallet(w) + err = client.SetWallet(w) + if err != nil { + l.Logger.Error(err) + return WithJSON(false, err) + } l.Logger.Info("InitWallet success") zboxApiClient.SetWallet(client.Wallet().ClientID, client.Wallet().Keys[0].PrivateKey, client.Wallet().ClientKey) diff --git a/zboxcore/sdk/networkworker.go b/zboxcore/sdk/networkworker.go index 44d3c188f..067cecc2c 100644 --- a/zboxcore/sdk/networkworker.go +++ b/zboxcore/sdk/networkworker.go @@ -9,6 +9,7 @@ import ( "strconv" "time" + "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/node" l "github.com/0chain/gosdk/zboxcore/logger" "go.uber.org/zap" @@ -73,10 +74,9 @@ func forceUpdateNetworkDetails(networkDetails *Network) { blockchain.SetMiners(networkDetails.Miners) blockchain.SetSharders(networkDetails.Sharders) node.InitCache(blockchain.Sharders) - // conf.InitChainNetwork(&conf.Network{ - // Sharders: networkDetails.Sharders, - // Miners: networkDetails.Miners, - // }) + n, _ := conf.NewNetwork(networkDetails.Miners, networkDetails.Sharders) + networkDetails.Miners = n.Miners() + networkDetails.Sharders = n.Sharders() sdkInitialized = true } diff --git a/zcncore/mswallet_mobile.go b/zcncore/mswallet_mobile.go index c78e1c66a..efb502c74 100644 --- a/zcncore/mswallet_mobile.go +++ b/zcncore/mswallet_mobile.go @@ -9,6 +9,7 @@ import ( "fmt" "strconv" + "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/zcncrypto" ) @@ -257,7 +258,12 @@ func CreateMSVote(proposal, grpClientID, signerWalletstr, toClientID string, tok buff, _ := json.Marshal(transfer) hash := encryption.Hash(buff) - sigScheme := zcncrypto.NewSignatureScheme(_config.chain.SignatureScheme) + cfg, err := conf.GetClientConfig() + if err != nil { + return "", err + } + + sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) if err := sigScheme.SetPrivateKey(signerWallet.Keys[0].PrivateKey); err != nil { return "", err } diff --git a/zcncore/networkworker_mobile.go b/zcncore/networkworker_mobile.go index ca0863ff8..a5c407361 100644 --- a/zcncore/networkworker_mobile.go +++ b/zcncore/networkworker_mobile.go @@ -3,162 +3,162 @@ package zcncore -import ( - "context" - "encoding/json" - "reflect" - "time" - - "github.com/0chain/errors" - "github.com/0chain/gosdk/core/conf" - "github.com/0chain/gosdk/core/node" - "github.com/0chain/gosdk/core/util" - "go.uber.org/zap" -) - -const NETWORK_ENDPOINT = "/network" - -var networkWorkerTimerInHours = 1 - -type Network struct { - net network -} - -func NewNetwork() *Network { - return &Network{} -} - -func (net *Network) AddMiner(miner string) { - net.net.Miners = append(net.net.Miners, miner) -} - -func (net *Network) AddSharder(sharder string) { - net.net.Sharders = append(net.net.Sharders, sharder) -} - -type network struct { - Miners []string `json:"miners"` - Sharders []string `json:"sharders"` -} - -func updateNetworkDetailsWorker(ctx context.Context) { - ticker := time.NewTicker(time.Duration(networkWorkerTimerInHours) * time.Hour) - for { - select { - case <-ctx.Done(): - logging.Info("Network stopped by user") - return - case <-ticker.C: - err := UpdateNetworkDetails() - if err != nil { - logging.Error("Update network detail worker fail", zap.Error(err)) - return - } - logging.Info("Successfully updated network details") - return - } - } -} - -func UpdateNetworkDetails() error { - networkDetails, err := GetNetworkDetails() - if err != nil { - logging.Error("Failed to update network details ", zap.Error(err)) - return err - } - - shouldUpdate := UpdateRequired(networkDetails) - if shouldUpdate { - _config.isConfigured = false - _config.chain.Miners = networkDetails.net.Miners - _config.chain.Sharders = networkDetails.net.Sharders - consensus := _config.chain.SharderConsensous - if consensus < conf.DefaultSharderConsensous { - consensus = conf.DefaultSharderConsensous - } - if len(networkDetails.net.Sharders) < consensus { - consensus = len(networkDetails.net.Sharders) - } - - Sharders = node.NewHolder(networkDetails.net.Sharders, consensus) - node.InitCache(Sharders) - conf.InitChainNetwork(&conf.Network{ - Sharders: networkDetails.net.Sharders, - Miners: networkDetails.net.Miners, - }) - _config.isConfigured = true - } - return nil -} - -func UpdateRequired(networkDetails *Network) bool { - miners := _config.chain.Miners - sharders := _config.chain.Sharders - if len(miners) == 0 || len(sharders) == 0 { - return true - } - - minerSame := reflect.DeepEqual(miners, networkDetails.net.Miners) - sharderSame := reflect.DeepEqual(sharders, networkDetails.net.Sharders) - - if minerSame && sharderSame { - return false - } - return true -} - -func GetNetworkDetails() (*Network, error) { - req, err := util.NewHTTPGetRequest(_config.chain.BlockWorker + NETWORK_ENDPOINT) - if err != nil { - return nil, errors.New("get_network_details_error", "Unable to create new http request with error "+err.Error()) - } - - res, err := req.Get() - if err != nil { - return nil, errors.New("get_network_details_error", "Unable to get http request with error "+err.Error()) - } - - var networkResponse network - err = json.Unmarshal([]byte(res.Body), &networkResponse) - if err != nil { - return nil, errors.Wrap(err, "Error unmarshaling response :") - } - return &Network{net: networkResponse}, nil -} - -func GetNetwork() *Network { - return &Network{ - net: network{ - Miners: _config.chain.Miners, - Sharders: _config.chain.Sharders, - }, - } -} - -func SetNetwork(net *Network) { - _config.chain.Miners = net.net.Miners - _config.chain.Sharders = net.net.Sharders - - consensus := _config.chain.SharderConsensous - if consensus < conf.DefaultSharderConsensous { - consensus = conf.DefaultSharderConsensous - } - if len(net.net.Sharders) < consensus { - consensus = len(net.net.Sharders) - } - - Sharders = node.NewHolder(_config.chain.Sharders, consensus) - - node.InitCache(Sharders) - - conf.InitChainNetwork(&conf.Network{ - Miners: net.net.Miners, - Sharders: net.net.Sharders, - }) -} - -func GetNetworkJSON() string { - network := GetNetwork() - networkBytes, _ := json.Marshal(network) - return string(networkBytes) -} +// import ( +// "context" +// "encoding/json" +// "reflect" +// "time" + +// "github.com/0chain/errors" +// "github.com/0chain/gosdk/core/conf" +// "github.com/0chain/gosdk/core/node" +// "github.com/0chain/gosdk/core/util" +// "go.uber.org/zap" +// ) + +// const NETWORK_ENDPOINT = "/network" + +// var networkWorkerTimerInHours = 1 + +// type Network struct { +// net network +// } + +// func NewNetwork() *Network { +// return &Network{} +// } + +// func (net *Network) AddMiner(miner string) { +// net.net.Miners = append(net.net.Miners, miner) +// } + +// func (net *Network) AddSharder(sharder string) { +// net.net.Sharders = append(net.net.Sharders, sharder) +// } + +// type network struct { +// Miners []string `json:"miners"` +// Sharders []string `json:"sharders"` +// } + +// func updateNetworkDetailsWorker(ctx context.Context) { +// ticker := time.NewTicker(time.Duration(networkWorkerTimerInHours) * time.Hour) +// for { +// select { +// case <-ctx.Done(): +// logging.Info("Network stopped by user") +// return +// case <-ticker.C: +// err := UpdateNetworkDetails() +// if err != nil { +// logging.Error("Update network detail worker fail", zap.Error(err)) +// return +// } +// logging.Info("Successfully updated network details") +// return +// } +// } +// } + +// func UpdateNetworkDetails() error { +// networkDetails, err := GetNetworkDetails() +// if err != nil { +// logging.Error("Failed to update network details ", zap.Error(err)) +// return err +// } + +// shouldUpdate := UpdateRequired(networkDetails) +// if shouldUpdate { +// _config.isConfigured = false +// _config.chain.Miners = networkDetails.net.Miners +// _config.chain.Sharders = networkDetails.net.Sharders +// consensus := _config.chain.SharderConsensous +// if consensus < conf.DefaultSharderConsensous { +// consensus = conf.DefaultSharderConsensous +// } +// if len(networkDetails.net.Sharders) < consensus { +// consensus = len(networkDetails.net.Sharders) +// } + +// Sharders = node.NewHolder(networkDetails.net.Sharders, consensus) +// node.InitCache(Sharders) +// conf.InitChainNetwork(&conf.Network{ +// Sharders: networkDetails.net.Sharders, +// Miners: networkDetails.net.Miners, +// }) +// _config.isConfigured = true +// } +// return nil +// } + +// func UpdateRequired(networkDetails *Network) bool { +// miners := _config.chain.Miners +// sharders := _config.chain.Sharders +// if len(miners) == 0 || len(sharders) == 0 { +// return true +// } + +// minerSame := reflect.DeepEqual(miners, networkDetails.net.Miners) +// sharderSame := reflect.DeepEqual(sharders, networkDetails.net.Sharders) + +// if minerSame && sharderSame { +// return false +// } +// return true +// } + +// func GetNetworkDetails() (*Network, error) { +// req, err := util.NewHTTPGetRequest(_config.chain.BlockWorker + NETWORK_ENDPOINT) +// if err != nil { +// return nil, errors.New("get_network_details_error", "Unable to create new http request with error "+err.Error()) +// } + +// res, err := req.Get() +// if err != nil { +// return nil, errors.New("get_network_details_error", "Unable to get http request with error "+err.Error()) +// } + +// var networkResponse network +// err = json.Unmarshal([]byte(res.Body), &networkResponse) +// if err != nil { +// return nil, errors.Wrap(err, "Error unmarshaling response :") +// } +// return &Network{net: networkResponse}, nil +// } + +// func GetNetwork() *Network { +// return &Network{ +// net: network{ +// Miners: _config.chain.Miners, +// Sharders: _config.chain.Sharders, +// }, +// } +// } + +// func SetNetwork(net *Network) { +// _config.chain.Miners = net.net.Miners +// _config.chain.Sharders = net.net.Sharders + +// consensus := _config.chain.SharderConsensous +// if consensus < conf.DefaultSharderConsensous { +// consensus = conf.DefaultSharderConsensous +// } +// if len(net.net.Sharders) < consensus { +// consensus = len(net.net.Sharders) +// } + +// Sharders = node.NewHolder(_config.chain.Sharders, consensus) + +// node.InitCache(Sharders) + +// conf.InitChainNetwork(&conf.Network{ +// Miners: net.net.Miners, +// Sharders: net.net.Sharders, +// }) +// } + +// func GetNetworkJSON() string { +// network := GetNetwork() +// networkBytes, _ := json.Marshal(network) +// return string(networkBytes) +// } diff --git a/zcncore/transaction.go b/zcncore/transaction.go index 7293798e6..b4c6197bc 100644 --- a/zcncore/transaction.go +++ b/zcncore/transaction.go @@ -849,6 +849,9 @@ func NewMSTransaction(walletstr string, cb TransactionCallback) (*Transaction, e return nil, err } cfg, err := conf.GetClientConfig() + if err != nil { + return nil, err + } t := &Transaction{} t.txn = transaction.NewTransactionEntity(w.ClientID, cfg.ChainID, w.ClientKey, w.Nonce) t.txnStatus, t.verifyStatus = StatusUnknown, StatusUnknown @@ -1394,47 +1397,53 @@ func (nc *NonceCache) Evict(clientId string) { delete(nc.cache, clientId) } -// func WithEthereumNode(uri string) func(c *ChainConfig) error { -// return func(c *ChainConfig) error { -// c.EthNode = uri -// return nil -// } -// } - -// func WithChainID(id string) func(c *ChainConfig) error { -// return func(c *ChainConfig) error { -// c.ChainID = id -// return nil -// } -// } - -// func WithMinSubmit(m int) func(c *ChainConfig) error { -// return func(c *ChainConfig) error { -// c.MinSubmit = m -// return nil -// } -// } - -// func WithMinConfirmation(m int) func(c *ChainConfig) error { -// return func(c *ChainConfig) error { -// c.MinConfirmation = m -// return nil -// } -// } - -// func WithConfirmationChainLength(m int) func(c *ChainConfig) error { -// return func(c *ChainConfig) error { -// c.ConfirmationChainLength = m -// return nil -// } -// } - -// func WithSharderConsensous(m int) func(c *ChainConfig) error { -// return func(c *ChainConfig) error { -// c.SharderConsensous = m -// return nil -// } -// } +//Deprecated: client.Init() in core package +func WithEthereumNode(uri string) func(c *ChainConfig) error { + return func(c *ChainConfig) error { + c.EthNode = uri + return nil + } +} + +//Deprecated: client.Init() in core package +func WithChainID(id string) func(c *ChainConfig) error { + return func(c *ChainConfig) error { + c.ChainID = id + return nil + } +} + +//Deprecated: client.Init() in core package +func WithMinSubmit(m int) func(c *ChainConfig) error { + return func(c *ChainConfig) error { + c.MinSubmit = m + return nil + } +} + +//Deprecated: client.Init() in core package +func WithMinConfirmation(m int) func(c *ChainConfig) error { + return func(c *ChainConfig) error { + c.MinConfirmation = m + return nil + } +} + +//Deprecated: client.Init() in core package +func WithConfirmationChainLength(m int) func(c *ChainConfig) error { + return func(c *ChainConfig) error { + c.ConfirmationChainLength = m + return nil + } +} + +//Deprecated: client.Init() in core package +func WithSharderConsensous(m int) func(c *ChainConfig) error { + return func(c *ChainConfig) error { + c.SharderConsensous = m + return nil + } +} // UpdateValidatorSettings update settings of a validator. func (t *Transaction) UpdateValidatorSettings(v *Validator) (err error) { diff --git a/zcncore/transaction_base.go b/zcncore/transaction_base.go index 974583ea3..93507225b 100644 --- a/zcncore/transaction_base.go +++ b/zcncore/transaction_base.go @@ -100,62 +100,47 @@ type TransactionCallback interface { // isSplitWallet bool // } -// type ChainConfig struct { -// ChainID string `json:"chain_id,omitempty"` -// BlockWorker string `json:"block_worker"` -// Miners []string `json:"miners"` -// Sharders []string `json:"sharders"` -// SignatureScheme string `json:"signature_scheme"` -// MinSubmit int `json:"min_submit"` -// MinConfirmation int `json:"min_confirmation"` -// ConfirmationChainLength int `json:"confirmation_chain_length"` -// EthNode string `json:"eth_node"` -// SharderConsensous int `json:"sharder_consensous"` -// } +type ChainConfig struct { + ChainID string `json:"chain_id,omitempty"` + BlockWorker string `json:"block_worker"` + Miners []string `json:"miners"` + Sharders []string `json:"sharders"` + SignatureScheme string `json:"signature_scheme"` + MinSubmit int `json:"min_submit"` + MinConfirmation int `json:"min_confirmation"` + ConfirmationChainLength int `json:"confirmation_chain_length"` + EthNode string `json:"eth_node"` + SharderConsensous int `json:"sharder_consensous"` +} // var Sharders *node.NodeHolder +// Deprecated: Use client.Init() in core/client package // InitZCNSDK initializes the SDK with miner, sharder and signature scheme provided. -// func InitZCNSDK(blockWorker string, signscheme string, configs ...func(*ChainConfig) error) error { -// if signscheme != "ed25519" && signscheme != "bls0chain" { -// return errors.New("", "invalid/unsupported signature scheme") -// } -// _config.chain.BlockWorker = blockWorker -// _config.chain.SignatureScheme = signscheme - -// err := UpdateNetworkDetails() -// if err != nil { -// fmt.Println("UpdateNetworkDetails:", err) -// return err -// } - -// go updateNetworkDetailsWorker(context.Background()) - -// for _, conf := range configs { -// err := conf(&_config.chain) -// if err != nil { -// return errors.Wrap(err, "invalid/unsupported options.") -// } -// } -// assertConfig() -// _config.isConfigured = true -// logging.Info("******* Wallet SDK Version:", version.VERSIONSTR, " ******* (InitZCNSDK)") - -// cfg := &conf.Config{ -// BlockWorker: _config.chain.BlockWorker, -// MinSubmit: _config.chain.MinSubmit, -// MinConfirmation: _config.chain.MinConfirmation, -// ConfirmationChainLength: _config.chain.ConfirmationChainLength, -// SignatureScheme: _config.chain.SignatureScheme, -// ChainID: _config.chain.ChainID, -// EthereumNode: _config.chain.EthNode, -// SharderConsensous: _config.chain.SharderConsensous, -// } - -// conf.InitClientConfig(cfg) - -// return nil -// } +func InitZCNSDK(blockWorker string, signscheme string, configs ...func(*ChainConfig) error) error { + if signscheme != "ed25519" && signscheme != "bls0chain" { + return errors.New("", "invalid/unsupported signature scheme") + } + + chainCfg := &ChainConfig{} + for _, conf := range configs { + err := conf(chainCfg) + if err != nil { + return errors.Wrap(err, "invalid/unsupported options.") + } + } + cfg := conf.Config{ + BlockWorker: blockWorker, + SignatureScheme: signscheme, + MinSubmit: chainCfg.MinSubmit, + MinConfirmation: chainCfg.MinConfirmation, + ConfirmationChainLength: chainCfg.ConfirmationChainLength, + ChainID: chainCfg.ChainID, + EthereumNode: chainCfg.EthNode, + SharderConsensous: chainCfg.SharderConsensous, + } + return client.Init(context.Background(), cfg) +} /*Confirmation - a data structure that provides the confirmation that a transaction is included into the block chain */ type confirmation struct { @@ -588,7 +573,7 @@ func queryFromMinersContext(ctx context.Context, numMiners int, query string, re nodeClient, err := client.GetNode() if err != nil { panic(err) - } + } randomMiners := util.Shuffle(nodeClient.Network().Miners())[:numMiners] for _, miner := range randomMiners { go func(minerurl string) { @@ -654,7 +639,7 @@ func getBlockInfoByRound(round int64, content string) (*blockHeader, error) { nodeClient, err := client.GetNode() if err != nil { return nil, err - } + } numSharders := len(nodeClient.Sharders().Healthy()) // overwrite, use all resultC := make(chan *util.GetResponse, numSharders) nodeClient.Sharders().QueryFromSharders(numSharders, fmt.Sprintf("%vround=%v&content=%v", GET_BLOCK_INFO, round, content), resultC) diff --git a/zcncore/transaction_mobile.go b/zcncore/transaction_mobile.go index 52ba609be..c43c8c611 100644 --- a/zcncore/transaction_mobile.go +++ b/zcncore/transaction_mobile.go @@ -14,6 +14,7 @@ import ( "github.com/0chain/errors" "github.com/0chain/gosdk/core/block" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/node" @@ -348,8 +349,8 @@ func NewTransaction(cb TransactionCallback, txnFee string, nonce int64) (Transac if err != nil { return nil, err } - if _config.isSplitWallet { - if _config.authUrl == "" { + if client.SplitKeyWallet() { + if client.AuthUrl() == "" { return nil, errors.New("", "auth url not set") } logging.Info("New transaction interface with auth") @@ -945,7 +946,12 @@ func (t *Transaction) Verify() error { t.txn.CreationDate = int64(common.Now()) } - tq, err := newTransactionQuery(Sharders.Healthy()) + nodeClient, err := client.GetNode() + if err != nil { + return err + } + + tq, err := newTransactionQuery(nodeClient.Sharders().Healthy()) if err != nil { logging.Error(err) return err @@ -971,8 +977,8 @@ func (t *Transaction) Verify() error { // transaction is done or expired. it means random sharder might be outdated, try to query it from s/S sharders to confirm it if util.MaxInt64(lfbBlockHeader.getCreationDate(now), now) >= (t.txn.CreationDate + int64(defaultTxnExpirationSeconds)) { - logging.Info("falling back to ", getMinShardersVerify(), " of ", len(_config.chain.Sharders), " Sharders", len(Sharders.Healthy()), "Healthy sharders") - confirmBlockHeader, confirmationBlock, lfbBlockHeader, err = tq.getConsensusConfirmation(getMinShardersVerify(), t.txnHash, nil) + logging.Info("falling back to ", nodeClient.GetMinShardersVerify(), " of ", len(nodeClient.Network().Sharders()), " Sharders", len(nodeClient.Sharders().Healthy()), "Healthy sharders") + confirmBlockHeader, confirmationBlock, lfbBlockHeader, err = tq.getConsensusConfirmation(nodeClient.GetMinShardersVerify(), t.txnHash, nil) } // txn not found in fast confirmation/consensus confirmation @@ -1042,7 +1048,11 @@ func (t *Transaction) ZCNSCAddAuthorizer(ip AddAuthorizerPayload) (err error) { // EstimateFee estimates transaction fee func (t *Transaction) EstimateFee(reqPercent float32) (int64, error) { - fee, err := transaction.EstimateFee(t.txn, _config.chain.Miners, reqPercent) + nodeClient, err := client.GetNode() + if err != nil { + return 0, err + } + fee, err := transaction.EstimateFee(t.txn, nodeClient.Network().Miners(), reqPercent) return int64(fee), err } @@ -1071,14 +1081,18 @@ func makeTimeoutContext(tm RequestTimeout) (context.Context, func()) { } func GetLatestFinalized(numSharders int, timeout RequestTimeout) (b *BlockHeader, err error) { + nodeClient, err := client.GetNode() + if err != nil { + return nil, err + } var result = make(chan *util.GetResponse, numSharders) defer close(result) ctx, cancel := makeTimeoutContext(timeout) defer cancel() - numSharders = len(Sharders.Healthy()) // overwrite, use all - Sharders.QueryFromShardersContext(ctx, numSharders, GET_LATEST_FINALIZED, result) + numSharders = len(nodeClient.Sharders().Healthy()) // overwrite, use all + nodeClient.Sharders().QueryFromShardersContext(ctx, numSharders, GET_LATEST_FINALIZED, result) var ( maxConsensus int @@ -1119,20 +1133,23 @@ func GetLatestFinalized(numSharders int, timeout RequestTimeout) (b *BlockHeader } func GetLatestFinalizedMagicBlock(numSharders int, timeout RequestTimeout) ([]byte, error) { + nodeClient, err := client.GetNode() + if err != nil { + return nil, err + } var result = make(chan *util.GetResponse, numSharders) defer close(result) ctx, cancel := makeTimeoutContext(timeout) defer cancel() - numSharders = len(Sharders.Healthy()) // overwrite, use all - Sharders.QueryFromShardersContext(ctx, numSharders, GET_LATEST_FINALIZED_MAGIC_BLOCK, result) + numSharders = len(nodeClient.Sharders().Healthy()) // overwrite, use all + nodeClient.Sharders().QueryFromShardersContext(ctx, numSharders, GET_LATEST_FINALIZED_MAGIC_BLOCK, result) var ( maxConsensus int roundConsensus = make(map[string]int) m *block.MagicBlock - err error ) type respObj struct { @@ -1181,6 +1198,10 @@ func GetLatestFinalizedMagicBlock(numSharders int, timeout RequestTimeout) ([]by // GetChainStats gets chain stats with time out // timeout in milliseconds func GetChainStats(timeout RequestTimeout) ([]byte, error) { + nodeClient, err := client.GetNode() + if err != nil { + return nil, err + } var result = make(chan *util.GetResponse, 1) defer close(result) @@ -1189,11 +1210,10 @@ func GetChainStats(timeout RequestTimeout) ([]byte, error) { var ( b *block.ChainStats - err error ) - var numSharders = len(Sharders.Healthy()) // overwrite, use all - Sharders.QueryFromShardersContext(ctx, numSharders, GET_CHAIN_STATS, result) + var numSharders = len(nodeClient.Sharders().Healthy()) // overwrite, use all + nodeClient.Sharders().QueryFromShardersContext(ctx, numSharders, GET_CHAIN_STATS, result) var rsp *util.GetResponse for i := 0; i < numSharders; i++ { var x = <-result @@ -1219,11 +1239,15 @@ func GetChainStats(timeout RequestTimeout) ([]byte, error) { } func GetFeeStats(timeout RequestTimeout) ([]byte, error) { + nodeClient, err := client.GetNode() + if err != nil { + return nil ,err + } var numMiners = 4 - if numMiners > len(_config.chain.Miners) { - numMiners = len(_config.chain.Miners) + if numMiners > len(nodeClient.Network().Miners()) { + numMiners = len(nodeClient.Network().Miners()) } var result = make(chan *util.GetResponse, numMiners) @@ -1233,7 +1257,6 @@ func GetFeeStats(timeout RequestTimeout) ([]byte, error) { var ( b *block.FeeStats - err error ) queryFromMinersContext(ctx, numMiners, GET_FEE_STATS, result) @@ -1433,14 +1456,18 @@ func (t *timeoutCtx) Get() int64 { } func GetBlockByRound(numSharders int, round int64, timeout RequestTimeout) (b *Block, err error) { + nodeClient, err := client.GetNode() + if err != nil { + return nil, err + } var result = make(chan *util.GetResponse, numSharders) defer close(result) ctx, cancel := makeTimeoutContext(timeout) defer cancel() - numSharders = len(Sharders.Healthy()) // overwrite, use all - Sharders.QueryFromShardersContext(ctx, numSharders, + numSharders = len(nodeClient.Sharders().Healthy()) // overwrite, use all + nodeClient.Sharders().QueryFromShardersContext(ctx, numSharders, fmt.Sprintf("%sround=%d&content=full,header", GET_BLOCK_INFO, round), result) @@ -1518,14 +1545,18 @@ func GetBlockByRound(numSharders int, round int64, timeout RequestTimeout) (b *B } func GetMagicBlockByNumber(numSharders int, number int64, timeout RequestTimeout) ([]byte, error) { + nodeClient, err := client.GetNode() + if err != nil { + return nil, err + } var result = make(chan *util.GetResponse, numSharders) defer close(result) ctx, cancel := makeTimeoutContext(timeout) defer cancel() - numSharders = len(Sharders.Healthy()) // overwrite, use all - Sharders.QueryFromShardersContext(ctx, numSharders, + numSharders = len(nodeClient.Sharders().Healthy()) // overwrite, use all + nodeClient.Sharders().QueryFromShardersContext(ctx, numSharders, fmt.Sprintf("%smagic_block_number=%d", GET_MAGIC_BLOCK_INFO, number), result) @@ -1533,7 +1564,6 @@ func GetMagicBlockByNumber(numSharders int, number int64, timeout RequestTimeout maxConsensus int roundConsensus = make(map[string]int) ret []byte - err error ) type respObj struct { @@ -1581,8 +1611,12 @@ func GetMagicBlockByNumber(numSharders int, number int64, timeout RequestTimeout // GetFeesTable get fee tables func GetFeesTable(reqPercent float32) (string, error) { + nodeClient, err := client.GetNode() + if err != nil { + return "", err + } - fees, err := transaction.GetFeesTable(_config.chain.Miners, reqPercent) + fees, err := transaction.GetFeesTable(nodeClient.Network().Miners(), reqPercent) if err != nil { return "", err } diff --git a/zcncore/transaction_query_mobile.go b/zcncore/transaction_query_mobile.go index 5756b586e..c2a81532d 100644 --- a/zcncore/transaction_query_mobile.go +++ b/zcncore/transaction_query_mobile.go @@ -15,6 +15,7 @@ import ( "time" thrown "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/resty" "github.com/0chain/gosdk/core/util" ) @@ -457,8 +458,13 @@ func (tq *transactionQuery) getFastConfirmation(txnHash string, timeout RequestT } func GetInfoFromSharders(urlSuffix string, op int, cb GetInfoCallback) { + nodeClient, err := client.GetNode() + if err != nil { + cb.OnInfoAvailable(op, StatusError, "", err.Error()) + return + } - tq, err := newTransactionQuery(util.Shuffle(Sharders.Healthy())) + tq, err := newTransactionQuery(util.Shuffle(nodeClient.Sharders().Healthy())) if err != nil { cb.OnInfoAvailable(op, StatusError, "", err.Error()) return @@ -474,8 +480,13 @@ func GetInfoFromSharders(urlSuffix string, op int, cb GetInfoCallback) { } func GetInfoFromAnySharder(urlSuffix string, op int, cb GetInfoCallback) { + nodeClient, err := client.GetNode() + if err != nil { + cb.OnInfoAvailable(op, StatusError, "", err.Error()) + return + } - tq, err := newTransactionQuery(util.Shuffle(Sharders.Healthy())) + tq, err := newTransactionQuery(util.Shuffle(nodeClient.Sharders().Healthy())) if err != nil { cb.OnInfoAvailable(op, StatusError, "", err.Error()) return diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 76e34e131..9000bcbb0 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -310,6 +310,35 @@ func SetLogFile(logFile string, verbose bool) { logging.Info("******* Wallet SDK Version:", version.VERSIONSTR, " ******* (SetLogFile)") } +//Deprecated: Use client.Init() in core/client package +// Init initialize the SDK with miner, sharder and signature scheme provided in configuration provided in JSON format +// # Inputs +// - chainConfigJSON: json format of zcn config +// { +// "block_worker": "https://dev.0chain.net/dns", +// "signature_scheme": "bls0chain", +// "min_submit": 50, +// "min_confirmation": 50, +// "confirmation_chain_length": 3, +// "max_txn_query": 5, +// "query_sleep_time": 5, +// "preferred_blobbers": ["https://dev.0chain.net/blobber02","https://dev.0chain.net/blobber03"], +// "chain_id":"0afc093ffb509f059c55478bc1a60351cef7b4e9c008a53a6cc8241ca8617dfe", +// "ethereum_node":"https://ropsten.infura.io/v3/xxxxxxxxxxxxxxx", +// "zbox_host":"https://0box.dev.0chain.net", +// "zbox_app_type":"vult", +// "sharder_consensous": 2, +// } +func Init(chainConfigJSON string) error { + cfg := conf.Config{} + err := json.Unmarshal([]byte(chainConfigJSON), &cfg) + if err != nil { + return err + } + client.Init(context.Background(), cfg) + return nil +} + // InitSignatureScheme initializes signature scheme only. func InitSignatureScheme(scheme string) { cfg := conf.Config{ diff --git a/zcncore/wallet_mobile.go b/zcncore/wallet_mobile.go index 6dff0924a..68103f28a 100644 --- a/zcncore/wallet_mobile.go +++ b/zcncore/wallet_mobile.go @@ -4,6 +4,7 @@ package zcncore import ( + "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/zcncrypto" ) @@ -16,8 +17,12 @@ type wallet struct { } func (w *wallet) Sign(hash string) (string, error) { - sigScheme := zcncrypto.NewSignatureScheme(_config.chain.SignatureScheme) - err := sigScheme.SetPrivateKey(w.Keys[0].PrivateKey) + cfg, err := conf.GetClientConfig() + if err != nil { + return "", err + } + sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) + err = sigScheme.SetPrivateKey(w.Keys[0].PrivateKey) if err != nil { return "", err } From d86f0c6898ae8635eec6a72dd55b566fedcea201 Mon Sep 17 00:00:00 2001 From: storybehind Date: Sat, 10 Feb 2024 20:43:25 +0530 Subject: [PATCH 005/319] fix lint and system test issues --- core/client/init_node.go | 6 +- zcncore/networkworker.go | 108 ++++++++++++++ zcncore/networkworker_mobile.go | 244 +++++++++++++++----------------- zcncore/wallet_base.go | 3 +- 4 files changed, 225 insertions(+), 136 deletions(-) create mode 100644 zcncore/networkworker.go diff --git a/core/client/init_node.go b/core/client/init_node.go index 4c5bf6c8a..6230cb284 100644 --- a/core/client/init_node.go +++ b/core/client/init_node.go @@ -83,7 +83,7 @@ func (n *Node) ShouldUpdateNetwork() (bool, *conf.Network, error) { if err != nil { return false, nil, err } - network, err := getNetwork(n.clientCtx, cfg.BlockWorker) + network, err := GetNetwork(n.clientCtx, cfg.BlockWorker) if err != nil { logging.Error("Failed to get network details ", zap.Error(err)) return false, nil, err @@ -125,7 +125,7 @@ func Init(ctx context.Context, cfg conf.Config) error { // set default value for options if unset setOptionsDefaultValue(&cfg) - network, err := getNetwork(ctx, cfg.BlockWorker) + network, err := GetNetwork(ctx, cfg.BlockWorker) if err != nil { logging.Error("Failed to get network details ", zap.Error(err)) return err @@ -178,7 +178,7 @@ func GetNode() (*Node, error) { return nil, errors.New("0chain-sdk is not initialized") } -func getNetwork(ctx context.Context, blockWorker string) (*conf.Network, error) { +func GetNetwork(ctx context.Context, blockWorker string) (*conf.Network, error) { networkUrl := blockWorker + "/network" networkGetCtx, networkGetCancelCtx := context.WithTimeout(ctx, 60*time.Second) defer networkGetCancelCtx() diff --git a/zcncore/networkworker.go b/zcncore/networkworker.go new file mode 100644 index 000000000..4f7ae7c1b --- /dev/null +++ b/zcncore/networkworker.go @@ -0,0 +1,108 @@ +//go:build !mobile +// +build !mobile + +package zcncore + +import ( + "context" + "encoding/json" + + "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/conf" +) + +const NETWORK_ENDPOINT = "/network" + +type Network struct { + Miners []string `json:"miners"` + Sharders []string `json:"sharders"` +} + +//Deprecated: Get client.Node instance to check whether network update is required and update network accordingly +func UpdateNetworkDetails() error { + nodeClient, err := client.GetNode() + if err != nil { + return err + } + shouldUpdate, network, err := nodeClient.ShouldUpdateNetwork() + if err != nil { + logging.Error("error on ShouldUpdateNetwork check: ", err) + return err + } + if shouldUpdate { + logging.Info("Updating network") + if err = nodeClient.UpdateNetwork(network); err != nil { + logging.Error("error on updating network: ", err) + return err + } + logging.Info("network updated successfully") + } + return nil +} + +//Deprecated: Get client.Node instance to check whether network update is required +func UpdateRequired(networkDetails *Network) bool { + nodeClient, err := client.GetNode() + if err != nil { + panic(err) + } + shouldUpdate, _, err := nodeClient.ShouldUpdateNetwork() + if err != nil { + logging.Error("error on ShouldUpdateNetwork check: ", err) + panic(err) + } + return shouldUpdate +} + +//Deprecated: Use client.GetNetwork() function +func GetNetworkDetails() (*Network, error) { + cfg, err := conf.GetClientConfig() + if err != nil { + return nil, err + } + network, err := client.GetNetwork(context.Background(), cfg.BlockWorker) + if err != nil { + return nil, err + } + return &Network{ + Miners: network.Miners(), + Sharders: network.Sharders(), + }, nil +} + +//Deprecated: Use client.Node instance to get its network details +func GetNetwork() *Network { + nodeClient, err := client.GetNode() + if err != nil { + panic(err) + } + return &Network{ + Miners: nodeClient.Network().Miners(), + Sharders: nodeClient.Network().Sharders(), + } +} + +//Deprecated: Use client.Node instance UpdateNetwork() method +func SetNetwork(miners []string, sharders []string) { + nodeClient, err := client.GetNode() + if err != nil { + panic(err) + } + network, err := conf.NewNetwork(miners, sharders) + if err != nil { + panic(err) + } + err = nodeClient.UpdateNetwork(network) + if err != nil { + logging.Error("error updating network: ", err) + panic(err) + } + logging.Info("network updated successfully") +} + +//Deprecated: Use client.GetNetwork() function +func GetNetworkJSON() string { + network := GetNetwork() + networkBytes, _ := json.Marshal(network) + return string(networkBytes) +} diff --git a/zcncore/networkworker_mobile.go b/zcncore/networkworker_mobile.go index a5c407361..0ee0805e2 100644 --- a/zcncore/networkworker_mobile.go +++ b/zcncore/networkworker_mobile.go @@ -3,43 +3,38 @@ package zcncore -// import ( -// "context" -// "encoding/json" -// "reflect" -// "time" +import ( + "context" + "encoding/json" -// "github.com/0chain/errors" -// "github.com/0chain/gosdk/core/conf" -// "github.com/0chain/gosdk/core/node" -// "github.com/0chain/gosdk/core/util" -// "go.uber.org/zap" -// ) + "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/conf" +) -// const NETWORK_ENDPOINT = "/network" +const NETWORK_ENDPOINT = "/network" -// var networkWorkerTimerInHours = 1 +var networkWorkerTimerInHours = 1 -// type Network struct { -// net network -// } +type Network struct { + net network +} -// func NewNetwork() *Network { -// return &Network{} -// } +func NewNetwork() *Network { + return &Network{} +} -// func (net *Network) AddMiner(miner string) { -// net.net.Miners = append(net.net.Miners, miner) -// } +func (net *Network) AddMiner(miner string) { + net.net.Miners = append(net.net.Miners, miner) +} -// func (net *Network) AddSharder(sharder string) { -// net.net.Sharders = append(net.net.Sharders, sharder) -// } +func (net *Network) AddSharder(sharder string) { + net.net.Sharders = append(net.net.Sharders, sharder) +} -// type network struct { -// Miners []string `json:"miners"` -// Sharders []string `json:"sharders"` -// } +type network struct { + Miners []string `json:"miners"` + Sharders []string `json:"sharders"` +} // func updateNetworkDetailsWorker(ctx context.Context) { // ticker := time.NewTicker(time.Duration(networkWorkerTimerInHours) * time.Hour) @@ -60,105 +55,92 @@ package zcncore // } // } -// func UpdateNetworkDetails() error { -// networkDetails, err := GetNetworkDetails() -// if err != nil { -// logging.Error("Failed to update network details ", zap.Error(err)) -// return err -// } - -// shouldUpdate := UpdateRequired(networkDetails) -// if shouldUpdate { -// _config.isConfigured = false -// _config.chain.Miners = networkDetails.net.Miners -// _config.chain.Sharders = networkDetails.net.Sharders -// consensus := _config.chain.SharderConsensous -// if consensus < conf.DefaultSharderConsensous { -// consensus = conf.DefaultSharderConsensous -// } -// if len(networkDetails.net.Sharders) < consensus { -// consensus = len(networkDetails.net.Sharders) -// } - -// Sharders = node.NewHolder(networkDetails.net.Sharders, consensus) -// node.InitCache(Sharders) -// conf.InitChainNetwork(&conf.Network{ -// Sharders: networkDetails.net.Sharders, -// Miners: networkDetails.net.Miners, -// }) -// _config.isConfigured = true -// } -// return nil -// } - -// func UpdateRequired(networkDetails *Network) bool { -// miners := _config.chain.Miners -// sharders := _config.chain.Sharders -// if len(miners) == 0 || len(sharders) == 0 { -// return true -// } - -// minerSame := reflect.DeepEqual(miners, networkDetails.net.Miners) -// sharderSame := reflect.DeepEqual(sharders, networkDetails.net.Sharders) - -// if minerSame && sharderSame { -// return false -// } -// return true -// } - -// func GetNetworkDetails() (*Network, error) { -// req, err := util.NewHTTPGetRequest(_config.chain.BlockWorker + NETWORK_ENDPOINT) -// if err != nil { -// return nil, errors.New("get_network_details_error", "Unable to create new http request with error "+err.Error()) -// } - -// res, err := req.Get() -// if err != nil { -// return nil, errors.New("get_network_details_error", "Unable to get http request with error "+err.Error()) -// } - -// var networkResponse network -// err = json.Unmarshal([]byte(res.Body), &networkResponse) -// if err != nil { -// return nil, errors.Wrap(err, "Error unmarshaling response :") -// } -// return &Network{net: networkResponse}, nil -// } - -// func GetNetwork() *Network { -// return &Network{ -// net: network{ -// Miners: _config.chain.Miners, -// Sharders: _config.chain.Sharders, -// }, -// } -// } - -// func SetNetwork(net *Network) { -// _config.chain.Miners = net.net.Miners -// _config.chain.Sharders = net.net.Sharders - -// consensus := _config.chain.SharderConsensous -// if consensus < conf.DefaultSharderConsensous { -// consensus = conf.DefaultSharderConsensous -// } -// if len(net.net.Sharders) < consensus { -// consensus = len(net.net.Sharders) -// } - -// Sharders = node.NewHolder(_config.chain.Sharders, consensus) - -// node.InitCache(Sharders) - -// conf.InitChainNetwork(&conf.Network{ -// Miners: net.net.Miners, -// Sharders: net.net.Sharders, -// }) -// } - -// func GetNetworkJSON() string { -// network := GetNetwork() -// networkBytes, _ := json.Marshal(network) -// return string(networkBytes) -// } +//Deprecated: Get client.Node instance to check whether network update is required and update network accordingly +func UpdateNetworkDetails() error { + nodeClient, err := client.GetNode() + if err != nil { + return err + } + shouldUpdate, network, err := nodeClient.ShouldUpdateNetwork() + if err != nil { + logging.Error("error on ShouldUpdateNetwork check: ", err) + return err + } + if shouldUpdate { + logging.Info("Updating network") + if err = nodeClient.UpdateNetwork(network); err != nil { + logging.Error("error on updating network: ", err) + return err + } + logging.Info("network updated successfully") + } + return nil +} + +//Deprecated: Get client.Node instance to check whether network update is required +func UpdateRequired(networkDetails *Network) bool { + nodeClient, err := client.GetNode() + if err != nil { + panic(err) + } + shouldUpdate, _, err := nodeClient.ShouldUpdateNetwork() + if err != nil { + logging.Error("error on ShouldUpdateNetwork check: ", err) + panic(err) + } + return shouldUpdate +} + +//Deprecated: Use client.GetNetwork() function +func GetNetworkDetails() (*Network, error) { + cfg, err := conf.GetClientConfig() + if err != nil { + return nil, err + } + network, err := client.GetNetwork(context.Background(), cfg.BlockWorker) + if err != nil { + return nil, err + } + n := NewNetwork() + n.net.Miners = network.Miners() + n.net.Sharders = network.Sharders() + return n, nil +} + +//Deprecated: Use client.Node instance to get its network details +func GetNetwork() *Network { + nodeClient, err := client.GetNode() + if err != nil { + panic(err) + } + n := NewNetwork() + n.net.Miners = nodeClient.Network().Miners() + n.net.Sharders = nodeClient.Network().Sharders() + return n +} + +//Deprecated: Use client.Node instance UpdateNetwork() method +func SetNetwork(miners []string, sharders []string) { + nodeClient, err := client.GetNode() + if err != nil { + panic(err) + } + network, err := conf.NewNetwork(miners, sharders) + if err != nil { + panic(err) + } + err = nodeClient.UpdateNetwork(network) + if err != nil { + logging.Error("error updating network: ", err) + panic(err) + } + logging.Info("network updated successfully") +} + + +//Deprecated: Use client.GetNetwork() function +func GetNetworkJSON() string { + network := GetNetwork() + networkBytes, _ := json.Marshal(network) + return string(networkBytes) +} diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 9000bcbb0..de5a4d1e7 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -335,8 +335,7 @@ func Init(chainConfigJSON string) error { if err != nil { return err } - client.Init(context.Background(), cfg) - return nil + return client.Init(context.Background(), cfg) } // InitSignatureScheme initializes signature scheme only. From 8681bef57b280a59f5d7e5511239882ef5cc14c4 Mon Sep 17 00:00:00 2001 From: storybehind Date: Sun, 11 Feb 2024 13:41:39 +0530 Subject: [PATCH 006/319] fix system test failures --- core/client/init_node.go | 12 ++++++------ core/conf/network.go | 34 ++++++++++++++++----------------- zboxcore/sdk/networkworker.go | 9 ++++++--- zcnbridge/http/rest.go | 2 +- zcncore/networkworker.go | 8 ++++---- zcncore/networkworker_mobile.go | 8 ++++---- zcncore/transaction.go | 16 ++++++++-------- zcncore/transaction_base.go | 8 ++++---- zcncore/transaction_mobile.go | 10 +++++----- zcncore/transaction_query.go | 2 +- zcncore/wallet_base.go | 2 +- zmagmacore/http/sc-api.go | 2 +- 12 files changed, 58 insertions(+), 55 deletions(-) diff --git a/core/client/init_node.go b/core/client/init_node.go index 6230cb284..3714e31a9 100644 --- a/core/client/init_node.go +++ b/core/client/init_node.go @@ -49,8 +49,8 @@ func (n *Node) ResetStableMiners() { n.networkGuard.Lock() defer n.networkGuard.Unlock() cfg, _ := conf.GetClientConfig() - reqMiners := util.MaxInt(1, int(math.Ceil(float64(cfg.MinSubmit)*float64(len(n.network.Miners()))/100))) - n.stableMiners = util.GetRandom(n.network.Miners(), reqMiners) + reqMiners := util.MaxInt(1, int(math.Ceil(float64(cfg.MinSubmit)*float64(len(n.network.Miners))/100))) + n.stableMiners = util.GetRandom(n.network.Miners, reqMiners) } func (n *Node) GetMinShardersVerify() int { @@ -104,7 +104,7 @@ func (n *Node) UpdateNetwork(network *conf.Network) error { return err } n.network = network - n.sharders = node.NewHolder(n.network.Sharders(), util.MinInt(len(n.network.Sharders()), util.MaxInt(cfg.SharderConsensous, conf.DefaultSharderConsensous))) + n.sharders = node.NewHolder(n.network.Sharders, util.MinInt(len(n.network.Sharders), util.MaxInt(cfg.SharderConsensous, conf.DefaultSharderConsensous))) node.InitCache(n.sharders) return nil } @@ -131,10 +131,10 @@ func Init(ctx context.Context, cfg conf.Config) error { return err } - reqMiners := util.MaxInt(1, int(math.Ceil(float64(cfg.MinSubmit)*float64(len(network.Miners()))/100))) - sharders := node.NewHolder(network.Sharders(), util.MinInt(len(network.Sharders()), util.MaxInt(cfg.SharderConsensous, conf.DefaultSharderConsensous))) + reqMiners := util.MaxInt(1, int(math.Ceil(float64(cfg.MinSubmit)*float64(len(network.Miners))/100))) + sharders := node.NewHolder(network.Sharders, util.MinInt(len(network.Sharders), util.MaxInt(cfg.SharderConsensous, conf.DefaultSharderConsensous))) nodeClient = &Node{ - stableMiners: util.GetRandom(network.Miners(), reqMiners), + stableMiners: util.GetRandom(network.Miners, reqMiners), sharders: sharders, // config: &cfg, network: network, diff --git a/core/conf/network.go b/core/conf/network.go index bddcc060c..7c3f1e612 100644 --- a/core/conf/network.go +++ b/core/conf/network.go @@ -13,15 +13,15 @@ import ( // Network settings from ~/.zcn/network.yaml type Network struct { // Sharders sharder list of blockchain - sharders []string + Sharders []string // Miners miner list of blockchain - miners []string + Miners []string } func NewNetwork(miners, sharders []string) (*Network, error) { n := &Network{ - miners: miners, - sharders: sharders, + Miners: miners, + Sharders: sharders, } if !n.IsValid() { return nil, errors.New("network has no miners/sharders") @@ -32,24 +32,24 @@ func NewNetwork(miners, sharders []string) (*Network, error) { // IsValid check network if it has miners and sharders func (n *Network) IsValid() bool { - return n != nil && len(n.miners) > 0 && len(n.sharders) > 0 + return n != nil && len(n.Miners) > 0 && len(n.Sharders) > 0 } -func (n *Network) Miners() []string { - return n.miners -} +// func (n *Network) Miners() []string { +// return n.miners +// } -func (n *Network) Sharders() []string { - return n.sharders -} +// func (n *Network) Sharders() []string { +// return n.sharders +// } func (n *Network) NormalizeURLs() { - for i := 0; i < len(n.miners); i++ { - n.miners[i] = strings.TrimSuffix(n.miners[i], "/") + for i := 0; i < len(n.Miners); i++ { + n.Miners[i] = strings.TrimSuffix(n.Miners[i], "/") } - for i := 0; i < len(n.sharders); i++ { - n.sharders[i] = strings.TrimSuffix(n.sharders[i], "/") + for i := 0; i < len(n.Sharders); i++ { + n.Sharders[i] = strings.TrimSuffix(n.Sharders[i], "/") } } @@ -82,7 +82,7 @@ func LoadNetworkFile(file string) (Network, error) { // LoadNetwork load and parse network func LoadNetwork(v Reader) Network { return Network{ - sharders: v.GetStringSlice("sharders"), - miners: v.GetStringSlice("miners"), + Sharders: v.GetStringSlice("sharders"), + Miners: v.GetStringSlice("miners"), } } diff --git a/zboxcore/sdk/networkworker.go b/zboxcore/sdk/networkworker.go index 067cecc2c..882d30b01 100644 --- a/zboxcore/sdk/networkworker.go +++ b/zboxcore/sdk/networkworker.go @@ -74,9 +74,12 @@ func forceUpdateNetworkDetails(networkDetails *Network) { blockchain.SetMiners(networkDetails.Miners) blockchain.SetSharders(networkDetails.Sharders) node.InitCache(blockchain.Sharders) - n, _ := conf.NewNetwork(networkDetails.Miners, networkDetails.Sharders) - networkDetails.Miners = n.Miners() - networkDetails.Sharders = n.Sharders() + n, err := conf.NewNetwork(networkDetails.Miners, networkDetails.Sharders) + if err != nil { + panic(err) + } + networkDetails.Miners = n.Miners + networkDetails.Sharders = n.Sharders sdkInitialized = true } diff --git a/zcnbridge/http/rest.go b/zcnbridge/http/rest.go index 091fb3222..8c4cf3dbe 100644 --- a/zcnbridge/http/rest.go +++ b/zcnbridge/http/rest.go @@ -173,7 +173,7 @@ func extractSharders() []string { if err != nil { panic(err) } - sharders := nodeClient.Network().Sharders() + sharders := nodeClient.Network().Sharders return util.GetRandom(sharders, len(sharders)) } diff --git a/zcncore/networkworker.go b/zcncore/networkworker.go index 4f7ae7c1b..d7582f235 100644 --- a/zcncore/networkworker.go +++ b/zcncore/networkworker.go @@ -65,8 +65,8 @@ func GetNetworkDetails() (*Network, error) { return nil, err } return &Network{ - Miners: network.Miners(), - Sharders: network.Sharders(), + Miners: network.Miners, + Sharders: network.Sharders, }, nil } @@ -77,8 +77,8 @@ func GetNetwork() *Network { panic(err) } return &Network{ - Miners: nodeClient.Network().Miners(), - Sharders: nodeClient.Network().Sharders(), + Miners: nodeClient.Network().Miners, + Sharders: nodeClient.Network().Sharders, } } diff --git a/zcncore/networkworker_mobile.go b/zcncore/networkworker_mobile.go index 0ee0805e2..09d45a6b7 100644 --- a/zcncore/networkworker_mobile.go +++ b/zcncore/networkworker_mobile.go @@ -102,8 +102,8 @@ func GetNetworkDetails() (*Network, error) { return nil, err } n := NewNetwork() - n.net.Miners = network.Miners() - n.net.Sharders = network.Sharders() + n.net.Miners = network.Miners + n.net.Sharders = network.Sharders return n, nil } @@ -114,8 +114,8 @@ func GetNetwork() *Network { panic(err) } n := NewNetwork() - n.net.Miners = nodeClient.Network().Miners() - n.net.Sharders = nodeClient.Network().Sharders() + n.net.Miners = nodeClient.Network().Miners + n.net.Sharders = nodeClient.Network().Sharders return n } diff --git a/zcncore/transaction.go b/zcncore/transaction.go index 234ca622c..5a27878f4 100644 --- a/zcncore/transaction.go +++ b/zcncore/transaction.go @@ -297,7 +297,7 @@ func (t *Transaction) Send(toClientID string, val uint64, desc string) error { t.txn.Value = val t.txn.TransactionData = string(txnData) if t.txn.TransactionFee == 0 { - fee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners(), 0.2) + fee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners, 0.2) if err != nil { return err } @@ -327,7 +327,7 @@ func (t *Transaction) SendWithSignatureHash(toClientID string, val uint64, desc t.txn.Signature = sig t.txn.CreationDate = CreationDate if t.txn.TransactionFee == 0 { - fee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners(), 0.2) + fee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners, 0.2) if err != nil { return err } @@ -825,7 +825,7 @@ func (t *Transaction) RegisterMultiSig(walletstr string, mswallet string) error t.txn.TransactionNonce = nonce if t.txn.TransactionFee == 0 { - fee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners(), 0.2) + fee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners, 0.2) if err != nil { return } @@ -897,7 +897,7 @@ func (t *Transaction) RegisterVote(signerwalletstr string, msvstr string) error t.txn.TransactionNonce = nonce if t.txn.TransactionFee == 0 { - fee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners(), 0.2) + fee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners, 0.2) if err != nil { return } @@ -1006,7 +1006,7 @@ func (t *Transaction) Verify() error { t.txn.CreationDate = int64(common.Now()) } - tq, err := NewTransactionQuery(clientNode.Sharders().Healthy(), clientNode.Network().Miners()) + tq, err := NewTransactionQuery(clientNode.Sharders().Healthy(), clientNode.Network().Miners) if err != nil { logging.Error(err) return err @@ -1031,7 +1031,7 @@ func (t *Transaction) Verify() error { // transaction is done or expired. it means random sharder might be outdated, try to query it from s/S sharders to confirm it if util.MaxInt64(lfbBlockHeader.getCreationDate(now), now) >= (t.txn.CreationDate + int64(defaultTxnExpirationSeconds)) { - logging.Info("falling back to ", clientNode.GetMinShardersVerify(), " of ", len(clientNode.Network().Sharders()), " Sharders") + logging.Info("falling back to ", clientNode.GetMinShardersVerify(), " of ", len(clientNode.Network().Sharders), " Sharders") confirmBlockHeader, confirmationBlock, lfbBlockHeader, err = tq.getConsensusConfirmation(context.TODO(), clientNode.GetMinShardersVerify(), t.txnHash) } @@ -1241,8 +1241,8 @@ func GetFeeStats(ctx context.Context) (b *block.FeeStats, err error) { } var numMiners = 4 - if numMiners > len(clientNode.Network().Miners()) { - numMiners = len(clientNode.Network().Miners()) + if numMiners > len(clientNode.Network().Miners) { + numMiners = len(clientNode.Network().Miners) } var result = make(chan *util.GetResponse, numMiners) diff --git a/zcncore/transaction_base.go b/zcncore/transaction_base.go index b708dcdc1..6003fd052 100644 --- a/zcncore/transaction_base.go +++ b/zcncore/transaction_base.go @@ -522,7 +522,7 @@ func (t *Transaction) createSmartContractTxn(address, methodName string, input i } // TODO: check if transaction is exempt to avoid unnecessary fee estimation - minFee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners(), 0.2) + minFee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners, 0.2) if err != nil { logger.Logger.Error("failed estimate txn fee", zap.Any("txn", t.txn.Hash), @@ -609,7 +609,7 @@ func queryFromMinersContext(ctx context.Context, numMiners int, query string, re if err != nil { panic(err) } - randomMiners := util.Shuffle(nodeClient.Network().Miners())[:numMiners] + randomMiners := util.Shuffle(nodeClient.Network().Miners)[:numMiners] for _, miner := range randomMiners { go func(minerurl string) { logging.Info("Query from ", minerurl+query) @@ -753,7 +753,7 @@ func validateChain(confirmBlock *blockHeader) bool { for { nextBlock, err := getBlockInfoByRound(round, "header") if err != nil { - logging.Info(err, " after a second falling thru to ", nodeClient.GetMinShardersVerify(), "of ", len(nodeClient.Network().Sharders()), "Sharders", len(nodeClient.Sharders().Healthy()), "Healthy sharders") + logging.Info(err, " after a second falling thru to ", nodeClient.GetMinShardersVerify(), "of ", len(nodeClient.Network().Sharders), "Sharders", len(nodeClient.Sharders().Healthy()), "Healthy sharders") sys.Sleep(1 * time.Second) nextBlock, err = getBlockInfoByRound(round, "header") if err != nil { @@ -904,7 +904,7 @@ func VerifyContentHash(metaTxnDataJSON string) (bool, error) { return false, errors.New("metaTxnData_decode_error", "Unable to decode metaTxnData json") } - t, err := transaction.VerifyTransaction(metaTxnData.TxnID, nodeClient.Network().Sharders()) + t, err := transaction.VerifyTransaction(metaTxnData.TxnID, nodeClient.Network().Sharders) if err != nil { return false, errors.New("fetch_txm_details", "Unable to fetch txn details") } diff --git a/zcncore/transaction_mobile.go b/zcncore/transaction_mobile.go index c43c8c611..617c44078 100644 --- a/zcncore/transaction_mobile.go +++ b/zcncore/transaction_mobile.go @@ -977,7 +977,7 @@ func (t *Transaction) Verify() error { // transaction is done or expired. it means random sharder might be outdated, try to query it from s/S sharders to confirm it if util.MaxInt64(lfbBlockHeader.getCreationDate(now), now) >= (t.txn.CreationDate + int64(defaultTxnExpirationSeconds)) { - logging.Info("falling back to ", nodeClient.GetMinShardersVerify(), " of ", len(nodeClient.Network().Sharders()), " Sharders", len(nodeClient.Sharders().Healthy()), "Healthy sharders") + logging.Info("falling back to ", nodeClient.GetMinShardersVerify(), " of ", len(nodeClient.Network().Sharders), " Sharders", len(nodeClient.Sharders().Healthy()), "Healthy sharders") confirmBlockHeader, confirmationBlock, lfbBlockHeader, err = tq.getConsensusConfirmation(nodeClient.GetMinShardersVerify(), t.txnHash, nil) } @@ -1052,7 +1052,7 @@ func (t *Transaction) EstimateFee(reqPercent float32) (int64, error) { if err != nil { return 0, err } - fee, err := transaction.EstimateFee(t.txn, nodeClient.Network().Miners(), reqPercent) + fee, err := transaction.EstimateFee(t.txn, nodeClient.Network().Miners, reqPercent) return int64(fee), err } @@ -1246,8 +1246,8 @@ func GetFeeStats(timeout RequestTimeout) ([]byte, error) { var numMiners = 4 - if numMiners > len(nodeClient.Network().Miners()) { - numMiners = len(nodeClient.Network().Miners()) + if numMiners > len(nodeClient.Network().Miners) { + numMiners = len(nodeClient.Network().Miners) } var result = make(chan *util.GetResponse, numMiners) @@ -1616,7 +1616,7 @@ func GetFeesTable(reqPercent float32) (string, error) { return "", err } - fees, err := transaction.GetFeesTable(nodeClient.Network().Miners(), reqPercent) + fees, err := transaction.GetFeesTable(nodeClient.Network().Miners, reqPercent) if err != nil { return "", err } diff --git a/zcncore/transaction_query.go b/zcncore/transaction_query.go index 4f5a26bcc..ee18bd079 100644 --- a/zcncore/transaction_query.go +++ b/zcncore/transaction_query.go @@ -576,7 +576,7 @@ func GetInfoFromAnyMiner(urlSuffix string, op int, cb getInfoCallback) { return } - tq, err := NewTransactionQuery([]string{}, util.Shuffle(clientNode.Network().Miners())) + tq, err := NewTransactionQuery([]string{}, util.Shuffle(clientNode.Network().Miners)) if err != nil { cb.OnInfoAvailable(op, StatusError, "", err.Error()) diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index de5a4d1e7..49ad36317 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -446,7 +446,7 @@ func GetClientDetails(clientID string) (*GetClientResponse, error) { if err != nil { panic(err) } - minerurl := util.GetRandom(clientNode.Network().Miners(), 1)[0] + minerurl := util.GetRandom(clientNode.Network().Miners, 1)[0] url := minerurl + GET_CLIENT url = fmt.Sprintf("%v?id=%v", url, clientID) req, err := util.NewHTTPGetRequest(url) diff --git a/zmagmacore/http/sc-api.go b/zmagmacore/http/sc-api.go index 4180bcff6..777b69122 100644 --- a/zmagmacore/http/sc-api.go +++ b/zmagmacore/http/sc-api.go @@ -83,7 +83,7 @@ func extractSharders() []string { if err != nil { panic(err) } - sharders := nodeClient.Network().Sharders() + sharders := nodeClient.Network().Sharders return util.GetRandom(sharders, len(sharders)) } From 4e1e9f843e6c73ba538ac78f12e440e8124f7bed Mon Sep 17 00:00:00 2001 From: storybehind Date: Sun, 11 Feb 2024 16:15:02 +0530 Subject: [PATCH 007/319] fix issues and provide godocs --- core/client/init_node.go | 37 ++++++++------- core/client/set.go | 4 ++ core/conf/config.go | 5 -- core/conf/network.go | 8 ---- core/conf/vars.go | 51 ++++++++++---------- core/node/cache.go | 7 --- winsdk/sdk.go | 1 - zboxcore/sdk/networkworker.go | 10 ++-- zcnbridge/http/rest.go | 2 +- zcncore/ethwallet_base_test.go | 84 +++------------------------------ zcncore/mswallet_base.go | 2 +- zcncore/networkworker_mobile.go | 21 --------- zcncore/transaction.go | 12 ++--- zcncore/transaction_base.go | 20 ++------ zcncore/transaction_query.go | 14 +++--- zcncore/wallet_base.go | 15 +++--- 16 files changed, 87 insertions(+), 206 deletions(-) diff --git a/core/client/init_node.go b/core/client/init_node.go index 3714e31a9..1d2d87480 100644 --- a/core/client/init_node.go +++ b/core/client/init_node.go @@ -10,6 +10,7 @@ import ( "sync" "time" + "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/core/node" @@ -26,25 +27,28 @@ func init() { logging.Init(logger.DEBUG, "0chain-core") } -// Container to hold global state. +// Maintains central states of SDK (client's context, network). // Initialized through [Init] function. +// Use client.GetNode() to get its instance after Init is called. type Node struct { stableMiners []string sharders *node.NodeHolder - // config *conf.Config - network *conf.Network - // nonceCache *node.NonceCache + network *conf.Network clientCtx context.Context networkGuard sync.RWMutex } +// Returns stable miner urls. +// Length of stable miners is depedent on config's MinSubmit and number of miners in network. func (n *Node) GetStableMiners() []string { n.networkGuard.RLock() defer n.networkGuard.RUnlock() return n.stableMiners } +// ResetStableMiners resets stable miners as a random permutation of network miners. +// Length of stable miners is depedent on config's MinSubmit and number of miners in network. func (n *Node) ResetStableMiners() { n.networkGuard.Lock() defer n.networkGuard.Unlock() @@ -53,6 +57,7 @@ func (n *Node) ResetStableMiners() { n.stableMiners = util.GetRandom(n.network.Miners, reqMiners) } +// Returns minimum sharders used for verification func (n *Node) GetMinShardersVerify() int { n.networkGuard.RLock() defer n.networkGuard.RUnlock() @@ -62,22 +67,23 @@ func (n *Node) GetMinShardersVerify() int { return minSharders } +// Returns NodeHolder instance func (n *Node) Sharders() *node.NodeHolder { n.networkGuard.RLock() defer n.networkGuard.RUnlock() return n.sharders } -// func (n *NodeClient) Config() *conf.Config { -// return n.config -// } - +// Returns network configuration func (n *Node) Network() *conf.Network { n.networkGuard.RLock() defer n.networkGuard.RUnlock() return n.network } +// Gets network details and return it as second value. +// First value is true iff current network details doesn't match existing network details. +// Use node.UpdateNetwork() method to set the new network. func (n *Node) ShouldUpdateNetwork() (bool, *conf.Network, error) { cfg, err := conf.GetClientConfig() if err != nil { @@ -96,6 +102,7 @@ func (n *Node) ShouldUpdateNetwork() (bool, *conf.Network, error) { return true, network, nil } +// Use node.UpdateNetwork() method to set the new network. func (n *Node) UpdateNetwork(network *conf.Network) error { n.networkGuard.Lock() defer n.networkGuard.Unlock() @@ -109,12 +116,7 @@ func (n *Node) UpdateNetwork(network *conf.Network) error { return nil } -// func (n *Node) NonceCache() *node.NonceCache { -// n.networkGuard.RLock() -// defer n.networkGuard.RUnlock() -// return n.nonceCache -// } - +// Initializes SDK. func Init(ctx context.Context, cfg conf.Config) error { // validate err := validate(&cfg) @@ -136,9 +138,7 @@ func Init(ctx context.Context, cfg conf.Config) error { nodeClient = &Node{ stableMiners: util.GetRandom(network.Miners, reqMiners), sharders: sharders, - // config: &cfg, network: network, - // nonceCache: node.NewNonceCache(sharders), clientCtx: ctx, } @@ -171,6 +171,7 @@ func Init(ctx context.Context, cfg conf.Config) error { return nil } +// Returns Node instance. If this function is called before Init(), error is returned. func GetNode() (*Node, error) { if nodeClient != nil { return nodeClient, nil @@ -178,6 +179,7 @@ func GetNode() (*Node, error) { return nil, errors.New("0chain-sdk is not initialized") } +// GetNetwork gets current network details from 0chain network. func GetNetwork(ctx context.Context, blockWorker string) (*conf.Network, error) { networkUrl := blockWorker + "/network" networkGetCtx, networkGetCancelCtx := context.WithTimeout(ctx, 60*time.Second) @@ -209,6 +211,9 @@ func validate(cfg *conf.Config) error { if cfg.BlockWorker == "" { return errors.New("chain BlockWorker can't be empty") } + if cfg.SignatureScheme != string(constants.BLS0CHAIN) && cfg.SignatureScheme != string(constants.ED25519) { + return errors.New("invalid/unsupported signature scheme") + } return nil } diff --git a/core/client/set.go b/core/client/set.go index 4a8a1bb49..7dff29687 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -9,6 +9,7 @@ import ( "github.com/0chain/gosdk/core/zcncrypto" ) +// maintains client's data var ( wallet *zcncrypto.Wallet splitKeyWallet bool @@ -21,11 +22,13 @@ func init() { wallet = &zcncrypto.Wallet{} } +// SetWallet should be set before any transaction or client specific APIs func SetWallet(w zcncrypto.Wallet) error { wallet = &w return nil } +// splitKeyWallet parameter is valid only if SignatureScheme is "BLS0Chain" func SetSplitKeyWallet(isSplitKeyWallet bool) error { cfg, err := conf.GetClientConfig() if err != nil { @@ -37,6 +40,7 @@ func SetSplitKeyWallet(isSplitKeyWallet bool) error { return nil } +// SetAuthUrl will be called by app to set zauth URL to SDK func SetAuthUrl(url string) error { if !splitKeyWallet { return errors.New("wallet type is not split key") diff --git a/core/conf/config.go b/core/conf/config.go index 387fd388d..5b770768e 100644 --- a/core/conf/config.go +++ b/core/conf/config.go @@ -74,11 +74,6 @@ type Config struct { ZboxAppType string `json:"zbox_app_type"` // SharderConsensous is consensous for when quering for SCRestAPI calls SharderConsensous int `json:"sharder_consensous"` - - // Wallet zcncrypto.Wallet `json:"wallet"` - // SplitKeyWallet bool `json:"split_key_wallet"` - // //the url of zAuth server - // AuthUrl string `json:"auth_url"` } // LoadConfigFile load and parse Config from file diff --git a/core/conf/network.go b/core/conf/network.go index 7c3f1e612..49766cc74 100644 --- a/core/conf/network.go +++ b/core/conf/network.go @@ -35,14 +35,6 @@ func (n *Network) IsValid() bool { return n != nil && len(n.Miners) > 0 && len(n.Sharders) > 0 } -// func (n *Network) Miners() []string { -// return n.miners -// } - -// func (n *Network) Sharders() []string { -// return n.sharders -// } - func (n *Network) NormalizeURLs() { for i := 0; i < len(n.Miners); i++ { n.Miners[i] = strings.TrimSuffix(n.Miners[i], "/") diff --git a/core/conf/vars.go b/core/conf/vars.go index 3a1fa0376..2c0a5dbb3 100644 --- a/core/conf/vars.go +++ b/core/conf/vars.go @@ -2,6 +2,7 @@ package conf import ( "errors" + "strings" "sync" ) @@ -10,7 +11,8 @@ var ( cfg *Config onceCfg sync.Once // global sharders and miners - // network *Network + //TODO: remove as it is not used + network *Network ) var ( @@ -44,33 +46,34 @@ func InitClientConfig(c *Config) { }) } +//Deprecated: Use client.Init() function. To normalize urls, use network.NormalizeURLs() method // // InitChainNetwork set global chain network -// func InitChainNetwork(n *Network) { -// if n == nil { -// return -// } +func InitChainNetwork(n *Network) { + if n == nil { + return + } -// normalizeURLs(n) + normalizeURLs(n) -// if network == nil { -// network = n -// return -// } + if network == nil { + network = n + return + } -// network.sharders = n.Sharders -// network.miners = n.Miners -// } + network.Sharders = n.Sharders + network.Miners = n.Miners +} -// func normalizeURLs(network *Network) { -// if network == nil { -// return -// } +func normalizeURLs(network *Network) { + if network == nil { + return + } -// for i := 0; i < len(network.Miners); i++ { -// network.Miners[i] = strings.TrimSuffix(network.Miners[i], "/") -// } + for i := 0; i < len(network.Miners); i++ { + network.Miners[i] = strings.TrimSuffix(network.Miners[i], "/") + } -// for i := 0; i < len(network.Sharders); i++ { -// network.Sharders[i] = strings.TrimSuffix(network.Sharders[i], "/") -// } -// } + for i := 0; i < len(network.Sharders); i++ { + network.Sharders[i] = strings.TrimSuffix(network.Sharders[i], "/") + } +} diff --git a/core/node/cache.go b/core/node/cache.go index 4ed8e229f..3e19d424d 100644 --- a/core/node/cache.go +++ b/core/node/cache.go @@ -25,13 +25,6 @@ func init() { }) } -// func NewNonceCache(sharders *NodeHolder) *NonceCache { -// return &NonceCache{ -// cache: make(map[string]int64), -// sharders: sharders, -// } -// } - func (nc *NonceCache) GetNextNonce(clientId string) int64 { nc.guard.Lock() defer nc.guard.Unlock() diff --git a/winsdk/sdk.go b/winsdk/sdk.go index c0252e15b..f16b53eac 100644 --- a/winsdk/sdk.go +++ b/winsdk/sdk.go @@ -14,7 +14,6 @@ import ( "os" "github.com/0chain/gosdk/zboxapi" - // "github.com/0chain/gosdk/zboxcore/client" l "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/0chain/gosdk/zboxcore/zboxutil" diff --git a/zboxcore/sdk/networkworker.go b/zboxcore/sdk/networkworker.go index 882d30b01..7f3d8fd01 100644 --- a/zboxcore/sdk/networkworker.go +++ b/zboxcore/sdk/networkworker.go @@ -74,12 +74,10 @@ func forceUpdateNetworkDetails(networkDetails *Network) { blockchain.SetMiners(networkDetails.Miners) blockchain.SetSharders(networkDetails.Sharders) node.InitCache(blockchain.Sharders) - n, err := conf.NewNetwork(networkDetails.Miners, networkDetails.Sharders) - if err != nil { - panic(err) - } - networkDetails.Miners = n.Miners - networkDetails.Sharders = n.Sharders + conf.InitChainNetwork(&conf.Network{ + Sharders: networkDetails.Sharders, + Miners: networkDetails.Miners, + }) sdkInitialized = true } diff --git a/zcnbridge/http/rest.go b/zcnbridge/http/rest.go index 8c4cf3dbe..31e2b593f 100644 --- a/zcnbridge/http/rest.go +++ b/zcnbridge/http/rest.go @@ -173,7 +173,7 @@ func extractSharders() []string { if err != nil { panic(err) } - sharders := nodeClient.Network().Sharders + sharders := nodeClient.Sharders().Healthy() return util.GetRandom(sharders, len(sharders)) } diff --git a/zcncore/ethwallet_base_test.go b/zcncore/ethwallet_base_test.go index ebb4c01e4..018bf4513 100644 --- a/zcncore/ethwallet_base_test.go +++ b/zcncore/ethwallet_base_test.go @@ -8,7 +8,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus/ethash" - ethCore "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" @@ -50,7 +50,6 @@ func TestTokensConversion(t *testing.T) { func TestValidEthAddress(t *testing.T) { t.Run("Valid Eth wallet, but no balance", func(t *testing.T) { - // _config.chain.EthNode = "test" backend, _ := newTestBackend(t) client, _ := backend.Attach() defer backend.Close() @@ -60,13 +59,6 @@ func TestValidEthAddress(t *testing.T) { getEthClient = func() (*ethclient.Client, error) { return realClient, nil } - // getZcnNode = func() (*ZcnNode, error) { - // zcnnode := &ZcnNode{ - // NodeClient: &core.NodeClient{}, - // } - // zcnnode.Config().EthereumNode = "test" - // return zcnnode, nil - // } res, err := IsValidEthAddress("0x531f9349ed2Fe5c526B47fa7841D35c90482e6cF") require.Nil(t, err, "") @@ -74,7 +66,6 @@ func TestValidEthAddress(t *testing.T) { }) t.Run("Valid Eth wallet", func(t *testing.T) { - // _config.chain.EthNode = "test" backend, _ := newTestBackend(t) client, _ := backend.Attach() defer backend.Close() @@ -87,13 +78,6 @@ func TestValidEthAddress(t *testing.T) { getEthClient = func() (*ethclient.Client, error) { return realClient, nil } - // getZcnNode = func() (*ZcnNode, error) { - // zcnnode := &ZcnNode{ - // NodeClient: &core.NodeClient{}, - // } - // zcnnode.Config().EthereumNode = "test" - // return zcnnode, nil - // } res, err := IsValidEthAddress(testAddr.String()) require.Nil(t, err, "") @@ -101,7 +85,6 @@ func TestValidEthAddress(t *testing.T) { }) t.Run("Invalid Eth wallet", func(t *testing.T) { - // _config.chain.EthNode = "test" backend, _ := newTestBackend(t) client, _ := backend.Attach() defer backend.Close() @@ -111,13 +94,6 @@ func TestValidEthAddress(t *testing.T) { getEthClient = func() (*ethclient.Client, error) { return realClient, nil } - // getZcnNode = func() (*ZcnNode, error) { - // zcnnode := &ZcnNode{ - // NodeClient: &core.NodeClient{}, - // } - // zcnnode.Config().EthereumNode = "test" - // return zcnnode, nil - // } res, err := IsValidEthAddress("testAddr.String()") require.NotNil(t, err, "") @@ -127,14 +103,6 @@ func TestValidEthAddress(t *testing.T) { func TestGetWalletAddrFromEthMnemonic(t *testing.T) { t.Run("Success", func(t *testing.T) { - // _config.chain.EthNode = "test" - // getZcnNode = func() (*ZcnNode, error) { - // zcnnode := &ZcnNode{ - // NodeClient: &core.NodeClient{}, - // } - // zcnnode.Config().EthereumNode = "test" - // return zcnnode, nil - // } mnemonic := "expect domain water near beauty bag pond clap chronic chronic length leisure" res, err := GetWalletAddrFromEthMnemonic(mnemonic) require.Nil(t, err, "") @@ -142,14 +110,6 @@ func TestGetWalletAddrFromEthMnemonic(t *testing.T) { }) t.Run("Wrong", func(t *testing.T) { - // _config.chain.EthNode = "test" - // getZcnNode = func() (*ZcnNode, error) { - // zcnnode := &ZcnNode{ - // NodeClient: &core.NodeClient{}, - // } - // zcnnode.Config().EthereumNode = "test" - // return zcnnode, nil - // } mnemonic := "this is wrong mnemonic" _, err := GetWalletAddrFromEthMnemonic(mnemonic) require.NotNil(t, err, "") @@ -158,7 +118,6 @@ func TestGetWalletAddrFromEthMnemonic(t *testing.T) { func TestGetEthBalance(t *testing.T) { t.Run("Success", func(t *testing.T) { - // _config.chain.EthNode = "test" backend, _ := newTestBackend(t) client, _ := backend.Attach() defer backend.Close() @@ -171,13 +130,6 @@ func TestGetEthBalance(t *testing.T) { getEthClient = func() (*ethclient.Client, error) { return realClient, nil } - // getZcnNode = func() (*ZcnNode, error) { - // zcnnode := &ZcnNode{ - // NodeClient: &core.NodeClient{}, - // } - // zcnnode.Config().EthereumNode = "test" - // return zcnnode, nil - // } tcb := &MockBalanceCallback{} tcb.wg = &sync.WaitGroup{} @@ -197,7 +149,6 @@ func TestGetEthBalance(t *testing.T) { func TestCheckEthHashStatus(t *testing.T) { t.Run("Pending transaction", func(t *testing.T) { - // _config.chain.EthNode = "test" backend, _ := newTestBackend(t) client, _ := backend.Attach() defer backend.Close() @@ -210,13 +161,6 @@ func TestCheckEthHashStatus(t *testing.T) { getEthClient = func() (*ethclient.Client, error) { return realClient, nil } - // getZcnNode = func() (*ZcnNode, error) { - // zcnnode := &ZcnNode{ - // NodeClient: &core.NodeClient{}, - // } - // zcnnode.Config().EthereumNode = "test" - // return zcnnode, nil - // } result := CheckEthHashStatus("0x05aa8890d4778e292f837dd36b59a50931c175f4648c3d8157525f5454475cf7") require.True(t, result < 0, "") }) @@ -224,7 +168,6 @@ func TestCheckEthHashStatus(t *testing.T) { func TestSuggestEthGasPrice(t *testing.T) { t.Run("suggest gas price success", func(t *testing.T) { - // _config.chain.EthNode = "test" backend, _ := newTestBackend(t) client, _ := backend.Attach() defer backend.Close() @@ -234,13 +177,6 @@ func TestSuggestEthGasPrice(t *testing.T) { getEthClient = func() (*ethclient.Client, error) { return realClient, nil } - // getZcnNode = func() (*ZcnNode, error) { - // zcnnode := &ZcnNode{ - // NodeClient: &core.NodeClient{}, - // } - // zcnnode.Config().EthereumNode = "test" - // return zcnnode, nil - // } gas, err := SuggestEthGasPrice() require.Nil(t, err) require.True(t, gas > 0) @@ -249,7 +185,6 @@ func TestSuggestEthGasPrice(t *testing.T) { func TestTransferEthTokens(t *testing.T) { t.Run("success transfer", func(t *testing.T) { - // _config.chain.EthNode = "test" backend, _ := newTestBackend(t) client, _ := backend.Attach() defer backend.Close() @@ -259,13 +194,6 @@ func TestTransferEthTokens(t *testing.T) { getEthClient = func() (*ethclient.Client, error) { return realClient, nil } - // getZcnNode = func() (*ZcnNode, error) { - // zcnnode := &ZcnNode{ - // NodeClient: &core.NodeClient{}, - // } - // zcnnode.Config().EthereumNode = "test" - // return zcnnode, nil - // } hash, err := TransferEthTokens("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291", 1000000000000, 10000) require.Nil(t, err) @@ -313,24 +241,24 @@ func newTestBackend(t *testing.T) (*node.Node, []*types.Block) { return n, blocks } -func generateTestChain() (*ethCore.Genesis, []*types.Block) { +func generateTestChain() (*core.Genesis, []*types.Block) { db := rawdb.NewMemoryDatabase() config := params.AllEthashProtocolChanges - genesis := ðCore.Genesis{ + genesis := &core.Genesis{ Config: config, - Alloc: ethCore.GenesisAlloc{testAddr: {Balance: testBalance}}, + Alloc: core.GenesisAlloc{testAddr: {Balance: testBalance}}, ExtraData: []byte("test genesis"), Timestamp: 9000, } // BaseFee: big.NewInt(params.InitialBaseFee), - generate := func(i int, g *ethCore.BlockGen) { + generate := func(i int, g *core.BlockGen) { g.OffsetTime(5) g.SetExtra([]byte("test")) } gblock := genesis.ToBlock() genesis.Commit(db) //nolint: errcheck engine := ethash.NewFaker() - blocks, _ := ethCore.GenerateChain(config, gblock, engine, db, 1, generate) + blocks, _ := core.GenerateChain(config, gblock, engine, db, 1, generate) blocks = append([]*types.Block{gblock}, blocks...) return genesis, blocks } diff --git a/zcncore/mswallet_base.go b/zcncore/mswallet_base.go index f0a5fde6a..495d6b84f 100644 --- a/zcncore/mswallet_base.go +++ b/zcncore/mswallet_base.go @@ -60,7 +60,7 @@ func CreateMSWallet(t, n int) (string, string, []string, error) { msw := MSWallet{ Id: id, - SignatureScheme: string(signScheme), + SignatureScheme: signScheme, GroupClientID: groupClientID, GroupKey: groupKey, SignerClientIDs: signerClientIDs, diff --git a/zcncore/networkworker_mobile.go b/zcncore/networkworker_mobile.go index 09d45a6b7..e953356bf 100644 --- a/zcncore/networkworker_mobile.go +++ b/zcncore/networkworker_mobile.go @@ -13,8 +13,6 @@ import ( const NETWORK_ENDPOINT = "/network" -var networkWorkerTimerInHours = 1 - type Network struct { net network } @@ -36,25 +34,6 @@ type network struct { Sharders []string `json:"sharders"` } -// func updateNetworkDetailsWorker(ctx context.Context) { -// ticker := time.NewTicker(time.Duration(networkWorkerTimerInHours) * time.Hour) -// for { -// select { -// case <-ctx.Done(): -// logging.Info("Network stopped by user") -// return -// case <-ticker.C: -// err := UpdateNetworkDetails() -// if err != nil { -// logging.Error("Update network detail worker fail", zap.Error(err)) -// return -// } -// logging.Info("Successfully updated network details") -// return -// } -// } -// } - //Deprecated: Get client.Node instance to check whether network update is required and update network accordingly func UpdateNetworkDetails() error { nodeClient, err := client.GetNode() diff --git a/zcncore/transaction.go b/zcncore/transaction.go index 5a27878f4..0fe33cbcb 100644 --- a/zcncore/transaction.go +++ b/zcncore/transaction.go @@ -1405,7 +1405,7 @@ func (nc *NonceCache) Evict(clientId string) { delete(nc.cache, clientId) } -//Deprecated: client.Init() in core package +//Deprecated: To initialize SDK, Use conf.Config and client.Init() in core package func WithEthereumNode(uri string) func(c *ChainConfig) error { return func(c *ChainConfig) error { c.EthNode = uri @@ -1413,7 +1413,7 @@ func WithEthereumNode(uri string) func(c *ChainConfig) error { } } -//Deprecated: client.Init() in core package +//Deprecated: To initialize SDK, Use conf.Config and client.Init() in core package func WithChainID(id string) func(c *ChainConfig) error { return func(c *ChainConfig) error { c.ChainID = id @@ -1421,7 +1421,7 @@ func WithChainID(id string) func(c *ChainConfig) error { } } -//Deprecated: client.Init() in core package +//Deprecated: To initialize SDK, Use conf.Config and client.Init() in core package func WithMinSubmit(m int) func(c *ChainConfig) error { return func(c *ChainConfig) error { c.MinSubmit = m @@ -1429,7 +1429,7 @@ func WithMinSubmit(m int) func(c *ChainConfig) error { } } -//Deprecated: client.Init() in core package +//Deprecated: To initialize SDK, Use conf.Config and client.Init() in core package func WithMinConfirmation(m int) func(c *ChainConfig) error { return func(c *ChainConfig) error { c.MinConfirmation = m @@ -1437,7 +1437,7 @@ func WithMinConfirmation(m int) func(c *ChainConfig) error { } } -//Deprecated: client.Init() in core package +//Deprecated: To initialize SDK, Use conf.Config and client.Init() in core package func WithConfirmationChainLength(m int) func(c *ChainConfig) error { return func(c *ChainConfig) error { c.ConfirmationChainLength = m @@ -1445,7 +1445,7 @@ func WithConfirmationChainLength(m int) func(c *ChainConfig) error { } } -//Deprecated: client.Init() in core package +//Deprecated: To initialize SDK, Use conf.Config and client.Init() in core package func WithSharderConsensous(m int) func(c *ChainConfig) error { return func(c *ChainConfig) error { c.SharderConsensous = m diff --git a/zcncore/transaction_base.go b/zcncore/transaction_base.go index 6003fd052..a62ff5fa4 100644 --- a/zcncore/transaction_base.go +++ b/zcncore/transaction_base.go @@ -15,6 +15,7 @@ import ( "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/node" "github.com/0chain/gosdk/core/sys" + "github.com/0chain/gosdk/zboxcore/blockchain" "github.com/0chain/gosdk/zboxcore/logger" "go.uber.org/zap" @@ -91,15 +92,6 @@ type TransactionCallback interface { OnAuthComplete(t *Transaction, status int) } -// type localConfig struct { -// chain ChainConfig -// wallet zcncrypto.Wallet -// authUrl string -// isConfigured bool -// isValidWallet bool -// isSplitWallet bool -// } - type ChainConfig struct { ChainID string `json:"chain_id,omitempty"` BlockWorker string `json:"block_worker"` @@ -113,8 +105,6 @@ type ChainConfig struct { SharderConsensous int `json:"sharder_consensous"` } -// var Sharders *node.NodeHolder - // Deprecated: Use client.Init() in core/client package // InitZCNSDK initializes the SDK with miner, sharder and signature scheme provided. func InitZCNSDK(blockWorker string, signscheme string, configs ...func(*ChainConfig) error) error { @@ -894,17 +884,13 @@ type MinerSCUnlock struct { } func VerifyContentHash(metaTxnDataJSON string) (bool, error) { - nodeClient, err := client.GetNode() - if err != nil { - return false, err - } var metaTxnData sdk.CommitMetaResponse - err = json.Unmarshal([]byte(metaTxnDataJSON), &metaTxnData) + err := json.Unmarshal([]byte(metaTxnDataJSON), &metaTxnData) if err != nil { return false, errors.New("metaTxnData_decode_error", "Unable to decode metaTxnData json") } - t, err := transaction.VerifyTransaction(metaTxnData.TxnID, nodeClient.Network().Sharders) + t, err := transaction.VerifyTransaction(metaTxnData.TxnID, blockchain.GetSharders()) if err != nil { return false, errors.New("fetch_txm_details", "Unable to fetch txn details") } diff --git a/zcncore/transaction_query.go b/zcncore/transaction_query.go index ee18bd079..5e1b402ba 100644 --- a/zcncore/transaction_query.go +++ b/zcncore/transaction_query.go @@ -231,7 +231,7 @@ func (tq *TransactionQuery) getRandomSharderWithHealthcheck(ctx context.Context) return "", ErrNoOnlineSharders } -// getRandomMiner returns a random miner +//getRandomMiner returns a random miner func (tq *TransactionQuery) getRandomMiner(ctx context.Context) (string, error) { if tq.miners == nil || len(tq.miners) == 0 { @@ -527,12 +527,12 @@ func (tq *TransactionQuery) getFastConfirmation(ctx context.Context, txnHash str } func GetInfoFromSharders(urlSuffix string, op int, cb GetInfoCallback) { - clientNode, err := client.GetNode() + nodeClient, err := client.GetNode() if err != nil { cb.OnInfoAvailable(op, StatusError, "", err.Error()) return } - tq, err := NewTransactionQuery(util.Shuffle(clientNode.Sharders().Healthy()), []string{}) + tq, err := NewTransactionQuery(util.Shuffle(nodeClient.Sharders().Healthy()), []string{}) if err != nil { cb.OnInfoAvailable(op, StatusError, "", err.Error()) return @@ -548,13 +548,13 @@ func GetInfoFromSharders(urlSuffix string, op int, cb GetInfoCallback) { } func GetInfoFromAnySharder(urlSuffix string, op int, cb GetInfoCallback) { - clientNode, err := client.GetNode() + nodeClient, err := client.GetNode() if err != nil { cb.OnInfoAvailable(op, StatusError, "", err.Error()) return } - tq, err := NewTransactionQuery(util.Shuffle(clientNode.Sharders().Healthy()), []string{}) + tq, err := NewTransactionQuery(util.Shuffle(nodeClient.Sharders().Healthy()), []string{}) if err != nil { cb.OnInfoAvailable(op, StatusError, "", err.Error()) return @@ -570,13 +570,13 @@ func GetInfoFromAnySharder(urlSuffix string, op int, cb GetInfoCallback) { } func GetInfoFromAnyMiner(urlSuffix string, op int, cb getInfoCallback) { - clientNode, err := client.GetNode() + nodeClient, err := client.GetNode() if err != nil { cb.OnInfoAvailable(op, StatusError, "", err.Error()) return } - tq, err := NewTransactionQuery([]string{}, util.Shuffle(clientNode.Network().Miners)) + tq, err := NewTransactionQuery([]string{}, util.Shuffle(nodeClient.Network().Miners)) if err != nil { cb.OnInfoAvailable(op, StatusError, "", err.Error()) diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 49ad36317..cb3a784df 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -14,7 +14,6 @@ import ( "errors" - // "github.com/0chain/errors" "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" @@ -223,14 +222,14 @@ type AuthCallback interface { } var ( - logging logger.Logger + logging logger.Logger ) func init() { logging.Init(defaultLogLevel, "0chain-core-sdk") } -// Deprecated: Use core.GetNode().GetStableMiners() +//Deprecated: Use client.GetNode().GetStableMiners() func GetStableMiners() []string { clientNode, err := client.GetNode() if err != nil { @@ -239,7 +238,7 @@ func GetStableMiners() []string { return clientNode.GetStableMiners() } -// Deprecated: Use core.GetNode().ResetStableMiners() +//Deprecated: Use client.GetNode().ResetStableMiners() func ResetStableMiners() { clientNode, err := client.GetNode() if err != nil { @@ -275,7 +274,7 @@ func CheckConfig() error { return nil } -// Deprecated: Use core.GetNode().GetMinShardersVerify() after core's Init call +//Deprecated: Use client.GetNode().GetMinShardersVerify() after Init call func GetMinShardersVerify() int { clientNode, err := client.GetNode() if err != nil { @@ -469,7 +468,7 @@ func GetClientDetails(clientID string) (*GetClientResponse, error) { return &clientDetails, nil } -// Deprecated: Use zcncrypto.IsMnemonicValid() +//Deprecated: Use zcncrypto.IsMnemonicValid() // IsMnemonicValid is an utility function to check the mnemonic valid // // # Inputs @@ -478,7 +477,7 @@ func IsMnemonicValid(mnemonic string) bool { return zcncrypto.IsMnemonicValid(mnemonic) } -// Deprecated: Use client.SetWallet() and client.SetSplitKeyWallet() to set wallet and splitKeyWallet respectively +//Deprecated: Use client.SetWallet() and client.SetSplitKeyWallet() to set wallet and splitKeyWallet respectively // SetWallet should be set before any transaction or client specific APIs // splitKeyWallet parameter is valid only if SignatureScheme is "BLS0Chain" func SetWallet(w zcncrypto.Wallet, splitKeyWallet bool) error { @@ -493,7 +492,7 @@ func SetWallet(w zcncrypto.Wallet, splitKeyWallet bool) error { return nil } -// Deprecated: Use client.Wallet() in core package +//Deprecated: Use client.Wallet() in core/client package func GetWalletRaw() zcncrypto.Wallet { return *client.Wallet() } From d92282fd6296e025357626f32820468559320ce9 Mon Sep 17 00:00:00 2001 From: storybehind Date: Sun, 11 Feb 2024 16:53:39 +0530 Subject: [PATCH 008/319] call InitCache function --- core/client/init_node.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/client/init_node.go b/core/client/init_node.go index 1d2d87480..dabbb2d3b 100644 --- a/core/client/init_node.go +++ b/core/client/init_node.go @@ -144,6 +144,7 @@ func Init(ctx context.Context, cfg conf.Config) error { //init packages conf.InitClientConfig(&cfg) + node.InitCache(nodeClient.sharders) // update Network periodically go func() { From 3ae9191db982de70d5f27c8f625f33f25b6ff89e Mon Sep 17 00:00:00 2001 From: storybehind Date: Sun, 11 Feb 2024 21:08:55 +0530 Subject: [PATCH 009/319] revert InitClientConfig() changes --- core/conf/vars.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/conf/vars.go b/core/conf/vars.go index 2c0a5dbb3..3d7ac63e7 100644 --- a/core/conf/vars.go +++ b/core/conf/vars.go @@ -42,7 +42,12 @@ func GetClientConfig() (*Config, error) { // InitClientConfig set global client config func InitClientConfig(c *Config) { onceCfg.Do(func() { + sharderConsensous := c.SharderConsensous + if sharderConsensous < 1 { + sharderConsensous = DefaultSharderConsensous + } cfg = c + cfg.SharderConsensous = sharderConsensous }) } From fc9dca361ead00aa1fd01a087d9dc512b66bcb5d Mon Sep 17 00:00:00 2001 From: storybehind Date: Mon, 12 Feb 2024 10:23:46 +0530 Subject: [PATCH 010/319] fix InitSignatureScheme() function --- core/client/init_node.go | 2 +- zcncore/wallet_base.go | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/core/client/init_node.go b/core/client/init_node.go index dabbb2d3b..60e138dae 100644 --- a/core/client/init_node.go +++ b/core/client/init_node.go @@ -232,7 +232,7 @@ func setOptionsDefaultValue(cfg *conf.Config) { cfg.MaxTxnQuery = conf.DefaultMaxTxnQuery } if cfg.QuerySleepTime <= 0 { - cfg.QuerySleepTime = conf.DefaultMaxTxnQuery + cfg.QuerySleepTime = conf.DefaultQuerySleepTime } if cfg.SharderConsensous <= 0 { cfg.SharderConsensous = conf.DefaultSharderConsensous diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index cb3a784df..d2d0a5765 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -339,10 +339,14 @@ func Init(chainConfigJSON string) error { // InitSignatureScheme initializes signature scheme only. func InitSignatureScheme(scheme string) { - cfg := conf.Config{ - SignatureScheme: scheme, + cfg, err := conf.GetClientConfig() + if err != nil { + conf.InitClientConfig(&conf.Config{ + SignatureScheme: scheme, + }) + return } - conf.InitClientConfig(&cfg) + cfg.SignatureScheme = scheme } // CreateWalletOffline creates the wallet for the config signature scheme. From 2e7b9c1e682b7e50b72c3925b57d305f8b2006a1 Mon Sep 17 00:00:00 2001 From: storybehind Date: Mon, 12 Feb 2024 12:50:17 +0530 Subject: [PATCH 011/319] Deprecate InitSignatureScheme function --- zcnbridge/authorizer/proofBurnTicket_test.go | 8 +++++--- zcncore/transaction_base.go | 2 +- zcncore/wallet.go | 2 +- zcncore/wallet_base.go | 7 +++---- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/zcnbridge/authorizer/proofBurnTicket_test.go b/zcnbridge/authorizer/proofBurnTicket_test.go index b844cf048..1bf7aac01 100644 --- a/zcnbridge/authorizer/proofBurnTicket_test.go +++ b/zcnbridge/authorizer/proofBurnTicket_test.go @@ -4,8 +4,8 @@ import ( "encoding/json" "testing" - "github.com/0chain/gosdk/zcncore" - + "github.com/0chain/gosdk/constants" + "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zcnbridge/authorizer" "github.com/stretchr/testify/require" @@ -63,7 +63,9 @@ func (suite *TicketTestSuite) TestTicketSignature() { Signature: nil, } - zcncore.InitSignatureScheme("bls0chain") + conf.InitClientConfig(&conf.Config{ + SignatureScheme: constants.BLS0CHAIN.String(), + }) err := pb.SignWith0Chain(suite.w) require.NoError(suite.T(), err) require.NotEmpty(suite.T(), pb.Signature) diff --git a/zcncore/transaction_base.go b/zcncore/transaction_base.go index a62ff5fa4..224534171 100644 --- a/zcncore/transaction_base.go +++ b/zcncore/transaction_base.go @@ -105,7 +105,7 @@ type ChainConfig struct { SharderConsensous int `json:"sharder_consensous"` } -// Deprecated: Use client.Init() in core/client package +//Deprecated: Use client.Init() in core/client package // InitZCNSDK initializes the SDK with miner, sharder and signature scheme provided. func InitZCNSDK(blockWorker string, signscheme string, configs ...func(*ChainConfig) error) error { if signscheme != "ed25519" && signscheme != "bls0chain" { diff --git a/zcncore/wallet.go b/zcncore/wallet.go index d15bf6881..a83d771b2 100644 --- a/zcncore/wallet.go +++ b/zcncore/wallet.go @@ -18,7 +18,7 @@ func GetWalletBalance(clientId string) (common.Balance, error) { return getWalletBalance(clientId) } -// Deprecated: use Sign() method in zcncrypto.Wallet +//Deprecated: use Sign() method in zcncrypto.Wallet func SignWith0Wallet(hash string, w *zcncrypto.Wallet) (string, error) { cfg, err := conf.GetClientConfig() if err != nil { diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index d2d0a5765..11367ca7c 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -337,14 +337,13 @@ func Init(chainConfigJSON string) error { return client.Init(context.Background(), cfg) } +//Deprecated: Use client.Init() in core/client package to initialize SDK. // InitSignatureScheme initializes signature scheme only. +// Panics if this function is called before Init() call func InitSignatureScheme(scheme string) { cfg, err := conf.GetClientConfig() if err != nil { - conf.InitClientConfig(&conf.Config{ - SignatureScheme: scheme, - }) - return + panic(errors.New("InitSignatureScheme() is called before Init function call")) } cfg.SignatureScheme = scheme } From 487e83c81de34693cfb9baabf6972e248e3d8911 Mon Sep 17 00:00:00 2001 From: storybehind Date: Mon, 12 Feb 2024 18:41:22 +0530 Subject: [PATCH 012/319] fix InitSignatureScheme --- zcncore/wallet_base.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 11367ca7c..bd5ce37b0 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -337,13 +337,16 @@ func Init(chainConfigJSON string) error { return client.Init(context.Background(), cfg) } -//Deprecated: Use client.Init() in core/client package to initialize SDK. +// Use client.Init() in core/client package to initialize SDK. // InitSignatureScheme initializes signature scheme only. -// Panics if this function is called before Init() call +// Either InitSignatureScheme() or client.Init() should be called to initialize SDK. Calling both produces unexpected behaviour. func InitSignatureScheme(scheme string) { cfg, err := conf.GetClientConfig() if err != nil { - panic(errors.New("InitSignatureScheme() is called before Init function call")) + conf.InitClientConfig(&conf.Config{ + SignatureScheme: scheme, + }) + return } cfg.SignatureScheme = scheme } From d197fe1868a71705891507f5eea8a6844e1f9313 Mon Sep 17 00:00:00 2001 From: storybehind Date: Tue, 13 Feb 2024 21:24:04 +0530 Subject: [PATCH 013/319] create signature scheme var --- zcncore/mswallet.go | 7 +----- zcncore/mswallet_base.go | 12 +++------ zcncore/mswallet_mobile.go | 8 +----- zcncore/transaction_base.go | 31 ++++++----------------- zcncore/transactionauth_base.go | 9 ++----- zcncore/wallet_base.go | 44 +++++++++------------------------ zcncore/wallet_mobile.go | 9 ++----- 7 files changed, 29 insertions(+), 91 deletions(-) diff --git a/zcncore/mswallet.go b/zcncore/mswallet.go index 1b40f8971..517d81a37 100644 --- a/zcncore/mswallet.go +++ b/zcncore/mswallet.go @@ -8,7 +8,6 @@ import ( "fmt" "github.com/0chain/errors" - "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/zcncrypto" ) @@ -183,11 +182,7 @@ func CreateMSVote(proposal, grpClientID, signerWalletstr, toClientID string, tok buff, _ := json.Marshal(transfer) hash := encryption.Hash(buff) - cfg, err := conf.GetClientConfig() - if err != nil { - return "", err - } - sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) + sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) if err := sigScheme.SetPrivateKey(signerWallet.Keys[0].PrivateKey); err != nil { return "", err } diff --git a/zcncore/mswallet_base.go b/zcncore/mswallet_base.go index 495d6b84f..6b9b215ca 100644 --- a/zcncore/mswallet_base.go +++ b/zcncore/mswallet_base.go @@ -11,7 +11,6 @@ import ( "github.com/0chain/errors" "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/zcncrypto" ) @@ -26,19 +25,14 @@ func CreateMSWallet(t, n int) (string, string, []string, error) { if t < 1 || t > n { return "", "", nil, errors.New("bls0_generate_threshold_key_shares", fmt.Sprintf("Given threshold (%d) is less than 1 or greater than numsigners (%d)", t, n)) } - cfg, err := conf.GetClientConfig() - if err != nil { - return "", "", nil, err - } id := 0 - if cfg.SignatureScheme != constants.BLS0CHAIN.String() { + if signatureScheme != constants.BLS0CHAIN.String() { return "", "", nil, errors.New("", "encryption scheme for this blockchain is not bls0chain") } - signScheme := cfg.SignatureScheme - groupKey := zcncrypto.NewSignatureScheme(string(signScheme)) + groupKey := zcncrypto.NewSignatureScheme(signatureScheme) wallet, err := groupKey.GenerateKeys() if err != nil { return "", "", nil, err @@ -60,7 +54,7 @@ func CreateMSWallet(t, n int) (string, string, []string, error) { msw := MSWallet{ Id: id, - SignatureScheme: signScheme, + SignatureScheme: signatureScheme, GroupClientID: groupClientID, GroupKey: groupKey, SignerClientIDs: signerClientIDs, diff --git a/zcncore/mswallet_mobile.go b/zcncore/mswallet_mobile.go index efb502c74..8e51db735 100644 --- a/zcncore/mswallet_mobile.go +++ b/zcncore/mswallet_mobile.go @@ -9,7 +9,6 @@ import ( "fmt" "strconv" - "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/zcncrypto" ) @@ -258,12 +257,7 @@ func CreateMSVote(proposal, grpClientID, signerWalletstr, toClientID string, tok buff, _ := json.Marshal(transfer) hash := encryption.Hash(buff) - cfg, err := conf.GetClientConfig() - if err != nil { - return "", err - } - - sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) + sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) if err := sigScheme.SetPrivateKey(signerWallet.Keys[0].PrivateKey); err != nil { return "", err } diff --git a/zcncore/transaction_base.go b/zcncore/transaction_base.go index 224534171..968936d35 100644 --- a/zcncore/transaction_base.go +++ b/zcncore/transaction_base.go @@ -191,12 +191,8 @@ type SendTxnData struct { } func Sign(hash string) (string, error) { - cfg, err := conf.GetClientConfig() - if err != nil { - return "", err - } - sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) - err = sigScheme.SetPrivateKey(client.Wallet().Keys[0].PrivateKey) + sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) + err := sigScheme.SetPrivateKey(client.Wallet().Keys[0].PrivateKey) if err != nil { return "", err } @@ -222,12 +218,8 @@ func VerifyWithKey(pubKey, signature, hash string) (bool, error) { } var SignFn = func(hash string) (string, error) { - cfg, err := conf.GetClientConfig() - if err != nil { - return "", err - } - sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) - err = sigScheme.SetPrivateKey(client.Wallet().Keys[0].PrivateKey) + sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) + err := sigScheme.SetPrivateKey(client.Wallet().Keys[0].PrivateKey) if err != nil { return "", err } @@ -235,12 +227,9 @@ var SignFn = func(hash string) (string, error) { } var AddSignature = func(privateKey, signature string, hash string) (string, error) { - cfg, err := conf.GetClientConfig() - if err != nil { - return "", err - } var ( - ss = zcncrypto.NewSignatureScheme(cfg.SignatureScheme) + ss = zcncrypto.NewSignatureScheme(signatureScheme) + err error ) err = ss.SetPrivateKey(privateKey) @@ -258,12 +247,8 @@ func signWithWallet(hash string, wi interface{}) (string, error) { fmt.Printf("Error in casting to wallet") return "", errors.New("", "error in casting to wallet") } - cfg, err := conf.GetClientConfig() - if err != nil { - return "", err - } - sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) - err = sigScheme.SetPrivateKey(w.Keys[0].PrivateKey) + sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) + err := sigScheme.SetPrivateKey(w.Keys[0].PrivateKey) if err != nil { return "", err } diff --git a/zcncore/transactionauth_base.go b/zcncore/transactionauth_base.go index 225a2e7cc..a6134b71f 100644 --- a/zcncore/transactionauth_base.go +++ b/zcncore/transactionauth_base.go @@ -5,7 +5,6 @@ import ( "github.com/0chain/errors" "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/node" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/core/transaction" @@ -88,12 +87,8 @@ func (ta *TransactionWithAuth) completeTxn(status int, out string, err error) { } func verifyFn(signature, msgHash, publicKey string) (bool, error) { - cfg, err := conf.GetClientConfig() - if err != nil { - return false, err - } - v := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) - err = v.SetPublicKey(publicKey) + v := zcncrypto.NewSignatureScheme(signatureScheme) + err := v.SetPublicKey(publicKey) if err != nil { return false, err } diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index bd5ce37b0..537413e86 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -337,27 +337,20 @@ func Init(chainConfigJSON string) error { return client.Init(context.Background(), cfg) } +var signatureScheme string + // Use client.Init() in core/client package to initialize SDK. // InitSignatureScheme initializes signature scheme only. -// Either InitSignatureScheme() or client.Init() should be called to initialize SDK. Calling both produces unexpected behaviour. func InitSignatureScheme(scheme string) { - cfg, err := conf.GetClientConfig() - if err != nil { - conf.InitClientConfig(&conf.Config{ - SignatureScheme: scheme, - }) - return + if scheme != constants.BLS0CHAIN.String() && scheme != constants.ED25519.String() { + panic("invalid/unsupported signature scheme") } - cfg.SignatureScheme = scheme + signatureScheme = scheme } // CreateWalletOffline creates the wallet for the config signature scheme. func CreateWalletOffline() (string, error) { - cfg, err := conf.GetClientConfig() - if err != nil { - return "", err - } - sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) + sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) wallet, err := sigScheme.GenerateKeys() if err != nil { return "", errors.New("failed to generate keys: " + err.Error()) @@ -374,12 +367,7 @@ func RecoverOfflineWallet(mnemonic string) (string, error) { if !zcncrypto.IsMnemonicValid(mnemonic) { return "", errors.New("Invalid mnemonic") } - - cfg, err := conf.GetClientConfig() - if err != nil { - return "", err - } - sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) + sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) wallet, err := sigScheme.RecoverKeys(mnemonic) if err != nil { return "", err @@ -399,13 +387,9 @@ func RecoverWallet(mnemonic string, statusCb WalletCallback) error { if !zcncrypto.IsMnemonicValid(mnemonic) { return errors.New("Invalid mnemonic") } - cfg, err := conf.GetClientConfig() - if err != nil { - return err - } go func() { - sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) - _, err = sigScheme.RecoverKeys(mnemonic) + sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) + _, err := sigScheme.RecoverKeys(mnemonic) if err != nil { statusCb.OnWalletCreateComplete(StatusError, "", err.Error()) return @@ -416,15 +400,11 @@ func RecoverWallet(mnemonic string, statusCb WalletCallback) error { // Split keys from the primary master key func SplitKeys(privateKey string, numSplits int) (string, error) { - cfg, err := conf.GetClientConfig() - if err != nil { - return "", err - } - if cfg.SignatureScheme != constants.BLS0CHAIN.String() { + if signatureScheme != constants.BLS0CHAIN.String() { return "", errors.New("signature key doesn't support split key") } - sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) - err = sigScheme.SetPrivateKey(privateKey) + sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) + err := sigScheme.SetPrivateKey(privateKey) if err != nil { return "", errors.New("set private key failed." + err.Error()) } diff --git a/zcncore/wallet_mobile.go b/zcncore/wallet_mobile.go index 68103f28a..58dc92c0e 100644 --- a/zcncore/wallet_mobile.go +++ b/zcncore/wallet_mobile.go @@ -4,7 +4,6 @@ package zcncore import ( - "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/zcncrypto" ) @@ -17,12 +16,8 @@ type wallet struct { } func (w *wallet) Sign(hash string) (string, error) { - cfg, err := conf.GetClientConfig() - if err != nil { - return "", err - } - sigScheme := zcncrypto.NewSignatureScheme(cfg.SignatureScheme) - err = sigScheme.SetPrivateKey(w.Keys[0].PrivateKey) + sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) + err := sigScheme.SetPrivateKey(w.Keys[0].PrivateKey) if err != nil { return "", err } From 5e013f381599603183c0cb3c0419e9fca43889cc Mon Sep 17 00:00:00 2001 From: storybehind Date: Tue, 20 Feb 2024 13:44:29 +0530 Subject: [PATCH 014/319] refactor zboxcore to use states defined in core/client package --- core/client/set.go | 143 ++++++++-- mobilesdk/sdk/sdk.go | 9 +- mobilesdk/sdk/sign.go | 2 +- mobilesdk/zbox/util.go | 15 +- mobilesdk/zboxapi/client.go | 13 +- sdks/client.go | 4 +- sdks/zbox.go | 2 +- wasmsdk/proxy.go | 5 +- wasmsdk/wallet.go | 11 +- winsdk/zboxapi.go | 10 +- zboxcore/allocationchange/newfile.go | 2 +- zboxcore/allocationchange/updatefile.go | 2 +- zboxcore/blockchain/entity.go | 292 ++++++++++---------- zboxcore/client/entity.go | 149 ---------- zboxcore/marker/authticket.go | 2 +- zboxcore/marker/deletetoken.go | 2 +- zboxcore/marker/readmarker.go | 2 +- zboxcore/marker/writemarker.go | 2 +- zboxcore/sdk/allocation_file_delete_test.go | 12 +- zboxcore/sdk/allocation_file_test.go | 7 +- zboxcore/sdk/allocation_test.go | 102 +++---- zboxcore/sdk/blockdownloadworker.go | 8 +- zboxcore/sdk/chunked_upload.go | 6 +- zboxcore/sdk/chunked_upload_blobber.go | 4 +- zboxcore/sdk/chunked_upload_form_builder.go | 2 +- zboxcore/sdk/commitworker.go | 6 +- zboxcore/sdk/copyworker.go | 3 +- zboxcore/sdk/copyworker_test.go | 12 +- zboxcore/sdk/deleteworker.go | 3 +- zboxcore/sdk/deleteworker_test.go | 12 +- zboxcore/sdk/dirworker.go | 3 +- zboxcore/sdk/downloadworker.go | 12 +- zboxcore/sdk/filemetaworker_test.go | 12 +- zboxcore/sdk/filestatsworker_test.go | 14 +- zboxcore/sdk/listworker_test.go | 12 +- zboxcore/sdk/moveworker.go | 3 +- zboxcore/sdk/multi_operation_worker.go | 3 +- zboxcore/sdk/networkworker.go | 258 ++++++++--------- zboxcore/sdk/playlist.go | 14 +- zboxcore/sdk/renameworker.go | 3 +- zboxcore/sdk/renameworker_test.go | 12 +- zboxcore/sdk/rollback.go | 8 +- zboxcore/sdk/sdk.go | 143 ++++++---- zboxcore/sdk/sharerequest.go | 6 +- zboxcore/sdk/writemarker_mutex.go | 3 +- zboxcore/sdk/writemarker_mutex_test.go | 7 +- zboxcore/zboxutil/http.go | 23 +- zcncore/transaction_base.go | 7 +- 48 files changed, 651 insertions(+), 736 deletions(-) delete mode 100644 zboxcore/client/entity.go diff --git a/core/client/set.go b/core/client/set.go index 7dff29687..2cb135bfe 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -5,83 +5,176 @@ import ( "strings" "github.com/0chain/gosdk/constants" - "github.com/0chain/gosdk/core/conf" + "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/core/zcncrypto" ) -// maintains client's data var ( - wallet *zcncrypto.Wallet + client Client +) + +type SignFunc func(hash string) (string, error) + +// maintains client's information +type Client struct { + wallet *zcncrypto.Wallet + signatureScheme string splitKeyWallet bool authUrl string nonce int64 - fee uint64 -) + txnFee uint64 + sign SignFunc +} func init() { - wallet = &zcncrypto.Wallet{} + sys.Sign = signHash + client = Client { + wallet: &zcncrypto.Wallet{}, + sign: func(hash string) (string, error) { + return sys.Sign(hash, client.signatureScheme, GetClientSysKeys()) + }, + } + sys.Verify = verifySignature + sys.VerifyWith = verifySignatureWith +} + +func signHash(hash string, signatureScheme string, keys []sys.KeyPair) (string, error) { + retSignature := "" + for _, kv := range keys { + ss := zcncrypto.NewSignatureScheme(signatureScheme) + err := ss.SetPrivateKey(kv.PrivateKey) + if err != nil { + return "", err + } + + if len(retSignature) == 0 { + retSignature, err = ss.Sign(hash) + } else { + retSignature, err = ss.Add(retSignature, hash) + } + if err != nil { + return "", err + } + } + return retSignature, nil +} + +func verifySignature(signature string, msg string) (bool, error) { + ss := zcncrypto.NewSignatureScheme(client.signatureScheme) + if err := ss.SetPublicKey(client.wallet.ClientKey); err != nil { + return false, err + } + + return ss.Verify(signature, msg) +} + +func verifySignatureWith(pubKey, signature, hash string) (bool, error) { + sch := zcncrypto.NewSignatureScheme(client.signatureScheme) + err := sch.SetPublicKey(pubKey) + if err != nil { + return false, err + } + return sch.Verify(signature, hash) +} + +func GetClientSysKeys() []sys.KeyPair { + var keys []sys.KeyPair + for _, kv := range client.wallet.Keys { + keys = append(keys, sys.KeyPair{ + PrivateKey: kv.PrivateKey, + PublicKey: kv.PublicKey, + }) + } + return keys } // SetWallet should be set before any transaction or client specific APIs func SetWallet(w zcncrypto.Wallet) error { - wallet = &w + client.wallet = &w return nil } // splitKeyWallet parameter is valid only if SignatureScheme is "BLS0Chain" func SetSplitKeyWallet(isSplitKeyWallet bool) error { - cfg, err := conf.GetClientConfig() - if err != nil { - return err - } - if cfg.SignatureScheme == constants.BLS0CHAIN.String() { - splitKeyWallet = isSplitKeyWallet + if client.signatureScheme == constants.BLS0CHAIN.String() { + client.splitKeyWallet = isSplitKeyWallet } return nil } // SetAuthUrl will be called by app to set zauth URL to SDK func SetAuthUrl(url string) error { - if !splitKeyWallet { + if !client.splitKeyWallet { return errors.New("wallet type is not split key") } if url == "" { return errors.New("invalid auth url") } - authUrl = strings.TrimRight(url, "/") + client.authUrl = strings.TrimRight(url, "/") return nil } func SetNonce(n int64) error { - nonce = n + client.nonce = n + return nil +} + +func SetTxnFee(f uint64) error { + client.txnFee = f return nil } -func SetFee(f uint64) error { - fee = f +func SetSignatureScheme(signatureScheme string) error { + if signatureScheme != constants.BLS0CHAIN.String() && signatureScheme != constants.ED25519.String() { + return errors.New("invalid/unsupported signature scheme") + } + client.signatureScheme = signatureScheme return nil } func Wallet() *zcncrypto.Wallet { - return wallet + return client.wallet +} + +func SignatureScheme() string { + return client.signatureScheme } func SplitKeyWallet() bool { - return splitKeyWallet + return client.splitKeyWallet } func AuthUrl() string { - return authUrl + return client.authUrl } func Nonce() int64 { - return nonce + return client.nonce } -func Fee() uint64 { - return fee +func TxnFee() uint64 { + return client.txnFee +} + +func Sign(hash string) (string, error) { + return client.sign(hash) } func IsWalletSet() bool { - return wallet == nil || wallet.ClientID != "" + return client.wallet.ClientID != "" +} + +func PublicKey() string { + return client.wallet.ClientKey +} + +func PrivateKey() string { + for _, kv := range client.wallet.Keys { + return kv.PrivateKey + } + return "" +} + +func ClientID() string { + return client.wallet.ClientID } diff --git a/mobilesdk/sdk/sdk.go b/mobilesdk/sdk/sdk.go index 3a18186f0..70c5e642b 100644 --- a/mobilesdk/sdk/sdk.go +++ b/mobilesdk/sdk/sdk.go @@ -16,7 +16,7 @@ import ( "github.com/0chain/gosdk/core/util" "github.com/0chain/gosdk/core/version" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" l "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/sdk" @@ -45,8 +45,7 @@ type ChainConfig struct { // StorageSDK - storage SDK config type StorageSDK struct { - chainconfig *ChainConfig - client *client.Client + // chainconfig *ChainConfig } // SetLogFile - setting up log level for core libraries @@ -138,7 +137,7 @@ func InitStorageSDK(clientJson string, configJson string) (*StorageSDK, error) { l.Logger.Info("Init successful") - return &StorageSDK{client: client.GetClient(), chainconfig: configObj}, nil + return &StorageSDK{}, nil } // CreateAllocation - creating new allocation @@ -383,7 +382,7 @@ func (s *StorageSDK) RedeemFreeStorage(ticket string) (string, error) { return "", err } - if recipientPublicKey != client.GetClientPublicKey() { + if recipientPublicKey != client.PublicKey() { return "", fmt.Errorf("invalid_free_marker: free marker is not assigned to your wallet") } diff --git a/mobilesdk/sdk/sign.go b/mobilesdk/sdk/sign.go index c9e705f99..0a5203197 100644 --- a/mobilesdk/sdk/sign.go +++ b/mobilesdk/sdk/sign.go @@ -6,7 +6,7 @@ import ( "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/core/zcncrypto" - _ "github.com/0chain/gosdk/zboxcore/client" //import it to initialize sys.Sign + _ "github.com/0chain/gosdk/core/client" //import it to initialize sys.Sign ) var ErrInvalidSignatureScheme = errors.New("invalid_signature_scheme") diff --git a/mobilesdk/zbox/util.go b/mobilesdk/zbox/util.go index dafb63935..a7321d40c 100644 --- a/mobilesdk/zbox/util.go +++ b/mobilesdk/zbox/util.go @@ -8,7 +8,7 @@ import ( "strconv" "github.com/0chain/gosdk/core/sys" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/0chain/gosdk/zcncore" @@ -63,7 +63,18 @@ func Decrypt(key, text string) (string, error) { // GetNetwork - get current network func GetNetwork() (string, error) { - networkDetails := sdk.GetNetwork() + type Network struct { + Miners []string `json:"miners"` + Sharders []string `json:"sharders"` + } + nodeClient, err := client.GetNode() + if err != nil { + return "", err + } + networkDetails := &Network { + Miners: nodeClient.Network().Miners, + Sharders: nodeClient.Network().Sharders, + } networkDetailsBytes, err := json.Marshal(networkDetails) if err != nil { return "", err diff --git a/mobilesdk/zboxapi/client.go b/mobilesdk/zboxapi/client.go index 2e21f4fde..549d30029 100644 --- a/mobilesdk/zboxapi/client.go +++ b/mobilesdk/zboxapi/client.go @@ -14,7 +14,7 @@ import ( "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/zboxapi" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "go.uber.org/zap" ) @@ -29,14 +29,9 @@ func Init(baseUrl, appType string) { zboxApiClient = zboxapi.NewClient() zboxApiClient.SetRequest(baseUrl, appType) - c := client.GetClient() - if c != nil { - err := SetWallet(client.GetClientID(), client.GetClientPrivateKey(), client.GetClientPublicKey()) //nolint: errcheck - if err != nil { - logging.Error("SetWallet", zap.Error(err)) - } - } else { - logging.Info("SetWallet: skipped") + err := SetWallet(client.ClientID(), client.PrivateKey(), client.PublicKey()) //nolint: errcheck + if err != nil { + logging.Error("SetWallet", zap.Error(err)) } } diff --git a/sdks/client.go b/sdks/client.go index d5c4df2ff..7305a52c0 100644 --- a/sdks/client.go +++ b/sdks/client.go @@ -8,7 +8,7 @@ import ( "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/sys" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" ) // Client a client instance of restful api @@ -41,7 +41,7 @@ func (c *Client) SignRequest(req *http.Request, allocation string) error { req.Header.Set("X-App-Client-ID", c.ClientID) req.Header.Set("X-App-Client-Key", c.ClientPublicKey) - sign, err := sys.Sign(encryption.Hash(allocation), client.GetClient().SignatureScheme, client.GetClientSysKeys()) + sign, err := sys.Sign(encryption.Hash(allocation), client.SignatureScheme(), client.GetClientSysKeys()) if err != nil { return err } diff --git a/sdks/zbox.go b/sdks/zbox.go index 85b4e9682..8cd971ae0 100644 --- a/sdks/zbox.go +++ b/sdks/zbox.go @@ -14,7 +14,7 @@ import ( "github.com/0chain/gosdk/core/resty" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/core/zcncrypto" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" ) // ZBox sdk client instance diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index 1bac10b20..a49d95510 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -13,7 +13,7 @@ import ( "github.com/0chain/gosdk/core/version" "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/wasmsdk/jsbridge" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/0chain/gosdk/zcncore" @@ -45,8 +45,7 @@ func main() { if !(jsSign.IsNull() || jsSign.IsUndefined()) { signFunc := func(hash string) (string, error) { - c := client.GetClient() - pk := c.Keys[0].PrivateKey + pk := client.Wallet().Keys[0].PrivateKey result, err := jsbridge.Await(jsSign.Invoke(hash, pk)) if len(err) > 0 && !err[0].IsNull() { diff --git a/wasmsdk/wallet.go b/wasmsdk/wallet.go index 19911d53b..2515771dc 100644 --- a/wasmsdk/wallet.go +++ b/wasmsdk/wallet.go @@ -5,8 +5,7 @@ package main import ( "github.com/0chain/gosdk/core/zcncrypto" - "github.com/0chain/gosdk/zboxcore/client" - "github.com/0chain/gosdk/zcncore" + "github.com/0chain/gosdk/core/client" ) func setWallet(clientID, publicKey, privateKey, mnemonic string) error { @@ -17,19 +16,13 @@ func setWallet(clientID, publicKey, privateKey, mnemonic string) error { }, } - c := client.GetClient() - c.Mnemonic = mnemonic - c.ClientID = clientID - c.ClientKey = publicKey - c.Keys = keys - w := &zcncrypto.Wallet{ ClientID: clientID, ClientKey: publicKey, Mnemonic: mnemonic, Keys: keys, } - err := zcncore.SetWallet(*w, false) + err := client.SetWallet(*w) if err != nil { return err } diff --git a/winsdk/zboxapi.go b/winsdk/zboxapi.go index 0a8c5a628..4576d7773 100644 --- a/winsdk/zboxapi.go +++ b/winsdk/zboxapi.go @@ -15,7 +15,7 @@ import ( "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/zboxapi" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" ) var ( @@ -39,13 +39,7 @@ func InitZBox(zboxHost, zboxAppType *C.char) { } zboxApiClient.SetRequest(C.GoString(zboxHost), C.GoString(zboxAppType)) - - c := client.GetClient() - if c != nil { - zboxApiClient.SetWallet(client.GetClientID(), client.GetClientPrivateKey(), client.GetClientPublicKey()) - } else { - logging.Info("SetWallet: skipped") - } + zboxApiClient.SetWallet(client.ClientID(), client.PrivateKey(), client.PublicKey()) } // SetZBoxWallet set wallet on zbox api diff --git a/zboxcore/allocationchange/newfile.go b/zboxcore/allocationchange/newfile.go index d7961c505..d65641e3d 100644 --- a/zboxcore/allocationchange/newfile.go +++ b/zboxcore/allocationchange/newfile.go @@ -9,8 +9,8 @@ import ( zError "github.com/0chain/errors" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/pathutil" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/util" - "github.com/0chain/gosdk/zboxcore/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/google/uuid" ) diff --git a/zboxcore/allocationchange/updatefile.go b/zboxcore/allocationchange/updatefile.go index f3b89ae8d..0b4e46a83 100644 --- a/zboxcore/allocationchange/updatefile.go +++ b/zboxcore/allocationchange/updatefile.go @@ -6,7 +6,7 @@ import ( "github.com/0chain/errors" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/pathutil" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" ) diff --git a/zboxcore/blockchain/entity.go b/zboxcore/blockchain/entity.go index 77f4efe7d..3844edc1e 100644 --- a/zboxcore/blockchain/entity.go +++ b/zboxcore/blockchain/entity.go @@ -1,54 +1,48 @@ package blockchain import ( - "encoding/json" - "github.com/0chain/gosdk/core/util" - "math" - "sync" "sync/atomic" "github.com/0chain/gosdk/core/common" - "github.com/0chain/gosdk/core/conf" - "github.com/0chain/gosdk/core/node" ) -var miners []string -var mGuard sync.Mutex - -func getMinMinersSubmit() int { - minMiners := util.MaxInt(calculateMinRequired(float64(chain.MinSubmit), float64(len(chain.Miners))/100), 1) - return minMiners -} - -func calculateMinRequired(minRequired, percent float64) int { - return int(math.Ceil(minRequired * percent)) -} - -func GetStableMiners() []string { - mGuard.Lock() - defer mGuard.Unlock() - if len(miners) == 0 { - miners = util.GetRandom(chain.Miners, getMinMinersSubmit()) - } - - return miners -} -func ResetStableMiners() { - mGuard.Lock() - defer mGuard.Unlock() - miners = util.GetRandom(chain.Miners, getMinMinersSubmit()) -} - -type ChainConfig struct { - BlockWorker string - Sharders []string - Miners []string - MinSubmit int - MinConfirmation int - ChainID string - MaxTxnQuery int - QuerySleepTime int -} +// var miners []string +// var mGuard sync.Mutex + +// func getMinMinersSubmit() int { +// minMiners := util.MaxInt(calculateMinRequired(float64(chain.MinSubmit), float64(len(chain.Miners))/100), 1) +// return minMiners +// } + +// func calculateMinRequired(minRequired, percent float64) int { +// return int(math.Ceil(minRequired * percent)) +// } + +// func GetStableMiners() []string { +// mGuard.Lock() +// defer mGuard.Unlock() +// if len(miners) == 0 { +// miners = util.GetRandom(chain.Miners, getMinMinersSubmit()) +// } + +// return miners +// } +// func ResetStableMiners() { +// mGuard.Lock() +// defer mGuard.Unlock() +// miners = util.GetRandom(chain.Miners, getMinMinersSubmit()) +// } + +// type ChainConfig struct { +// BlockWorker string +// Sharders []string +// Miners []string +// MinSubmit int +// MinConfirmation int +// ChainID string +// MaxTxnQuery int +// QuerySleepTime int +// } // StakePoolSettings information. type StakePoolSettings struct { @@ -99,109 +93,109 @@ func (sn *StorageNode) IsSkip() bool { return atomic.LoadUint64(&sn.skip) > 0 } -func PopulateNodes(nodesjson string) ([]string, error) { - sharders := make([]string, 0) - err := json.Unmarshal([]byte(nodesjson), &sharders) - return sharders, err -} - -var chain *ChainConfig -var Sharders *node.NodeHolder - -func init() { - chain = &ChainConfig{ - MaxTxnQuery: 5, - QuerySleepTime: 5, - MinSubmit: 10, - MinConfirmation: 10, - } -} - -func GetChainID() string { - return chain.ChainID -} - -func PopulateChain(minerjson string, sharderjson string) error { - var err error - chain.Miners, err = PopulateNodes(minerjson) - if err != nil { - return err - } - sharders, err := PopulateNodes(sharderjson) - if err != nil { - return err - } - SetSharders(sharders) - return nil -} - -func GetBlockWorker() string { - return chain.BlockWorker -} - -func GetAllSharders() []string { - return Sharders.All() -} -func GetSharders() []string { - return Sharders.Healthy() -} - -func GetMiners() []string { - return chain.Miners -} - -func GetMaxTxnQuery() int { - return chain.MaxTxnQuery -} - -func GetQuerySleepTime() int { - return chain.QuerySleepTime -} - -func GetMinSubmit() int { - return chain.MinSubmit -} - -func GetMinConfirmation() int { - return chain.MinConfirmation -} - -func SetBlockWorker(blockWorker string) { - chain.BlockWorker = blockWorker -} - -func SetSharders(sharderArray []string) { - consensus := conf.DefaultSharderConsensous - config, err := conf.GetClientConfig() - if err == nil && config != nil { - consensus = config.SharderConsensous - } - if len(sharderArray) < consensus { - consensus = len(sharderArray) - } - Sharders = node.NewHolder(sharderArray, consensus) -} - -func SetMiners(minerArray []string) { - chain.Miners = minerArray -} - -func SetChainID(id string) { - chain.ChainID = id -} - -func SetMaxTxnQuery(num int) { - chain.MaxTxnQuery = num -} - -func SetQuerySleepTime(time int) { - chain.QuerySleepTime = time -} - -func SetMinSubmit(minSubmit int) { - chain.MinSubmit = minSubmit -} - -func SetMinConfirmation(minConfirmation int) { - chain.MinConfirmation = minConfirmation -} +// func PopulateNodes(nodesjson string) ([]string, error) { +// sharders := make([]string, 0) +// err := json.Unmarshal([]byte(nodesjson), &sharders) +// return sharders, err +// } + +// var chain *ChainConfig +// var Sharders *node.NodeHolder + +// func init() { +// chain = &ChainConfig{ +// MaxTxnQuery: 5, +// QuerySleepTime: 5, +// MinSubmit: 10, +// MinConfirmation: 10, +// } +// } + +// func GetChainID() string { +// return chain.ChainID +// } + +// func PopulateChain(minerjson string, sharderjson string) error { +// var err error +// chain.Miners, err = PopulateNodes(minerjson) +// if err != nil { +// return err +// } +// sharders, err := PopulateNodes(sharderjson) +// if err != nil { +// return err +// } +// SetSharders(sharders) +// return nil +// } + +// func GetBlockWorker() string { +// return chain.BlockWorker +// } + +// func GetAllSharders() []string { +// return Sharders.All() +// } +// func GetSharders() []string { +// return Sharders.Healthy() +// } + +// func GetMiners() []string { +// return chain.Miners +// } + +// func GetMaxTxnQuery() int { +// return chain.MaxTxnQuery +// } + +// func GetQuerySleepTime() int { +// return chain.QuerySleepTime +// } + +// func GetMinSubmit() int { +// return chain.MinSubmit +// } + +// func GetMinConfirmation() int { +// return chain.MinConfirmation +// } + +// func SetBlockWorker(blockWorker string) { +// chain.BlockWorker = blockWorker +// } + +// func SetSharders(sharderArray []string) { +// consensus := conf.DefaultSharderConsensous +// config, err := conf.GetClientConfig() +// if err == nil && config != nil { +// consensus = config.SharderConsensous +// } +// if len(sharderArray) < consensus { +// consensus = len(sharderArray) +// } +// Sharders = node.NewHolder(sharderArray, consensus) +// } + +// func SetMiners(minerArray []string) { +// chain.Miners = minerArray +// } + +// func SetChainID(id string) { +// chain.ChainID = id +// } + +// func SetMaxTxnQuery(num int) { +// chain.MaxTxnQuery = num +// } + +// func SetQuerySleepTime(time int) { +// chain.QuerySleepTime = time +// } + +// func SetMinSubmit(minSubmit int) { +// chain.MinSubmit = minSubmit +// } + +// func SetMinConfirmation(minConfirmation int) { +// chain.MinConfirmation = minConfirmation +// } diff --git a/zboxcore/client/entity.go b/zboxcore/client/entity.go deleted file mode 100644 index f36e0e30a..000000000 --- a/zboxcore/client/entity.go +++ /dev/null @@ -1,149 +0,0 @@ -package client - -import ( - "encoding/json" - - "github.com/0chain/gosdk/core/sys" - "github.com/0chain/gosdk/core/zcncrypto" -) - -type SignFunc func(hash string) (string, error) - -type Client struct { - *zcncrypto.Wallet - SignatureScheme string - txnFee uint64 -} - -var ( - client *Client - clients []*Client - Sign SignFunc -) - -func init() { - client = &Client{ - Wallet: &zcncrypto.Wallet{}, - } - - sys.Sign = signHash - // initialize SignFunc as default implementation - Sign = func(hash string) (string, error) { - return sys.Sign(hash, client.SignatureScheme, GetClientSysKeys()) - } - - sys.Verify = VerifySignature - sys.VerifyWith = VerifySignatureWith -} - -// PopulateClient populates single client -func PopulateClient(clientjson string, signatureScheme string) error { - err := json.Unmarshal([]byte(clientjson), &client) - client.SignatureScheme = signatureScheme - return err -} - -func SetClientNonce(nonce int64) { - client.Nonce = nonce -} - -// SetTxnFee sets general transaction fee -func SetTxnFee(fee uint64) { - client.txnFee = fee -} - -// TxnFee gets general txn fee -func TxnFee() uint64 { - return client.txnFee -} - -// PopulateClients This is a workaround for blobber tests that requires multiple clients to test authticket functionality -func PopulateClients(clientJsons []string, signatureScheme string) error { - for _, clientJson := range clientJsons { - c := new(Client) - if err := json.Unmarshal([]byte(clientJson), c); err != nil { - return err - } - c.SignatureScheme = signatureScheme - clients = append(clients, c) - } - return nil -} - -func GetClient() *Client { - return client -} - -func GetClients() []*Client { - return clients -} - -func GetClientID() string { - return client.ClientID -} - -func GetClientPublicKey() string { - return client.ClientKey -} - -func GetClientPrivateKey() string { - for _, kv := range client.Keys { - return kv.PrivateKey - } - - return "" -} - -// GetClientSysKeys convert client.KeyPair to sys.KeyPair -func GetClientSysKeys() []sys.KeyPair { - var keys []sys.KeyPair - if client != nil { - for _, kv := range client.Keys { - keys = append(keys, sys.KeyPair{ - PrivateKey: kv.PrivateKey, - PublicKey: kv.PublicKey, - }) - } - } - - return keys -} - -func signHash(hash string, signatureScheme string, keys []sys.KeyPair) (string, error) { - retSignature := "" - for _, kv := range keys { - ss := zcncrypto.NewSignatureScheme(signatureScheme) - err := ss.SetPrivateKey(kv.PrivateKey) - if err != nil { - return "", err - } - - if len(retSignature) == 0 { - retSignature, err = ss.Sign(hash) - } else { - retSignature, err = ss.Add(retSignature, hash) - } - if err != nil { - return "", err - } - } - return retSignature, nil -} - -func VerifySignature(signature string, msg string) (bool, error) { - ss := zcncrypto.NewSignatureScheme(client.SignatureScheme) - if err := ss.SetPublicKey(client.ClientKey); err != nil { - return false, err - } - - return ss.Verify(signature, msg) -} - -func VerifySignatureWith(pubKey, signature, hash string) (bool, error) { - sch := zcncrypto.NewSignatureScheme(client.SignatureScheme) - err := sch.SetPublicKey(pubKey) - if err != nil { - return false, err - } - return sch.Verify(signature, hash) -} diff --git a/zboxcore/marker/authticket.go b/zboxcore/marker/authticket.go index 4cf3a94a9..cb0e38e47 100644 --- a/zboxcore/marker/authticket.go +++ b/zboxcore/marker/authticket.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/0chain/gosdk/core/encryption" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" ) type AuthTicket struct { diff --git a/zboxcore/marker/deletetoken.go b/zboxcore/marker/deletetoken.go index 0c8ae945d..37594c003 100644 --- a/zboxcore/marker/deletetoken.go +++ b/zboxcore/marker/deletetoken.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/0chain/gosdk/core/encryption" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" ) type DeleteToken struct { diff --git a/zboxcore/marker/readmarker.go b/zboxcore/marker/readmarker.go index dbc771142..53db55ccd 100644 --- a/zboxcore/marker/readmarker.go +++ b/zboxcore/marker/readmarker.go @@ -7,7 +7,7 @@ import ( "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/sys" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" ) type ReadMarker struct { diff --git a/zboxcore/marker/writemarker.go b/zboxcore/marker/writemarker.go index 9d3ffba01..efaa3cdc9 100644 --- a/zboxcore/marker/writemarker.go +++ b/zboxcore/marker/writemarker.go @@ -6,7 +6,7 @@ import ( "github.com/0chain/errors" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/sys" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" ) type WriteMarker struct { diff --git a/zboxcore/sdk/allocation_file_delete_test.go b/zboxcore/sdk/allocation_file_delete_test.go index e1bee5283..50b675a56 100644 --- a/zboxcore/sdk/allocation_file_delete_test.go +++ b/zboxcore/sdk/allocation_file_delete_test.go @@ -11,7 +11,7 @@ import ( "github.com/0chain/gosdk/core/resty" "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zboxcore/blockchain" - zclient "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/mocks" "github.com/0chain/gosdk/zboxcore/zboxutil" @@ -29,11 +29,10 @@ func TestAllocation_DeleteFile(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) zboxutil.Client = &mockClient resty.CreateClient = func(t *http.Transport, timeout time.Duration) resty.Client { @@ -89,11 +88,10 @@ func TestAllocation_deleteFile(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) zboxutil.Client = &mockClient resty.CreateClient = func(t *http.Transport, timeout time.Duration) resty.Client { diff --git a/zboxcore/sdk/allocation_file_test.go b/zboxcore/sdk/allocation_file_test.go index 91ff49239..030dbb0b4 100644 --- a/zboxcore/sdk/allocation_file_test.go +++ b/zboxcore/sdk/allocation_file_test.go @@ -21,7 +21,7 @@ import ( "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zboxcore/blockchain" - zclient "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/mocks" "github.com/0chain/gosdk/zboxcore/zboxutil" @@ -641,11 +641,10 @@ func TestAllocation_RepairFile(t *testing.T) { resty.CreateClient = createClient }() - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) // setupHttpResponses := func(t *testing.T, testName string, numBlobbers, numCorrect int) { // require.True(t, numBlobbers >= numCorrect) diff --git a/zboxcore/sdk/allocation_test.go b/zboxcore/sdk/allocation_test.go index d72e155cc..ff7ccb943 100644 --- a/zboxcore/sdk/allocation_test.go +++ b/zboxcore/sdk/allocation_test.go @@ -28,7 +28,7 @@ import ( "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zboxcore/blockchain" - zclient "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/mocks" "github.com/0chain/gosdk/zboxcore/zboxutil" @@ -400,11 +400,10 @@ func TestAllocation_GetBlobberStats(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) tests := []struct { name string @@ -546,11 +545,10 @@ func TestAllocation_RepairRequired(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) tests := []struct { name string @@ -694,11 +692,10 @@ func TestAllocation_DownloadFileToFileHandler(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) type parameters struct { fileHandler sys.File @@ -776,11 +773,10 @@ func TestAllocation_DownloadFile(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) require := require.New(t) a := &Allocation{ @@ -824,11 +820,10 @@ func TestAllocation_DownloadFileByBlock(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) require := require.New(t) a := &Allocation{ @@ -879,11 +874,10 @@ func TestAllocation_downloadFile(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) type parameters struct { localPath, remotePath, contentMode string @@ -1019,11 +1013,10 @@ func TestAllocation_GetRefs(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) functionName := "TestAllocation_GetRefs" t.Run("Test_Get_Refs_Returns_Slice_Of_Length_0_When_File_Not_Present", func(t *testing.T) { a := &Allocation{ @@ -1064,11 +1057,10 @@ func TestAllocation_GetFileMeta(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) type parameters struct { path string @@ -1182,11 +1174,10 @@ func TestAllocation_GetAuthTicketForShare(t *testing.T) { mockClient.On("Do", mock.Anything).Return(&httpResponse, nil) } - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) require := require.New(t) a := &Allocation{DataShards: 1, ParityShards: 1, FileOptions: 63} a.InitAllocation() @@ -1388,11 +1379,10 @@ func TestAllocation_GetAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) require := require.New(t) a := &Allocation{ @@ -1598,11 +1588,10 @@ func TestAllocation_ListDirFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) a := &Allocation{ ID: mockAllocationId, Tx: mockAllocationTxId, @@ -1652,11 +1641,10 @@ func TestAllocation_downloadFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) a := &Allocation{ ID: mockAllocationId, @@ -1878,11 +1866,10 @@ func TestAllocation_listDir(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) require := require.New(t) a := &Allocation{ @@ -2012,11 +1999,10 @@ func TestAllocation_GetFileMetaFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) a := &Allocation{ ID: mockAllocationId, @@ -2061,11 +2047,10 @@ func TestAllocation_DownloadToFileHandlerFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) require := require.New(t) @@ -2097,11 +2082,10 @@ func TestAllocation_DownloadThumbnailFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) require := require.New(t) @@ -2140,11 +2124,10 @@ func TestAllocation_DownloadFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) require := require.New(t) @@ -2177,11 +2160,10 @@ func TestAllocation_DownloadFromAuthTicketByBlocks(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) require := require.New(t) @@ -2213,11 +2195,10 @@ func TestAllocation_StartRepair(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) type parameters struct { localPath, pathToRepair string @@ -2399,11 +2380,10 @@ func getMockAuthTicket(t *testing.T) string { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) a := &Allocation{ ID: mockAllocationId, Tx: mockAllocationTxId, diff --git a/zboxcore/sdk/blockdownloadworker.go b/zboxcore/sdk/blockdownloadworker.go index 9420375fd..df2fdb1c4 100644 --- a/zboxcore/sdk/blockdownloadworker.go +++ b/zboxcore/sdk/blockdownloadworker.go @@ -13,7 +13,7 @@ import ( "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/util" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" zlogger "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/marker" @@ -149,7 +149,7 @@ func (req *BlockDownloadRequest) downloadBlobberBlock() { header.ToHeader(httpreq) - zlogger.Logger.Debug(fmt.Sprintf("downloadBlobberBlock - blobberID: %v, clientID: %v, blockNum: %d", req.blobber.ID, client.GetClientID(), header.BlockNum)) + zlogger.Logger.Debug(fmt.Sprintf("downloadBlobberBlock - blobberID: %v, clientID: %v, blockNum: %d", req.blobber.ID, client.ClientID(), header.BlockNum)) err = zboxutil.HttpDo(ctx, cncl, httpreq, func(resp *http.Response, err error) error { if err != nil { @@ -188,7 +188,7 @@ func (req *BlockDownloadRequest) downloadBlobberBlock() { } } if resp.StatusCode != http.StatusOK { - zlogger.Logger.Debug(fmt.Sprintf("downloadBlobberBlock FAIL - blobberID: %v, clientID: %v, blockNum: %d, retry: %d, response: %v", req.blobber.ID, client.GetClientID(), header.BlockNum, retry, string(req.respBuf))) + zlogger.Logger.Debug(fmt.Sprintf("downloadBlobberBlock FAIL - blobberID: %v, clientID: %v, blockNum: %d, retry: %d, response: %v", req.blobber.ID, client.ClientID(), header.BlockNum, retry, string(req.respBuf))) if err = json.Unmarshal(req.respBuf, &rspData); err == nil { return errors.New("download_error", fmt.Sprintf("Response status: %d, Error: %v,", resp.StatusCode, rspData.err)) } @@ -237,7 +237,7 @@ func (req *BlockDownloadRequest) downloadBlobberBlock() { rspData.BlockChunks = req.splitData(dR.Data, req.chunkSize) } - zlogger.Logger.Debug(fmt.Sprintf("downloadBlobberBlock 200 OK: blobberID: %v, clientID: %v, blockNum: %d", req.blobber.ID, client.GetClientID(), header.BlockNum)) + zlogger.Logger.Debug(fmt.Sprintf("downloadBlobberBlock 200 OK: blobberID: %v, clientID: %v, blockNum: %d", req.blobber.ID, client.ClientID(), header.BlockNum)) req.result <- &rspData return nil diff --git a/zboxcore/sdk/chunked_upload.go b/zboxcore/sdk/chunked_upload.go index 366b0f63f..174bf63b3 100644 --- a/zboxcore/sdk/chunked_upload.go +++ b/zboxcore/sdk/chunked_upload.go @@ -23,7 +23,7 @@ import ( "github.com/0chain/gosdk/core/util" "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/encryption" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" @@ -249,7 +249,7 @@ func CreateChunkedUpload( } - su.writeMarkerMutex, err = CreateWriteMarkerMutex(client.GetClient(), su.allocationObj) + su.writeMarkerMutex, err = CreateWriteMarkerMutex(su.allocationObj) if err != nil { return nil, err } @@ -367,7 +367,7 @@ func (su *ChunkedUpload) createEncscheme() encryption.EncryptionScheme { return nil } } else { - privateKey, err := encscheme.Initialize(client.GetClient().Mnemonic) + privateKey, err := encscheme.Initialize(client.Wallet().Mnemonic) if err != nil { return nil } diff --git a/zboxcore/sdk/chunked_upload_blobber.go b/zboxcore/sdk/chunked_upload_blobber.go index 8b42c85f9..5a4e93d56 100644 --- a/zboxcore/sdk/chunked_upload_blobber.go +++ b/zboxcore/sdk/chunked_upload_blobber.go @@ -16,7 +16,7 @@ import ( "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/marker" @@ -216,7 +216,7 @@ func (sb *ChunkedUploadBlobber) processCommit(ctx context.Context, su *ChunkedUp wm.BlobberID = sb.blobber.ID wm.Timestamp = timestamp - wm.ClientID = client.GetClientID() + wm.ClientID = client.ClientID() err = wm.Sign() if err != nil { logger.Logger.Error("Signing writemarker failed: ", err) diff --git a/zboxcore/sdk/chunked_upload_form_builder.go b/zboxcore/sdk/chunked_upload_form_builder.go index f0637fa7b..922415f7b 100644 --- a/zboxcore/sdk/chunked_upload_form_builder.go +++ b/zboxcore/sdk/chunked_upload_form_builder.go @@ -8,7 +8,7 @@ import ( "mime/multipart" "sync" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "golang.org/x/crypto/sha3" ) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index e6cc22daf..fff74a233 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -17,7 +17,7 @@ import ( thrown "github.com/0chain/errors" "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" l "github.com/0chain/gosdk/zboxcore/logger" @@ -151,7 +151,7 @@ func (commitreq *CommitRequest) processCommit() { } if lR.LatestWM != nil { - err = lR.LatestWM.VerifySignature(client.GetClientPublicKey()) + err = lR.LatestWM.VerifySignature(client.PublicKey()) if err != nil { e := errors.New("signature_verification_failed", err.Error()) commitreq.result = ErrorCommitResult(e.Error()) @@ -217,7 +217,7 @@ func (req *CommitRequest) commitBlobber( wm.Size = size wm.BlobberID = req.blobber.ID wm.Timestamp = req.timestamp - wm.ClientID = client.GetClientID() + wm.ClientID = client.ClientID() err = wm.Sign() if err != nil { l.Logger.Error("Signing writemarker failed: ", err) diff --git a/zboxcore/sdk/copyworker.go b/zboxcore/sdk/copyworker.go index 7cb216ec6..c87655d82 100644 --- a/zboxcore/sdk/copyworker.go +++ b/zboxcore/sdk/copyworker.go @@ -17,7 +17,6 @@ import ( "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/util" - "github.com/0chain/gosdk/zboxcore/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" @@ -207,7 +206,7 @@ func (req *CopyRequest) ProcessCopy() error { req.Consensus.consensusThresh, req.Consensus.consensus)) } - writeMarkerMutex, err := CreateWriteMarkerMutex(client.GetClient(), req.allocationObj) + writeMarkerMutex, err := CreateWriteMarkerMutex(req.allocationObj) if err != nil { return fmt.Errorf("Copy failed: %s", err.Error()) } diff --git a/zboxcore/sdk/copyworker_test.go b/zboxcore/sdk/copyworker_test.go index 007b4b837..803f15aa3 100644 --- a/zboxcore/sdk/copyworker_test.go +++ b/zboxcore/sdk/copyworker_test.go @@ -20,7 +20,7 @@ import ( devMock "github.com/0chain/gosdk/dev/mock" "github.com/0chain/gosdk/sdks/blobber" "github.com/0chain/gosdk/zboxcore/blockchain" - zclient "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/mocks" "github.com/0chain/gosdk/zboxcore/zboxutil" @@ -43,11 +43,10 @@ func TestCopyRequest_copyBlobberObject(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) type parameters struct { referencePathToRetrieve fileref.ReferencePath @@ -249,11 +248,10 @@ func TestCopyRequest_ProcessCopy(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) zboxutil.Client = &mockClient diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index 9e8c2330e..651e32144 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -18,7 +18,6 @@ import ( "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" l "github.com/0chain/gosdk/zboxcore/logger" @@ -227,7 +226,7 @@ func (req *DeleteRequest) ProcessDelete() (err error) { req.consensus.consensusThresh, req.consensus.getConsensus())) } - writeMarkerMutex, err := CreateWriteMarkerMutex(client.GetClient(), req.allocationObj) + writeMarkerMutex, err := CreateWriteMarkerMutex(req.allocationObj) if err != nil { return fmt.Errorf("Delete failed: %s", err.Error()) } diff --git a/zboxcore/sdk/deleteworker_test.go b/zboxcore/sdk/deleteworker_test.go index b5073fc2a..1bc3bb3cd 100644 --- a/zboxcore/sdk/deleteworker_test.go +++ b/zboxcore/sdk/deleteworker_test.go @@ -17,7 +17,7 @@ import ( "github.com/0chain/gosdk/core/resty" "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zboxcore/blockchain" - zclient "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/mocks" "github.com/0chain/gosdk/zboxcore/zboxutil" @@ -40,11 +40,10 @@ func TestDeleteRequest_deleteBlobberFile(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) var wg sync.WaitGroup @@ -214,11 +213,10 @@ func TestDeleteRequest_ProcessDelete(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) zboxutil.Client = &mockClient resty.CreateClient = func(t *http.Transport, timeout time.Duration) resty.Client { diff --git a/zboxcore/sdk/dirworker.go b/zboxcore/sdk/dirworker.go index 6ec68becd..3a05edbf6 100644 --- a/zboxcore/sdk/dirworker.go +++ b/zboxcore/sdk/dirworker.go @@ -17,7 +17,6 @@ import ( "github.com/0chain/gosdk/core/util" "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" l "github.com/0chain/gosdk/zboxcore/logger" @@ -84,7 +83,7 @@ func (req *DirRequest) ProcessDir(a *Allocation) error { return errors.New("consensus_not_met", "directory creation failed due to consensus not met") } - writeMarkerMU, err := CreateWriteMarkerMutex(client.GetClient(), a) + writeMarkerMU, err := CreateWriteMarkerMutex(a) if err != nil { return fmt.Errorf("directory creation failed. Err: %s", err.Error()) } diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index 2937e2644..7da0eb3c0 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -21,7 +21,7 @@ import ( "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/encryption" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" @@ -659,8 +659,8 @@ func (req *DownloadRequest) attemptSubmitReadMarker(blobber *blockchain.StorageN lockBlobberReadCtr(req.allocationID, blobber.ID) defer unlockBlobberReadCtr(req.allocationID, blobber.ID) rm := &marker.ReadMarker{ - ClientID: client.GetClientID(), - ClientPublicKey: client.GetClientPublicKey(), + ClientID: client.ClientID(), + ClientPublicKey: client.PublicKey(), BlobberID: blobber.ID, AllocationID: req.allocationID, OwnerID: req.allocOwnerID, @@ -799,14 +799,14 @@ func (req *DownloadRequest) initEC() error { // initEncryption will initialize encScheme with client's keys func (req *DownloadRequest) initEncryption() (err error) { req.encScheme = encryption.NewEncryptionScheme() - mnemonic := client.GetClient().Mnemonic + mnemonic := client.Wallet().Mnemonic if mnemonic != "" { - _, err = req.encScheme.Initialize(client.GetClient().Mnemonic) + _, err = req.encScheme.Initialize(client.Wallet().Mnemonic) if err != nil { return err } } else { - key, err := hex.DecodeString(client.GetClientPrivateKey()) + key, err := hex.DecodeString(client.PrivateKey()) if err != nil { return err } diff --git a/zboxcore/sdk/filemetaworker_test.go b/zboxcore/sdk/filemetaworker_test.go index 1bc06cd71..aec411358 100644 --- a/zboxcore/sdk/filemetaworker_test.go +++ b/zboxcore/sdk/filemetaworker_test.go @@ -19,7 +19,7 @@ import ( "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zboxcore/blockchain" - zclient "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/marker" "github.com/0chain/gosdk/zboxcore/mocks" @@ -41,11 +41,10 @@ func TestListRequest_getFileMetaInfoFromBlobber(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) type parameters struct { fileRefToRetrieve fileref.FileRef @@ -206,11 +205,10 @@ func TestListRequest_getFileConsensusFromBlobbers(t *testing.T) { const mockClientId = "mock client id" const mockClientKey = "mock client key" - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) setupHttpResponses := func(t *testing.T, name string, numBlobbers, numCorrect int) { require.True(t, numBlobbers >= numCorrect) diff --git a/zboxcore/sdk/filestatsworker_test.go b/zboxcore/sdk/filestatsworker_test.go index e2e4938b6..62d1e1e12 100644 --- a/zboxcore/sdk/filestatsworker_test.go +++ b/zboxcore/sdk/filestatsworker_test.go @@ -18,7 +18,7 @@ import ( "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zboxcore/blockchain" - zclient "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/mocks" "github.com/0chain/gosdk/zboxcore/zboxutil" @@ -42,11 +42,10 @@ func TestListRequest_getFileStatsInfoFromBlobber(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - var client = zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) type parameters struct { fileStatsHttpResp FileStats @@ -127,7 +126,7 @@ func TestListRequest_getFileStatsInfoFromBlobber(t *testing.T) { require.NoError(t, err) require.EqualValues(t, expected, string(actual)) - sign, _ := zclient.Sign(encryption.Hash(mockAllocationTxId)) + sign, _ := client.Sign(encryption.Hash(mockAllocationTxId)) return req.URL.Path == "Test_Success"+zboxutil.FILE_STATS_ENDPOINT+mockAllocationTxId && req.Method == "POST" && req.Header.Get("X-App-Client-ID") == mockClientId && @@ -188,11 +187,10 @@ func TestListRequest_getFileStatsFromBlobbers(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) setupHttpResponses := func(t *testing.T, name string, numBlobbers, numCorrect int) { for i := 0; i < numBlobbers; i++ { diff --git a/zboxcore/sdk/listworker_test.go b/zboxcore/sdk/listworker_test.go index 85890520c..894d86f69 100644 --- a/zboxcore/sdk/listworker_test.go +++ b/zboxcore/sdk/listworker_test.go @@ -16,7 +16,7 @@ import ( "github.com/0chain/errors" "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zboxcore/blockchain" - zclient "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/marker" "github.com/0chain/gosdk/zboxcore/mocks" @@ -40,11 +40,10 @@ func TestListRequest_getListInfoFromBlobber(t *testing.T) { ) var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) type parameters struct { listHttpResp listResponse ListResult fileref.ListResult @@ -217,11 +216,10 @@ func TestListRequest_GetListFromBlobbers(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) setupHttpResponses := func(t *testing.T, name string, numBlobbers int) { for i := 0; i < numBlobbers; i++ { diff --git a/zboxcore/sdk/moveworker.go b/zboxcore/sdk/moveworker.go index 2380cd542..dac39a83e 100644 --- a/zboxcore/sdk/moveworker.go +++ b/zboxcore/sdk/moveworker.go @@ -17,7 +17,6 @@ import ( "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/common" - "github.com/0chain/gosdk/zboxcore/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" @@ -203,7 +202,7 @@ func (req *MoveRequest) ProcessMove() error { req.Consensus.consensusThresh, req.Consensus.consensus)) } - writeMarkerMutex, err := CreateWriteMarkerMutex(client.GetClient(), req.allocationObj) + writeMarkerMutex, err := CreateWriteMarkerMutex(req.allocationObj) if err != nil { return fmt.Errorf("Move failed: %s", err.Error()) } diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 7f18a26e6..2282af0e8 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -16,7 +16,6 @@ import ( "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/util" "github.com/0chain/gosdk/zboxcore/allocationchange" - "github.com/0chain/gosdk/zboxcore/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" l "github.com/0chain/gosdk/zboxcore/logger" @@ -217,7 +216,7 @@ func (mo *MultiOperation) Process() error { start := time.Now() mo.changes = zboxutil.Transpose(mo.changes) - writeMarkerMutex, err := CreateWriteMarkerMutex(client.GetClient(), mo.allocationObj) + writeMarkerMutex, err := CreateWriteMarkerMutex(mo.allocationObj) if err != nil { return fmt.Errorf("Operation failed: %s", err.Error()) } diff --git a/zboxcore/sdk/networkworker.go b/zboxcore/sdk/networkworker.go index 7f3d8fd01..8dffe6a7b 100644 --- a/zboxcore/sdk/networkworker.go +++ b/zboxcore/sdk/networkworker.go @@ -1,131 +1,131 @@ package sdk -import ( - "context" - "encoding/json" - "io/ioutil" - "net/http" - "reflect" - "strconv" - "time" - - "github.com/0chain/gosdk/core/conf" - "github.com/0chain/gosdk/core/node" - l "github.com/0chain/gosdk/zboxcore/logger" - "go.uber.org/zap" - - "github.com/0chain/errors" - "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/zboxutil" -) - -const NETWORK_ENDPOINT = "/network" - -type Network struct { - Miners []string `json:"miners"` - Sharders []string `json:"sharders"` -} - -func UpdateNetworkDetailsWorker(ctx context.Context) { - ticker := time.NewTicker(time.Duration(networkWorkerTimerInHours) * time.Hour) - for { - select { - case <-ctx.Done(): - l.Logger.Info("Network stopped by user") - return - case <-ticker.C: - err := UpdateNetworkDetails() - if err != nil { - l.Logger.Error("Update network detail worker fail", zap.Error(err)) - return - } - l.Logger.Info("Successfully updated network details") - return - } - } -} - -func UpdateNetworkDetails() error { - networkDetails, err := GetNetworkDetails() - if err != nil { - l.Logger.Error("Failed to update network details ", zap.Error(err)) - return err - } - - shouldUpdate := UpdateRequired(networkDetails) - if shouldUpdate { - forceUpdateNetworkDetails(networkDetails) - } - return nil -} - -func InitNetworkDetails() error { - networkDetails, err := GetNetworkDetails() - if err != nil { - l.Logger.Error("Failed to update network details ", zap.Error(err)) - return err - } - forceUpdateNetworkDetails(networkDetails) - return nil -} - -func forceUpdateNetworkDetails(networkDetails *Network) { - sdkInitialized = false - blockchain.SetMiners(networkDetails.Miners) - blockchain.SetSharders(networkDetails.Sharders) - node.InitCache(blockchain.Sharders) - conf.InitChainNetwork(&conf.Network{ - Sharders: networkDetails.Sharders, - Miners: networkDetails.Miners, - }) - sdkInitialized = true -} - -func UpdateRequired(networkDetails *Network) bool { - miners := blockchain.GetMiners() - sharders := blockchain.GetAllSharders() - if len(miners) == 0 || len(sharders) == 0 { - return true - } - - minerSame := reflect.DeepEqual(miners, networkDetails.Miners) - sharderSame := reflect.DeepEqual(sharders, networkDetails.Sharders) - - if minerSame && sharderSame { - return false - } - return true -} - -func GetNetworkDetails() (*Network, error) { - req, ctx, cncl, err := zboxutil.NewHTTPRequest(http.MethodGet, blockchain.GetBlockWorker()+NETWORK_ENDPOINT, nil) - if err != nil { - return nil, errors.New("get_network_details_error", "Unable to create new http request with error "+err.Error()) - } - - var networkResponse Network - err = zboxutil.HttpDo(ctx, cncl, req, func(resp *http.Response, err error) error { - if err != nil { - l.Logger.Error("Get network error : ", err) - return err - } - - defer resp.Body.Close() - respBody, err := ioutil.ReadAll(resp.Body) - if err != nil { - return errors.Wrap(err, "Error reading response : ") - } - - l.Logger.Debug("Get network result:", string(respBody)) - if resp.StatusCode == http.StatusOK { - err = json.Unmarshal(respBody, &networkResponse) - if err != nil { - return errors.Wrap(err, "Error unmarshaling response :") - } - return nil - } - return errors.New(strconv.Itoa(resp.StatusCode), "Get network details status not OK") - - }) - return &networkResponse, err -} +// import ( +// "context" +// "encoding/json" +// "io/ioutil" +// "net/http" +// "reflect" +// "strconv" +// "time" + +// "github.com/0chain/gosdk/core/conf" +// "github.com/0chain/gosdk/core/node" +// l "github.com/0chain/gosdk/zboxcore/logger" +// "go.uber.org/zap" + +// "github.com/0chain/errors" +// "github.com/0chain/gosdk/zboxcore/blockchain" +// "github.com/0chain/gosdk/zboxcore/zboxutil" +// ) + +// const NETWORK_ENDPOINT = "/network" + +// type Network struct { +// Miners []string `json:"miners"` +// Sharders []string `json:"sharders"` +// } + +// func UpdateNetworkDetailsWorker(ctx context.Context) { +// ticker := time.NewTicker(time.Duration(networkWorkerTimerInHours) * time.Hour) +// for { +// select { +// case <-ctx.Done(): +// l.Logger.Info("Network stopped by user") +// return +// case <-ticker.C: +// err := UpdateNetworkDetails() +// if err != nil { +// l.Logger.Error("Update network detail worker fail", zap.Error(err)) +// return +// } +// l.Logger.Info("Successfully updated network details") +// return +// } +// } +// } + +// func UpdateNetworkDetails() error { +// networkDetails, err := GetNetworkDetails() +// if err != nil { +// l.Logger.Error("Failed to update network details ", zap.Error(err)) +// return err +// } + +// shouldUpdate := UpdateRequired(networkDetails) +// if shouldUpdate { +// forceUpdateNetworkDetails(networkDetails) +// } +// return nil +// } + +// func InitNetworkDetails() error { +// networkDetails, err := GetNetworkDetails() +// if err != nil { +// l.Logger.Error("Failed to update network details ", zap.Error(err)) +// return err +// } +// forceUpdateNetworkDetails(networkDetails) +// return nil +// } + +// func forceUpdateNetworkDetails(networkDetails *Network) { +// sdkInitialized = false +// blockchain.SetMiners(networkDetails.Miners) +// blockchain.SetSharders(networkDetails.Sharders) +// node.InitCache(blockchain.Sharders) +// conf.InitChainNetwork(&conf.Network{ +// Sharders: networkDetails.Sharders, +// Miners: networkDetails.Miners, +// }) +// sdkInitialized = true +// } + +// func UpdateRequired(networkDetails *Network) bool { +// miners := blockchain.GetMiners() +// sharders := blockchain.GetAllSharders() +// if len(miners) == 0 || len(sharders) == 0 { +// return true +// } + +// minerSame := reflect.DeepEqual(miners, networkDetails.Miners) +// sharderSame := reflect.DeepEqual(sharders, networkDetails.Sharders) + +// if minerSame && sharderSame { +// return false +// } +// return true +// } + +// func GetNetworkDetails() (*Network, error) { +// req, ctx, cncl, err := zboxutil.NewHTTPRequest(http.MethodGet, blockchain.GetBlockWorker()+NETWORK_ENDPOINT, nil) +// if err != nil { +// return nil, errors.New("get_network_details_error", "Unable to create new http request with error "+err.Error()) +// } + +// var networkResponse Network +// err = zboxutil.HttpDo(ctx, cncl, req, func(resp *http.Response, err error) error { +// if err != nil { +// l.Logger.Error("Get network error : ", err) +// return err +// } + +// defer resp.Body.Close() +// respBody, err := ioutil.ReadAll(resp.Body) +// if err != nil { +// return errors.Wrap(err, "Error reading response : ") +// } + +// l.Logger.Debug("Get network result:", string(respBody)) +// if resp.StatusCode == http.StatusOK { +// err = json.Unmarshal(respBody, &networkResponse) +// if err != nil { +// return errors.Wrap(err, "Error unmarshaling response :") +// } +// return nil +// } +// return errors.New(strconv.Itoa(resp.StatusCode), "Get network details status not OK") + +// }) +// return &networkResponse, err +// } diff --git a/zboxcore/sdk/playlist.go b/zboxcore/sdk/playlist.go index 52c464ff6..b6e6e9d25 100644 --- a/zboxcore/sdk/playlist.go +++ b/zboxcore/sdk/playlist.go @@ -10,7 +10,7 @@ import ( "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/resty" "github.com/0chain/gosdk/core/sys" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/zboxutil" @@ -65,11 +65,11 @@ func getPlaylistFromBlobbers(ctx context.Context, alloc *Allocation, query strin opts = append(opts, resty.WithRetry(resty.DefaultRetry)) opts = append(opts, resty.WithRequestInterceptor(func(req *http.Request) error { - req.Header.Set("X-App-Client-ID", client.GetClientID()) - req.Header.Set("X-App-Client-Key", client.GetClientPublicKey()) + req.Header.Set("X-App-Client-ID", client.ClientID()) + req.Header.Set("X-App-Client-Key", client.PublicKey()) hash := encryption.Hash(alloc.ID) - sign, err := sys.Sign(hash, client.GetClient().SignatureScheme, client.GetClientSysKeys()) + sign, err := sys.Sign(hash, client.SignatureScheme(), client.GetClientSysKeys()) if err != nil { return err } @@ -146,11 +146,11 @@ func getPlaylistFileFromBlobbers(ctx context.Context, alloc *Allocation, query s opts = append(opts, resty.WithRetry(resty.DefaultRetry)) opts = append(opts, resty.WithRequestInterceptor(func(req *http.Request) error { - req.Header.Set("X-App-Client-ID", client.GetClientID()) - req.Header.Set("X-App-Client-Key", client.GetClientPublicKey()) + req.Header.Set("X-App-Client-ID", client.ClientID()) + req.Header.Set("X-App-Client-Key", client.PublicKey()) hash := encryption.Hash(alloc.ID) - sign, err := sys.Sign(hash, client.GetClient().SignatureScheme, client.GetClientSysKeys()) + sign, err := sys.Sign(hash, client.SignatureScheme(), client.GetClientSysKeys()) if err != nil { return err } diff --git a/zboxcore/sdk/renameworker.go b/zboxcore/sdk/renameworker.go index c366e527f..1f5950496 100644 --- a/zboxcore/sdk/renameworker.go +++ b/zboxcore/sdk/renameworker.go @@ -15,7 +15,6 @@ import ( "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/common" - "github.com/0chain/gosdk/zboxcore/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" @@ -199,7 +198,7 @@ func (req *RenameRequest) ProcessRename() error { req.consensus.consensusThresh, req.consensus.getConsensus())) } - writeMarkerMutex, err := CreateWriteMarkerMutex(client.GetClient(), req.allocationObj) + writeMarkerMutex, err := CreateWriteMarkerMutex(req.allocationObj) if err != nil { return fmt.Errorf("rename failed: %s", err.Error()) } diff --git a/zboxcore/sdk/renameworker_test.go b/zboxcore/sdk/renameworker_test.go index 29aa6304f..7d633b529 100644 --- a/zboxcore/sdk/renameworker_test.go +++ b/zboxcore/sdk/renameworker_test.go @@ -20,7 +20,7 @@ import ( devMock "github.com/0chain/gosdk/dev/mock" "github.com/0chain/gosdk/sdks/blobber" "github.com/0chain/gosdk/zboxcore/blockchain" - zclient "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/mocks" "github.com/0chain/gosdk/zboxcore/zboxutil" @@ -48,11 +48,10 @@ func TestRenameRequest_renameBlobberObject(t *testing.T) { zboxutil.Client = rawClient }() - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) type parameters struct { referencePathToRetrieve fileref.ReferencePath @@ -279,11 +278,10 @@ func TestRenameRequest_ProcessRename(t *testing.T) { zboxutil.Client = rawClient }() - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) setupHttpResponses := func(t *testing.T, testName string, numBlobbers int, numCorrect int, req *RenameRequest) { diff --git a/zboxcore/sdk/rollback.go b/zboxcore/sdk/rollback.go index dd5151655..2d968b793 100644 --- a/zboxcore/sdk/rollback.go +++ b/zboxcore/sdk/rollback.go @@ -18,7 +18,7 @@ import ( "github.com/0chain/common/core/common" thrown "github.com/0chain/errors" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" l "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/marker" "github.com/0chain/gosdk/zboxcore/zboxutil" @@ -87,12 +87,12 @@ func GetWritemarker(allocID, allocTx, id, baseUrl string) (*LatestPrevWriteMarke return nil, err } if lpm.LatestWM != nil { - err = lpm.LatestWM.VerifySignature(client.GetClientPublicKey()) + err = lpm.LatestWM.VerifySignature(client.PublicKey()) if err != nil { return nil, fmt.Errorf("signature verification failed for latest writemarker: %s", err.Error()) } if lpm.PrevWM != nil { - err = lpm.PrevWM.VerifySignature(client.GetClientPublicKey()) + err = lpm.PrevWM.VerifySignature(client.PublicKey()) if err != nil { return nil, fmt.Errorf("signature verification failed for latest writemarker: %s", err.Error()) } @@ -110,7 +110,7 @@ func (rb *RollbackBlobber) processRollback(ctx context.Context, tx string) error wm.AllocationID = rb.lpm.LatestWM.AllocationID wm.Timestamp = rb.lpm.LatestWM.Timestamp wm.BlobberID = rb.lpm.LatestWM.BlobberID - wm.ClientID = client.GetClientID() + wm.ClientID = client.ClientID() wm.Size = 0 if rb.lpm.PrevWM != nil { wm.AllocationRoot = rb.lpm.PrevWM.AllocationRoot diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index 2fc200919..ad18230e0 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -18,14 +18,15 @@ import ( "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/core/node" "github.com/0chain/gosdk/core/sys" + "github.com/0chain/gosdk/core/zcncrypto" "go.uber.org/zap" "gopkg.in/natefinch/lumberjack.v2" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/transaction" "github.com/0chain/gosdk/core/version" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" "github.com/0chain/gosdk/zboxcore/encryption" l "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/marker" @@ -97,68 +98,80 @@ func InitStorageSDK(walletJSON string, preferredBlobbers []string, nonce int64, fee ...uint64) error { - err := client.PopulateClient(walletJSON, signatureScheme) + + wallet := zcncrypto.Wallet{} + err := json.Unmarshal([]byte(walletJSON), &wallet) if err != nil { return err } - blockchain.SetChainID(chainID) - blockchain.SetBlockWorker(blockWorker) - - err = InitNetworkDetails() - if err != nil { - return err - } - - client.SetClientNonce(nonce) + client.SetWallet(wallet) + client.SetSignatureScheme(signatureScheme) + client.SetNonce(nonce) if len(fee) > 0 { client.SetTxnFee(fee[0]) } - go UpdateNetworkDetailsWorker(context.Background()) + err = client.Init(context.Background(), conf.Config{ + BlockWorker: blockWorker, + SignatureScheme: signatureScheme, + ChainID: chainID, + PreferredBlobbers: preferredBlobbers, + MaxTxnQuery: 5, + QuerySleepTime: 5, + MinSubmit: 10, + MinConfirmation: 10, + }) + if err != nil { + return err + } sdkInitialized = true return nil } -func GetNetwork() *Network { - return &Network{ - Miners: blockchain.GetMiners(), - Sharders: blockchain.GetAllSharders(), - } -} +// func GetNetwork() *Network { +// nodeClient, err := client.GetNode() +// if err != nil { +// panic(err) +// } +// return &Network{ +// Miners: nodeClient.Network().Miners, +// Sharders: nodeClient.Network().Sharders, +// } +// } -func SetMaxTxnQuery(num int) { - blockchain.SetMaxTxnQuery(num) +// func SetMaxTxnQuery(num int) { +// blockchain.SetMaxTxnQuery(num) - cfg, _ := conf.GetClientConfig() - if cfg != nil { - cfg.MaxTxnQuery = num - } +// cfg, _ := conf.GetClientConfig() +// if cfg != nil { +// cfg.MaxTxnQuery = num +// } -} +// } -func SetQuerySleepTime(time int) { - blockchain.SetQuerySleepTime(time) +// func SetQuerySleepTime(time int) { +// blockchain.SetQuerySleepTime(time) - cfg, _ := conf.GetClientConfig() - if cfg != nil { - cfg.QuerySleepTime = time - } +// cfg, _ := conf.GetClientConfig() +// if cfg != nil { +// cfg.QuerySleepTime = time +// } -} +// } -func SetMinSubmit(num int) { - blockchain.SetMinSubmit(num) -} -func SetMinConfirmation(num int) { - blockchain.SetMinConfirmation(num) -} +// func SetMinSubmit(num int) { +// blockchain.SetMinSubmit(num) +// } +// func SetMinConfirmation(num int) { +// blockchain.SetMinConfirmation(num) +// } -func SetNetwork(miners []string, sharders []string) { - blockchain.SetMiners(miners) - blockchain.SetSharders(sharders) - node.InitCache(blockchain.Sharders) -} +// func SetNetwork(miners []string, sharders []string) { +// blockchain.SetMiners(miners) +// blockchain.SetSharders(sharders) +// node.InitCache(blockchain.Sharders) +// } // // read pool @@ -195,7 +208,7 @@ func GetReadPoolInfo(clientID string) (info *ReadPool, err error) { } if clientID == "" { - clientID = client.GetClientID() + clientID = client.ClientID() } var b []byte @@ -330,7 +343,7 @@ func GetStakePoolUserInfo(clientID string, offset, limit int) (info *StakePoolUs return nil, sdkNotInitialized } if clientID == "" { - clientID = client.GetClientID() + clientID = client.ClientID() } var b []byte @@ -838,7 +851,7 @@ func GetClientEncryptedPublicKey() (string, error) { return "", sdkNotInitialized } encScheme := encryption.NewEncryptionScheme() - _, err := encScheme.Initialize(client.GetClient().Mnemonic) + _, err := encScheme.Initialize(client.Wallet().Mnemonic) if err != nil { return "", err } @@ -931,7 +944,7 @@ func SetVerifyHash(verify bool) { } func GetAllocations() ([]*Allocation, error) { - return GetAllocationsForClient(client.GetClientID()) + return GetAllocationsForClient(client.ClientID()) } func getAllocationsInternal(clientID string, limit, offset int) ([]*Allocation, error) { @@ -1012,8 +1025,8 @@ type CreateAllocationOptions struct { func CreateAllocationWith(options CreateAllocationOptions) ( string, int64, *transaction.Transaction, error) { - return CreateAllocationForOwner(client.GetClientID(), - client.GetClientPublicKey(), options.DataShards, options.ParityShards, + return CreateAllocationForOwner(client.ClientID(), + client.PublicKey(), options.DataShards, options.ParityShards, options.Size, options.ReadPrice, options.WritePrice, options.Lock, options.BlobberIds, options.ThirdPartyExtendable, options.FileOptionsParams) } @@ -1200,7 +1213,7 @@ func CreateFreeAllocation(marker string, value uint64) (string, int64, error) { return "", 0, sdkNotInitialized } - recipientPublicKey := client.GetClientPublicKey() + recipientPublicKey := client.PublicKey() var input = map[string]interface{}{ "recipient_public_key": recipientPublicKey, @@ -1245,7 +1258,7 @@ func UpdateAllocation( } updateAllocationRequest := make(map[string]interface{}) - updateAllocationRequest["owner_id"] = client.GetClientID() + updateAllocationRequest["owner_id"] = client.ClientID() updateAllocationRequest["owner_public_key"] = "" updateAllocationRequest["id"] = allocationID updateAllocationRequest["size"] = size @@ -1481,8 +1494,18 @@ func smartContractTxnValueFee(scAddress string, sn transaction.SmartContractTxnD return } - txn := transaction.NewTransactionEntity(client.GetClientID(), - blockchain.GetChainID(), client.GetClientPublicKey(), nonce) + cfg, err := conf.GetClientConfig() + if err != nil { + return + } + + nodeClient, err := client.GetNode() + if err != nil { + return + } + + txn := transaction.NewTransactionEntity(client.ClientID(), + cfg.ChainID, client.PublicKey(), nonce) txn.TransactionData = string(requestBytes) txn.ToClientID = scAddress @@ -1492,7 +1515,7 @@ func smartContractTxnValueFee(scAddress string, sn transaction.SmartContractTxnD // adjust fees if not set if fee == 0 { - fee, err = transaction.EstimateFee(txn, blockchain.GetMiners(), 0.2) + fee, err = transaction.EstimateFee(txn, nodeClient.Network().Miners, 0.2) if err != nil { l.Logger.Error("failed to estimate txn fee", zap.Error(err), @@ -1514,23 +1537,23 @@ func smartContractTxnValueFee(scAddress string, sn transaction.SmartContractTxnD l.Logger.Info(msg) l.Logger.Info("estimated txn fee: ", txn.TransactionFee) - err = transaction.SendTransactionSync(txn, blockchain.GetStableMiners()) + err = transaction.SendTransactionSync(txn, nodeClient.GetStableMiners()) if err != nil { l.Logger.Info("transaction submission failed", zap.Error(err)) node.Cache.Evict(txn.ClientID) - blockchain.ResetStableMiners() + nodeClient.ResetStableMiners() return } var ( - querySleepTime = time.Duration(blockchain.GetQuerySleepTime()) * time.Second + querySleepTime = time.Duration(cfg.QuerySleepTime) * time.Second retries = 0 ) sys.Sleep(querySleepTime) - for retries < blockchain.GetMaxTxnQuery() { - t, err = transaction.VerifyTransaction(txn.Hash, blockchain.GetSharders()) + for retries < cfg.MaxTxnQuery { + t, err = transaction.VerifyTransaction(txn.Hash, nodeClient.Sharders().Healthy()) if err == nil { break } @@ -1653,7 +1676,7 @@ func GetUpdateAllocationMinLock( addBlobberId, removeBlobberId string) (int64, error) { updateAllocationRequest := make(map[string]interface{}) - updateAllocationRequest["owner_id"] = client.GetClientID() + updateAllocationRequest["owner_id"] = client.ClientID() updateAllocationRequest["owner_public_key"] = "" updateAllocationRequest["id"] = allocationID updateAllocationRequest["size"] = size diff --git a/zboxcore/sdk/sharerequest.go b/zboxcore/sdk/sharerequest.go index 0d84ed373..ad85239ac 100644 --- a/zboxcore/sdk/sharerequest.go +++ b/zboxcore/sdk/sharerequest.go @@ -8,7 +8,7 @@ import ( "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/encryption" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/marker" @@ -52,7 +52,7 @@ func (req *ShareRequest) getAuthTicket(clientID, encPublicKey string) (*marker.A at := &marker.AuthTicket{ AllocationID: req.allocationID, - OwnerID: client.GetClientID(), + OwnerID: client.ClientID(), ClientID: clientID, FileName: req.remotefilename, FilePathHash: fileref.GetReferenceLookup(req.allocationID, req.remotefilepath), @@ -68,7 +68,7 @@ func (req *ShareRequest) getAuthTicket(clientID, encPublicKey string) (*marker.A if encPublicKey != "" { // file is encrypted encScheme := encryption.NewEncryptionScheme() - if _, err := encScheme.Initialize((client.GetClient().Mnemonic)); err != nil { + if _, err := encScheme.Initialize((client.Wallet().Mnemonic)); err != nil { return nil, err } diff --git a/zboxcore/sdk/writemarker_mutex.go b/zboxcore/sdk/writemarker_mutex.go index bbf4046be..61e89a412 100644 --- a/zboxcore/sdk/writemarker_mutex.go +++ b/zboxcore/sdk/writemarker_mutex.go @@ -12,7 +12,6 @@ import ( "github.com/0chain/errors" "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/zboxutil" ) @@ -40,7 +39,7 @@ type WriteMarkerMutex struct { } // CreateWriteMarkerMutex create WriteMarkerMutex for allocation -func CreateWriteMarkerMutex(client *client.Client, allocationObj *Allocation) (*WriteMarkerMutex, error) { +func CreateWriteMarkerMutex(allocationObj *Allocation) (*WriteMarkerMutex, error) { if allocationObj == nil { return nil, errors.Throw(constants.ErrInvalidParameter, "allocationObj") } diff --git a/zboxcore/sdk/writemarker_mutex_test.go b/zboxcore/sdk/writemarker_mutex_test.go index 8d8358970..480a47f96 100644 --- a/zboxcore/sdk/writemarker_mutex_test.go +++ b/zboxcore/sdk/writemarker_mutex_test.go @@ -13,7 +13,6 @@ import ( "time" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" "github.com/0chain/gosdk/zboxcore/mocks" "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/stretchr/testify/mock" @@ -78,7 +77,7 @@ func TestWriteMarkerMutext_Should_Lock(t *testing.T) { mask := zboxutil.NewUint128(1).Lsh(uint64(len(a.Blobbers))).Sub64(1) mu := &sync.Mutex{} - mutex, _ := CreateWriteMarkerMutex(client.GetClient(), a) + mutex, _ := CreateWriteMarkerMutex(a) consensus := &Consensus{RWMutex: &sync.RWMutex{}} consensus.Init(a.consensusThreshold, a.fullconsensus) @@ -146,7 +145,7 @@ func TestWriteMarkerMutext_Some_Blobbers_Down_Should_Lock(t *testing.T) { } setupHttpResponses(t, t.Name(), len(a.Blobbers), len(a.Blobbers)-1) - mutex, _ := CreateWriteMarkerMutex(client.GetClient(), a) + mutex, _ := CreateWriteMarkerMutex(a) mask := zboxutil.NewUint128(1).Lsh(uint64(len(a.Blobbers))).Sub64(1) mu := &sync.Mutex{} consensus := &Consensus{RWMutex: &sync.RWMutex{}} @@ -213,7 +212,7 @@ func TestWriteMarkerMutext_Too_Less_Blobbers_Response_Should_Not_Lock(t *testing } setupHttpResponses(t, t.Name(), len(a.Blobbers), a.consensusThreshold-1) - mutex, err := CreateWriteMarkerMutex(client.GetClient(), a) + mutex, err := CreateWriteMarkerMutex(a) require.NoError(t, err) mask := zboxutil.NewUint128(1).Lsh(uint64(len(a.Blobbers))).Sub64(1) mu := &sync.Mutex{} diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index 73c0519e0..d8b6415be 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -17,11 +17,10 @@ import ( "time" "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/logger" - "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" ) const SC_REST_API_URL = "v1/screst/" @@ -163,8 +162,8 @@ func NewHTTPRequest(method string, url string, data []byte) (*http.Request, cont } func setClientInfo(req *http.Request) { - req.Header.Set("X-App-Client-ID", client.GetClientID()) - req.Header.Set("X-App-Client-Key", client.GetClientPublicKey()) + req.Header.Set("X-App-Client-ID", client.ClientID()) + req.Header.Set("X-App-Client-Key", client.PublicKey()) } func setClientInfoWithSign(req *http.Request, allocation string) error { @@ -804,8 +803,12 @@ func NewRollbackRequest(baseUrl, allocationID string, allocationTx string, body } func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string, handler SCRestAPIHandler) ([]byte, error) { - numSharders := len(blockchain.GetSharders()) - sharders := blockchain.GetSharders() + nodeClient, err := client.GetNode() + if err != nil { + return nil, err + } + numSharders := len(nodeClient.Sharders().Healthy()) + sharders := nodeClient.Sharders().Healthy() responses := make(map[int]int) mu := &sync.Mutex{} entityResult := make(map[string][]byte) @@ -837,7 +840,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] client := &http.Client{Transport: DefaultTransport} response, err := client.Get(urlObj.String()) if err != nil { - blockchain.Sharders.Fail(sharder) + nodeClient.Sharders().Fail(sharder) return } @@ -845,9 +848,9 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] entityBytes, _ := ioutil.ReadAll(response.Body) mu.Lock() if response.StatusCode > http.StatusBadRequest { - blockchain.Sharders.Fail(sharder) + nodeClient.Sharders().Fail(sharder) } else { - blockchain.Sharders.Success(sharder) + nodeClient.Sharders().Success(sharder) } responses[response.StatusCode]++ if responses[response.StatusCode] > maxCount { @@ -860,7 +863,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] } entityResult[sharder] = entityBytes - blockchain.Sharders.Success(sharder) + nodeClient.Sharders().Success(sharder) mu.Unlock() }(sharder) } diff --git a/zcncore/transaction_base.go b/zcncore/transaction_base.go index 968936d35..37b8e1382 100644 --- a/zcncore/transaction_base.go +++ b/zcncore/transaction_base.go @@ -15,7 +15,6 @@ import ( "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/node" "github.com/0chain/gosdk/core/sys" - "github.com/0chain/gosdk/zboxcore/blockchain" "github.com/0chain/gosdk/zboxcore/logger" "go.uber.org/zap" @@ -875,7 +874,11 @@ func VerifyContentHash(metaTxnDataJSON string) (bool, error) { return false, errors.New("metaTxnData_decode_error", "Unable to decode metaTxnData json") } - t, err := transaction.VerifyTransaction(metaTxnData.TxnID, blockchain.GetSharders()) + nodeClient, err := client.GetNode() + if err != nil { + return false, err + } + t, err := transaction.VerifyTransaction(metaTxnData.TxnID, nodeClient.Sharders().Healthy()) if err != nil { return false, errors.New("fetch_txm_details", "Unable to fetch txn details") } From cdcd98e9c6b250069fa9fa8ee35e6e8eef342165 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 22 Aug 2024 21:17:18 +0530 Subject: [PATCH 015/319] Cleanup --- zboxcore/blockchain/entity.go | 145 ---------- zboxcore/sdk/sdk.go | 492 +++++++++++++++++++++++++--------- 2 files changed, 367 insertions(+), 270 deletions(-) diff --git a/zboxcore/blockchain/entity.go b/zboxcore/blockchain/entity.go index 3844edc1e..91a005e18 100644 --- a/zboxcore/blockchain/entity.go +++ b/zboxcore/blockchain/entity.go @@ -6,44 +6,6 @@ import ( "github.com/0chain/gosdk/core/common" ) -// var miners []string -// var mGuard sync.Mutex - -// func getMinMinersSubmit() int { -// minMiners := util.MaxInt(calculateMinRequired(float64(chain.MinSubmit), float64(len(chain.Miners))/100), 1) -// return minMiners -// } - -// func calculateMinRequired(minRequired, percent float64) int { -// return int(math.Ceil(minRequired * percent)) -// } - -// func GetStableMiners() []string { -// mGuard.Lock() -// defer mGuard.Unlock() -// if len(miners) == 0 { -// miners = util.GetRandom(chain.Miners, getMinMinersSubmit()) -// } - -// return miners -// } -// func ResetStableMiners() { -// mGuard.Lock() -// defer mGuard.Unlock() -// miners = util.GetRandom(chain.Miners, getMinMinersSubmit()) -// } - -// type ChainConfig struct { -// BlockWorker string -// Sharders []string -// Miners []string -// MinSubmit int -// MinConfirmation int -// ChainID string -// MaxTxnQuery int -// QuerySleepTime int -// } - // StakePoolSettings information. type StakePoolSettings struct { DelegateWallet string `json:"delegate_wallet"` @@ -92,110 +54,3 @@ func (sn *StorageNode) SetSkip(t bool) { func (sn *StorageNode) IsSkip() bool { return atomic.LoadUint64(&sn.skip) > 0 } - -// func PopulateNodes(nodesjson string) ([]string, error) { -// sharders := make([]string, 0) -// err := json.Unmarshal([]byte(nodesjson), &sharders) -// return sharders, err -// } - -// var chain *ChainConfig -// var Sharders *node.NodeHolder - -// func init() { -// chain = &ChainConfig{ -// MaxTxnQuery: 5, -// QuerySleepTime: 5, -// MinSubmit: 10, -// MinConfirmation: 10, -// } -// } - -// func GetChainID() string { -// return chain.ChainID -// } - -// func PopulateChain(minerjson string, sharderjson string) error { -// var err error -// chain.Miners, err = PopulateNodes(minerjson) -// if err != nil { -// return err -// } -// sharders, err := PopulateNodes(sharderjson) -// if err != nil { -// return err -// } -// SetSharders(sharders) -// return nil -// } - -// func GetBlockWorker() string { -// return chain.BlockWorker -// } - -// func GetAllSharders() []string { -// return Sharders.All() -// } -// func GetSharders() []string { -// return Sharders.Healthy() -// } - -// func GetMiners() []string { -// return chain.Miners -// } - -// func GetMaxTxnQuery() int { -// return chain.MaxTxnQuery -// } - -// func GetQuerySleepTime() int { -// return chain.QuerySleepTime -// } - -// func GetMinSubmit() int { -// return chain.MinSubmit -// } - -// func GetMinConfirmation() int { -// return chain.MinConfirmation -// } - -// func SetBlockWorker(blockWorker string) { -// chain.BlockWorker = blockWorker -// } - -// func SetSharders(sharderArray []string) { -// consensus := conf.DefaultSharderConsensous -// config, err := conf.GetClientConfig() -// if err == nil && config != nil { -// consensus = config.SharderConsensous -// } -// if len(sharderArray) < consensus { -// consensus = len(sharderArray) -// } -// Sharders = node.NewHolder(sharderArray, consensus) -// } - -// func SetMiners(minerArray []string) { -// chain.Miners = minerArray -// } - -// func SetChainID(id string) { -// chain.ChainID = id -// } - -// func SetMaxTxnQuery(num int) { -// chain.MaxTxnQuery = num -// } - -// func SetQuerySleepTime(time int) { -// chain.QuerySleepTime = time -// } - -// func SetMinSubmit(minSubmit int) { -// chain.MinSubmit = minSubmit -// } - -// func SetMinConfirmation(minConfirmation int) { -// chain.MinConfirmation = minConfirmation -// } diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index ad18230e0..a62c6f00f 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -56,10 +56,25 @@ type StatusCallback interface { RepairCompleted(filesRepaired int) } -var numBlockDownloads = 100 -var sdkInitialized = false -var networkWorkerTimerInHours = 1 -var shouldVerifyHash = true +var ( + numBlockDownloads = 100 + sdkInitialized = false + networkWorkerTimerInHours = 1 + singleClientMode = false + shouldVerifyHash = true +) + +func SetSingleClietnMode(mode bool) { + singleClientMode = mode +} + +func SetShouldVerifyHash(verify bool) { + shouldVerifyHash = verify +} + +func SetSaveProgress(save bool) { + shouldSaveProgress = save +} // GetVersion - returns version string func GetVersion() string { @@ -67,14 +82,14 @@ func GetVersion() string { } // SetLogLevel set the log level. -// lvl - 0 disabled; higher number (upto 4) more verbosity +// - lvl: 0 disabled; higher number (upto 4) more verbosity func SetLogLevel(lvl int) { l.Logger.SetLevel(lvl) } -// SetLogFile -// logFile - Log file -// verbose - true - console output; false - no console output +// SetLogFile set the log file and verbosity levels +// - logFile: Log file +// - verbose: true - console output; false - no console output func SetLogFile(logFile string, verbose bool) { var ioWriter = &lumberjack.Logger{ Filename: logFile, @@ -89,16 +104,26 @@ func SetLogFile(logFile string, verbose bool) { l.Logger.Info("******* Storage SDK Version: ", version.VERSIONSTR, " *******") } +// GetLogger retrieves logger instance func GetLogger() *logger.Logger { return &l.Logger } +// InitStorageSDK Initialize the storage SDK +// +// - walletJSON: Client's wallet JSON +// - blockWorker: Block worker URL (block worker refers to 0DNS) +// - chainID: ID of the blokcchain network +// - signatureScheme: Signature scheme that will be used for signing transactions +// - preferredBlobbers: List of preferred blobbers to use when creating an allocation. This is usually configured by the client in the configuration files +// - nonce: Initial nonce value for the transactions +// - fee: Preferred value for the transaction fee, just the first value is taken func InitStorageSDK(walletJSON string, blockWorker, chainID, signatureScheme string, preferredBlobbers []string, nonce int64, fee ...uint64) error { - + wallet := zcncrypto.Wallet{} err := json.Unmarshal([]byte(walletJSON), &wallet) if err != nil { @@ -113,14 +138,14 @@ func InitStorageSDK(walletJSON string, } err = client.Init(context.Background(), conf.Config{ - BlockWorker: blockWorker, - SignatureScheme: signatureScheme, - ChainID: chainID, + BlockWorker: blockWorker, + SignatureScheme: signatureScheme, + ChainID: chainID, PreferredBlobbers: preferredBlobbers, - MaxTxnQuery: 5, - QuerySleepTime: 5, - MinSubmit: 10, - MinConfirmation: 10, + MaxTxnQuery: 5, + QuerySleepTime: 5, + MinSubmit: 10, + MinConfirmation: 10, }) if err != nil { return err @@ -129,54 +154,6 @@ func InitStorageSDK(walletJSON string, return nil } -// func GetNetwork() *Network { -// nodeClient, err := client.GetNode() -// if err != nil { -// panic(err) -// } -// return &Network{ -// Miners: nodeClient.Network().Miners, -// Sharders: nodeClient.Network().Sharders, -// } -// } - -// func SetMaxTxnQuery(num int) { -// blockchain.SetMaxTxnQuery(num) - -// cfg, _ := conf.GetClientConfig() -// if cfg != nil { -// cfg.MaxTxnQuery = num -// } - -// } - -// func SetQuerySleepTime(time int) { -// blockchain.SetQuerySleepTime(time) - -// cfg, _ := conf.GetClientConfig() -// if cfg != nil { -// cfg.QuerySleepTime = time -// } - -// } - -// func SetMinSubmit(num int) { -// blockchain.SetMinSubmit(num) -// } -// func SetMinConfirmation(num int) { -// blockchain.SetMinConfirmation(num) -// } - -// func SetNetwork(miners []string, sharders []string) { -// blockchain.SetMiners(miners) -// blockchain.SetSharders(sharders) -// node.InitCache(blockchain.Sharders) -// } - -// -// read pool -// - func CreateReadPool() (hash string, nonce int64, err error) { if !sdkInitialized { return "", 0, sdkNotInitialized @@ -230,6 +207,8 @@ func GetReadPoolInfo(clientID string) (info *ReadPool, err error) { } // ReadPoolLock locks given number of tokes for given duration in read pool. +// - tokens: number of tokens to lock +// - fee: transaction fee func ReadPoolLock(tokens, fee uint64) (hash string, nonce int64, err error) { if !sdkInitialized { return "", 0, sdkNotInitialized @@ -244,6 +223,7 @@ func ReadPoolLock(tokens, fee uint64) (hash string, nonce int64, err error) { } // ReadPoolUnlock unlocks tokens in expired read pool +// - fee: transaction fee func ReadPoolUnlock(fee uint64) (hash string, nonce int64, err error) { if !sdkInitialized { return "", 0, sdkNotInitialized @@ -291,7 +271,7 @@ type StakePoolDelegatePoolInfo struct { StakedAt common.Timestamp `json:"staked_at"` } -// StakePool full info. +// StakePool information of stake pool of a provider. type StakePoolInfo struct { ID common.Key `json:"pool_id"` // pool ID Balance common.Balance `json:"balance"` // total balance @@ -306,8 +286,9 @@ type StakePoolInfo struct { Settings blockchain.StakePoolSettings `json:"settings"` } -// GetStakePoolInfo for given client, or, if the given clientID is empty, -// for current client of the sdk. +// GetStakePoolInfo retrieve stake pool info for the current client configured to the sdk, given provider type and provider ID. +// - providerType: provider type +// - providerID: provider ID func GetStakePoolInfo(providerType ProviderType, providerID string) (info *StakePoolInfo, err error) { if !sdkInitialized { return nil, sdkNotInitialized @@ -336,8 +317,11 @@ type StakePoolUserInfo struct { Pools map[common.Key][]*StakePoolDelegatePoolInfo `json:"pools"` } -// GetStakePoolUserInfo obtains blobbers/validators delegate pools statistic -// for a user. If given clientID is empty string, then current client used. +// GetStakePoolUserInfo obtains blobbers/validators delegate pools statistic for a user. +// If given clientID is empty string, then current client used. +// - clientID: client ID +// - offset: offset +// - limit: limit func GetStakePoolUserInfo(clientID string, offset, limit int) (info *StakePoolUserInfo, err error) { if !sdkInitialized { return nil, sdkNotInitialized @@ -374,7 +358,14 @@ type stakePoolRequest struct { ProviderID string `json:"provider_id,omitempty"` } -// StakePoolLock locks tokens lack in stake pool +// StakePoolLock locks tokens in a stake pool. +// This function is the entry point for the staking operation. +// Provided the provider type and provider ID, the value is locked in the stake pool between the SDK client and the provider. +// Based on the locked amount, the client will get rewards as share of the provider's rewards. +// - providerType: provider type +// - providerID: provider ID +// - value: value to lock +// - fee: transaction fee func StakePoolLock(providerType ProviderType, providerID string, value, fee uint64) (hash string, nonce int64, err error) { if !sdkInitialized { return "", 0, sdkNotInitialized @@ -405,6 +396,9 @@ func StakePoolLock(providerType ProviderType, providerID string, value, fee uint case ProviderMiner, ProviderSharder: scAddress = MINERSC_SCADDRESS sn.Name = transaction.MINERSC_LOCK + case ProviderAuthorizer: + scAddress = ZCNSC_SCADDRESS + sn.Name = transaction.ZCNSC_LOCK default: return "", 0, errors.Newf("stake_pool_lock", "unsupported provider type: %v", providerType) } @@ -428,6 +422,9 @@ type stakePoolLock struct { // future. The time is maximal time that can be lesser in some cases. To // unlock tokens can't be unlocked now, wait the time and unlock them (call // this function again). +// - providerType: provider type +// - providerID: provider ID +// - fee: transaction fee func StakePoolUnlock(providerType ProviderType, providerID string, fee uint64) (unstake int64, nonce int64, err error) { if !sdkInitialized { return 0, 0, sdkNotInitialized @@ -458,6 +455,9 @@ func StakePoolUnlock(providerType ProviderType, providerID string, fee uint64) ( case ProviderMiner, ProviderSharder: scAddress = MINERSC_SCADDRESS sn.Name = transaction.MINERSC_UNLOCK + case ProviderAuthorizer: + scAddress = ZCNSC_SCADDRESS + sn.Name = transaction.ZCNSC_UNLOCK default: return 0, 0, errors.Newf("stake_pool_unlock", "unsupported provider type: %v", providerType) } @@ -480,6 +480,9 @@ func StakePoolUnlock(providerType ProviderType, providerID string, fee uint64) ( // // WritePoolLock locks given number of tokes for given duration in read pool. +// - allocID: allocation ID +// - tokens: number of tokens to lock +// - fee: transaction fee func WritePoolLock(allocID string, tokens, fee uint64) (hash string, nonce int64, err error) { if !sdkInitialized { return "", 0, sdkNotInitialized @@ -501,7 +504,9 @@ func WritePoolLock(allocID string, tokens, fee uint64) (hash string, nonce int64 return } -// WritePoolUnlock unlocks tokens in expired read pool +// WritePoolUnlock unlocks ALL tokens of a write pool. Needs to be cancelled first. +// - allocID: allocation ID +// - fee: transaction fee func WritePoolUnlock(allocID string, fee uint64) (hash string, nonce int64, err error) { if !sdkInitialized { return "", 0, sdkNotInitialized @@ -535,7 +540,8 @@ type ChallengePoolInfo struct { Finalized bool `json:"finalized"` } -// GetChallengePoolInfo for given allocation. +// GetChallengePoolInfo retrieve challenge pool info for given allocation. +// - allocID: allocation ID func GetChallengePoolInfo(allocID string) (info *ChallengePoolInfo, err error) { if !sdkInitialized { return nil, sdkNotInitialized @@ -560,6 +566,7 @@ func GetChallengePoolInfo(allocID string) (info *ChallengePoolInfo, err error) { return } +// GetMptData retrieves mpt key data. func GetMptData(key string) ([]byte, error) { if !sdkInitialized { return nil, sdkNotInitialized @@ -588,6 +595,7 @@ type InputMap struct { Fields map[string]interface{} `json:"fields"` } +// GetStorageSCConfig retrieves storage SC configurations. func GetStorageSCConfig() (conf *InputMap, err error) { if !sdkInitialized { return nil, sdkNotInitialized @@ -612,23 +620,58 @@ func GetStorageSCConfig() (conf *InputMap, err error) { return } +// Blobber type represents blobber information. type Blobber struct { - ID common.Key `json:"id"` - BaseURL string `json:"url"` - Terms Terms `json:"terms"` - Capacity common.Size `json:"capacity"` - Allocated common.Size `json:"allocated"` - LastHealthCheck common.Timestamp `json:"last_health_check"` - PublicKey string `json:"-"` - StakePoolSettings blockchain.StakePoolSettings `json:"stake_pool_settings"` - TotalStake int64 `json:"total_stake"` - UsedAllocation int64 `json:"used_allocation"` - TotalOffers int64 `json:"total_offers"` - TotalServiceCharge int64 `json:"total_service_charge"` - UncollectedServiceCharge int64 `json:"uncollected_service_charge"` - IsKilled bool `json:"is_killed"` - IsShutdown bool `json:"is_shutdown"` - NotAvailable bool `json:"not_available"` + // ID of the blobber + ID common.Key `json:"id"` + + // BaseURL of the blobber + BaseURL string `json:"url"` + + // Terms of the blobber + Terms Terms `json:"terms"` + + // Capacity of the blobber + Capacity common.Size `json:"capacity"` + + // Allocated size of the blobber + Allocated common.Size `json:"allocated"` + + // LastHealthCheck of the blobber + LastHealthCheck common.Timestamp `json:"last_health_check"` + + // PublicKey of the blobber + PublicKey string `json:"-"` + + // StakePoolSettings settings of the blobber staking + StakePoolSettings blockchain.StakePoolSettings `json:"stake_pool_settings"` + + // TotalStake of the blobber in SAS + TotalStake int64 `json:"total_stake"` + + // UsedAllocation of the blobber in SAS + UsedAllocation int64 `json:"used_allocation"` + + // TotalOffers of the blobber in SAS + TotalOffers int64 `json:"total_offers"` + + // TotalServiceCharge of the blobber in SAS + TotalServiceCharge int64 `json:"total_service_charge"` + + // UncollectedServiceCharge of the blobber in SAS + UncollectedServiceCharge int64 `json:"uncollected_service_charge"` + + // IsKilled flag of the blobber, if true then the blobber is killed + IsKilled bool `json:"is_killed"` + + // IsShutdown flag of the blobber, if true then the blobber is shutdown + IsShutdown bool `json:"is_shutdown"` + + // NotAvailable flag of the blobber, if true then the blobber is not available + NotAvailable bool `json:"not_available"` + + // IsRestricted flag of the blobber, if true then the blobber is restricted + IsRestricted bool `json:"is_restricted"` } // UpdateBlobber is used during update blobber settings calls. @@ -650,8 +693,19 @@ type UpdateBlobber struct { IsKilled *bool `json:"is_killed,omitempty"` IsShutdown *bool `json:"is_shutdown,omitempty"` NotAvailable *bool `json:"not_available,omitempty"` + IsRestricted *bool `json:"is_restricted,omitempty"` +} + +// ResetBlobberStatsDto represents blobber stats reset request. +type ResetBlobberStatsDto struct { + BlobberID string `json:"blobber_id"` + PrevAllocated int64 `json:"prev_allocated"` + PrevSavedData int64 `json:"prev_saved_data"` + NewAllocated int64 `json:"new_allocated"` + NewSavedData int64 `json:"new_saved_data"` } +// Validator represents validator information. type Validator struct { ID common.Key `json:"validator_id"` BaseURL string `json:"url"` @@ -669,6 +723,9 @@ type Validator struct { IsShutdown bool `json:"is_shutdown"` } +// UpdateValidator is used during update validator settings calls. +// Note the types are of pointer types with omitempty json property. +// This is done to correctly identify which properties are actually changing. type UpdateValidator struct { ID common.Key `json:"validator_id"` BaseURL *string `json:"url,omitempty"` @@ -685,6 +742,7 @@ type UpdateValidator struct { IsShutdown *bool `json:"is_shutdown,omitempty"` } +// ConvertToValidationNode converts UpdateValidator request to blockchain.UpdateValidationNode. func (v *UpdateValidator) ConvertToValidationNode() *blockchain.UpdateValidationNode { blockValidator := &blockchain.UpdateValidationNode{ ID: string(v.ID), @@ -693,8 +751,6 @@ func (v *UpdateValidator) ConvertToValidationNode() *blockchain.UpdateValidation sp := &blockchain.UpdateStakePoolSettings{ DelegateWallet: v.DelegateWallet, - MinStake: v.MinStake, - MaxStake: v.MaxStake, NumDelegates: v.NumDelegates, ServiceCharge: v.ServiceCharge, } @@ -710,15 +766,16 @@ func (v *UpdateValidator) ConvertToValidationNode() *blockchain.UpdateValidation return blockValidator } -func getBlobbersInternal(active bool, limit, offset int) (bs []*Blobber, err error) { +func getBlobbersInternal(active, stakable bool, limit, offset int) (bs []*Blobber, err error) { type nodes struct { Nodes []*Blobber } - url := fmt.Sprintf("/getblobbers?active=%s&limit=%d&offset=%d", + url := fmt.Sprintf("/getblobbers?active=%s&limit=%d&offset=%d&stakable=%s", strconv.FormatBool(active), limit, offset, + strconv.FormatBool(stakable), ) b, err := zboxutil.MakeSCRestAPICall(STORAGE_SCADDRESS, url, nil, nil) var wrap nodes @@ -736,14 +793,17 @@ func getBlobbersInternal(active bool, limit, offset int) (bs []*Blobber, err err return wrap.Nodes, nil } -func GetBlobbers(active bool) (bs []*Blobber, err error) { +// GetBlobbers returns list of blobbers. +// - active: if true then only active blobbers are returned +// - stakable: if true then only stakable blobbers are returned +func GetBlobbers(active, stakable bool) (bs []*Blobber, err error) { if !sdkInitialized { return nil, sdkNotInitialized } limit, offset := 20, 0 - blobbers, err := getBlobbersInternal(active, limit, offset) + blobbers, err := getBlobbersInternal(active, stakable, limit, offset) if err != nil { return nil, err } @@ -758,7 +818,7 @@ func GetBlobbers(active bool) (bs []*Blobber, err error) { // get the next set of blobbers offset += 20 - blobbers, err = getBlobbersInternal(active, limit, offset) + blobbers, err = getBlobbersInternal(active, stakable, limit, offset) if err != nil { return blobbers, err } @@ -768,10 +828,8 @@ func GetBlobbers(active bool) (bs []*Blobber, err error) { return blobbersSl, nil } -// GetBlobber instance. -// -// # Inputs -// - blobberID: the id of blobber +// GetBlobber retrieve blobber by id. +// - blobberID: the id of blobber func GetBlobber(blobberID string) (blob *Blobber, err error) { if !sdkInitialized { return nil, sdkNotInitialized @@ -795,7 +853,8 @@ func GetBlobber(blobberID string) (blob *Blobber, err error) { return } -// GetValidator instance. +// GetValidator retrieve validator instance by id. +// - validatorID: the id of validator func GetValidator(validatorID string) (validator *Validator, err error) { if !sdkInitialized { return nil, sdkNotInitialized @@ -819,8 +878,9 @@ func GetValidator(validatorID string) (validator *Validator, err error) { return } -// List all validators -func GetValidators() (validators []*Validator, err error) { +// GetValidators returns list of validators. +// - stakable: if true then only stakable validators are returned +func GetValidators(stakable bool) (validators []*Validator, err error) { if !sdkInitialized { return nil, sdkNotInitialized } @@ -828,7 +888,9 @@ func GetValidators() (validators []*Validator, err error) { b, err = zboxutil.MakeSCRestAPICall( STORAGE_SCADDRESS, "/validators", - nil, + map[string]string{ + "stakable": strconv.FormatBool(stakable), + }, nil) if err != nil { return nil, errors.Wrap(err, "requesting validator list") @@ -842,10 +904,7 @@ func GetValidators() (validators []*Validator, err error) { return } -// -// --- -// - +// GetClientEncryptedPublicKey - get the client's public key func GetClientEncryptedPublicKey() (string, error) { if !sdkInitialized { return "", sdkNotInitialized @@ -858,6 +917,11 @@ func GetClientEncryptedPublicKey() (string, error) { return encScheme.GetPublicKey() } +// GetAllocationFromAuthTicket - get allocation from given auth ticket hash. +// AuthTicket is used to access free allocations, and it's generated by the Free Storage Assigner. +// - authTicket: the auth ticket hash +// +// returns the allocation instance and error if any func GetAllocationFromAuthTicket(authTicket string) (*Allocation, error) { if !sdkInitialized { return nil, sdkNotInitialized @@ -874,6 +938,11 @@ func GetAllocationFromAuthTicket(authTicket string) (*Allocation, error) { return GetAllocation(at.AllocationID) } +// GetAllocation - get allocation from given allocation id +// +// - allocationID: the allocation id +// +// returns the allocation instance and error if any func GetAllocation(allocationID string) (*Allocation, error) { if !sdkInitialized { return nil, sdkNotInitialized @@ -933,16 +1002,17 @@ func GetAllocationUpdates(allocation *Allocation) error { return nil } +// SetNumBlockDownloads - set the number of block downloads, needs to be between 1 and 500 (inclusive). Default is 20. +// - num: the number of block downloads func SetNumBlockDownloads(num int) { if num > 0 && num <= 500 { numBlockDownloads = num } } -func SetVerifyHash(verify bool) { - shouldVerifyHash = verify -} - +// GetAllocations - get all allocations for the current client +// +// returns the list of allocations and error if any func GetAllocations() ([]*Allocation, error) { return GetAllocationsForClient(client.ClientID()) } @@ -964,7 +1034,11 @@ func getAllocationsInternal(clientID string, limit, offset int) ([]*Allocation, return allocations, nil } -// get paginated results +// GetAllocationsForClient - get all allocations for given client id +// +// - clientID: the client id +// +// returns the list of allocations and error if any func GetAllocationsForClient(clientID string) ([]*Allocation, error) { if !sdkInitialized { return nil, sdkNotInitialized @@ -1001,6 +1075,7 @@ type FileOptionParam struct { Value bool } +// FileOptionsParameters is used to specify the file options parameters for an allocation, which control the usage permissions of the files in the allocation. type FileOptionsParameters struct { ForbidUpload FileOptionParam ForbidDelete FileOptionParam @@ -1010,6 +1085,7 @@ type FileOptionsParameters struct { ForbidRename FileOptionParam } +// CreateAllocationOptions is used to specify the options for creating a new allocation. type CreateAllocationOptions struct { DataShards int ParityShards int @@ -1018,24 +1094,47 @@ type CreateAllocationOptions struct { WritePrice PriceRange Lock uint64 BlobberIds []string + BlobberAuthTickets []string ThirdPartyExtendable bool + IsEnterprise bool FileOptionsParams *FileOptionsParameters + Force bool } +// CreateAllocationWith creates a new allocation with the given options for the current client using the SDK. +// Similar ro CreateAllocationForOwner but uses an options struct instead of individual parameters. +// - options is the options struct instance for creating the allocation. +// +// returns the hash of the new_allocation_request transaction, the nonce of the transaction, the transaction object and an error if any. func CreateAllocationWith(options CreateAllocationOptions) ( string, int64, *transaction.Transaction, error) { return CreateAllocationForOwner(client.ClientID(), client.PublicKey(), options.DataShards, options.ParityShards, options.Size, options.ReadPrice, options.WritePrice, options.Lock, - options.BlobberIds, options.ThirdPartyExtendable, options.FileOptionsParams) + options.BlobberIds, options.BlobberAuthTickets, options.ThirdPartyExtendable, options.IsEnterprise, options.Force, options.FileOptionsParams) } +// CreateAllocationForOwner creates a new allocation with the given options (txn: `storagesc.new_allocation_request`). +// +// - owner is the client id of the owner of the allocation. +// - ownerpublickey is the public key of the owner of the allocation. +// - datashards is the number of data shards for the allocation. +// - parityshards is the number of parity shards for the allocation. +// - size is the size of the allocation. +// - readPrice is the read price range for the allocation (Reads in Züs are free!). +// - writePrice is the write price range for the allocation. +// - lock is the lock value for the transaction (how much tokens to lock to the allocation, in SAS). +// - preferredBlobberIds is a list of preferred blobber ids for the allocation. +// - thirdPartyExtendable is a flag indicating whether the allocation can be extended by a third party. +// - fileOptionsParams is the file options parameters for the allocation, which control the usage permissions of the files in the allocation. +// +// returns the hash of the transaction, the nonce of the transaction, the transaction object and an error if any. func CreateAllocationForOwner( owner, ownerpublickey string, datashards, parityshards int, size int64, readPrice, writePrice PriceRange, - lock uint64, preferredBlobberIds []string, thirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, + lock uint64, preferredBlobberIds, blobberAuthTickets []string, thirdPartyExtendable, IsEnterprise, force bool, fileOptionsParams *FileOptionsParameters, ) (hash string, nonce int64, txn *transaction.Transaction, err error) { if lock > math.MaxInt64 { @@ -1047,7 +1146,7 @@ func CreateAllocationForOwner( } allocationRequest, err := getNewAllocationBlobbers( - datashards, parityshards, size, readPrice, writePrice, preferredBlobberIds) + datashards, parityshards, size, readPrice, writePrice, preferredBlobberIds, blobberAuthTickets, force) if err != nil { return "", 0, nil, errors.New("failed_get_allocation_blobbers", "failed to get blobbers for allocation: "+err.Error()) } @@ -1060,6 +1159,7 @@ func CreateAllocationForOwner( allocationRequest["owner_public_key"] = ownerpublickey allocationRequest["third_party_extendable"] = thirdPartyExtendable allocationRequest["file_options_changed"], allocationRequest["file_options"] = calculateAllocationFileOptions(63 /*0011 1111*/, fileOptionsParams) + allocationRequest["is_enterprise"] = IsEnterprise var sn = transaction.SmartContractTxnData{ Name: transaction.NEW_ALLOCATION_REQUEST, @@ -1069,9 +1169,20 @@ func CreateAllocationForOwner( return } +// GetAllocationBlobbers returns a list of blobber ids that can be used for a new allocation. +// +// - datashards is the number of data shards for the allocation. +// - parityshards is the number of parity shards for the allocation. +// - size is the size of the allocation. +// - readPrice is the read price range for the allocation (Reads in Züs are free!). +// - writePrice is the write price range for the allocation. +// - force is a flag indicating whether to force the allocation to be created. +// +// returns the list of blobber ids and an error if any. func GetAllocationBlobbers( datashards, parityshards int, size int64, + isRestricted int, readPrice, writePrice PriceRange, force ...bool, ) ([]string, error) { @@ -1081,6 +1192,7 @@ func GetAllocationBlobbers( "size": size, "read_price_range": readPrice, "write_price_range": writePrice, + "is_restricted": isRestricted, } allocationData, _ := json.Marshal(allocationRequest) @@ -1109,10 +1221,24 @@ func getNewAllocationBlobbers( datashards, parityshards int, size int64, readPrice, writePrice PriceRange, - preferredBlobberIds []string, + preferredBlobberIds, blobberAuthTickets []string, force bool, ) (map[string]interface{}, error) { + for _, authTicket := range blobberAuthTickets { + if len(authTicket) > 0 { + return map[string]interface{}{ + "data_shards": datashards, + "parity_shards": parityshards, + "size": size, + "blobbers": preferredBlobberIds, + "blobber_auth_tickets": blobberAuthTickets, + "read_price_range": readPrice, + "write_price_range": writePrice, + }, nil + } + } + allocBlobberIDs, err := GetAllocationBlobbers( - datashards, parityshards, size, readPrice, writePrice, + datashards, parityshards, size, 2, readPrice, writePrice, force, ) if err != nil { return nil, err @@ -1123,23 +1249,32 @@ func getNewAllocationBlobbers( // filter duplicates ids := make(map[string]bool) uniqueBlobbers := []string{} + uniqueBlobberAuthTickets := []string{} + for _, b := range blobbers { if !ids[b] { uniqueBlobbers = append(uniqueBlobbers, b) + uniqueBlobberAuthTickets = append(uniqueBlobberAuthTickets, "") ids[b] = true } } return map[string]interface{}{ - "data_shards": datashards, - "parity_shards": parityshards, - "size": size, - "blobbers": uniqueBlobbers, - "read_price_range": readPrice, - "write_price_range": writePrice, + "data_shards": datashards, + "parity_shards": parityshards, + "size": size, + "blobbers": uniqueBlobbers, + "blobber_auth_tickets": uniqueBlobberAuthTickets, + "read_price_range": readPrice, + "write_price_range": writePrice, }, nil } +// GetBlobberIds returns a list of blobber ids that can be used for a new allocation. +// +// - blobberUrls is a list of blobber urls. +// +// returns a list of blobber ids that can be used for the new allocation and an error if any. func GetBlobberIds(blobberUrls []string) ([]string, error) { if len(blobberUrls) == 0 { @@ -1167,6 +1302,11 @@ func GetBlobberIds(blobberUrls []string) ([]string, error) { return blobberIDs, nil } +// GetFreeAllocationBlobbers returns a list of blobber ids that can be used for a new free allocation. +// +// - request is the request data for the free allocation. +// +// returns a list of blobber ids that can be used for the new free allocation and an error if any. func GetFreeAllocationBlobbers(request map[string]interface{}) ([]string, error) { data, _ := json.Marshal(request) @@ -1187,6 +1327,15 @@ func GetFreeAllocationBlobbers(request map[string]interface{}) ([]string, error) return allocBlobberIDs, nil } +// AddFreeStorageAssigner adds a new free storage assigner (txn: `storagesc.add_free_allocation_assigner`). +// The free storage assigner is used to create free allocations. Can only be called by chain owner. +// +// - name is the name of the assigner. +// - publicKey is the public key of the assigner. +// - individualLimit is the individual limit of the assigner for a single free allocation request +// - totalLimit is the total limit of the assigner for all free allocation requests. +// +// returns the hash of the transaction, the nonce of the transaction and an error if any. func AddFreeStorageAssigner(name, publicKey string, individualLimit, totalLimit float64) (string, int64, error) { if !sdkInitialized { return "", 0, sdkNotInitialized @@ -1208,6 +1357,11 @@ func AddFreeStorageAssigner(name, publicKey string, individualLimit, totalLimit return hash, n, err } +// CreateFreeAllocation creates a new free allocation (txn: `storagesc.free_allocation_request`). +// - marker is the marker for the free allocation. +// - value is the value of the free allocation. +// +// returns the hash of the transaction, the nonce of the transaction and an error if any. func CreateFreeAllocation(marker string, value uint64) (string, int64, error) { if !sdkInitialized { return "", 0, sdkNotInitialized @@ -1235,12 +1389,25 @@ func CreateFreeAllocation(marker string, value uint64) (string, int64, error) { return hash, n, err } +// UpdateAllocation sends an update request for an allocation (txn: `storagesc.update_allocation_request`) +// +// - size is the size of the allocation. +// - extend is a flag indicating whether to extend the allocation. +// - allocationID is the id of the allocation. +// - lock is the lock value for the transaction (how much tokens to lock to the allocation, in SAS). +// - addBlobberId is the id of the blobber to add to the allocation. +// - addBlobberAuthTicket is the auth ticket of the blobber to add to the allocation, in case the blobber is restricted. +// - removeBlobberId is the id of the blobber to remove from the allocation. +// - setThirdPartyExtendable is a flag indicating whether the allocation can be extended by a third party. +// - fileOptionsParams is the file options parameters for the allocation, which control the usage permissions of the files in the allocation. +// +// returns the hash of the transaction, the nonce of the transaction and an error if any. func UpdateAllocation( size int64, extend bool, allocationID string, lock uint64, - addBlobberId, removeBlobberId string, + addBlobberId, addBlobberAuthTicket, removeBlobberId string, setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, ) (hash string, nonce int64, err error) { @@ -1264,6 +1431,7 @@ func UpdateAllocation( updateAllocationRequest["size"] = size updateAllocationRequest["extend"] = extend updateAllocationRequest["add_blobber_id"] = addBlobberId + updateAllocationRequest["add_blobber_auth_ticket"] = addBlobberAuthTicket updateAllocationRequest["remove_blobber_id"] = removeBlobberId updateAllocationRequest["set_third_party_extendable"] = setThirdPartyExtendable updateAllocationRequest["file_options_changed"], updateAllocationRequest["file_options"] = calculateAllocationFileOptions(alloc.FileOptions, fileOptionsParams) @@ -1276,6 +1444,11 @@ func UpdateAllocation( return } +// FinalizeAllocation sends a finalize request for an allocation (txn: `storagesc.finalize_allocation`) +// +// - allocID is the id of the allocation. +// +// returns the hash of the transaction, the nonce of the transaction and an error if any. func FinalizeAllocation(allocID string) (hash string, nonce int64, err error) { if !sdkInitialized { return "", 0, sdkNotInitialized @@ -1288,6 +1461,11 @@ func FinalizeAllocation(allocID string) (hash string, nonce int64, err error) { return } +// CancelAllocation sends a cancel request for an allocation (txn: `storagesc.cancel_allocation`) +// +// - allocID is the id of the allocation. +// +// returns the hash of the transaction, the nonce of the transaction and an error if any. func CancelAllocation(allocID string) (hash string, nonce int64, err error) { if !sdkInitialized { return "", 0, sdkNotInitialized @@ -1300,6 +1478,7 @@ func CancelAllocation(allocID string) (hash string, nonce int64, err error) { return } +// ProviderType is the type of the provider. type ProviderType int const ( @@ -1310,6 +1489,9 @@ const ( ProviderAuthorizer ) +// KillProvider kills a blobber or a validator (txn: `storagesc.kill_blobber` or `storagesc.kill_validator`) +// - providerId is the id of the provider. +// - providerType` is the type of the provider, either 3 for `ProviderBlobber` or 4 for `ProviderValidator. func KillProvider(providerId string, providerType ProviderType) (string, int64, error) { if !sdkInitialized { return "", 0, sdkNotInitialized @@ -1333,6 +1515,9 @@ func KillProvider(providerId string, providerType ProviderType) (string, int64, return hash, n, err } +// ShutdownProvider shuts down a blobber or a validator (txn: `storagesc.shutdown_blobber` or `storagesc.shutdown_validator`) +// - providerId is the id of the provider. +// - providerType` is the type of the provider, either 3 for `ProviderBlobber` or 4 for `ProviderValidator. func ShutdownProvider(providerType ProviderType, providerID string) (string, int64, error) { if !sdkInitialized { return "", 0, sdkNotInitialized @@ -1357,6 +1542,9 @@ func ShutdownProvider(providerType ProviderType, providerID string) (string, int return hash, n, err } +// CollectRewards collects the rewards for a provider (txn: `storagesc.collect_reward`) +// - providerId is the id of the provider. +// - providerType is the type of the provider. func CollectRewards(providerId string, providerType ProviderType) (string, int64, error) { if !sdkInitialized { return "", 0, sdkNotInitialized @@ -1390,6 +1578,13 @@ func CollectRewards(providerId string, providerType ProviderType) (string, int64 return hash, n, err } +// TransferAllocation transfers the ownership of an allocation to a new owner. (txn: `storagesc.update_allocation_request`) +// +// - allocationId is the id of the allocation. +// - newOwner is the client id of the new owner. +// - newOwnerPublicKey is the public key of the new owner. +// +// returns the hash of the transaction, the nonce of the transaction and an error if any. func TransferAllocation(allocationId, newOwner, newOwnerPublicKey string) (string, int64, error) { if !sdkInitialized { return "", 0, sdkNotInitialized @@ -1421,6 +1616,8 @@ func TransferAllocation(allocationId, newOwner, newOwnerPublicKey string) (strin return hash, n, err } +// UpdateBlobberSettings updates the settings of a blobber (txn: `storagesc.update_blobber_settings`) +// - blob is the update blobber request inputs. func UpdateBlobberSettings(blob *UpdateBlobber) (resp string, nonce int64, err error) { if !sdkInitialized { return "", 0, sdkNotInitialized @@ -1433,6 +1630,8 @@ func UpdateBlobberSettings(blob *UpdateBlobber) (resp string, nonce int64, err e return } +// UpdateValidatorSettings updates the settings of a validator (txn: `storagesc.update_validator_settings`) +// - v is the update validator request inputs. func UpdateValidatorSettings(v *UpdateValidator) (resp string, nonce int64, err error) { if !sdkInitialized { return "", 0, sdkNotInitialized @@ -1446,6 +1645,34 @@ func UpdateValidatorSettings(v *UpdateValidator) (resp string, nonce int64, err return } +// ResetBlobberStats resets the stats of a blobber (txn: `storagesc.reset_blobber_stats`) +// - rbs is the reset blobber stats dto, contains the blobber id and its stats. +func ResetBlobberStats(rbs *ResetBlobberStatsDto) (string, int64, error) { + if !sdkInitialized { + return "", 0, sdkNotInitialized + } + + var sn = transaction.SmartContractTxnData{ + Name: transaction.STORAGESC_RESET_BLOBBER_STATS, + InputArgs: rbs, + } + hash, _, n, _, err := storageSmartContractTxn(sn) + return hash, n, err +} + +func ResetAllocationStats(allocationId string) (string, int64, error) { + if !sdkInitialized { + return "", 0, sdkNotInitialized + } + + var sn = transaction.SmartContractTxnData{ + Name: transaction.STORAGESC_RESET_ALLOCATION_STATS, + InputArgs: allocationId, + } + hash, _, n, _, err := storageSmartContractTxn(sn) + return hash, n, err +} + func smartContractTxn(scAddress string, sn transaction.SmartContractTxnData) ( hash, out string, nonce int64, txn *transaction.Transaction, err error) { return smartContractTxnValue(scAddress, sn, 0) @@ -1646,7 +1873,13 @@ func CommitToFabric(metaTxnData, fabricConfigJSON string) (string, error) { return fabricResponse, err } -// expire in milliseconds +// GetAllocationMinLock calculates and returns the minimum lock demand for creating a new allocation, which represents the cost of the creation process. +// - datashards is the number of data shards for the allocation. +// - parityshards is the number of parity shards for the allocation. +// - size is the size of the allocation. +// - writePrice is the write price range for the allocation. +// +// returns the minimum lock demand for the creation process and an error if any. func GetAllocationMinLock( datashards, parityshards int, size int64, @@ -1669,6 +1902,15 @@ func GetAllocationMinLock( return i, nil } +// GetUpdateAllocationMinLock returns the minimum lock demand for updating an allocation, which represents the cost of the update operation. +// +// - allocationID is the id of the allocation. +// - size is the new size of the allocation. +// - extend is a flag indicating whether to extend the expiry of the allocation. +// - addBlobberId is the id of the blobber to add to the allocation. +// - removeBlobberId is the id of the blobber to remove from the allocation. +// +// returns the minimum lock demand for the update operation and an error if any. func GetUpdateAllocationMinLock( allocationID string, size int64, From 3be769c3434efc2906f34704fedefa0f5a9f7db2 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 22 Aug 2024 21:20:20 +0530 Subject: [PATCH 016/319] Cleanup --- zboxcore/sdk/networkworker.go | 131 ---------------------------------- zcncore/networkworker.go | 108 ---------------------------- 2 files changed, 239 deletions(-) delete mode 100644 zboxcore/sdk/networkworker.go delete mode 100644 zcncore/networkworker.go diff --git a/zboxcore/sdk/networkworker.go b/zboxcore/sdk/networkworker.go deleted file mode 100644 index 8dffe6a7b..000000000 --- a/zboxcore/sdk/networkworker.go +++ /dev/null @@ -1,131 +0,0 @@ -package sdk - -// import ( -// "context" -// "encoding/json" -// "io/ioutil" -// "net/http" -// "reflect" -// "strconv" -// "time" - -// "github.com/0chain/gosdk/core/conf" -// "github.com/0chain/gosdk/core/node" -// l "github.com/0chain/gosdk/zboxcore/logger" -// "go.uber.org/zap" - -// "github.com/0chain/errors" -// "github.com/0chain/gosdk/zboxcore/blockchain" -// "github.com/0chain/gosdk/zboxcore/zboxutil" -// ) - -// const NETWORK_ENDPOINT = "/network" - -// type Network struct { -// Miners []string `json:"miners"` -// Sharders []string `json:"sharders"` -// } - -// func UpdateNetworkDetailsWorker(ctx context.Context) { -// ticker := time.NewTicker(time.Duration(networkWorkerTimerInHours) * time.Hour) -// for { -// select { -// case <-ctx.Done(): -// l.Logger.Info("Network stopped by user") -// return -// case <-ticker.C: -// err := UpdateNetworkDetails() -// if err != nil { -// l.Logger.Error("Update network detail worker fail", zap.Error(err)) -// return -// } -// l.Logger.Info("Successfully updated network details") -// return -// } -// } -// } - -// func UpdateNetworkDetails() error { -// networkDetails, err := GetNetworkDetails() -// if err != nil { -// l.Logger.Error("Failed to update network details ", zap.Error(err)) -// return err -// } - -// shouldUpdate := UpdateRequired(networkDetails) -// if shouldUpdate { -// forceUpdateNetworkDetails(networkDetails) -// } -// return nil -// } - -// func InitNetworkDetails() error { -// networkDetails, err := GetNetworkDetails() -// if err != nil { -// l.Logger.Error("Failed to update network details ", zap.Error(err)) -// return err -// } -// forceUpdateNetworkDetails(networkDetails) -// return nil -// } - -// func forceUpdateNetworkDetails(networkDetails *Network) { -// sdkInitialized = false -// blockchain.SetMiners(networkDetails.Miners) -// blockchain.SetSharders(networkDetails.Sharders) -// node.InitCache(blockchain.Sharders) -// conf.InitChainNetwork(&conf.Network{ -// Sharders: networkDetails.Sharders, -// Miners: networkDetails.Miners, -// }) -// sdkInitialized = true -// } - -// func UpdateRequired(networkDetails *Network) bool { -// miners := blockchain.GetMiners() -// sharders := blockchain.GetAllSharders() -// if len(miners) == 0 || len(sharders) == 0 { -// return true -// } - -// minerSame := reflect.DeepEqual(miners, networkDetails.Miners) -// sharderSame := reflect.DeepEqual(sharders, networkDetails.Sharders) - -// if minerSame && sharderSame { -// return false -// } -// return true -// } - -// func GetNetworkDetails() (*Network, error) { -// req, ctx, cncl, err := zboxutil.NewHTTPRequest(http.MethodGet, blockchain.GetBlockWorker()+NETWORK_ENDPOINT, nil) -// if err != nil { -// return nil, errors.New("get_network_details_error", "Unable to create new http request with error "+err.Error()) -// } - -// var networkResponse Network -// err = zboxutil.HttpDo(ctx, cncl, req, func(resp *http.Response, err error) error { -// if err != nil { -// l.Logger.Error("Get network error : ", err) -// return err -// } - -// defer resp.Body.Close() -// respBody, err := ioutil.ReadAll(resp.Body) -// if err != nil { -// return errors.Wrap(err, "Error reading response : ") -// } - -// l.Logger.Debug("Get network result:", string(respBody)) -// if resp.StatusCode == http.StatusOK { -// err = json.Unmarshal(respBody, &networkResponse) -// if err != nil { -// return errors.Wrap(err, "Error unmarshaling response :") -// } -// return nil -// } -// return errors.New(strconv.Itoa(resp.StatusCode), "Get network details status not OK") - -// }) -// return &networkResponse, err -// } diff --git a/zcncore/networkworker.go b/zcncore/networkworker.go deleted file mode 100644 index d7582f235..000000000 --- a/zcncore/networkworker.go +++ /dev/null @@ -1,108 +0,0 @@ -//go:build !mobile -// +build !mobile - -package zcncore - -import ( - "context" - "encoding/json" - - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/conf" -) - -const NETWORK_ENDPOINT = "/network" - -type Network struct { - Miners []string `json:"miners"` - Sharders []string `json:"sharders"` -} - -//Deprecated: Get client.Node instance to check whether network update is required and update network accordingly -func UpdateNetworkDetails() error { - nodeClient, err := client.GetNode() - if err != nil { - return err - } - shouldUpdate, network, err := nodeClient.ShouldUpdateNetwork() - if err != nil { - logging.Error("error on ShouldUpdateNetwork check: ", err) - return err - } - if shouldUpdate { - logging.Info("Updating network") - if err = nodeClient.UpdateNetwork(network); err != nil { - logging.Error("error on updating network: ", err) - return err - } - logging.Info("network updated successfully") - } - return nil -} - -//Deprecated: Get client.Node instance to check whether network update is required -func UpdateRequired(networkDetails *Network) bool { - nodeClient, err := client.GetNode() - if err != nil { - panic(err) - } - shouldUpdate, _, err := nodeClient.ShouldUpdateNetwork() - if err != nil { - logging.Error("error on ShouldUpdateNetwork check: ", err) - panic(err) - } - return shouldUpdate -} - -//Deprecated: Use client.GetNetwork() function -func GetNetworkDetails() (*Network, error) { - cfg, err := conf.GetClientConfig() - if err != nil { - return nil, err - } - network, err := client.GetNetwork(context.Background(), cfg.BlockWorker) - if err != nil { - return nil, err - } - return &Network{ - Miners: network.Miners, - Sharders: network.Sharders, - }, nil -} - -//Deprecated: Use client.Node instance to get its network details -func GetNetwork() *Network { - nodeClient, err := client.GetNode() - if err != nil { - panic(err) - } - return &Network{ - Miners: nodeClient.Network().Miners, - Sharders: nodeClient.Network().Sharders, - } -} - -//Deprecated: Use client.Node instance UpdateNetwork() method -func SetNetwork(miners []string, sharders []string) { - nodeClient, err := client.GetNode() - if err != nil { - panic(err) - } - network, err := conf.NewNetwork(miners, sharders) - if err != nil { - panic(err) - } - err = nodeClient.UpdateNetwork(network) - if err != nil { - logging.Error("error updating network: ", err) - panic(err) - } - logging.Info("network updated successfully") -} - -//Deprecated: Use client.GetNetwork() function -func GetNetworkJSON() string { - network := GetNetwork() - networkBytes, _ := json.Marshal(network) - return string(networkBytes) -} From 4fae16e0942cf938e22e3bf027ad78ea99ecc3d4 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 23 Aug 2024 10:01:15 +0530 Subject: [PATCH 017/319] Fix --- core/client/set.go | 25 +- core/tokenrate/tokenrate_test.go | 1 - zboxcore/sdk/allocation_file_delete_test.go | 4 +- zboxcore/sdk/allocation_file_test.go | 22 +- zboxcore/sdk/allocation_test.go | 5 +- zboxcore/sdk/blockdownloadworker.go | 6 +- zboxcore/sdk/chunked_upload.go | 6 +- zboxcore/sdk/chunked_upload_form_builder.go | 6 +- zboxcore/sdk/chunked_upload_model.go | 4 +- zboxcore/sdk/chunked_upload_process.go | 8 +- zboxcore/sdk/chunked_upload_process_js.go | 2 +- zboxcore/sdk/copyworker_test.go | 5 +- zboxcore/sdk/deleteworker_test.go | 5 +- zboxcore/sdk/downloadworker.go | 12 +- zboxcore/sdk/filemetaworker_test.go | 5 +- zboxcore/sdk/filestatsworker_test.go | 5 +- zboxcore/sdk/listworker_test.go | 5 +- zboxcore/sdk/renameworker_test.go | 5 +- zboxcore/sdk/rollback.go | 8 +- zboxcore/sdk/writemarker_mutex_test.go | 3 +- zboxcore/zboxutil/http.go | 31 +- zcncore/transaction.go | 469 ++------------------ zcncore/transactionauth.go | 274 +----------- zcncore/transactionauth_mobile.go | 232 ---------- zcncore/wallet_base.go | 33 +- 25 files changed, 154 insertions(+), 1027 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index 2cb135bfe..456df86bd 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -17,22 +17,22 @@ type SignFunc func(hash string) (string, error) // maintains client's information type Client struct { - wallet *zcncrypto.Wallet + wallet *zcncrypto.Wallet signatureScheme string - splitKeyWallet bool - authUrl string - nonce int64 - txnFee uint64 - sign SignFunc + splitKeyWallet bool + authUrl string + nonce int64 + txnFee uint64 + sign SignFunc } func init() { sys.Sign = signHash - client = Client { - wallet: &zcncrypto.Wallet{}, + client = Client{ + wallet: &zcncrypto.Wallet{}, sign: func(hash string) (string, error) { return sys.Sign(hash, client.signatureScheme, GetClientSysKeys()) - }, + }, } sys.Verify = verifySignature sys.VerifyWith = verifySignatureWith @@ -89,9 +89,8 @@ func GetClientSysKeys() []sys.KeyPair { } // SetWallet should be set before any transaction or client specific APIs -func SetWallet(w zcncrypto.Wallet) error { +func SetWallet(w zcncrypto.Wallet) { client.wallet = &w - return nil } // splitKeyWallet parameter is valid only if SignatureScheme is "BLS0Chain" @@ -168,6 +167,10 @@ func PublicKey() string { return client.wallet.ClientKey } +func Mnemonic() string { + return client.wallet.Mnemonic +} + func PrivateKey() string { for _, kv := range client.wallet.Keys { return kv.PrivateKey diff --git a/core/tokenrate/tokenrate_test.go b/core/tokenrate/tokenrate_test.go index 7a1d4b31f..419693974 100644 --- a/core/tokenrate/tokenrate_test.go +++ b/core/tokenrate/tokenrate_test.go @@ -16,7 +16,6 @@ import ( "github.com/stretchr/testify/require" "github.com/0chain/gosdk/core/resty" - "github.com/0chain/gosdk/zboxcore/mocks" ) func TestGetUSD(t *testing.T) { diff --git a/zboxcore/sdk/allocation_file_delete_test.go b/zboxcore/sdk/allocation_file_delete_test.go index cd2e3c135..1b80f36fc 100644 --- a/zboxcore/sdk/allocation_file_delete_test.go +++ b/zboxcore/sdk/allocation_file_delete_test.go @@ -8,12 +8,12 @@ import ( "time" "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/resty" "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" - "github.com/0chain/gosdk/zboxcore/mocks" + "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/stretchr/testify/require" ) diff --git a/zboxcore/sdk/allocation_file_test.go b/zboxcore/sdk/allocation_file_test.go index d833b3334..ed3920133 100644 --- a/zboxcore/sdk/allocation_file_test.go +++ b/zboxcore/sdk/allocation_file_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "github.com/0chain/gosdk/zboxcore/mocks" "io" "io/ioutil" "net/http" @@ -21,11 +22,10 @@ import ( "github.com/0chain/gosdk/core/zcncrypto" "github.com/hitenjain14/fasthttp" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" - zclient "github.com/0chain/gosdk/zboxcore/client" "github.com/0chain/gosdk/zboxcore/fileref" - "github.com/0chain/gosdk/zboxcore/mocks" + "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" @@ -37,7 +37,12 @@ func setupHttpResponses( numBlobbers, numCorrect int, isUpdate bool) { walletJSON := `{"client_id":"00d2d56d0d573329fe61b8252a4b1715f93fac15176e5d90c413bc92a42e498b","client_key":"000b47144eb0366c3039bca10bc6df3ac289d8823de14ffc08cfdfe83f03e4079ab94bdc3932e7e9bc053f38834c7da63ce6f9c6e540d93cf0c52ba4149f2280","keys":[{"public_key":"000b47144eb0366c3039bca10bc6df3ac289d8823de14ffc08cfdfe83f03e4079ab94bdc3932e7e9bc053f38834c7da63ce6f9c6e540d93cf0c52ba4149f2280","private_key":"77a7faf0dcc1865a475963fee7ce71ca6dc6a20198209eb75d9fc1dc9df41f0f"}],"mnemonics":"mistake alone lumber swamp tape device flight oppose room combine useful typical deal lion device hope glad once million pudding artist brush sing vicious","version":"1.0","date_created":"2024-03-11T20:06:33+05:30","nonce":0}` - client.PopulateClient(walletJSON, "bls0chain") //nolint:errcheck + wallet := zcncrypto.Wallet{} + err := json.Unmarshal([]byte(walletJSON), &wallet) + require.NoError(t, err) + + client.SetWallet(wallet) + client.SetSignatureScheme("bls0chain") for i := 0; i < numBlobbers; i++ { metaBlobberBase := t.Name() + "/" + mockBlobberUrl + strconv.Itoa(i) + zboxutil.FILE_META_ENDPOINT @@ -59,7 +64,7 @@ func setupHttpResponses( } return http.StatusBadRequest }(), - Body: ioutil.NopCloser(bytes.NewReader(fileMetaInput)), + Body: io.NopCloser(bytes.NewReader(fileMetaInput)), }, nil) mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { @@ -72,7 +77,7 @@ func setupHttpResponses( } return http.StatusBadRequest }(), - Body: ioutil.NopCloser(bytes.NewReader(refsInput)), + Body: io.NopCloser(bytes.NewReader(refsInput)), }, nil) mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { @@ -678,11 +683,10 @@ func TestAllocation_RepairFile(t *testing.T) { resty.CreateClient = createClient }() - client := zclient.GetClient() - client.Wallet = &zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, - } + }) // setupHttpResponses := func(t *testing.T, testName string, numBlobbers, numCorrect int) { // require.True(t, numBlobbers >= numCorrect) diff --git a/zboxcore/sdk/allocation_test.go b/zboxcore/sdk/allocation_test.go index 99ed3008d..f73a5a830 100644 --- a/zboxcore/sdk/allocation_test.go +++ b/zboxcore/sdk/allocation_test.go @@ -5,6 +5,7 @@ import ( "context" "encoding/hex" "encoding/json" + "github.com/0chain/gosdk/zboxcore/mocks" "io" "io/fs" "io/ioutil" @@ -26,11 +27,11 @@ import ( "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/sys" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" - "github.com/0chain/gosdk/zboxcore/mocks" + "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" diff --git a/zboxcore/sdk/blockdownloadworker.go b/zboxcore/sdk/blockdownloadworker.go index 603003903..0ebddceec 100644 --- a/zboxcore/sdk/blockdownloadworker.go +++ b/zboxcore/sdk/blockdownloadworker.go @@ -10,10 +10,10 @@ import ( "time" "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/util" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" "github.com/0chain/gosdk/zboxcore/fileref" zlogger "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/marker" @@ -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.GetClientID(), 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.ClientID(), 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)) } @@ -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.GetClientID(), header.BlockNum)) + zlogger.Logger.Debug(fmt.Sprintf("downloadBlobberBlock 200 OK: blobberID: %v, clientID: %v, blockNum: %d", req.blobber.ID, client.ClientID(), header.BlockNum)) req.result <- &rspData return nil diff --git a/zboxcore/sdk/chunked_upload.go b/zboxcore/sdk/chunked_upload.go index c86942f75..c319968a9 100644 --- a/zboxcore/sdk/chunked_upload.go +++ b/zboxcore/sdk/chunked_upload.go @@ -17,13 +17,13 @@ import ( thrown "github.com/0chain/errors" "github.com/0chain/gosdk/constants" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" coreEncryption "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/core/util" "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" "github.com/0chain/gosdk/zboxcore/encryption" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" @@ -256,7 +256,7 @@ func CreateChunkedUpload( } - su.writeMarkerMutex, err = CreateWriteMarkerMutex(client.GetClient(), su.allocationObj) + su.writeMarkerMutex, err = CreateWriteMarkerMutex(su.allocationObj) if err != nil { return nil, err } @@ -388,7 +388,7 @@ func (su *ChunkedUpload) createEncscheme() encryption.EncryptionScheme { return nil } } else { - mnemonic := client.GetClient().Mnemonic + mnemonic := client.Mnemonic() if mnemonic == "" { return nil } diff --git a/zboxcore/sdk/chunked_upload_form_builder.go b/zboxcore/sdk/chunked_upload_form_builder.go index e8e7f3f8a..81b6fb2c1 100644 --- a/zboxcore/sdk/chunked_upload_form_builder.go +++ b/zboxcore/sdk/chunked_upload_form_builder.go @@ -21,7 +21,7 @@ type ChunkedUploadFormBuilder interface { chunkSize int64, chunkStartIndex, chunkEndIndex int, isFinal bool, encryptedKey, encryptedKeyPoint string, fileChunksData [][]byte, thumbnailChunkData []byte, shardSize int64, - ) (blobberData, error) + ) (BlobberData, error) } // ChunkedUploadFormMetadata upload form metadata @@ -49,13 +49,13 @@ func (b *chunkedUploadFormBuilder) Build( chunkSize int64, chunkStartIndex, chunkEndIndex int, isFinal bool, encryptedKey, encryptedKeyPoint string, fileChunksData [][]byte, thumbnailChunkData []byte, shardSize int64, -) (blobberData, error) { +) (BlobberData, error) { metadata := ChunkedUploadFormMetadata{ ThumbnailBytesLen: len(thumbnailChunkData), } - var res blobberData + var res BlobberData if len(fileChunksData) == 0 { return res, nil diff --git a/zboxcore/sdk/chunked_upload_model.go b/zboxcore/sdk/chunked_upload_model.go index 356272ee1..25b9d1c6d 100644 --- a/zboxcore/sdk/chunked_upload_model.go +++ b/zboxcore/sdk/chunked_upload_model.go @@ -215,10 +215,10 @@ type UploadData struct { chunkEndIndex int isFinal bool uploadLength int64 - uploadBody []blobberData + uploadBody []BlobberData } -type blobberData struct { +type BlobberData struct { dataBuffers []*bytes.Buffer formData ChunkedUploadFormMetadata contentSlice []string diff --git a/zboxcore/sdk/chunked_upload_process.go b/zboxcore/sdk/chunked_upload_process.go index da58f19de..aa5662bd8 100644 --- a/zboxcore/sdk/chunked_upload_process.go +++ b/zboxcore/sdk/chunked_upload_process.go @@ -70,19 +70,19 @@ func (su *ChunkedUpload) processUpload(chunkStartIndex, chunkEndIndex int, var ( errCount int32 - finalBuffer []blobberData + finalBuffer []BlobberData pos uint64 wg sync.WaitGroup lastBufferOnly bool ) if isFinal { - finalBuffer = make([]blobberData, len(su.blobbers)) + finalBuffer = make([]BlobberData, len(su.blobbers)) } blobberUpload := UploadData{ chunkStartIndex: chunkStartIndex, chunkEndIndex: chunkEndIndex, isFinal: isFinal, - uploadBody: make([]blobberData, len(su.blobbers)), + uploadBody: make([]BlobberData, len(su.blobbers)), uploadLength: uploadLength, } @@ -117,7 +117,7 @@ func (su *ChunkedUpload) processUpload(chunkStartIndex, chunkEndIndex int, return } if isFinal { - finalBuffer[pos] = blobberData{ + finalBuffer[pos] = BlobberData{ dataBuffers: uploadData.dataBuffers[len(uploadData.dataBuffers)-1:], formData: uploadData.formData, contentSlice: uploadData.contentSlice[len(uploadData.contentSlice)-1:], diff --git a/zboxcore/sdk/chunked_upload_process_js.go b/zboxcore/sdk/chunked_upload_process_js.go index e0e17e118..0b2d8c407 100644 --- a/zboxcore/sdk/chunked_upload_process_js.go +++ b/zboxcore/sdk/chunked_upload_process_js.go @@ -434,7 +434,7 @@ func ProcessEventData(data safejs.Value) { if !formInfo.IsFinal { wp.wg.Add(1) } - go func(blobberData blobberData, remotePath string, wg *sync.WaitGroup) { + go func(blobberData BlobberData, remotePath string, wg *sync.WaitGroup) { if formInfo.IsFinal && len(blobberData.dataBuffers) > 1 { err = sendUploadRequest(blobberData.dataBuffers[:len(blobberData.dataBuffers)-1], blobberData.contentSlice[:len(blobberData.contentSlice)-1], blobberURL, formInfo.AllocationID, formInfo.AllocationTx, formInfo.HttpMethod) if err != nil { diff --git a/zboxcore/sdk/copyworker_test.go b/zboxcore/sdk/copyworker_test.go index 803f15aa3..dc4aad0bd 100644 --- a/zboxcore/sdk/copyworker_test.go +++ b/zboxcore/sdk/copyworker_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "github.com/0chain/gosdk/zboxcore/mocks" "io" "io/ioutil" "mime" @@ -15,14 +16,14 @@ import ( "testing" "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/dev" devMock "github.com/0chain/gosdk/dev/mock" "github.com/0chain/gosdk/sdks/blobber" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" - "github.com/0chain/gosdk/zboxcore/mocks" + "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" diff --git a/zboxcore/sdk/deleteworker_test.go b/zboxcore/sdk/deleteworker_test.go index 52ac5d2b8..7b0de8e5a 100644 --- a/zboxcore/sdk/deleteworker_test.go +++ b/zboxcore/sdk/deleteworker_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "github.com/0chain/gosdk/zboxcore/mocks" "io" "io/ioutil" "net/http" @@ -14,12 +15,12 @@ import ( "time" "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/resty" "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" - "github.com/0chain/gosdk/zboxcore/mocks" + "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index b5b29781f..e81c20c36 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -20,10 +20,10 @@ import ( "time" "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" "github.com/0chain/gosdk/zboxcore/encryption" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" @@ -47,7 +47,7 @@ var ( type DownloadRequestOption func(dr *DownloadRequest) // WithDownloadProgressStorer set download progress storer of download request options. -// - storer: download progress storer instance, used to store download progress. +// - storer: download progress storer instance, used to store download progress. func WithDownloadProgressStorer(storer DownloadProgressStorer) DownloadRequestOption { return func(dr *DownloadRequest) { dr.downloadStorer = storer @@ -826,8 +826,8 @@ func (req *DownloadRequest) attemptSubmitReadMarker(blobber *blockchain.StorageN lockBlobberReadCtr(req.allocationID, blobber.ID) defer unlockBlobberReadCtr(req.allocationID, blobber.ID) rm := &marker.ReadMarker{ - ClientID: client.GetClientID(), - ClientPublicKey: client.GetClientPublicKey(), + ClientID: client.ClientID(), + ClientPublicKey: client.PublicKey(), BlobberID: blobber.ID, AllocationID: req.allocationID, OwnerID: req.allocOwnerID, @@ -966,9 +966,9 @@ func (req *DownloadRequest) initEC() error { // initEncryption will initialize encScheme with client's keys func (req *DownloadRequest) initEncryption() (err error) { req.encScheme = encryption.NewEncryptionScheme() - mnemonic := client.GetClient().Mnemonic + mnemonic := client.Mnemonic() if mnemonic != "" { - _, err = req.encScheme.Initialize(client.GetClient().Mnemonic) + _, err = req.encScheme.Initialize(client.Mnemonic()) if err != nil { return err } diff --git a/zboxcore/sdk/filemetaworker_test.go b/zboxcore/sdk/filemetaworker_test.go index 0b3f7a0c6..395abb6c4 100644 --- a/zboxcore/sdk/filemetaworker_test.go +++ b/zboxcore/sdk/filemetaworker_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "github.com/0chain/gosdk/zboxcore/mocks" "io" "io/ioutil" "mime" @@ -17,12 +18,12 @@ import ( "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/marker" - "github.com/0chain/gosdk/zboxcore/mocks" + "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" diff --git a/zboxcore/sdk/filestatsworker_test.go b/zboxcore/sdk/filestatsworker_test.go index 5eec70231..a5981f389 100644 --- a/zboxcore/sdk/filestatsworker_test.go +++ b/zboxcore/sdk/filestatsworker_test.go @@ -4,6 +4,8 @@ import ( "bytes" "context" "encoding/json" + "github.com/0chain/gosdk/zboxcore/mocks" + "io" "io/ioutil" "mime" @@ -14,12 +16,11 @@ import ( "testing" "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" - "github.com/0chain/gosdk/zboxcore/mocks" "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" diff --git a/zboxcore/sdk/listworker_test.go b/zboxcore/sdk/listworker_test.go index bf0c112c6..0145304f1 100644 --- a/zboxcore/sdk/listworker_test.go +++ b/zboxcore/sdk/listworker_test.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/0chain/gosdk/zboxcore/mocks" "io" "io/ioutil" "net/http" @@ -14,12 +15,12 @@ import ( "testing" "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/marker" - "github.com/0chain/gosdk/zboxcore/mocks" + "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" diff --git a/zboxcore/sdk/renameworker_test.go b/zboxcore/sdk/renameworker_test.go index 7d633b529..b14eaab10 100644 --- a/zboxcore/sdk/renameworker_test.go +++ b/zboxcore/sdk/renameworker_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "github.com/0chain/gosdk/zboxcore/mocks" "io" "io/ioutil" "mime" @@ -15,14 +16,14 @@ import ( "testing" "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/dev" devMock "github.com/0chain/gosdk/dev/mock" "github.com/0chain/gosdk/sdks/blobber" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" - "github.com/0chain/gosdk/zboxcore/mocks" + "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" diff --git a/zboxcore/sdk/rollback.go b/zboxcore/sdk/rollback.go index e39f8eecd..39866bd3e 100644 --- a/zboxcore/sdk/rollback.go +++ b/zboxcore/sdk/rollback.go @@ -19,8 +19,8 @@ import ( "github.com/0chain/common/core/common" thrown "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" l "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/marker" "github.com/0chain/gosdk/zboxcore/zboxutil" @@ -101,12 +101,12 @@ func GetWritemarker(allocID, allocTx, id, baseUrl string) (*LatestPrevWriteMarke return nil, err } if lpm.LatestWM != nil { - err = lpm.LatestWM.VerifySignature(client.GetClientPublicKey()) + err = lpm.LatestWM.VerifySignature(client.PublicKey()) if err != nil { return nil, fmt.Errorf("signature verification failed for latest writemarker: %s", err.Error()) } if lpm.PrevWM != nil { - err = lpm.PrevWM.VerifySignature(client.GetClientPublicKey()) + err = lpm.PrevWM.VerifySignature(client.PublicKey()) if err != nil { return nil, fmt.Errorf("signature verification failed for latest writemarker: %s", err.Error()) } @@ -124,7 +124,7 @@ func (rb *RollbackBlobber) processRollback(ctx context.Context, tx string) error wm.AllocationID = rb.lpm.LatestWM.AllocationID wm.Timestamp = rb.lpm.LatestWM.Timestamp wm.BlobberID = rb.lpm.LatestWM.BlobberID - wm.ClientID = client.GetClientID() + wm.ClientID = client.ClientID() wm.Size = -rb.lpm.LatestWM.Size wm.ChainSize = wm.Size + rb.lpm.LatestWM.ChainSize diff --git a/zboxcore/sdk/writemarker_mutex_test.go b/zboxcore/sdk/writemarker_mutex_test.go index 480a47f96..8645810e4 100644 --- a/zboxcore/sdk/writemarker_mutex_test.go +++ b/zboxcore/sdk/writemarker_mutex_test.go @@ -3,6 +3,7 @@ package sdk import ( "bytes" "context" + "github.com/0chain/gosdk/zboxcore/mocks" "io" "io/ioutil" "net/http" @@ -13,7 +14,7 @@ import ( "time" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/mocks" + "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index 63a8d748a..d4cf681a6 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -17,11 +17,10 @@ import ( "time" "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/logger" - "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/zboxcore/client" "github.com/hitenjain14/fasthttp" ) @@ -206,8 +205,8 @@ func NewHTTPRequest(method string, url string, data []byte) (*http.Request, cont } func setClientInfo(req *http.Request) { - req.Header.Set("X-App-Client-ID", client.GetClientID()) - req.Header.Set("X-App-Client-Key", client.GetClientPublicKey()) + req.Header.Set("X-App-Client-ID", client.ClientID()) + req.Header.Set("X-App-Client-Key", client.PublicKey()) } func setClientInfoWithSign(req *http.Request, allocation, baseURL string) error { @@ -230,8 +229,8 @@ func setClientInfoWithSign(req *http.Request, allocation, baseURL string) error } func setFastClientInfoWithSign(req *fasthttp.Request, allocation string) error { - req.Header.Set("X-App-Client-ID", client.GetClientID()) - req.Header.Set("X-App-Client-Key", client.GetClientPublicKey()) + req.Header.Set("X-App-Client-ID", client.ClientID()) + req.Header.Set("X-App-Client-Key", client.PublicKey()) sign, err := client.Sign(encryption.Hash(allocation)) if err != nil { @@ -765,8 +764,8 @@ func NewFastDownloadRequest(baseUrl, allocationID, allocationTx string) (*fastht // } req := fasthttp.AcquireRequest() req.SetRequestURI(u.String()) - req.Header.Set("X-App-Client-ID", client.GetClientID()) - req.Header.Set("X-App-Client-Key", client.GetClientPublicKey()) + req.Header.Set("X-App-Client-ID", client.ClientID()) + req.Header.Set("X-App-Client-Key", client.PublicKey()) req.Header.Set(ALLOCATION_ID_HEADER, allocationID) @@ -913,8 +912,12 @@ func NewRollbackRequest(baseUrl, allocationID string, allocationTx string, body // - params is the query parameters // - handler is the handler function to handle the response func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string, handler SCRestAPIHandler) ([]byte, error) { - numSharders := len(blockchain.GetSharders()) - sharders := blockchain.GetSharders() + nodeClient, err := client.GetNode() + if err != nil { + return nil, err + } + numSharders := len(nodeClient.Sharders().Healthy()) + sharders := nodeClient.Sharders().Healthy() responses := make(map[int]int) mu := &sync.Mutex{} entityResult := make(map[string][]byte) @@ -946,7 +949,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] client := &http.Client{Transport: DefaultTransport} response, err := client.Get(urlObj.String()) if err != nil { - blockchain.Sharders.Fail(sharder) + nodeClient.Sharders().Fail(sharder) return } @@ -954,9 +957,9 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] entityBytes, _ := ioutil.ReadAll(response.Body) mu.Lock() if response.StatusCode > http.StatusBadRequest { - blockchain.Sharders.Fail(sharder) + nodeClient.Sharders().Fail(sharder) } else { - blockchain.Sharders.Success(sharder) + nodeClient.Sharders().Success(sharder) } responses[response.StatusCode]++ if responses[response.StatusCode] > maxCount { @@ -969,7 +972,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] } entityResult[sharder] = entityBytes - blockchain.Sharders.Success(sharder) + nodeClient.Sharders().Success(sharder) mu.Unlock() }(sharder) } diff --git a/zcncore/transaction.go b/zcncore/transaction.go index 0fe33cbcb..62d77c8da 100644 --- a/zcncore/transaction.go +++ b/zcncore/transaction.go @@ -23,6 +23,7 @@ import ( "github.com/0chain/gosdk/core/util" ) +// Provider represents the type of provider. type Provider int const ( @@ -48,6 +49,8 @@ type ConfirmationStatus int const ( Undefined ConfirmationStatus = iota Success + + // ChargeableError is an error that still charges the user for the transaction. ChargeableError ) @@ -63,11 +66,13 @@ type Miner struct { Stat interface{} `json:"stat"` } +// Node represents a node (miner or sharder) in the network. type Node struct { Miner Miner `json:"simple_miner"` StakePool `json:"stake_pool"` } +// MinerSCNodes list of nodes registered to the miner smart contract type MinerSCNodes struct { Nodes []Node `json:"Nodes"` } @@ -108,6 +113,7 @@ type MinerSCDelegatePoolInfo struct { Status string `json:"status"` } +// MinerSCUserPoolsInfo represents the user stake pools information type MinerSCUserPoolsInfo struct { Pools map[string][]*MinerSCDelegatePoolInfo `json:"pools"` } @@ -130,20 +136,6 @@ type TransactionCommon interface { StorageSCCollectReward(providerID string, providerType Provider) error - FinalizeAllocation(allocID string) error - CancelAllocation(allocID string) error - CreateAllocation(car *CreateAllocationRequest, lock uint64) error // - CreateReadPool() error - ReadPoolLock(allocID string, blobberID string, duration int64, lock uint64) error - ReadPoolUnlock() error - StakePoolLock(providerId string, providerType Provider, lock uint64) error - StakePoolUnlock(providerId string, providerType Provider) error - UpdateBlobberSettings(blobber *Blobber) error - UpdateValidatorSettings(validator *Validator) error - UpdateAllocation(allocID string, sizeDiff int64, expirationDiff int64, lock uint64) error - WritePoolLock(allocID string, blobberID string, duration int64, lock uint64) error - WritePoolUnlock(allocID string) error - VestingUpdateConfig(*InputMap) error MinerScUpdateConfig(*InputMap) error MinerScUpdateGlobals(*InputMap) error @@ -194,11 +186,9 @@ type CreateAllocationRequest struct { } type StakePoolSettings struct { - DelegateWallet *string `json:"delegate_wallet,omitempty"` - MinStake *common.Balance `json:"min_stake,omitempty"` - MaxStake *common.Balance `json:"max_stake,omitempty"` - NumDelegates *int `json:"num_delegates,omitempty"` - ServiceCharge *float64 `json:"service_charge,omitempty"` + DelegateWallet *string `json:"delegate_wallet,omitempty"` + NumDelegates *int `json:"num_delegates,omitempty"` + ServiceCharge *float64 `json:"service_charge,omitempty"` } type Terms struct { @@ -207,15 +197,28 @@ type Terms struct { MaxOfferDuration time.Duration `json:"max_offer_duration"` } +// Blobber represents a blobber node. type Blobber struct { - ID common.Key `json:"id"` - BaseURL string `json:"url"` - Terms Terms `json:"terms"` - Capacity common.Size `json:"capacity"` - Allocated common.Size `json:"allocated"` - LastHealthCheck common.Timestamp `json:"last_health_check"` + // ID is the blobber ID. + ID common.Key `json:"id"` + // BaseURL is the blobber's base URL used to access the blobber + BaseURL string `json:"url"` + // Terms of storage service of the blobber (read/write price, max offer duration) + Terms Terms `json:"terms"` + // Capacity is the total capacity of the blobber + Capacity common.Size `json:"capacity"` + // Used is the capacity of the blobber used to create allocations + Allocated common.Size `json:"allocated"` + // LastHealthCheck is the last time the blobber was checked for health + LastHealthCheck common.Timestamp `json:"last_health_check"` + // StakePoolSettings is the settings of the blobber's stake pool StakePoolSettings StakePoolSettings `json:"stake_pool_settings"` - NotAvailable bool `json:"not_available"` + // NotAvailable is true if the blobber is not available + NotAvailable bool `json:"not_available"` + // IsRestricted is true if the blobber is restricted. + // Restricted blobbers needs to be authenticated using AuthTickets in order to be used for allocation creation. + // Check Restricted Blobbers documentation for more details. + IsRestricted bool `json:"is_restricted"` } type Validator struct { @@ -224,37 +227,43 @@ type Validator struct { StakePoolSettings StakePoolSettings `json:"stake_pool_settings"` } +// AddAuthorizerPayload represents the payload for adding an authorizer. type AddAuthorizerPayload struct { PublicKey string `json:"public_key"` URL string `json:"url"` StakePoolSettings AuthorizerStakePoolSettings `json:"stake_pool_settings"` // Used to initially create stake pool } +// DeleteAuthorizerPayload represents the payload for deleting an authorizer. type DeleteAuthorizerPayload struct { ID string `json:"id"` // authorizer ID } +// AuthorizerHealthCheckPayload represents the payload for authorizer health check. type AuthorizerHealthCheckPayload struct { ID string `json:"id"` // authorizer ID } +// AuthorizerStakePoolSettings represents the settings for an authorizer's stake pool. type AuthorizerStakePoolSettings struct { - DelegateWallet string `json:"delegate_wallet"` - MinStake common.Balance `json:"min_stake"` - MaxStake common.Balance `json:"max_stake"` - NumDelegates int `json:"num_delegates"` - ServiceCharge float64 `json:"service_charge"` + DelegateWallet string `json:"delegate_wallet"` + NumDelegates int `json:"num_delegates"` + ServiceCharge float64 `json:"service_charge"` } type AuthorizerConfig struct { Fee common.Balance `json:"fee"` } +// InputMap represents a map of input fields. type InputMap struct { Fields map[string]string `json:"Fields"` } -// NewTransaction allocation new generic transaction object for any operation +// NewTransaction new generic transaction object for any operation +// - cb: callback for transaction state +// - txnFee: Transaction fees (in SAS tokens) +// - nonce: latest nonce of current wallet. please set it with 0 if you don't know the latest value func NewTransaction(cb TransactionCallback, txnFee uint64, nonce int64) (TransactionScheme, error) { err := CheckConfig() if err != nil { @@ -437,269 +446,6 @@ func (t *Transaction) MinerSCKill(providerId string, providerType Provider) erro return err } -func (t *Transaction) StorageSCCollectReward(providerId string, providerType Provider) error { - pr := &scCollectReward{ - ProviderId: providerId, - ProviderType: int(providerType), - } - err := t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_COLLECT_REWARD, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go t.setNonceAndSubmit() - return err -} - -// FinalizeAllocation transaction. -func (t *Transaction) FinalizeAllocation(allocID string) ( - err error) { - - type finiRequest struct { - AllocationID string `json:"allocation_id"` - } - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_FINALIZE_ALLOCATION, &finiRequest{ - AllocationID: allocID, - }, 0) - if err != nil { - logging.Error(err) - return - } - - go func() { t.setNonceAndSubmit() }() - return -} - -// CancelAllocation transaction. -func (t *Transaction) CancelAllocation(allocID string) ( - err error) { - - type cancelRequest struct { - AllocationID string `json:"allocation_id"` - } - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_CANCEL_ALLOCATION, &cancelRequest{ - AllocationID: allocID, - }, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -// CreateAllocation transaction. -func (t *Transaction) CreateAllocation(car *CreateAllocationRequest, - lock uint64) (err error) { - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_CREATE_ALLOCATION, car, lock) - if err != nil { - logging.Error(err) - return - } - - go func() { t.setNonceAndSubmit() }() - return -} - -// CreateReadPool for current user. -func (t *Transaction) CreateReadPool() (err error) { - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_CREATE_READ_POOL, nil, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -// ReadPoolLock locks tokens for current user and given allocation, using given -// duration. If blobberID is not empty, then tokens will be locked for given -// allocation->blobber only. -func (t *Transaction) ReadPoolLock(allocID, blobberID string, duration int64, lock uint64) (err error) { - if lock > math.MaxInt64 { - return errors.New("invalid_lock", "int64 overflow on lock value") - } - - type lockRequest struct { - Duration time.Duration `json:"duration"` - AllocationID string `json:"allocation_id"` - BlobberID string `json:"blobber_id,omitempty"` - } - - var lr lockRequest - lr.Duration = time.Duration(duration) - lr.AllocationID = allocID - lr.BlobberID = blobberID - - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_READ_POOL_LOCK, &lr, lock) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -// ReadPoolUnlock for current user and given pool. -func (t *Transaction) ReadPoolUnlock() (err error) { - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_READ_POOL_UNLOCK, nil, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -// StakePoolLock used to lock tokens in a stake pool of a blobber. -func (t *Transaction) StakePoolLock(providerId string, providerType Provider, lock uint64) error { - - if lock > math.MaxInt64 { - return errors.New("invalid_lock", "int64 overflow on lock value") - } - - type stakePoolRequest struct { - ProviderType Provider `json:"provider_type,omitempty"` - ProviderID string `json:"provider_id,omitempty"` - } - - spr := stakePoolRequest{ - ProviderType: providerType, - ProviderID: providerId, - } - - err := t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_STAKE_POOL_LOCK, &spr, lock) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return nil -} - -// StakePoolUnlock by blobberID and poolID. -func (t *Transaction) StakePoolUnlock(providerId string, providerType Provider) error { - - type stakePoolRequest struct { - ProviderType Provider `json:"provider_type,omitempty"` - ProviderID string `json:"provider_id,omitempty"` - } - - spr := stakePoolRequest{ - ProviderType: providerType, - ProviderID: providerId, - } - - err := t.createSmartContractTxn(StorageSmartContractAddress, transaction.STORAGESC_STAKE_POOL_UNLOCK, &spr, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return nil -} - -// UpdateBlobberSettings update settings of a blobber. -func (t *Transaction) UpdateBlobberSettings(b *Blobber) (err error) { - - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_UPDATE_BLOBBER_SETTINGS, b, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -// UpdateAllocation transaction. -func (t *Transaction) UpdateAllocation(allocID string, sizeDiff int64, - expirationDiff int64, lock uint64) (err error) { - - if lock > math.MaxInt64 { - return errors.New("invalid_lock", "int64 overflow on lock value") - } - - type updateAllocationRequest struct { - ID string `json:"id"` // allocation id - Size int64 `json:"size"` // difference - Expiration int64 `json:"expiration_date"` // difference - } - - var uar updateAllocationRequest - uar.ID = allocID - uar.Size = sizeDiff - uar.Expiration = expirationDiff - - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_UPDATE_ALLOCATION, &uar, lock) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -// WritePoolLock locks tokens for current user and given allocation, using given -// duration. If blobberID is not empty, then tokens will be locked for given -// allocation->blobber only. -func (t *Transaction) WritePoolLock(allocID, blobberID string, duration int64, - lock uint64) (err error) { - - if lock > math.MaxInt64 { - return errors.New("invalid_lock", "int64 overflow on lock value") - } - - type lockRequest struct { - Duration time.Duration `json:"duration"` - AllocationID string `json:"allocation_id"` - BlobberID string `json:"blobber_id,omitempty"` - } - - var lr lockRequest - lr.Duration = time.Duration(duration) - lr.AllocationID = allocID - lr.BlobberID = blobberID - - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_WRITE_POOL_LOCK, &lr, lock) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -// WritePoolUnlock for current user and given pool. -func (t *Transaction) WritePoolUnlock(allocID string) ( - err error) { - - var ur = struct { - AllocationID string `json:"allocation_id"` - }{ - AllocationID: allocID, - } - - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_WRITE_POOL_UNLOCK, &ur, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - func (t *Transaction) VestingUpdateConfig(vscc *InputMap) (err error) { err = t.createSmartContractTxn(VestingSmartContractAddress, @@ -810,7 +556,7 @@ func (t *Transaction) RegisterMultiSig(walletstr string, mswallet string) error if err != nil { return err } - + go func() { t.txn.TransactionType = transaction.TxnTypeSmartContract t.txn.ToClientID = MultiSigSmartContractAddress @@ -917,10 +663,12 @@ type MinerSCDelegatePool struct { Settings StakePoolSettings `json:"settings"` } +// SimpleMiner represents a node in the network, miner or sharder. type SimpleMiner struct { ID string `json:"id"` } +// MinerSCMinerInfo interface for miner/sharder info functions on miner smart contract. type MinerSCMinerInfo struct { SimpleMiner `json:"simple_miner"` MinerSCDelegatePool `json:"stake_pool"` @@ -970,6 +718,7 @@ func (t *Transaction) MinerSCDeleteSharder(info *MinerSCMinerInfo) (err error) { return } +// AuthorizerNode represents an authorizer node in the network type AuthorizerNode struct { ID string `json:"id"` URL string `json:"url"` @@ -1145,6 +894,9 @@ func GetLatestFinalized(ctx context.Context, numSharders int) (b *block.Header, return } +// GetLatestFinalizedMagicBlock gets latest finalized magic block +// - numSharders: number of sharders +// - timeout: request timeout func GetLatestFinalizedMagicBlock(ctx context.Context, numSharders int) (m *block.MagicBlock, err error) { clientNode, err := client.GetNode() if err != nil { @@ -1405,131 +1157,6 @@ func (nc *NonceCache) Evict(clientId string) { delete(nc.cache, clientId) } -//Deprecated: To initialize SDK, Use conf.Config and client.Init() in core package -func WithEthereumNode(uri string) func(c *ChainConfig) error { - return func(c *ChainConfig) error { - c.EthNode = uri - return nil - } -} - -//Deprecated: To initialize SDK, Use conf.Config and client.Init() in core package -func WithChainID(id string) func(c *ChainConfig) error { - return func(c *ChainConfig) error { - c.ChainID = id - return nil - } -} - -//Deprecated: To initialize SDK, Use conf.Config and client.Init() in core package -func WithMinSubmit(m int) func(c *ChainConfig) error { - return func(c *ChainConfig) error { - c.MinSubmit = m - return nil - } -} - -//Deprecated: To initialize SDK, Use conf.Config and client.Init() in core package -func WithMinConfirmation(m int) func(c *ChainConfig) error { - return func(c *ChainConfig) error { - c.MinConfirmation = m - return nil - } -} - -//Deprecated: To initialize SDK, Use conf.Config and client.Init() in core package -func WithConfirmationChainLength(m int) func(c *ChainConfig) error { - return func(c *ChainConfig) error { - c.ConfirmationChainLength = m - return nil - } -} - -//Deprecated: To initialize SDK, Use conf.Config and client.Init() in core package -func WithSharderConsensous(m int) func(c *ChainConfig) error { - return func(c *ChainConfig) error { - c.SharderConsensous = m - return nil - } -} - -// UpdateValidatorSettings update settings of a validator. -func (t *Transaction) UpdateValidatorSettings(v *Validator) (err error) { - - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_UPDATE_VALIDATOR_SETTINGS, v, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -type VestingClientList struct { - Pools []common.Key `json:"pools"` -} - -func GetVestingClientList(clientID string, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - if clientID == "" { - clientID = client.Wallet().ClientID // if not blank - } - go GetInfoFromSharders(WithParams(GET_VESTING_CLIENT_POOLS, Params{ - "client_id": clientID, - }), 0, cb) - return -} - -type VestingDestInfo struct { - ID common.Key `json:"id"` // identifier - Wanted common.Balance `json:"wanted"` // wanted amount for entire period - Earned common.Balance `json:"earned"` // can unlock - Vested common.Balance `json:"vested"` // already vested - Last common.Timestamp `json:"last"` // last time unlocked -} - -type VestingPoolInfo struct { - ID common.Key `json:"pool_id"` // pool ID - Balance common.Balance `json:"balance"` // real pool balance - Left common.Balance `json:"left"` // owner can unlock - Description string `json:"description"` // description - StartTime common.Timestamp `json:"start_time"` // from - ExpireAt common.Timestamp `json:"expire_at"` // until - Destinations []*VestingDestInfo `json:"destinations"` // receivers - ClientID common.Key `json:"client_id"` // owner -} - -func GetVestingPoolInfo(poolID string, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - GetInfoFromSharders(WithParams(GET_VESTING_POOL_INFO, Params{ - "pool_id": poolID, - }), 0, cb) - return -} - -func GetVestingSCConfig(cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - go GetInfoFromSharders(GET_VESTING_CONFIG, 0, cb) - return -} - -// faucet - -func GetFaucetSCConfig(cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - go GetInfoFromSharders(GET_FAUCETSC_CONFIG, 0, cb) - return -} - func (t *Transaction) ZCNSCAddAuthorizer(ip *AddAuthorizerPayload) (err error) { err = t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_ADD_AUTHORIZER, ip, 0) if err != nil { diff --git a/zcncore/transactionauth.go b/zcncore/transactionauth.go index 1561678a2..4d44cfea3 100644 --- a/zcncore/transactionauth.go +++ b/zcncore/transactionauth.go @@ -6,11 +6,9 @@ package zcncore import ( "encoding/json" "fmt" - "math" - "time" - "github.com/0chain/errors" "github.com/0chain/gosdk/core/transaction" + "math" ) func (ta *TransactionWithAuth) ExecuteSmartContract(address, methodName string, @@ -87,276 +85,6 @@ func (ta *TransactionWithAuth) MinerSCUnlock(providerId string, providerType Pro return err } -// FinalizeAllocation transaction. -func (ta *TransactionWithAuth) FinalizeAllocation(allocID string) ( - err error) { - - type finiRequest struct { - AllocationID string `json:"allocation_id"` - } - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_FINALIZE_ALLOCATION, &finiRequest{ - AllocationID: allocID, - }, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -// CancelAllocation transaction. -func (ta *TransactionWithAuth) CancelAllocation(allocID string) ( - err error) { - - type cancelRequest struct { - AllocationID string `json:"allocation_id"` - } - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_CANCEL_ALLOCATION, &cancelRequest{ - AllocationID: allocID, - }, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -// CreateAllocation transaction. -func (ta *TransactionWithAuth) CreateAllocation(car *CreateAllocationRequest, - lock uint64) (err error) { - - if lock > math.MaxInt64 { - return errors.New("invalid_lock", "int64 overflow on lock value") - } - - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_CREATE_ALLOCATION, car, lock) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -// CreateReadPool for current user. -func (ta *TransactionWithAuth) CreateReadPool() (err error) { - - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_CREATE_READ_POOL, nil, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -// ReadPoolLock locks tokens for current user and given allocation, using given -// duration. If blobberID is not empty, then tokens will be locked for given -// allocation->blobber only. -func (ta *TransactionWithAuth) ReadPoolLock(allocID, blobberID string, - duration int64, lock uint64) (err error) { - - if lock > math.MaxInt64 { - return errors.New("invalid_lock", "int64 overflow on lock value") - } - - type lockRequest struct { - Duration time.Duration `json:"duration"` - AllocationID string `json:"allocation_id"` - BlobberID string `json:"blobber_id,omitempty"` - } - - var lr lockRequest - lr.Duration = time.Duration(duration) - lr.AllocationID = allocID - lr.BlobberID = blobberID - - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_READ_POOL_LOCK, &lr, lock) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -// ReadPoolUnlock for current user and given pool. -func (ta *TransactionWithAuth) ReadPoolUnlock() ( - err error) { - - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_READ_POOL_UNLOCK, nil, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -// StakePoolLock used to lock tokens in a stake pool of a blobber. -func (ta *TransactionWithAuth) StakePoolLock(providerId string, providerType Provider, lock uint64) error { - - if lock > math.MaxInt64 { - return errors.New("invalid_lock", "int64 overflow on lock value") - } - - type stakePoolRequest struct { - ProviderType Provider `json:"provider_type,omitempty"` - ProviderID string `json:"provider_id,omitempty"` - } - - spr := stakePoolRequest{ - ProviderType: providerType, - ProviderID: providerId, - } - - err := ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_STAKE_POOL_LOCK, &spr, lock) - if err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return nil -} - -// StakePoolUnlock by blobberID -func (ta *TransactionWithAuth) StakePoolUnlock(providerId string, providerType Provider) error { - - type stakePoolRequest struct { - ProviderType Provider `json:"provider_type,omitempty"` - ProviderID string `json:"provider_id,omitempty"` - } - - spr := stakePoolRequest{ - ProviderType: providerType, - ProviderID: providerId, - } - - err := ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_STAKE_POOL_UNLOCK, &spr, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return nil -} - -// UpdateBlobberSettings update settings of a blobber. -func (ta *TransactionWithAuth) UpdateBlobberSettings(blob *Blobber) ( - err error) { - - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_UPDATE_BLOBBER_SETTINGS, blob, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -// UpdateValidatorSettings update settings of a validator. -func (ta *TransactionWithAuth) UpdateValidatorSettings(v *Validator) ( - err error) { - - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_UPDATE_VALIDATOR_SETTINGS, v, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -// UpdateAllocation transaction. -func (ta *TransactionWithAuth) UpdateAllocation(allocID string, sizeDiff int64, - expirationDiff int64, lock uint64) (err error) { - - if lock > math.MaxInt64 { - return errors.New("invalid_lock", "int64 overflow on lock value") - } - - type updateAllocationRequest struct { - ID string `json:"id"` // allocation id - Size int64 `json:"size"` // difference - Expiration int64 `json:"expiration_date"` // difference - } - - var uar updateAllocationRequest - uar.ID = allocID - uar.Size = sizeDiff - uar.Expiration = expirationDiff - - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_UPDATE_ALLOCATION, &uar, lock) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -// WritePoolLock locks tokens for current user and given allocation, using given -// duration. If blobberID is not empty, then tokens will be locked for given -// allocation->blobber only. -func (ta *TransactionWithAuth) WritePoolLock(allocID, blobberID string, - duration int64, lock uint64) (err error) { - - if lock > math.MaxInt64 { - return errors.New("invalid_lock", "int64 overflow on lock value") - } - - type lockRequest struct { - Duration time.Duration `json:"duration"` - AllocationID string `json:"allocation_id"` - BlobberID string `json:"blobber_id,omitempty"` - } - - var lr lockRequest - lr.Duration = time.Duration(duration) - lr.AllocationID = allocID - lr.BlobberID = blobberID - - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_WRITE_POOL_LOCK, &lr, lock) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -// WritePoolUnlock for current user and given pool. -func (ta *TransactionWithAuth) WritePoolUnlock(allocID string) (err error) { - type unlockRequest struct { - AllocationID string `json:"allocation_id"` - } - - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_WRITE_POOL_UNLOCK, &unlockRequest{ - AllocationID: allocID, - }, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - func (ta *TransactionWithAuth) MinerSCCollectReward(providerId string, providerType Provider) error { pr := &scCollectReward{ ProviderId: providerId, diff --git a/zcncore/transactionauth_mobile.go b/zcncore/transactionauth_mobile.go index 119e38bbb..e4a0d93b9 100644 --- a/zcncore/transactionauth_mobile.go +++ b/zcncore/transactionauth_mobile.go @@ -5,8 +5,6 @@ package zcncore import ( "encoding/json" - "time" - "github.com/0chain/errors" "github.com/0chain/gosdk/core/transaction" ) @@ -99,236 +97,6 @@ func (ta *TransactionWithAuth) MinerSCUnlock(providerId string, providerType int return err } -// FinalizeAllocation transaction. -func (ta *TransactionWithAuth) FinalizeAllocation(allocID string) error { - type finiRequest struct { - AllocationID string `json:"allocation_id"` - } - err := ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_FINALIZE_ALLOCATION, &finiRequest{ - AllocationID: allocID, - }, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return nil -} - -// CancelAllocation transaction. -func (ta *TransactionWithAuth) CancelAllocation(allocID string) error { - type cancelRequest struct { - AllocationID string `json:"allocation_id"` - } - err := ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_CANCEL_ALLOCATION, &cancelRequest{ - AllocationID: allocID, - }, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return nil -} - -// CreateAllocation transaction. -func (ta *TransactionWithAuth) CreateAllocation(car *CreateAllocationRequest, - lock string) error { - lv, err := parseCoinStr(lock) - if err != nil { - return err - } - - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_CREATE_ALLOCATION, car, lv) - if err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return nil -} - -// CreateReadPool for current user. -func (ta *TransactionWithAuth) CreateReadPool() error { - if err := ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_CREATE_READ_POOL, nil, 0); err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return nil -} - -// ReadPoolLock locks tokens for current user and given allocation, using given -// duration. If blobberID is not empty, then tokens will be locked for given -// allocation->blobber only. -func (ta *TransactionWithAuth) ReadPoolLock(allocID, blobberID string, - duration int64, lock string) error { - lv, err := parseCoinStr(lock) - if err != nil { - return err - } - - type lockRequest struct { - Duration time.Duration `json:"duration"` - AllocationID string `json:"allocation_id"` - BlobberID string `json:"blobber_id,omitempty"` - } - - var lr lockRequest - lr.Duration = time.Duration(duration) - lr.AllocationID = allocID - lr.BlobberID = blobberID - - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_READ_POOL_LOCK, &lr, lv) - if err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return nil -} - -// ReadPoolUnlock for current user and given pool. -func (ta *TransactionWithAuth) ReadPoolUnlock() error { - if err := ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_READ_POOL_UNLOCK, nil, 0); err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return nil -} - -// StakePoolLock used to lock tokens in a stake pool of a blobber. -func (ta *TransactionWithAuth) StakePoolLock(providerId string, providerType int, - lock string) error { - lv, err := parseCoinStr(lock) - if err != nil { - return err - } - - type stakePoolRequest struct { - ProviderType int `json:"provider_type,omitempty"` - ProviderID string `json:"provider_id,omitempty"` - } - - spr := stakePoolRequest{ - ProviderType: providerType, - ProviderID: providerId, - } - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_STAKE_POOL_LOCK, &spr, lv) - if err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return nil -} - -// StakePoolUnlock by blobberID -func (ta *TransactionWithAuth) StakePoolUnlock(providerId string, providerType int) error { - spr := stakePoolRequest{ - ProviderType: providerType, - ProviderID: providerId, - } - - if err := ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_STAKE_POOL_UNLOCK, &spr, 0); err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return nil -} - -// UpdateBlobberSettings update settings of a blobber. -func (ta *TransactionWithAuth) UpdateBlobberSettings(blob Blobber) error { - if err := ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_UPDATE_BLOBBER_SETTINGS, blob, 0); err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return nil -} - -// UpdateAllocation transaction. -func (ta *TransactionWithAuth) UpdateAllocation(allocID string, sizeDiff int64, - expirationDiff int64, lock string) error { - lv, err := parseCoinStr(lock) - if err != nil { - return err - } - - type updateAllocationRequest struct { - ID string `json:"id"` // allocation id - Size int64 `json:"size"` // difference - Expiration int64 `json:"expiration_date"` // difference - } - - var uar updateAllocationRequest - uar.ID = allocID - uar.Size = sizeDiff - uar.Expiration = expirationDiff - - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_UPDATE_ALLOCATION, &uar, lv) - if err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return nil -} - -// WritePoolLock locks tokens for current user and given allocation, using given -// duration. If blobberID is not empty, then tokens will be locked for given -// allocation->blobber only. -func (ta *TransactionWithAuth) WritePoolLock(allocID, lock string) error { - lv, err := parseCoinStr(lock) - if err != nil { - return err - } - - var lr = struct { - AllocationID string `json:"allocation_id"` - }{ - AllocationID: allocID, - } - - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_WRITE_POOL_LOCK, &lr, lv) - if err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return nil -} - -// WritePoolUnlock for current user and given pool. -func (ta *TransactionWithAuth) WritePoolUnlock(allocID string) error { - var ur = struct { - AllocationID string `json:"allocation_id"` - }{ - AllocationID: allocID, - } - - if err := ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_WRITE_POOL_UNLOCK, &ur, 0); err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return nil -} - func (ta *TransactionWithAuth) MinerSCCollectReward(providerId string, providerType int) error { pr := &scCollectReward{ ProviderId: providerId, diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 537413e86..df425fead 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -229,7 +229,7 @@ func init() { logging.Init(defaultLogLevel, "0chain-core-sdk") } -//Deprecated: Use client.GetNode().GetStableMiners() +// Deprecated: Use client.GetNode().GetStableMiners() func GetStableMiners() []string { clientNode, err := client.GetNode() if err != nil { @@ -238,7 +238,7 @@ func GetStableMiners() []string { return clientNode.GetStableMiners() } -//Deprecated: Use client.GetNode().ResetStableMiners() +// Deprecated: Use client.GetNode().ResetStableMiners() func ResetStableMiners() { clientNode, err := client.GetNode() if err != nil { @@ -274,7 +274,7 @@ func CheckConfig() error { return nil } -//Deprecated: Use client.GetNode().GetMinShardersVerify() after Init call +// Deprecated: Use client.GetNode().GetMinShardersVerify() after Init call func GetMinShardersVerify() int { clientNode, err := client.GetNode() if err != nil { @@ -309,7 +309,7 @@ func SetLogFile(logFile string, verbose bool) { logging.Info("******* Wallet SDK Version:", version.VERSIONSTR, " ******* (SetLogFile)") } -//Deprecated: Use client.Init() in core/client package +// Deprecated: Use client.Init() in core/client package // Init initialize the SDK with miner, sharder and signature scheme provided in configuration provided in JSON format // # Inputs // - chainConfigJSON: json format of zcn config @@ -345,7 +345,7 @@ func InitSignatureScheme(scheme string) { if scheme != constants.BLS0CHAIN.String() && scheme != constants.ED25519.String() { panic("invalid/unsupported signature scheme") } - signatureScheme = scheme + signatureScheme = scheme } // CreateWalletOffline creates the wallet for the config signature scheme. @@ -454,7 +454,7 @@ func GetClientDetails(clientID string) (*GetClientResponse, error) { return &clientDetails, nil } -//Deprecated: Use zcncrypto.IsMnemonicValid() +// Deprecated: Use zcncrypto.IsMnemonicValid() // IsMnemonicValid is an utility function to check the mnemonic valid // // # Inputs @@ -463,22 +463,7 @@ func IsMnemonicValid(mnemonic string) bool { return zcncrypto.IsMnemonicValid(mnemonic) } -//Deprecated: Use client.SetWallet() and client.SetSplitKeyWallet() to set wallet and splitKeyWallet respectively -// SetWallet should be set before any transaction or client specific APIs -// splitKeyWallet parameter is valid only if SignatureScheme is "BLS0Chain" -func SetWallet(w zcncrypto.Wallet, splitKeyWallet bool) error { - err := client.SetWallet(w) - if err != nil { - return err - } - err = client.SetSplitKeyWallet(splitKeyWallet) - if err != nil { - return err - } - return nil -} - -//Deprecated: Use client.Wallet() in core/client package +// Deprecated: Use client.Wallet() in core/client package func GetWalletRaw() zcncrypto.Wallet { return *client.Wallet() } @@ -507,7 +492,9 @@ func SetWalletInfo(jsonWallet string, splitKeyWallet bool) error { if err != nil { return errors.New("invalid jsonWallet: " + err.Error()) } - return SetWallet(wallet, splitKeyWallet) + + client.SetWallet(wallet) + return client.SetSplitKeyWallet(splitKeyWallet) } // SetAuthUrl will be called by app to set zauth URL to SDK. From 4f53f054dd58a1deb314e9c1ed87a2b6a59d80b7 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 23 Aug 2024 10:02:52 +0530 Subject: [PATCH 018/319] Fix --- zboxcore/sdk/allocation_file_delete_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zboxcore/sdk/allocation_file_delete_test.go b/zboxcore/sdk/allocation_file_delete_test.go index 1b80f36fc..e9be2c349 100644 --- a/zboxcore/sdk/allocation_file_delete_test.go +++ b/zboxcore/sdk/allocation_file_delete_test.go @@ -13,7 +13,7 @@ import ( "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zboxcore/blockchain" "github.com/0chain/gosdk/zboxcore/fileref" - + "github.com/0chain/gosdk/zboxcore/mocks" "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/stretchr/testify/require" ) From 044abac2bf668d853556bc37ebcc29929e30603d Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 23 Aug 2024 11:10:00 +0530 Subject: [PATCH 019/319] Debug --- core/client/init_node.go | 26 ++-- core/transaction/entity.go | 124 +++++++++++++++++ mobilesdk/sdk/sdk.go | 9 +- mobilesdk/zbox/util.go | 104 -------------- wasmsdk/cache.go | 2 +- wasmsdk/proxy.go | 2 +- zboxcore/sdk/sdk.go | 148 ++------------------ zcncore/sample/0Wallet.go | 12 +- zcncore/transaction.go | 2 - zcncore/transaction_base.go | 29 ++-- zcncore/transaction_mobile.go | 249 +--------------------------------- zcncore/wallet_base.go | 120 +++++++--------- 12 files changed, 234 insertions(+), 593 deletions(-) diff --git a/core/client/init_node.go b/core/client/init_node.go index 60e138dae..e782cb6b8 100644 --- a/core/client/init_node.go +++ b/core/client/init_node.go @@ -27,19 +27,19 @@ func init() { logging.Init(logger.DEBUG, "0chain-core") } -// Maintains central states of SDK (client's context, network). +// Node Maintains central states of SDK (client's context, network). // Initialized through [Init] function. // Use client.GetNode() to get its instance after Init is called. type Node struct { stableMiners []string sharders *node.NodeHolder - network *conf.Network - clientCtx context.Context + network *conf.Network + clientCtx context.Context networkGuard sync.RWMutex } -// Returns stable miner urls. +// GetStableMiners Returns stable miner urls. // Length of stable miners is depedent on config's MinSubmit and number of miners in network. func (n *Node) GetStableMiners() []string { n.networkGuard.RLock() @@ -57,7 +57,7 @@ func (n *Node) ResetStableMiners() { n.stableMiners = util.GetRandom(n.network.Miners, reqMiners) } -// Returns minimum sharders used for verification +// GetMinShardersVerify Returns minimum sharders used for verification func (n *Node) GetMinShardersVerify() int { n.networkGuard.RLock() defer n.networkGuard.RUnlock() @@ -67,21 +67,21 @@ func (n *Node) GetMinShardersVerify() int { return minSharders } -// Returns NodeHolder instance +// Sharders Returns NodeHolder instance func (n *Node) Sharders() *node.NodeHolder { n.networkGuard.RLock() defer n.networkGuard.RUnlock() return n.sharders } -// Returns network configuration +// Network Returns network configuration func (n *Node) Network() *conf.Network { n.networkGuard.RLock() defer n.networkGuard.RUnlock() return n.network } -// Gets network details and return it as second value. +// ShouldUpdateNetwork Gets network details and return it as second value. // First value is true iff current network details doesn't match existing network details. // Use node.UpdateNetwork() method to set the new network. func (n *Node) ShouldUpdateNetwork() (bool, *conf.Network, error) { @@ -102,7 +102,7 @@ func (n *Node) ShouldUpdateNetwork() (bool, *conf.Network, error) { return true, network, nil } -// Use node.UpdateNetwork() method to set the new network. +// UpdateNetwork Use node.UpdateNetwork() method to set the new network. func (n *Node) UpdateNetwork(network *conf.Network) error { n.networkGuard.Lock() defer n.networkGuard.Unlock() @@ -116,7 +116,7 @@ func (n *Node) UpdateNetwork(network *conf.Network) error { return nil } -// Initializes SDK. +// Init Initializes SDK. func Init(ctx context.Context, cfg conf.Config) error { // validate err := validate(&cfg) @@ -138,8 +138,8 @@ func Init(ctx context.Context, cfg conf.Config) error { nodeClient = &Node{ stableMiners: util.GetRandom(network.Miners, reqMiners), sharders: sharders, - network: network, - clientCtx: ctx, + network: network, + clientCtx: ctx, } //init packages @@ -172,7 +172,7 @@ func Init(ctx context.Context, cfg conf.Config) error { return nil } -// Returns Node instance. If this function is called before Init(), error is returned. +// GetNode Returns Node instance. If this function is called before Init(), error is returned. func GetNode() (*Node, error) { if nodeClient != nil { return nodeClient, nil diff --git a/core/transaction/entity.go b/core/transaction/entity.go index b2586a177..3dd2275ad 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -4,6 +4,12 @@ package transaction import ( "encoding/json" "fmt" + "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/conf" + "github.com/0chain/gosdk/core/node" + "github.com/0chain/gosdk/core/sys" + l "github.com/0chain/gosdk/zboxcore/logger" + "go.uber.org/zap" "net/http" "strings" "sync" @@ -481,3 +487,121 @@ 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) ( + hash, out string, nonce int64, txn *Transaction, err error) { + return SmartContractTxnValue(scAddress, sn, 0) +} + +func SmartContractTxnValue(scAddress string, sn SmartContractTxnData, value uint64) ( + hash, out string, nonce int64, txn *Transaction, err error) { + + return SmartContractTxnValueFeeWithRetry(scAddress, sn, value, client.TxnFee()) +} + +func SmartContractTxnValueFeeWithRetry(scAddress string, sn SmartContractTxnData, + value, fee uint64) (hash, out string, nonce int64, t *Transaction, err error) { + hash, out, nonce, t, err = SmartContractTxnValueFee(scAddress, sn, value, fee) + + if err != nil && strings.Contains(err.Error(), "invalid transaction nonce") { + return SmartContractTxnValueFee(scAddress, sn, value, fee) + } + return +} + +func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, + value, fee uint64) (hash, out string, nonce int64, t *Transaction, err error) { + + var requestBytes []byte + if requestBytes, err = json.Marshal(sn); err != nil { + return + } + + cfg, err := conf.GetClientConfig() + if err != nil { + return + } + + nodeClient, err := client.GetNode() + if err != nil { + return + } + + txn := NewTransactionEntity(client.ClientID(), + cfg.ChainID, client.PublicKey(), nonce) + + txn.TransactionData = string(requestBytes) + txn.ToClientID = scAddress + txn.Value = value + txn.TransactionFee = fee + txn.TransactionType = TxnTypeSmartContract + + // adjust fees if not set + if fee == 0 { + fee, err = EstimateFee(txn, nodeClient.Network().Miners, 0.2) + if err != nil { + l.Logger.Error("failed to estimate txn fee", + zap.Error(err), + zap.Any("txn", txn)) + return + } + txn.TransactionFee = fee + } + + if txn.TransactionNonce == 0 { + txn.TransactionNonce = node.Cache.GetNextNonce(txn.ClientID) + } + + if err = txn.ComputeHashAndSign(client.Sign); err != nil { + return + } + + msg := fmt.Sprintf("executing transaction '%s' with hash %s ", sn.Name, txn.Hash) + l.Logger.Info(msg) + l.Logger.Info("estimated txn fee: ", txn.TransactionFee) + + err = SendTransactionSync(txn, nodeClient.GetStableMiners()) + if err != nil { + l.Logger.Info("transaction submission failed", zap.Error(err)) + node.Cache.Evict(txn.ClientID) + nodeClient.ResetStableMiners() + return + } + + var ( + querySleepTime = time.Duration(cfg.QuerySleepTime) * time.Second + retries = 0 + ) + + sys.Sleep(querySleepTime) + + for retries < cfg.MaxTxnQuery { + t, err = VerifyTransaction(txn.Hash, nodeClient.Sharders().Healthy()) + if err == nil { + break + } + retries++ + sys.Sleep(querySleepTime) + } + + if err != nil { + l.Logger.Error("Error verifying the transaction", err.Error(), txn.Hash) + node.Cache.Evict(txn.ClientID) + return + } + + if t == nil { + return "", "", 0, txn, errors.New("transaction_validation_failed", + "Failed to get the transaction confirmation") + } + + if t.Status == TxnFail { + return t.Hash, t.TransactionOutput, 0, t, errors.New("", t.TransactionOutput) + } + + if t.Status == TxnChargeableError { + return t.Hash, t.TransactionOutput, t.TransactionNonce, t, errors.New("", t.TransactionOutput) + } + + return t.Hash, t.TransactionOutput, t.TransactionNonce, t, nil +} diff --git a/mobilesdk/sdk/sdk.go b/mobilesdk/sdk/sdk.go index f6fb2d53f..ef164bf7c 100644 --- a/mobilesdk/sdk/sdk.go +++ b/mobilesdk/sdk/sdk.go @@ -14,9 +14,9 @@ import ( "github.com/0chain/gosdk/core/sys" "github.com/pkg/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/util" "github.com/0chain/gosdk/core/version" - "github.com/0chain/gosdk/core/client" l "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/sdk" @@ -66,7 +66,12 @@ func SetLogLevel(logLevel int) { // Init init the sdk with chain config // - chainConfigJson: chain config json string func Init(chainConfigJson string) error { - return zcncore.Init(chainConfigJson) + cfg := conf.Config{} + err := json.Unmarshal([]byte(chainConfigJSON), &cfg) + if err != nil { + return err + } + return client.Init(context.Background(), cfg) } // InitStorageSDK init storage sdk from config diff --git a/mobilesdk/zbox/util.go b/mobilesdk/zbox/util.go index 50dbe855a..52ae76bf3 100644 --- a/mobilesdk/zbox/util.go +++ b/mobilesdk/zbox/util.go @@ -1,114 +1,10 @@ package zbox import ( - "encoding/hex" - "encoding/json" - "fmt" "regexp" "strconv" - - "github.com/0chain/gosdk/core/sys" - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/zboxcore/sdk" - "github.com/0chain/gosdk/zboxcore/zboxutil" - "github.com/0chain/gosdk/zcncore" ) -// GetClientEncryptedPublicKey - getting client encrypted pub key -func GetClientEncryptedPublicKey() (string, error) { - return sdk.GetClientEncryptedPublicKey() -} - -func TokensToEth(tokens int64) string { - return fmt.Sprintf("%f", zcncore.TokensToEth(tokens)) -} - -func GEthToTokens(tokens int64) string { - return fmt.Sprintf("%f", zcncore.GTokensToEth(tokens)) -} - -// ConvertZcnTokenToETH - converting Zcn tokens to Eth -func ConvertZcnTokenToETH(token float64) (string, error) { - res, err := zcncore.ConvertZcnTokenToETH(token) - return fmt.Sprintf("%f", res), err -} - -// SuggestEthGasPrice - return back suggested price for gas -func SuggestEthGasPrice() (string, error) { - res, err := zcncore.SuggestEthGasPrice() - return strconv.FormatInt(res, 10), err -} - -// Encrypt - encrypting text with key -func Encrypt(key, text string) (string, error) { - keyBytes := []byte(key) - textBytes := []byte(text) - response, err := zboxutil.Encrypt(keyBytes, textBytes) - if err != nil { - return "", err - } - return hex.EncodeToString(response), nil -} - -// Decrypt - decrypting text with key -func Decrypt(key, text string) (string, error) { - keyBytes := []byte(key) - textBytes, _ := hex.DecodeString(text) - response, err := zboxutil.Decrypt(keyBytes, textBytes) - if err != nil { - return "", err - } - return string(response), nil -} - -// GetNetwork - get current network -func GetNetwork() (string, error) { - type Network struct { - Miners []string `json:"miners"` - Sharders []string `json:"sharders"` - } - nodeClient, err := client.GetNode() - if err != nil { - return "", err - } - networkDetails := &Network { - Miners: nodeClient.Network().Miners, - Sharders: nodeClient.Network().Sharders, - } - networkDetailsBytes, err := json.Marshal(networkDetails) - if err != nil { - return "", err - } - return string(networkDetailsBytes), nil -} - -// GetBlobbers - get list of blobbers -func GetBlobbers() (string, error) { - blobbers, err := sdk.GetBlobbers(true, false) - if err != nil { - return "", err - } - - blobbersBytes, err := json.Marshal(blobbers) - if err != nil { - return "", err - } - return string(blobbersBytes), nil -} - -// Sign - sign hash -func Sign(hash string) (string, error) { - if len(hash) == 0 { - return "", fmt.Errorf("null sign") - } - return client.Sign(hash) -} - -// VerifySignatxure - verify message with signature -func VerifySignature(signature string, msg string) (bool, error) { - return sys.Verify(signature, msg) -} - func GetNumber(value string) int { re := regexp.MustCompile("[0-9]+") submatchall := re.FindAllString(value, -1) diff --git a/wasmsdk/cache.go b/wasmsdk/cache.go index ed00387a8..f7fdae5b0 100644 --- a/wasmsdk/cache.go +++ b/wasmsdk/cache.go @@ -8,8 +8,8 @@ import ( "errors" "time" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/wasmsdk/jsbridge" - "github.com/0chain/gosdk/zboxcore/client" "github.com/0chain/gosdk/zboxcore/sdk" lru "github.com/hashicorp/golang-lru/v2" ) diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index fac087a50..5fe581f59 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -11,11 +11,11 @@ import ( "sync" "time" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/core/version" "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/wasmsdk/jsbridge" - "github.com/0chain/gosdk/zboxcore/client" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/0chain/gosdk/zcncore" diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index a62c6f00f..b73d017f8 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -5,22 +5,16 @@ import ( "encoding/base64" "encoding/json" "fmt" - "io/ioutil" - "math" - "net/http" - "strconv" - "strings" - "time" - "github.com/0chain/common/core/currency" "github.com/0chain/errors" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/logger" - "github.com/0chain/gosdk/core/node" - "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/core/zcncrypto" - "go.uber.org/zap" "gopkg.in/natefinch/lumberjack.v2" + "io/ioutil" + "math" + "net/http" + "strconv" "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" @@ -218,7 +212,7 @@ func ReadPoolLock(tokens, fee uint64) (hash string, nonce int64, err error) { Name: transaction.STORAGESC_READ_POOL_LOCK, InputArgs: nil, } - hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, tokens, fee) + hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, tokens, fee) return } @@ -233,7 +227,7 @@ func ReadPoolUnlock(fee uint64) (hash string, nonce int64, err error) { Name: transaction.STORAGESC_READ_POOL_UNLOCK, InputArgs: nil, } - hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee) + hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee) return } @@ -403,7 +397,7 @@ func StakePoolLock(providerType ProviderType, providerID string, value, fee uint return "", 0, errors.Newf("stake_pool_lock", "unsupported provider type: %v", providerType) } - hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(scAddress, sn, value, fee) + hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, value, fee) return } @@ -463,7 +457,7 @@ func StakePoolUnlock(providerType ProviderType, providerID string, fee uint64) ( } var out string - if _, out, nonce, _, err = smartContractTxnValueFeeWithRetry(scAddress, sn, 0, fee); err != nil { + if _, out, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, 0, fee); err != nil { return // an error } @@ -500,7 +494,7 @@ func WritePoolLock(allocID string, tokens, fee uint64) (hash string, nonce int64 InputArgs: &req, } - hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, tokens, fee) + hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, tokens, fee) return } @@ -523,7 +517,7 @@ func WritePoolUnlock(allocID string, fee uint64) (hash string, nonce int64, err Name: transaction.STORAGESC_WRITE_POOL_UNLOCK, InputArgs: &req, } - hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee) + hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee) return } @@ -1574,7 +1568,7 @@ func CollectRewards(providerId string, providerType ProviderType) (string, int64 return "", 0, fmt.Errorf("collect rewards provider type %v not implimented", providerType) } - hash, _, n, _, err := smartContractTxn(scAddress, sn) + hash, _, n, _, err := transaction.SmartContractTxn(scAddress, sn) return hash, n, err } @@ -1673,11 +1667,6 @@ func ResetAllocationStats(allocationId string) (string, int64, error) { return hash, n, err } -func smartContractTxn(scAddress string, sn transaction.SmartContractTxnData) ( - hash, out string, nonce int64, txn *transaction.Transaction, err error) { - return smartContractTxnValue(scAddress, sn, 0) -} - func StorageSmartContractTxn(sn transaction.SmartContractTxnData) ( hash, out string, nonce int64, txn *transaction.Transaction, err error) { @@ -1690,124 +1679,11 @@ func storageSmartContractTxn(sn transaction.SmartContractTxnData) ( return storageSmartContractTxnValue(sn, 0) } -func smartContractTxnValue(scAddress string, sn transaction.SmartContractTxnData, value uint64) ( - hash, out string, nonce int64, txn *transaction.Transaction, err error) { - - return smartContractTxnValueFeeWithRetry(scAddress, sn, value, client.TxnFee()) -} - func storageSmartContractTxnValue(sn transaction.SmartContractTxnData, value uint64) ( hash, out string, nonce int64, txn *transaction.Transaction, err error) { // Fee is set during sdk initialization. - return smartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, value, client.TxnFee()) -} - -func smartContractTxnValueFeeWithRetry(scAddress string, sn transaction.SmartContractTxnData, - value, fee uint64) (hash, out string, nonce int64, t *transaction.Transaction, err error) { - hash, out, nonce, t, err = smartContractTxnValueFee(scAddress, sn, value, fee) - - if err != nil && strings.Contains(err.Error(), "invalid transaction nonce") { - return smartContractTxnValueFee(scAddress, sn, value, fee) - } - return -} - -func smartContractTxnValueFee(scAddress string, sn transaction.SmartContractTxnData, - value, fee uint64) (hash, out string, nonce int64, t *transaction.Transaction, err error) { - - var requestBytes []byte - if requestBytes, err = json.Marshal(sn); err != nil { - return - } - - cfg, err := conf.GetClientConfig() - if err != nil { - return - } - - nodeClient, err := client.GetNode() - if err != nil { - return - } - - txn := transaction.NewTransactionEntity(client.ClientID(), - cfg.ChainID, client.PublicKey(), nonce) - - txn.TransactionData = string(requestBytes) - txn.ToClientID = scAddress - txn.Value = value - txn.TransactionFee = fee - txn.TransactionType = transaction.TxnTypeSmartContract - - // adjust fees if not set - if fee == 0 { - fee, err = transaction.EstimateFee(txn, nodeClient.Network().Miners, 0.2) - if err != nil { - l.Logger.Error("failed to estimate txn fee", - zap.Error(err), - zap.Any("txn", txn)) - return - } - txn.TransactionFee = fee - } - - if txn.TransactionNonce == 0 { - txn.TransactionNonce = node.Cache.GetNextNonce(txn.ClientID) - } - - if err = txn.ComputeHashAndSign(client.Sign); err != nil { - return - } - - msg := fmt.Sprintf("executing transaction '%s' with hash %s ", sn.Name, txn.Hash) - l.Logger.Info(msg) - l.Logger.Info("estimated txn fee: ", txn.TransactionFee) - - err = transaction.SendTransactionSync(txn, nodeClient.GetStableMiners()) - if err != nil { - l.Logger.Info("transaction submission failed", zap.Error(err)) - node.Cache.Evict(txn.ClientID) - nodeClient.ResetStableMiners() - return - } - - var ( - querySleepTime = time.Duration(cfg.QuerySleepTime) * time.Second - retries = 0 - ) - - sys.Sleep(querySleepTime) - - for retries < cfg.MaxTxnQuery { - t, err = transaction.VerifyTransaction(txn.Hash, nodeClient.Sharders().Healthy()) - if err == nil { - break - } - retries++ - sys.Sleep(querySleepTime) - } - - if err != nil { - l.Logger.Error("Error verifying the transaction", err.Error(), txn.Hash) - node.Cache.Evict(txn.ClientID) - return - } - - if t == nil { - return "", "", 0, txn, errors.New("transaction_validation_failed", - "Failed to get the transaction confirmation") - } - - if t.Status == transaction.TxnFail { - return t.Hash, t.TransactionOutput, 0, t, errors.New("", t.TransactionOutput) - } - - if t.Status == transaction.TxnChargeableError { - return t.Hash, t.TransactionOutput, t.TransactionNonce, t, errors.New("", t.TransactionOutput) - } - - return t.Hash, t.TransactionOutput, t.TransactionNonce, t, nil + return transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, value, client.TxnFee()) } func CommitToFabric(metaTxnData, fabricConfigJSON string) (string, error) { diff --git a/zcncore/sample/0Wallet.go b/zcncore/sample/0Wallet.go index 07f109b25..f903fc966 100644 --- a/zcncore/sample/0Wallet.go +++ b/zcncore/sample/0Wallet.go @@ -10,6 +10,7 @@ import ( "fmt" "sync" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zcncore" ) @@ -141,11 +142,14 @@ func main() { return } - err := zcncore.Init(ChainConfig) - if err != nil { - fmt.Println("Init failed") - return + cfg := conf.Config{} + if err := json.Unmarshal([]byte(ChainConfig), &cfg); err != nil { + return err } + if err := client.Init(context.Background(), cfg); err != nil { + return err + } + err = zcncore.SetWalletInfo(wallet, false) if err != nil { fmt.Println("set wallet info failed: ", err) diff --git a/zcncore/transaction.go b/zcncore/transaction.go index 62d77c8da..016038960 100644 --- a/zcncore/transaction.go +++ b/zcncore/transaction.go @@ -134,8 +134,6 @@ type TransactionCommon interface { MinerSCCollectReward(providerID string, providerType Provider) error MinerSCKill(providerID string, providerType Provider) error - StorageSCCollectReward(providerID string, providerType Provider) error - VestingUpdateConfig(*InputMap) error MinerScUpdateConfig(*InputMap) error MinerScUpdateGlobals(*InputMap) error diff --git a/zcncore/transaction_base.go b/zcncore/transaction_base.go index 37b8e1382..cb043ace5 100644 --- a/zcncore/transaction_base.go +++ b/zcncore/transaction_base.go @@ -104,7 +104,7 @@ type ChainConfig struct { SharderConsensous int `json:"sharder_consensous"` } -//Deprecated: Use client.Init() in core/client package +// Deprecated: Use client.Init() in core/client package // InitZCNSDK initializes the SDK with miner, sharder and signature scheme provided. func InitZCNSDK(blockWorker string, signscheme string, configs ...func(*ChainConfig) error) error { if signscheme != "ed25519" && signscheme != "bls0chain" { @@ -119,14 +119,14 @@ func InitZCNSDK(blockWorker string, signscheme string, configs ...func(*ChainCon } } cfg := conf.Config{ - BlockWorker: blockWorker, - SignatureScheme: signscheme, - MinSubmit: chainCfg.MinSubmit, - MinConfirmation: chainCfg.MinConfirmation, - ConfirmationChainLength: chainCfg.ConfirmationChainLength, - ChainID: chainCfg.ChainID, - EthereumNode: chainCfg.EthNode, - SharderConsensous: chainCfg.SharderConsensous, + BlockWorker: blockWorker, + SignatureScheme: signscheme, + MinSubmit: chainCfg.MinSubmit, + MinConfirmation: chainCfg.MinConfirmation, + ConfirmationChainLength: chainCfg.ConfirmationChainLength, + ChainID: chainCfg.ChainID, + EthereumNode: chainCfg.EthNode, + SharderConsensous: chainCfg.SharderConsensous, } return client.Init(context.Background(), cfg) } @@ -347,8 +347,15 @@ func (t *Transaction) submitTxn() { } } + nodeClient, err := client.GetNode() + if err != nil { + t.completeTxn(StatusError, "", err) + node.Cache.Evict(t.txn.ClientID) + return + } + var ( - randomMiners = GetStableMiners() + randomMiners = nodeClient.GetStableMiners() minersN = len(randomMiners) failedCount int32 failC = make(chan struct{}) @@ -395,7 +402,7 @@ func (t *Transaction) submitTxn() { logging.Error("failed to submit transaction") t.completeTxn(StatusError, "", fmt.Errorf("failed to submit transaction to all miners")) node.Cache.Evict(t.txn.ClientID) - ResetStableMiners() + nodeClient.ResetStableMiners() return case ret := <-resultC: logging.Debug("finish txn submitting, ", ret.Url, ", Status: ", ret.Status, ", output:", ret.Body) diff --git a/zcncore/transaction_mobile.go b/zcncore/transaction_mobile.go index a403da082..008ffd489 100644 --- a/zcncore/transaction_mobile.go +++ b/zcncore/transaction_mobile.go @@ -510,249 +510,6 @@ func (t *Transaction) MinerSCCollectReward(providerId string, providerType int) return err } -func (t *Transaction) StorageSCCollectReward(providerId string, providerType int) error { - pr := &scCollectReward{ - ProviderId: providerId, - ProviderType: providerType, - } - err := t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_COLLECT_REWARD, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go t.setNonceAndSubmit() - return err -} - -// FinalizeAllocation transaction. -func (t *Transaction) FinalizeAllocation(allocID string) (err error) { - type finiRequest struct { - AllocationID string `json:"allocation_id"` - } - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_FINALIZE_ALLOCATION, &finiRequest{ - AllocationID: allocID, - }, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -// CancelAllocation transaction. -func (t *Transaction) CancelAllocation(allocID string) error { - type cancelRequest struct { - AllocationID string `json:"allocation_id"` - } - err := t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_CANCEL_ALLOCATION, &cancelRequest{ - AllocationID: allocID, - }, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return nil -} - -// CreateAllocation transaction. -func (t *Transaction) CreateAllocation(car *CreateAllocationRequest, lock string) error { - lv, err := parseCoinStr(lock) - if err != nil { - return err - } - - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_CREATE_ALLOCATION, car.toCreateAllocationSCInput(), lv) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return nil -} - -// CreateReadPool for current user. -func (t *Transaction) CreateReadPool() error { - err := t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_CREATE_READ_POOL, nil, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return nil -} - -// ReadPoolLock locks tokens for current user and given allocation, using given -// duration. If blobberID is not empty, then tokens will be locked for given -// allocation->blobber only. -func (t *Transaction) ReadPoolLock(allocID, blobberID string, - duration int64, lock string) error { - lv, err := parseCoinStr(lock) - if err != nil { - return err - } - - type lockRequest struct { - Duration time.Duration `json:"duration"` - AllocationID string `json:"allocation_id"` - BlobberID string `json:"blobber_id,omitempty"` - } - - var lr lockRequest - lr.Duration = time.Duration(duration) - lr.AllocationID = allocID - lr.BlobberID = blobberID - - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_READ_POOL_LOCK, &lr, lv) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return nil -} - -// ReadPoolUnlock for current user and given pool. -func (t *Transaction) ReadPoolUnlock() error { - err := t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_READ_POOL_UNLOCK, nil, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return nil -} - -// StakePoolLock used to lock tokens in a stake pool of a blobber. -func (t *Transaction) StakePoolLock(providerId string, providerType int, lock string) error { - lv, err := parseCoinStr(lock) - if err != nil { - return err - } - - spr := stakePoolRequest{ - ProviderType: providerType, - ProviderID: providerId, - } - - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_STAKE_POOL_LOCK, &spr, lv) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return nil -} - -// StakePoolUnlock by blobberID -func (t *Transaction) StakePoolUnlock(providerId string, providerType int) error { - spr := stakePoolRequest{ - ProviderType: providerType, - ProviderID: providerId, - } - - err := t.createSmartContractTxn(StorageSmartContractAddress, transaction.STORAGESC_STAKE_POOL_UNLOCK, &spr, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return nil -} - -// UpdateBlobberSettings update settings of a blobber. -func (t *Transaction) UpdateBlobberSettings(b Blobber) error { - err := t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_UPDATE_BLOBBER_SETTINGS, b, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return nil -} - -// UpdateAllocation transaction. -func (t *Transaction) UpdateAllocation(allocID string, sizeDiff int64, - expirationDiff int64, lock string) error { - lv, err := parseCoinStr(lock) - if err != nil { - return err - } - - type updateAllocationRequest struct { - ID string `json:"id"` // allocation id - Size int64 `json:"size"` // difference - Expiration int64 `json:"expiration_date"` // difference - } - - var uar updateAllocationRequest - uar.ID = allocID - uar.Size = sizeDiff - uar.Expiration = expirationDiff - - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_UPDATE_ALLOCATION, &uar, lv) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return nil -} - -// WritePoolLock locks tokens for current user and given allocation, using given -// duration. If blobberID is not empty, then tokens will be locked for given -// allocation->blobber only. -func (t *Transaction) WritePoolLock(allocID, lock string) error { - lv, err := parseCoinStr(lock) - if err != nil { - return err - } - - var lr = struct { - AllocationID string `json:"allocation_id"` - }{ - AllocationID: allocID, - } - - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_WRITE_POOL_LOCK, &lr, lv) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return nil -} - -// WritePoolUnlock for current user and given pool. -func (t *Transaction) WritePoolUnlock(allocID string) error { - var ur = struct { - AllocationID string `json:"allocation_id"` - }{ - AllocationID: allocID, - } - - err := t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_WRITE_POOL_UNLOCK, &ur, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return nil -} - func (t *Transaction) VestingUpdateConfig(vscc InputMap) (err error) { err = t.createSmartContractTxn(VestingSmartContractAddress, @@ -1233,7 +990,7 @@ func GetChainStats(timeout RequestTimeout) ([]byte, error) { defer cancel() var ( - b *block.ChainStats + b *block.ChainStats ) var numSharders = len(nodeClient.Sharders().Healthy()) // overwrite, use all @@ -1265,7 +1022,7 @@ func GetChainStats(timeout RequestTimeout) ([]byte, error) { func GetFeeStats(timeout RequestTimeout) ([]byte, error) { nodeClient, err := client.GetNode() if err != nil { - return nil ,err + return nil, err } var numMiners = 4 @@ -1280,7 +1037,7 @@ func GetFeeStats(timeout RequestTimeout) ([]byte, error) { defer cancel() var ( - b *block.FeeStats + b *block.FeeStats ) queryFromMinersContext(ctx, numMiners, GET_FEE_STATS, result) diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index df425fead..996bac5f5 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -17,7 +17,6 @@ import ( "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" - "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/core/tokenrate" "github.com/0chain/gosdk/core/util" @@ -52,7 +51,7 @@ const ( FAUCETSC_PFX = `/v1/screst/` + FaucetSmartContractAddress GET_FAUCETSC_CONFIG = FAUCETSC_PFX + `/faucet-config` - // zcn sc + // ZCNSC_PFX zcn sc ZCNSC_PFX = `/v1/screst/` + ZCNSCSmartContractAddress GET_MINT_NONCE = ZCNSC_PFX + `/v1/mint_nonce` GET_NOT_PROCESSED_BURN_TICKETS = ZCNSC_PFX + `/v1/not_processed_burn_tickets` @@ -132,7 +131,9 @@ const ( ) var defaultLogLevel = logger.DEBUG +var logging logger.Logger +// GetLogger returns the logger instance func GetLogger() *logger.Logger { return &logging } @@ -152,24 +153,61 @@ const ( OpGetNotProcessedBurnTickets OpGetMintNonce // storage SC ops + // OpStorageSCGetConfig Get global storage SC config OpStorageSCGetConfig + + // OpStorageSCGetChallengePoolInfo Get challenge pool info OpStorageSCGetChallengePoolInfo + + // OpStorageSCGetAllocation Get allocation info OpStorageSCGetAllocation + + // OpStorageSCGetAllocations Get all allocations OpStorageSCGetAllocations + + // OpStorageSCGetReadPoolInfo Get read pool info OpStorageSCGetReadPoolInfo + + // OpStorageSCGetStakePoolInfo Get stake pool info OpStorageSCGetStakePoolInfo + + // OpStorageSCGetStakePoolUserInfo Get blobbers OpStorageSCGetBlobbers + + // OpStorageSCGetBlobber Get blobber information OpStorageSCGetBlobber + + // OpStorageSCGetValidator Get transaction info OpStorageSCGetTransactions + + // OpStorageSCGetSnapshots Get global snapshots OpStorageSCGetSnapshots + + // OpStorageSCGetBlobberSnapshots Get blobber snapshots OpStorageSCGetBlobberSnapshots + + // OpStorageSCGetMinerSnapshots Get miner snapshots OpStorageSCGetMinerSnapshots + + // OpStorageSCGetSharderSnapshots Get sharder snapshots OpStorageSCGetSharderSnapshots + + // OpStorageSCGetAuthorizerSnapshots Get authorizer snapshots OpStorageSCGetAuthorizerSnapshots + + // OpStorageSCGetValidatorSnapshots Get validator snapshots OpStorageSCGetValidatorSnapshots + + // OpStorageSCGetUserSnapshots Get user snapshots OpStorageSCGetUserSnapshots + + // OpStorageSCGetUserLockedTotal Get global configuration OpZCNSCGetGlobalConfig + + // OpZCNSCGetMintNonce Get authorizer information OpZCNSCGetAuthorizer + + // OpZCNSCGetAuthorizerNodes Get authorizer nodes OpZCNSCGetAuthorizerNodes ) @@ -183,7 +221,7 @@ type GetBalanceCallback interface { OnBalanceAvailable(status int, value int64, info string) } -// BurnTicket model used for deserialization of the response received from sharders +// BurnTicket represents the burn ticket of native ZCN tokens used by the bridge protocol to mint ERC20 tokens type BurnTicket struct { Hash string `json:"hash"` Amount int64 `json:"amount"` @@ -207,7 +245,7 @@ func (g *GetNonceCallbackStub) OnNonceAvailable(status int, nonce int64, info st g.info = info } -// GetInfoCallback needs to be implemented by the caller of GetLockTokenConfig() and GetLockedTokens() +// GetInfoCallback represents the functions that will be called when the response of a GET request to the sharders is available type GetInfoCallback interface { // OnInfoAvailable will be called when GetLockTokenConfig is complete // if status == StatusSuccess then info is valid @@ -217,36 +255,14 @@ type GetInfoCallback interface { // AuthCallback needs to be implemented by the caller SetupAuth() type AuthCallback interface { - // This call back gives the status of the Two factor authenticator(zauth) setup. + // OnSetupComplete This call back gives the status of the Two factor authenticator(zauth) setup. OnSetupComplete(status int, err string) } -var ( - logging logger.Logger -) - func init() { logging.Init(defaultLogLevel, "0chain-core-sdk") } -// Deprecated: Use client.GetNode().GetStableMiners() -func GetStableMiners() []string { - clientNode, err := client.GetNode() - if err != nil { - panic(err) - } - return clientNode.GetStableMiners() -} - -// Deprecated: Use client.GetNode().ResetStableMiners() -func ResetStableMiners() { - clientNode, err := client.GetNode() - if err != nil { - panic(err) - } - clientNode.ResetStableMiners() -} - func checkSdkInit() error { _, err := client.GetNode() if err != nil { @@ -274,20 +290,6 @@ func CheckConfig() error { return nil } -// Deprecated: Use client.GetNode().GetMinShardersVerify() after Init call -func GetMinShardersVerify() int { - clientNode, err := client.GetNode() - if err != nil { - panic(err) - } - return clientNode.GetMinShardersVerify() -} - -// GetVersion - returns version string -func GetVersion() string { - return version.VERSIONSTR -} - // SetLogLevel set the log level. // lvl - 0 disabled; higher number (upto 4) more verbosity func SetLogLevel(lvl int) { @@ -309,37 +311,9 @@ func SetLogFile(logFile string, verbose bool) { logging.Info("******* Wallet SDK Version:", version.VERSIONSTR, " ******* (SetLogFile)") } -// Deprecated: Use client.Init() in core/client package -// Init initialize the SDK with miner, sharder and signature scheme provided in configuration provided in JSON format -// # Inputs -// - chainConfigJSON: json format of zcn config -// { -// "block_worker": "https://dev.0chain.net/dns", -// "signature_scheme": "bls0chain", -// "min_submit": 50, -// "min_confirmation": 50, -// "confirmation_chain_length": 3, -// "max_txn_query": 5, -// "query_sleep_time": 5, -// "preferred_blobbers": ["https://dev.0chain.net/blobber02","https://dev.0chain.net/blobber03"], -// "chain_id":"0afc093ffb509f059c55478bc1a60351cef7b4e9c008a53a6cc8241ca8617dfe", -// "ethereum_node":"https://ropsten.infura.io/v3/xxxxxxxxxxxxxxx", -// "zbox_host":"https://0box.dev.0chain.net", -// "zbox_app_type":"vult", -// "sharder_consensous": 2, -// } -func Init(chainConfigJSON string) error { - cfg := conf.Config{} - err := json.Unmarshal([]byte(chainConfigJSON), &cfg) - if err != nil { - return err - } - return client.Init(context.Background(), cfg) -} - var signatureScheme string -// Use client.Init() in core/client package to initialize SDK. +// InitSignatureScheme Use client.Init() in core/client package to initialize SDK. // InitSignatureScheme initializes signature scheme only. func InitSignatureScheme(scheme string) { if scheme != constants.BLS0CHAIN.String() && scheme != constants.ED25519.String() { @@ -398,7 +372,7 @@ func RecoverWallet(mnemonic string, statusCb WalletCallback) error { return nil } -// Split keys from the primary master key +// SplitKeys Split keys from the primary master key func SplitKeys(privateKey string, numSplits int) (string, error) { if signatureScheme != constants.BLS0CHAIN.String() { return "", errors.New("signature key doesn't support split key") @@ -577,7 +551,7 @@ func GetNotProcessedZCNBurnTickets(ethereumAddress, startNonce string, cb GetInf return nil } -// GetBalance retrieve wallet nonce from sharders +// GetNonce GetBalance retrieve wallet nonce from sharders func GetNonce(cb GetNonceCallback) error { if cb == nil { cb = &GetNonceCallbackStub{} @@ -602,7 +576,7 @@ func GetNonce(cb GetNonceCallback) error { return nil } -// GetWalletBalance retrieve wallet nonce from sharders +// GetWalletNonce GetWalletBalance retrieve wallet nonce from sharders func GetWalletNonce(clientID string) (int64, error) { cb := &GetNonceCallbackStub{} From 2841ef7cf3a062288535b2daf21bbac1e29a5dcb Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 23 Aug 2024 23:23:55 +0530 Subject: [PATCH 020/319] Fix --- core/block/block.go | 81 ++++++++++++++++++++++++++++++++++++++ core/logger/logger.go | 3 ++ core/node/node.go | 74 ---------------------------------- core/transaction/entity.go | 14 ++++--- 4 files changed, 92 insertions(+), 80 deletions(-) diff --git a/core/block/block.go b/core/block/block.go index aec92693d..8a73e780f 100644 --- a/core/block/block.go +++ b/core/block/block.go @@ -5,13 +5,23 @@ package block import ( + "context" + "encoding/json" "fmt" + "github.com/0chain/gosdk/core/logger" + "github.com/0chain/gosdk/core/node" + "github.com/0chain/gosdk/core/util" + "net/http" + + "github.com/0chain/errors" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/transaction" ) +const GET_BLOCK_INFO = `/v1/block/get?` + type Key []byte type Header struct { @@ -92,3 +102,74 @@ type FeeStats struct { MinFees common.Balance `json:"min_fees"` MeanFees common.Balance `json:"mean_fees"` } + +func GetBlockByRound(h *node.NodeHolder, ctx context.Context, numSharders int, round int64) (b *Block, err error) { + + var result = make(chan *util.GetResponse, numSharders) + defer close(result) + + numSharders = len(h.Healthy()) // overwrite, use all + h.QueryFromShardersContext(ctx, numSharders, + fmt.Sprintf("%sround=%d&content=full,header", GET_BLOCK_INFO, round), + result) + + var ( + maxConsensus int + roundConsensus = make(map[string]int) + ) + + type respObj struct { + Block *Block `json:"block"` + Header *Header `json:"header"` + } + + for i := 0; i < numSharders; i++ { + var rsp = <-result + if rsp == nil { + logger.Log.Error("nil response") + continue + } + logger.Log.Debug(rsp.Url, rsp.Status) + + if rsp.StatusCode != http.StatusOK { + logger.Log.Error(rsp.Body) + continue + } + + var respo respObj + if err = json.Unmarshal([]byte(rsp.Body), &respo); err != nil { + logger.Log.Error("block parse error: ", err) + err = nil + continue + } + + if respo.Block == nil { + logger.Log.Debug(rsp.Url, "no block in response:", rsp.Body) + continue + } + + if respo.Header == nil { + logger.Log.Debug(rsp.Url, "no block header in response:", rsp.Body) + continue + } + + if respo.Header.Hash != string(respo.Block.Hash) { + logger.Log.Debug(rsp.Url, "header and block hash mismatch:", rsp.Body) + continue + } + + b = respo.Block + b.Header = respo.Header + + var h = encryption.FastHash([]byte(b.Hash)) + if roundConsensus[h]++; roundConsensus[h] > maxConsensus { + maxConsensus = roundConsensus[h] + } + } + + if maxConsensus == 0 { + return nil, errors.New("", "round info not found") + } + + return +} diff --git a/core/logger/logger.go b/core/logger/logger.go index 69b361956..a875dae85 100644 --- a/core/logger/logger.go +++ b/core/logger/logger.go @@ -16,6 +16,9 @@ const ( DEBUG = 4 ) +// Log global logger instance +var Log Logger + const cRed = "\u001b[31m" const cReset = "\u001b[0m" diff --git a/core/node/node.go b/core/node/node.go index 2a3ef7de0..9f5f6f35c 100644 --- a/core/node/node.go +++ b/core/node/node.go @@ -14,8 +14,6 @@ import ( "time" "github.com/0chain/errors" - "github.com/0chain/gosdk/core/block" - "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/util" "github.com/0chain/gosdk/zboxcore/logger" "github.com/ethereum/go-ethereum/common/math" @@ -121,7 +119,6 @@ const consensusThresh = 25 const ( GET_BALANCE = `/v1/client/get/balance?client_id=` CURRENT_ROUND = "/v1/current-round" - GET_BLOCK_INFO = `/v1/block/get?` GET_HARDFORK_ROUND = `/v1/screst/6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d9/hardfork?name=` ) @@ -220,77 +217,6 @@ func (h *NodeHolder) QueryFromShardersContext(ctx context.Context, numSharders i } } -func (h *NodeHolder) GetBlockByRound(ctx context.Context, numSharders int, round int64) (b *block.Block, err error) { - - var result = make(chan *util.GetResponse, numSharders) - defer close(result) - - numSharders = len(h.Healthy()) // overwrite, use all - h.QueryFromShardersContext(ctx, numSharders, - fmt.Sprintf("%sround=%d&content=full,header", GET_BLOCK_INFO, round), - result) - - var ( - maxConsensus int - roundConsensus = make(map[string]int) - ) - - type respObj struct { - Block *block.Block `json:"block"` - Header *block.Header `json:"header"` - } - - for i := 0; i < numSharders; i++ { - var rsp = <-result - if rsp == nil { - logger.Logger.Error("nil response") - continue - } - logger.Logger.Debug(rsp.Url, rsp.Status) - - if rsp.StatusCode != http.StatusOK { - logger.Logger.Error(rsp.Body) - continue - } - - var respo respObj - if err = json.Unmarshal([]byte(rsp.Body), &respo); err != nil { - logger.Logger.Error("block parse error: ", err) - err = nil - continue - } - - if respo.Block == nil { - logger.Logger.Debug(rsp.Url, "no block in response:", rsp.Body) - continue - } - - if respo.Header == nil { - logger.Logger.Debug(rsp.Url, "no block header in response:", rsp.Body) - continue - } - - if respo.Header.Hash != string(respo.Block.Hash) { - logger.Logger.Debug(rsp.Url, "header and block hash mismatch:", rsp.Body) - continue - } - - b = respo.Block - b.Header = respo.Header - - var h = encryption.FastHash([]byte(b.Hash)) - if roundConsensus[h]++; roundConsensus[h] > maxConsensus { - maxConsensus = roundConsensus[h] - } - } - - if maxConsensus == 0 { - return nil, errors.New("", "round info not found") - } - - return -} - func (h *NodeHolder) GetRoundFromSharders() (int64, error) { sharders := h.Healthy() diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 3dd2275ad..47f0f3268 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -6,9 +6,9 @@ import ( "fmt" "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/conf" + "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/core/node" "github.com/0chain/gosdk/core/sys" - l "github.com/0chain/gosdk/zboxcore/logger" "go.uber.org/zap" "net/http" "strings" @@ -22,6 +22,8 @@ import ( lru "github.com/hashicorp/golang-lru" ) +var Logger logger.Logger + const TXN_SUBMIT_URL = "v1/transaction/put" const TXN_VERIFY_URL = "v1/transaction/get/confirmation?hash=" const BLOCK_BY_ROUND_URL = "v1/screst/6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d7/block?round=" @@ -540,7 +542,7 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, if fee == 0 { fee, err = EstimateFee(txn, nodeClient.Network().Miners, 0.2) if err != nil { - l.Logger.Error("failed to estimate txn fee", + Logger.Error("failed to estimate txn fee", zap.Error(err), zap.Any("txn", txn)) return @@ -557,12 +559,12 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, } msg := fmt.Sprintf("executing transaction '%s' with hash %s ", sn.Name, txn.Hash) - l.Logger.Info(msg) - l.Logger.Info("estimated txn fee: ", txn.TransactionFee) + Logger.Info(msg) + Logger.Info("estimated txn fee: ", txn.TransactionFee) err = SendTransactionSync(txn, nodeClient.GetStableMiners()) if err != nil { - l.Logger.Info("transaction submission failed", zap.Error(err)) + Logger.Info("transaction submission failed", zap.Error(err)) node.Cache.Evict(txn.ClientID) nodeClient.ResetStableMiners() return @@ -585,7 +587,7 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, } if err != nil { - l.Logger.Error("Error verifying the transaction", err.Error(), txn.Hash) + Logger.Error("Error verifying the transaction", err.Error(), txn.Hash) node.Cache.Evict(txn.ClientID) return } From 405b0cfc5a9b09fec43be2c0db1165f9cee21ff4 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 23 Aug 2024 23:31:00 +0530 Subject: [PATCH 021/319] Fix --- zcncore/transaction.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zcncore/transaction.go b/zcncore/transaction.go index 016038960..facc2cfc3 100644 --- a/zcncore/transaction.go +++ b/zcncore/transaction.go @@ -1030,7 +1030,7 @@ func GetBlockByRound(ctx context.Context, numSharders int, round int64) (b *bloc if err != nil { return nil, err } - return clientNode.Sharders().GetBlockByRound(ctx, numSharders, round) + return block.GetBlockByRound(clientNode.Sharders(), ctx, numSharders, round) } func GetRoundFromSharders() (int64, error) { From 8549136f189606c7a936956b5e075bb238835ed7 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 24 Aug 2024 01:12:43 +0530 Subject: [PATCH 022/319] Fix --- core/http/http.go | 204 ++++++++++++++++ {zboxcore/zboxutil => core/http}/transport.go | 4 +- zboxcore/sdk/sdk.go | 35 +-- zboxcore/zboxutil/http.go | 221 ++---------------- zboxcore/zboxutil/http_test.go | 3 +- zcnbridge/http/client.go | 8 +- zcncore/wallet_base.go | 15 ++ 7 files changed, 259 insertions(+), 231 deletions(-) create mode 100644 core/http/http.go rename {zboxcore/zboxutil => core/http}/transport.go (91%) diff --git a/core/http/http.go b/core/http/http.go new file mode 100644 index 000000000..90442d726 --- /dev/null +++ b/core/http/http.go @@ -0,0 +1,204 @@ +package http + +import ( + "encoding/json" + "fmt" + "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/conf" + "github.com/0chain/gosdk/core/logger" + "io/ioutil" + "net" + "net/http" + "net/url" + "os" + "sync" +) + +// SCRestAPIHandler is a function type to handle the response from the SC Rest API +// +// `response` - the response from the SC Rest API +// `numSharders` - the number of sharders that responded +// `err` - the error if any +type SCRestAPIHandler func(response map[string][]byte, numSharders int, err error) + +const SC_REST_API_URL = "v1/screst/" + +const MAX_RETRIES = 5 +const SLEEP_BETWEEN_RETRIES = 5 + +// In percentage +const consensusThresh = float32(25.0) + +// MakeSCRestAPICall makes a rest api call to the sharders. +// - scAddress is the address of the smart contract +// - relativePath is the relative path of the api +// - params is the query parameters +// - handler is the handler function to handle the response +func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string, handler SCRestAPIHandler) ([]byte, error) { + nodeClient, err := client.GetNode() + if err != nil { + return nil, err + } + numSharders := len(nodeClient.Sharders().Healthy()) + sharders := nodeClient.Sharders().Healthy() + responses := make(map[int]int) + mu := &sync.Mutex{} + entityResult := make(map[string][]byte) + var retObj []byte + maxCount := 0 + dominant := 200 + wg := sync.WaitGroup{} + + cfg, err := conf.GetClientConfig() + if err != nil { + return nil, err + } + + for _, sharder := range sharders { + wg.Add(1) + go func(sharder string) { + defer wg.Done() + urlString := fmt.Sprintf("%v/%v%v%v", sharder, SC_REST_API_URL, scAddress, relativePath) + urlObj, err := url.Parse(urlString) + if err != nil { + logger.Log.Error(err) + return + } + q := urlObj.Query() + for k, v := range params { + q.Add(k, v) + } + urlObj.RawQuery = q.Encode() + client := &http.Client{Transport: DefaultTransport} + response, err := client.Get(urlObj.String()) + if err != nil { + nodeClient.Sharders().Fail(sharder) + return + } + + defer response.Body.Close() + entityBytes, _ := ioutil.ReadAll(response.Body) + mu.Lock() + if response.StatusCode > http.StatusBadRequest { + nodeClient.Sharders().Fail(sharder) + } else { + nodeClient.Sharders().Success(sharder) + } + responses[response.StatusCode]++ + if responses[response.StatusCode] > maxCount { + maxCount = responses[response.StatusCode] + } + + if isCurrentDominantStatus(response.StatusCode, responses, maxCount) { + dominant = response.StatusCode + retObj = entityBytes + } + + entityResult[sharder] = entityBytes + nodeClient.Sharders().Success(sharder) + mu.Unlock() + }(sharder) + } + wg.Wait() + + rate := float32(maxCount*100) / float32(cfg.SharderConsensous) + if rate < consensusThresh { + err = errors.New("consensus_failed", "consensus failed on sharders") + } + + if dominant != 200 { + var objmap map[string]json.RawMessage + err := json.Unmarshal(retObj, &objmap) + if err != nil { + return nil, errors.New("", string(retObj)) + } + + var parsed string + err = json.Unmarshal(objmap["error"], &parsed) + if err != nil || parsed == "" { + return nil, errors.New("", string(retObj)) + } + + return nil, errors.New("", parsed) + } + + if handler != nil { + handler(entityResult, numSharders, err) + } + + if rate > consensusThresh { + return retObj, nil + } + return nil, err +} + +// IsCurrentDominantStatus determines whether the current response status is the dominant status among responses. +// +// The dominant status is where the response status is counted the most. +// On tie-breakers, 200 will be selected if included. +// +// Function assumes runningTotalPerStatus can be accessed safely concurrently. +func IsCurrentDominantStatus(respStatus int, currentTotalPerStatus map[int]int, currentMax int) bool { + // mark status as dominant if + // - running total for status is the max and response is 200 or + // - running total for status is the max and count for 200 is lower + return currentTotalPerStatus[respStatus] == currentMax && (respStatus == 200 || currentTotalPerStatus[200] < currentMax) +} + +func (pfe *proxyFromEnv) Proxy(req *http.Request) (proxy *url.URL, err error) { + if pfe.isLoopback(req.URL.Host) { + switch req.URL.Scheme { + case "http": + return pfe.http, nil + case "https": + return pfe.https, nil + default: + } + } + return http.ProxyFromEnvironment(req) +} + +var EnvProxy proxyFromEnv + +type proxyFromEnv struct { + HTTPProxy string + HTTPSProxy string + NoProxy string + + http, https *url.URL +} + +func (pfe *proxyFromEnv) Initialize() { + pfe.HTTPProxy = getEnvAny("HTTP_PROXY", "http_proxy") + pfe.HTTPSProxy = getEnvAny("HTTPS_PROXY", "https_proxy") + pfe.NoProxy = getEnvAny("NO_PROXY", "no_proxy") + + if pfe.NoProxy != "" { + return + } + + if pfe.HTTPProxy != "" { + pfe.http, _ = url.Parse(pfe.HTTPProxy) + } + if pfe.HTTPSProxy != "" { + pfe.https, _ = url.Parse(pfe.HTTPSProxy) + } +} + +func (pfe *proxyFromEnv) isLoopback(host string) (ok bool) { + host, _, _ = net.SplitHostPort(host) + if host == "localhost" { + return true + } + return net.ParseIP(host).IsLoopback() +} + +func getEnvAny(names ...string) string { + for _, n := range names { + if val := os.Getenv(n); val != "" { + return val + } + } + return "" +} diff --git a/zboxcore/zboxutil/transport.go b/core/http/transport.go similarity index 91% rename from zboxcore/zboxutil/transport.go rename to core/http/transport.go index 61fd3c509..01d56dc4d 100644 --- a/zboxcore/zboxutil/transport.go +++ b/core/http/transport.go @@ -1,7 +1,7 @@ //go:build !js && !wasm // +build !js,!wasm -package zboxutil +package http import ( "net" @@ -10,7 +10,7 @@ import ( ) var DefaultTransport = &http.Transport{ - Proxy: envProxy.Proxy, + Proxy: EnvProxy.Proxy, DialContext: (&net.Dialer{ Timeout: 3 * time.Minute, KeepAlive: 45 * time.Second, diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index b73d017f8..6a0a60f76 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -8,6 +8,7 @@ import ( "github.com/0chain/common/core/currency" "github.com/0chain/errors" "github.com/0chain/gosdk/core/conf" + coreHttp "github.com/0chain/gosdk/core/http" "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/core/zcncrypto" "gopkg.in/natefinch/lumberjack.v2" @@ -183,7 +184,7 @@ func GetReadPoolInfo(clientID string) (info *ReadPool, err error) { } var b []byte - b, err = zboxutil.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getReadPoolStat", + b, err = coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getReadPoolStat", map[string]string{"client_id": clientID}, nil) if err != nil { return nil, errors.Wrap(err, "error requesting read pool info") @@ -289,7 +290,7 @@ func GetStakePoolInfo(providerType ProviderType, providerID string) (info *Stake } var b []byte - b, err = zboxutil.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getStakePoolStat", + b, err = coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getStakePoolStat", map[string]string{"provider_type": strconv.Itoa(int(providerType)), "provider_id": providerID}, nil) if err != nil { return nil, errors.Wrap(err, "error requesting stake pool info:") @@ -330,7 +331,7 @@ func GetStakePoolUserInfo(clientID string, offset, limit int) (info *StakePoolUs "offset": strconv.FormatInt(int64(offset), 10), "limit": strconv.FormatInt(int64(limit), 10), } - b, err = zboxutil.MakeSCRestAPICall(STORAGE_SCADDRESS, + b, err = coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getUserStakePoolStat", params, nil) if err != nil { return nil, errors.Wrap(err, "error requesting stake pool user info:") @@ -542,7 +543,7 @@ func GetChallengePoolInfo(allocID string) (info *ChallengePoolInfo, err error) { } var b []byte - b, err = zboxutil.MakeSCRestAPICall(STORAGE_SCADDRESS, + b, err = coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getChallengePoolStat", map[string]string{"allocation_id": allocID}, nil) if err != nil { @@ -567,7 +568,7 @@ func GetMptData(key string) ([]byte, error) { } var b []byte - b, err := zboxutil.MakeSCRestAPICall(STORAGE_SCADDRESS, + b, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/get_mpt_key", map[string]string{"key": key}, nil, ) @@ -596,7 +597,7 @@ func GetStorageSCConfig() (conf *InputMap, err error) { } var b []byte - b, err = zboxutil.MakeSCRestAPICall(STORAGE_SCADDRESS, "/storage-config", nil, + b, err = coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/storage-config", nil, nil) if err != nil { return nil, errors.Wrap(err, "error requesting storage SC configs:") @@ -771,7 +772,7 @@ func getBlobbersInternal(active, stakable bool, limit, offset int) (bs []*Blobbe offset, strconv.FormatBool(stakable), ) - b, err := zboxutil.MakeSCRestAPICall(STORAGE_SCADDRESS, url, nil, nil) + b, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, url, nil, nil) var wrap nodes if err != nil { return nil, errors.Wrap(err, "error requesting blobbers:") @@ -829,7 +830,7 @@ func GetBlobber(blobberID string) (blob *Blobber, err error) { return nil, sdkNotInitialized } var b []byte - b, err = zboxutil.MakeSCRestAPICall( + b, err = coreHttp.MakeSCRestAPICall( STORAGE_SCADDRESS, "/getBlobber", map[string]string{"blobber_id": blobberID}, @@ -854,7 +855,7 @@ func GetValidator(validatorID string) (validator *Validator, err error) { return nil, sdkNotInitialized } var b []byte - b, err = zboxutil.MakeSCRestAPICall( + b, err = coreHttp.MakeSCRestAPICall( STORAGE_SCADDRESS, "/get_validator", map[string]string{"validator_id": validatorID}, @@ -879,7 +880,7 @@ func GetValidators(stakable bool) (validators []*Validator, err error) { return nil, sdkNotInitialized } var b []byte - b, err = zboxutil.MakeSCRestAPICall( + b, err = coreHttp.MakeSCRestAPICall( STORAGE_SCADDRESS, "/validators", map[string]string{ @@ -943,7 +944,7 @@ func GetAllocation(allocationID string) (*Allocation, error) { } params := make(map[string]string) params["allocation"] = allocationID - allocationBytes, err := zboxutil.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params, nil) + allocationBytes, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params, nil) if err != nil { return nil, errors.New("allocation_fetch_error", "Error fetching the allocation."+err.Error()) } @@ -964,7 +965,7 @@ func GetAllocationUpdates(allocation *Allocation) error { params := make(map[string]string) params["allocation"] = allocation.ID - allocationBytes, err := zboxutil.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params, nil) + allocationBytes, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params, nil) if err != nil { return errors.New("allocation_fetch_error", "Error fetching the allocation."+err.Error()) } @@ -1016,7 +1017,7 @@ func getAllocationsInternal(clientID string, limit, offset int) ([]*Allocation, params["client"] = clientID params["limit"] = fmt.Sprint(limit) params["offset"] = fmt.Sprint(offset) - allocationsBytes, err := zboxutil.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocations", params, nil) + allocationsBytes, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocations", params, nil) if err != nil { return nil, errors.New("allocations_fetch_error", "Error fetching the allocations."+err.Error()) } @@ -1197,7 +1198,7 @@ func GetAllocationBlobbers( params["force"] = strconv.FormatBool(force[0]) } - allocBlobber, err := zboxutil.MakeSCRestAPICall(STORAGE_SCADDRESS, "/alloc_blobbers", params, nil) + allocBlobber, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/alloc_blobbers", params, nil) if err != nil { return nil, err } @@ -1282,7 +1283,7 @@ func GetBlobberIds(blobberUrls []string) ([]string, error) { params := make(map[string]string) params["blobber_urls"] = string(urlsStr) - idsStr, err := zboxutil.MakeSCRestAPICall(STORAGE_SCADDRESS, "/blobber_ids", params, nil) + idsStr, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/blobber_ids", params, nil) if err != nil { return nil, err } @@ -1307,7 +1308,7 @@ func GetFreeAllocationBlobbers(request map[string]interface{}) ([]string, error) params := make(map[string]string) params["free_allocation_data"] = string(data) - allocBlobber, err := zboxutil.MakeSCRestAPICall(STORAGE_SCADDRESS, "/free_alloc_blobbers", params, nil) + allocBlobber, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/free_alloc_blobbers", params, nil) if err != nil { return nil, err } @@ -1810,7 +1811,7 @@ func GetUpdateAllocationMinLock( params := make(map[string]string) params["data"] = string(data) - responseBytes, err := zboxutil.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation-update-min-lock", params, nil) + responseBytes, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation-update-min-lock", params, nil) if err != nil { return 0, errors.Wrap(err, "failed to request allocation update min lock") } diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index d4cf681a6..a88df27eb 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -4,41 +4,22 @@ import ( "bytes" "context" "encoding/json" - "fmt" + http2 "github.com/0chain/gosdk/core/http" "io" - "io/ioutil" - "net" "net/http" "net/url" - "os" "path" "strconv" - "sync" "time" "github.com/0chain/errors" "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/encryption" + coreHttp "github.com/0chain/gosdk/core/http" "github.com/0chain/gosdk/core/logger" "github.com/hitenjain14/fasthttp" ) -const SC_REST_API_URL = "v1/screst/" - -const MAX_RETRIES = 5 -const SLEEP_BETWEEN_RETRIES = 5 - -// In percentage -const consensusThresh = float32(25.0) - -// SCRestAPIHandler is a function type to handle the response from the SC Rest API -// -// `response` - the response from the SC Rest API -// `numSharders` - the number of sharders that responded -// `err` - the error if any -type SCRestAPIHandler func(response map[string][]byte, numSharders int, err error) - type HttpClient interface { Do(req *http.Request) (*http.Response, error) } @@ -96,48 +77,6 @@ const ( ALLOCATION_ID_HEADER = "ALLOCATION-ID" ) -func getEnvAny(names ...string) string { - for _, n := range names { - if val := os.Getenv(n); val != "" { - return val - } - } - return "" -} - -type proxyFromEnv struct { - HTTPProxy string - HTTPSProxy string - NoProxy string - - http, https *url.URL -} - -func (pfe *proxyFromEnv) initialize() { - pfe.HTTPProxy = getEnvAny("HTTP_PROXY", "http_proxy") - pfe.HTTPSProxy = getEnvAny("HTTPS_PROXY", "https_proxy") - pfe.NoProxy = getEnvAny("NO_PROXY", "no_proxy") - - if pfe.NoProxy != "" { - return - } - - if pfe.HTTPProxy != "" { - pfe.http, _ = url.Parse(pfe.HTTPProxy) - } - if pfe.HTTPSProxy != "" { - pfe.https, _ = url.Parse(pfe.HTTPSProxy) - } -} - -func (pfe *proxyFromEnv) isLoopback(host string) (ok bool) { - host, _, _ = net.SplitHostPort(host) - if host == "localhost" { - return true - } - return net.ParseIP(host).IsLoopback() -} - func GetFastHTTPClient() *fasthttp.Client { fc, ok := FastHttpClient.(*fasthttp.Client) if ok { @@ -146,24 +85,9 @@ func GetFastHTTPClient() *fasthttp.Client { return nil } -func (pfe *proxyFromEnv) Proxy(req *http.Request) (proxy *url.URL, err error) { - if pfe.isLoopback(req.URL.Host) { - switch req.URL.Scheme { - case "http": - return pfe.http, nil - case "https": - return pfe.https, nil - default: - } - } - return http.ProxyFromEnvironment(req) -} - -var envProxy proxyFromEnv - func init() { Client = &http.Client{ - Transport: DefaultTransport, + Transport: http2.DefaultTransport, } FastHttpClient = &fasthttp.Client{ @@ -183,7 +107,7 @@ func init() { MaxConnsPerHost: 1024, } fasthttp.SetBodySizePoolLimit(respBodyPoolLimit, respBodyPoolLimit) - envProxy.initialize() + coreHttp.EnvProxy.Initialize() log.Init(logger.DEBUG, "0box-sdk") } @@ -906,155 +830,38 @@ func NewRollbackRequest(baseUrl, allocationID string, allocationTx string, body return req, nil } -// MakeSCRestAPICall makes a rest api call to the sharders. -// - scAddress is the address of the smart contract -// - relativePath is the relative path of the api -// - params is the query parameters -// - handler is the handler function to handle the response -func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string, handler SCRestAPIHandler) ([]byte, error) { - nodeClient, err := client.GetNode() - if err != nil { - return nil, err - } - numSharders := len(nodeClient.Sharders().Healthy()) - sharders := nodeClient.Sharders().Healthy() - responses := make(map[int]int) - mu := &sync.Mutex{} - entityResult := make(map[string][]byte) - var retObj []byte - maxCount := 0 - dominant := 200 - wg := sync.WaitGroup{} - - cfg, err := conf.GetClientConfig() - if err != nil { - return nil, err - } - - for _, sharder := range sharders { - wg.Add(1) - go func(sharder string) { - defer wg.Done() - urlString := fmt.Sprintf("%v/%v%v%v", sharder, SC_REST_API_URL, scAddress, relativePath) - urlObj, err := url.Parse(urlString) - if err != nil { - log.Error(err) - return - } - q := urlObj.Query() - for k, v := range params { - q.Add(k, v) - } - urlObj.RawQuery = q.Encode() - client := &http.Client{Transport: DefaultTransport} - response, err := client.Get(urlObj.String()) - if err != nil { - nodeClient.Sharders().Fail(sharder) - return - } - - defer response.Body.Close() - entityBytes, _ := ioutil.ReadAll(response.Body) - mu.Lock() - if response.StatusCode > http.StatusBadRequest { - nodeClient.Sharders().Fail(sharder) - } else { - nodeClient.Sharders().Success(sharder) - } - responses[response.StatusCode]++ - if responses[response.StatusCode] > maxCount { - maxCount = responses[response.StatusCode] - } - - if isCurrentDominantStatus(response.StatusCode, responses, maxCount) { - dominant = response.StatusCode - retObj = entityBytes - } - - entityResult[sharder] = entityBytes - nodeClient.Sharders().Success(sharder) - mu.Unlock() - }(sharder) - } - wg.Wait() - - rate := float32(maxCount*100) / float32(cfg.SharderConsensous) - if rate < consensusThresh { - err = errors.New("consensus_failed", "consensus failed on sharders") - } - - if dominant != 200 { - var objmap map[string]json.RawMessage - err := json.Unmarshal(retObj, &objmap) - if err != nil { - return nil, errors.New("", string(retObj)) - } - - var parsed string - err = json.Unmarshal(objmap["error"], &parsed) - if err != nil || parsed == "" { - return nil, errors.New("", string(retObj)) - } - - return nil, errors.New("", parsed) - } - - if handler != nil { - handler(entityResult, numSharders, err) - } - - if rate > consensusThresh { - return retObj, nil - } - return nil, err -} - func HttpDo(ctx context.Context, cncl context.CancelFunc, req *http.Request, f func(*http.Response, error) error) error { // Run the HTTP request in a goroutine and pass the response to f. c := make(chan error, 1) go func() { var err error - // indefinitely try if io.EOF error occurs. As per some research over google - // it occurs when client http tries to send byte stream in connection that is - // closed by the server for { - var resp *http.Response - resp, err = Client.Do(req.WithContext(ctx)) + // Perform the request with the context provided. + resp, err := Client.Do(req.WithContext(ctx)) if errors.Is(err, io.EOF) { + // If the error is io.EOF, continue to retry indefinitely. continue } + // Call the provided callback function with the response and error. err = f(resp, err) - break + break // Exit the loop after a successful request or a non-EOF error. } - c <- err + c <- err // Send the final error (or nil) back through the channel. }() - // TODO: Check cncl context required in any case - // defer cncl() + defer cncl() // Ensure the cancellation function is deferred to release resources. + select { case <-ctx.Done(): - DefaultTransport.CancelRequest(req) //nolint - <-c // Wait for f to return. + // If the context is canceled or times out, return the context's error. + <-c // Wait for the goroutine to complete before returning. return ctx.Err() case err := <-c: return err } } -// isCurrentDominantStatus determines whether the current response status is the dominant status among responses. -// -// The dominant status is where the response status is counted the most. -// On tie-breakers, 200 will be selected if included. -// -// Function assumes runningTotalPerStatus can be accessed safely concurrently. -func isCurrentDominantStatus(respStatus int, currentTotalPerStatus map[int]int, currentMax int) bool { - // mark status as dominant if - // - running total for status is the max and response is 200 or - // - running total for status is the max and count for 200 is lower - return currentTotalPerStatus[respStatus] == currentMax && (respStatus == 200 || currentTotalPerStatus[200] < currentMax) -} - func joinUrl(baseURl string, paths ...string) (*url.URL, error) { u, err := url.Parse(baseURl) if err != nil { diff --git a/zboxcore/zboxutil/http_test.go b/zboxcore/zboxutil/http_test.go index aa07f08a1..27da8395a 100644 --- a/zboxcore/zboxutil/http_test.go +++ b/zboxcore/zboxutil/http_test.go @@ -1,6 +1,7 @@ package zboxutil import ( + coreHttp "github.com/0chain/gosdk/core/http" "testing" "github.com/stretchr/testify/assert" @@ -59,7 +60,7 @@ func TestIsCurrentDominantStatus(t *testing.T) { } { tt := tc t.Run(tt.name, func(t *testing.T) { - got := isCurrentDominantStatus(tt.status, tt.runningTotalPerStatus, tt.runningMax) + got := coreHttp.IsCurrentDominantStatus(tt.status, tt.runningTotalPerStatus, tt.runningMax) assert.Equal(t, tt.wantIsDominant, got) }) diff --git a/zcnbridge/http/client.go b/zcnbridge/http/client.go index 008c4ad3c..99d7c33bc 100644 --- a/zcnbridge/http/client.go +++ b/zcnbridge/http/client.go @@ -1,10 +1,10 @@ package http import ( + http2 "github.com/0chain/gosdk/core/http" "net/http" "time" - "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/hashicorp/go-retryablehttp" ) @@ -16,13 +16,13 @@ const ( // NewClient creates default http.Client with timeouts. func NewClient() *http.Client { return &http.Client{ - Transport: zboxutil.DefaultTransport, + Transport: http2.DefaultTransport, } } func CleanClient() *http.Client { client := &http.Client{ - Transport: zboxutil.DefaultTransport, + Transport: http2.DefaultTransport, } client.Timeout = 250 * time.Second return client @@ -32,7 +32,7 @@ func CleanClient() *http.Client { func NewRetryableClient(verbose bool) *retryablehttp.Client { client := retryablehttp.NewClient() client.HTTPClient = &http.Client{ - Transport: zboxutil.DefaultTransport, + Transport: http2.DefaultTransport, } if !verbose { diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 996bac5f5..3604ec558 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -1099,6 +1099,8 @@ func getBlobbersInternal(cb GetInfoCallback, active bool, limit, offset int) { } // GetBlobber obtains blobber information. +// - blobberID: blobber id +// - cb: info callback instance, carries the response of the GET request to the sharders func GetBlobber(blobberID string, cb GetInfoCallback) (err error) { if err = CheckConfig(); err != nil { return @@ -1110,6 +1112,9 @@ func GetBlobber(blobberID string, cb GetInfoCallback) (err error) { return } +// GetValidator obtains validator information. +// - validatorID: validator id +// - cb: info callback instance, carries the response of the GET request to the sharders func GetValidator(validatorID string, cb GetInfoCallback) (err error) { if err = CheckConfig(); err != nil { return @@ -1121,6 +1126,9 @@ func GetValidator(validatorID string, cb GetInfoCallback) (err error) { return } +// GetAuthorizer obtains authorizer information from the sharders. +// - authorizerID: authorizer id +// - cb: info callback instance, carries the response of the GET request to the sharders func GetAuthorizer(authorizerID string, cb GetInfoCallback) (err error) { if err = CheckConfig(); err != nil { return @@ -1132,6 +1140,9 @@ func GetAuthorizer(authorizerID string, cb GetInfoCallback) (err error) { return } +// GetMinerSharder obtains miner sharder information from the sharders. +// - id: miner sharder id +// - cb: info callback instance, carries the response of the GET request to the sharders func GetMinerSharder(id string, cb GetInfoCallback) (err error) { if err = CheckConfig(); err != nil { return @@ -1194,6 +1205,9 @@ func Encrypt(key, text string) (string, error) { return hex.EncodeToString(response), nil } +// Decrypt decrypts encrypted text using the key. +// - key: key to use for decryption +// - text: text to decrypt func Decrypt(key, text string) (string, error) { keyBytes := []byte(key) textBytes, _ := hex.DecodeString(text) @@ -1225,6 +1239,7 @@ func CryptoJsDecrypt(passphrase, encryptedMessage string) (string, error) { return string(dec), nil } +// GetPublicEncryptionKey returns the public encryption key for the given mnemonic func GetPublicEncryptionKey(mnemonic string) (string, error) { encScheme := encryption.NewEncryptionScheme() _, err := encScheme.Initialize(mnemonic) From 91a68aa43265afbcd04ed5e6125b8c42ff8427b6 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 24 Aug 2024 01:17:44 +0530 Subject: [PATCH 023/319] Fix --- core/http/http.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/http/http.go b/core/http/http.go index 90442d726..a3b9b638a 100644 --- a/core/http/http.go +++ b/core/http/http.go @@ -90,7 +90,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] maxCount = responses[response.StatusCode] } - if isCurrentDominantStatus(response.StatusCode, responses, maxCount) { + if IsCurrentDominantStatus(response.StatusCode, responses, maxCount) { dominant = response.StatusCode retObj = entityBytes } From c84685a2ecb31eefe65d9ff2d9c4bb07ad46c2ac Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 24 Aug 2024 01:21:22 +0530 Subject: [PATCH 024/319] Fix unit tests --- core/tokenrate/tokenrate_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/tokenrate/tokenrate_test.go b/core/tokenrate/tokenrate_test.go index 419693974..f0ae824e2 100644 --- a/core/tokenrate/tokenrate_test.go +++ b/core/tokenrate/tokenrate_test.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/stretchr/testify/mock" "io/ioutil" "log" "net/http" @@ -12,7 +13,7 @@ import ( "testing" "time" - "github.com/stretchr/testify/mock" + "github.com/0chain/gosdk/zboxcore/mocks" "github.com/stretchr/testify/require" "github.com/0chain/gosdk/core/resty" From 69bb78cd7ee105aae452ec9da694b803fcac74fe Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 24 Aug 2024 20:25:13 +0530 Subject: [PATCH 025/319] Fix unit tests --- wasmsdk/bridge.go | 3 ++- zcncore/transaction_mobile.go | 13 ------------- zcncore/transaction_query.go | 4 ++-- zcncore/wallet_base.go | 5 ----- 4 files changed, 4 insertions(+), 21 deletions(-) diff --git a/wasmsdk/bridge.go b/wasmsdk/bridge.go index ff69e5409..af2d8f181 100644 --- a/wasmsdk/bridge.go +++ b/wasmsdk/bridge.go @@ -4,6 +4,7 @@ import ( "context" "encoding/base64" "encoding/json" + "github.com/0chain/gosdk/core/client" "path" "strconv" "time" @@ -37,7 +38,7 @@ func initBridge( gasLimit uint64, value int64, consensusThreshold float64) error { - if len(zcncore.GetWalletRaw().ClientID) == 0 { + if len(client.Wallet().ClientID) == 0 { return errors.New("wallet_error", "wallet is not set") } diff --git a/zcncore/transaction_mobile.go b/zcncore/transaction_mobile.go index 008ffd489..05c0cad0d 100644 --- a/zcncore/transaction_mobile.go +++ b/zcncore/transaction_mobile.go @@ -60,19 +60,6 @@ type TransactionCommon interface { MinerSCCollectReward(providerId string, providerType int) error StorageSCCollectReward(providerId string, providerType int) error - FinalizeAllocation(allocID string) error - CancelAllocation(allocID string) error - CreateAllocation(car *CreateAllocationRequest, lock string) error // - CreateReadPool() error - ReadPoolLock(allocID string, blobberID string, duration int64, lock string) error - ReadPoolUnlock() error - StakePoolLock(providerId string, providerType int, lock string) error - StakePoolUnlock(providerId string, providerType int) error - UpdateBlobberSettings(blobber Blobber) error - UpdateAllocation(allocID string, sizeDiff int64, expirationDiff int64, lock string) error - WritePoolLock(allocID string, lock string) error - WritePoolUnlock(allocID string) error - VestingUpdateConfig(InputMap) error MinerScUpdateConfig(InputMap) error MinerScUpdateGlobals(InputMap) error diff --git a/zcncore/transaction_query.go b/zcncore/transaction_query.go index 9588f4cf0..38d065d75 100644 --- a/zcncore/transaction_query.go +++ b/zcncore/transaction_query.go @@ -231,7 +231,7 @@ func (tq *TransactionQuery) getRandomSharderWithHealthcheck(ctx context.Context) return "", ErrNoOnlineSharders } -//getRandomMiner returns a random miner +// getRandomMiner returns a random miner func (tq *TransactionQuery) getRandomMiner(ctx context.Context) (string, error) { if tq.miners == nil || len(tq.miners) == 0 { @@ -541,7 +541,7 @@ func GetInfoFromSharders(urlSuffix string, op int, cb GetInfoCallback) { qr, err := tq.GetInfo(context.TODO(), urlSuffix) if err != nil { if qr != nil && op == OpGetMintNonce { - logging.Debug("OpGetMintNonce QueryResult error", "; Content = ", qr.Content, "; Error = ", qr.Error.Error(), "; StatusCode = ", qr.StatusCode) + logging.Debug("OpGetMintNonce QueryResult error", "; Content = ", qr.Content, "; Error = ", qr.Error.Error(), "; StatusCode = ", qr.StatusCode) cb.OnInfoAvailable(op, qr.StatusCode, "", qr.Error.Error()) return } diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 3604ec558..cb7542deb 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -437,11 +437,6 @@ func IsMnemonicValid(mnemonic string) bool { return zcncrypto.IsMnemonicValid(mnemonic) } -// Deprecated: Use client.Wallet() in core/client package -func GetWalletRaw() zcncrypto.Wallet { - return *client.Wallet() -} - // SetWalletInfo should be set before any transaction or client specific APIs // splitKeyWallet parameter is valid only if SignatureScheme is "BLS0Chain" // From be9fcce18009777b3ce488221e9184e841ea5dcc Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 24 Aug 2024 21:24:59 +0530 Subject: [PATCH 026/319] Fix --- zboxcore/zboxutil/http.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index a88df27eb..b2b5f752e 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -837,7 +837,8 @@ func HttpDo(ctx context.Context, cncl context.CancelFunc, req *http.Request, f f var err error for { // Perform the request with the context provided. - resp, err := Client.Do(req.WithContext(ctx)) + var resp *http.Response + resp, err = Client.Do(req.WithContext(ctx)) if errors.Is(err, io.EOF) { // If the error is io.EOF, continue to retry indefinitely. continue From 2512e2c21f1054fbfba3cdb49e6c2ffdeafc3d4c Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 24 Aug 2024 21:33:07 +0530 Subject: [PATCH 027/319] Fix wallet base --- zcncore/wallet_base.go | 192 +++++++++++++++++++++++++++++------------ 1 file changed, 138 insertions(+), 54 deletions(-) diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index cb7542deb..02b0a823e 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -473,10 +473,10 @@ func SetAuthUrl(url string) error { return client.SetAuthUrl(url) } -func getWalletBalance(clientId string) (common.Balance, error) { +func getWalletBalance(clientId string) (common.Balance, int64, error) { err := checkSdkInit() if err != nil { - return 0, err + return 0, 0, err } cb := &walletCallback{} @@ -494,13 +494,19 @@ func getWalletBalance(clientId string) (common.Balance, error) { cb.Wait() - return cb.balance, cb.err + var clientState struct { + Nonce int64 `json:"nonce"` + } + err = json.Unmarshal([]byte(cb.info), &clientState) + if err != nil { + return 0, 0, err + } + + return cb.balance, clientState.Nonce, cb.err } // GetBalance retrieve wallet balance from sharders -// -// # Inputs -// - cb: callback for checking result +// - cb: info callback instance, carries the response of the GET request to the sharders func GetBalance(cb GetBalanceCallback) error { err := CheckConfig() if err != nil { @@ -518,7 +524,8 @@ func GetBalance(cb GetBalanceCallback) error { return nil } -// GetMintNonce retrieve mint nonce from sharders +// GetMintNonce retrieve the client's latest mint nonce from sharders +// - cb: info callback instance, carries the response of the GET request to the sharders func GetMintNonce(cb GetInfoCallback) error { err := CheckConfig() if err != nil { @@ -531,7 +538,10 @@ func GetMintNonce(cb GetInfoCallback) error { return nil } -// GetNotProcessedZCNBurnTickets retrieve wallet burn tickets from sharders +// GetNotProcessedZCNBurnTickets retrieve burn tickets that are not compensated by minting +// - ethereumAddress: ethereum address for the issuer of the burn tickets +// - startNonce: start nonce for the burn tickets +// - cb: info callback instance, carries the response of the GET request to the sharders func GetNotProcessedZCNBurnTickets(ethereumAddress, startNonce string, cb GetInfoCallback) error { err := CheckConfig() if err != nil { @@ -546,7 +556,8 @@ func GetNotProcessedZCNBurnTickets(ethereumAddress, startNonce string, cb GetInf return nil } -// GetNonce GetBalance retrieve wallet nonce from sharders +// GetNonce retrieve wallet nonce from sharders +// - cb: info callback instance, carries the response of the GET request to the sharders func GetNonce(cb GetNonceCallback) error { if cb == nil { cb = &GetNonceCallbackStub{} @@ -571,7 +582,8 @@ func GetNonce(cb GetNonceCallback) error { return nil } -// GetWalletNonce GetWalletBalance retrieve wallet nonce from sharders +// GetWalletBalance retrieve wallet nonce from sharders +// - clientID: client id func GetWalletNonce(clientID string) (int64, error) { cb := &GetNonceCallbackStub{} @@ -602,6 +614,8 @@ func GetWalletNonce(clientID string) (int64, error) { } // GetBalanceWallet retreives wallet balance from sharders +// - walletStr: wallet string +// - cb: info callback instance, carries the response of the GET request to the sharders func GetBalanceWallet(walletStr string, cb GetBalanceCallback) error { w, err := getWallet(walletStr) if err != nil { @@ -638,12 +652,13 @@ func getNonceFromSharders(clientID string) (int64, string, error) { } // ConvertToToken converts the SAS tokens to ZCN tokens -// # Inputs -// - token: SAS tokens +// - token: SAS tokens amount func ConvertToToken(token int64) float64 { return float64(token) / float64(common.TokenUnit) } +// ConvertTokenToUSD converts the ZCN tokens to USD amount +// - token: ZCN tokens amount func ConvertTokenToUSD(token float64) (float64, error) { zcnRate, err := getTokenUSDRate() if err != nil { @@ -652,6 +667,8 @@ func ConvertTokenToUSD(token float64) (float64, error) { return token * zcnRate, nil } +// ConvertUSDToToken converts the USD amount to ZCN tokens +// - usd: USD amount func ConvertUSDToToken(usd float64) (float64, error) { zcnRate, err := getTokenUSDRate() if err != nil { @@ -676,7 +693,8 @@ func getWallet(walletStr string) (*zcncrypto.Wallet, error) { return &w, nil } -// GetWalletClientID -- given a walletstr return ClientID +// GetWalletClientID extract wallet client id from wallet string +// - walletStr: wallet string to get client id func GetWalletClientID(walletStr string) (string, error) { w, err := getWallet(walletStr) if err != nil { @@ -714,6 +732,8 @@ func SetupAuth(authHost, clientID, clientKey, publicKey, privateKey, localPublic return nil } +// GetIdForUrl retrieve the ID of the network node (miner/sharder) given its url. +// - url: url of the node. func GetIdForUrl(url string) string { url = strings.TrimRight(url, "/") url = fmt.Sprintf("%v/_nh/whoami", url) @@ -757,49 +777,50 @@ func (p Params) Query() string { // // GetMiners obtains list of all active miners. -// -// # Inputs -// - cb: callback for checking result -// - limit: how many miners should be fetched -// - offset: how many miners should be skipped -// - active: only fetch active miners -func GetMiners(cb GetInfoCallback, limit, offset int, active bool) { - getMinersInternal(cb, active, limit, offset) +// - cb: info callback instance, carries the response of the GET request to the sharders +// - limit: how many miners should be fetched +// - offset: how many miners should be skipped +// - active: retrieve only active miners +// - stakable: retreive only stakable miners +func GetMiners(cb GetInfoCallback, limit, offset int, active bool, stakable bool) { + getMinersInternal(cb, active, stakable, limit, offset) } -func getMinersInternal(cb GetInfoCallback, active bool, limit, offset int) { +func getMinersInternal(cb GetInfoCallback, active, stakable bool, limit, offset int) { if err := CheckConfig(); err != nil { return } var url = withParams(GET_MINERSC_MINERS, Params{ - "active": strconv.FormatBool(active), - "offset": strconv.FormatInt(int64(offset), 10), - "limit": strconv.FormatInt(int64(limit), 10), + "active": strconv.FormatBool(active), + "stakable": strconv.FormatBool(stakable), + "offset": strconv.FormatInt(int64(offset), 10), + "limit": strconv.FormatInt(int64(limit), 10), }) go GetInfoFromSharders(url, 0, cb) } -// GetSharders obtains list of all active sharders. -// # Inputs -// - cb: callback for checking result +// GetSharders obtains a list of sharders given the following parameters. +// - cb: info callback instance, carries the response of the GET request to the sharders // - limit: how many sharders should be fetched // - offset: how many sharders should be skipped -// - active: only fetch active sharders -func GetSharders(cb GetInfoCallback, limit, offset int, active bool) { - getShardersInternal(cb, active, limit, offset) +// - active: retrieve only active sharders +// - stakable: retrieve only sharders that can be staked +func GetSharders(cb GetInfoCallback, limit, offset int, active, stakable bool) { + getShardersInternal(cb, active, stakable, limit, offset) } -func getShardersInternal(cb GetInfoCallback, active bool, limit, offset int) { +func getShardersInternal(cb GetInfoCallback, active, stakable bool, limit, offset int) { if err := CheckConfig(); err != nil { return } var url = withParams(GET_MINERSC_SHARDERS, Params{ - "active": strconv.FormatBool(active), - "offset": strconv.FormatInt(int64(offset), 10), - "limit": strconv.FormatInt(int64(limit), 10), + "active": strconv.FormatBool(active), + "stakable": strconv.FormatBool(stakable), + "offset": strconv.FormatInt(int64(offset), 10), + "limit": strconv.FormatInt(int64(limit), 10), }) go GetInfoFromSharders(url, 0, cb) @@ -810,9 +831,8 @@ func withParams(uri string, params Params) string { } // GetMinerSCNodeInfo get miner information from sharders -// # Inputs // - id: the id of miner -// - cb: callback for checking result +// - cb: info callback instance, carries the response of the GET request to the sharders func GetMinerSCNodeInfo(id string, cb GetInfoCallback) (err error) { if err = CheckConfig(); err != nil { @@ -825,6 +845,9 @@ func GetMinerSCNodeInfo(id string, cb GetInfoCallback) (err error) { return } +// GetMinerSCNodePool get miner smart contract node pool +// - id: the id of miner +// - cb: info callback instance, carries the response of the GET request to the sharders func GetMinerSCNodePool(id string, cb GetInfoCallback) (err error) { if err = CheckConfig(); err != nil { return @@ -837,10 +860,9 @@ func GetMinerSCNodePool(id string, cb GetInfoCallback) (err error) { return } -// GetMinerSCUserInfo get user pool -// # Inputs -// - clientID: the id of wallet -// - cb: callback for checking result +// GetMinerSCUserInfo retrieve user stake pools for the providers related to the Miner SC (miners/sharders). +// - clientID: user's wallet id +// - cb: info callback instance, carries the response of the GET request to the sharders func GetMinerSCUserInfo(clientID string, cb GetInfoCallback) (err error) { if err = CheckConfig(); err != nil { return @@ -855,6 +877,8 @@ func GetMinerSCUserInfo(clientID string, cb GetInfoCallback) (err error) { return } +// GetMinerSCConfig get miner SC configuration +// - cb: info callback instance, carries the response of the GET request to the sharders func GetMinerSCConfig(cb GetInfoCallback) (err error) { if err = CheckConfig(); err != nil { return @@ -863,6 +887,8 @@ func GetMinerSCConfig(cb GetInfoCallback) (err error) { return } +// GetMinerSCGlobals get miner SC globals +// - cb: info callback instance, carries the response of the GET request to the sharders func GetMinerSCGlobals(cb GetInfoCallback) (err error) { if err = CheckConfig(); err != nil { return @@ -876,6 +902,7 @@ func GetMinerSCGlobals(cb GetInfoCallback) (err error) { // // GetStorageSCConfig obtains Storage SC configurations. +// - cb: info callback instance, carries the response of the GET request to the sharders func GetStorageSCConfig(cb GetInfoCallback) (err error) { if err = CheckConfig(); err != nil { return @@ -923,7 +950,12 @@ func GetAllocations(clientID string, cb GetInfoCallback) (err error) { return } -// GetSnapshots obtains list of allocations of a user. +// GetSnapshots obtains list of global snapshots, given an initial round and a limit. +// Global snapshots are historical records of some aggregate data related +// to the network (like total staked amount and total reward amount). +// - round: round number to start fetching snapshots +// - limit: how many snapshots should be fetched +// - cb: info callback instance, carries the response of the GET request to the sharders func GetSnapshots(round int64, limit int64, cb GetInfoCallback) (err error) { if err = CheckConfig(); err != nil { return @@ -937,6 +969,13 @@ func GetSnapshots(round int64, limit int64, cb GetInfoCallback) (err error) { } // GetBlobberSnapshots obtains list of allocations of a blobber. +// Blobber snapshots are historical records of the blobber instance to track its change over time and serve graph requests, +// which are requests that need multiple data points, distributed over an interval of time, usually to plot them on a +// graph. +// - round: round number +// - limit: how many blobber snapshots should be fetched +// - offset: how many blobber snapshots should be skipped +// - cb: info callback instance, carries the response of the GET request to the sharders func GetBlobberSnapshots(round int64, limit int64, offset int64, cb GetInfoCallback) (err error) { if err = CheckConfig(); err != nil { return @@ -950,7 +989,14 @@ func GetBlobberSnapshots(round int64, limit int64, offset int64, cb GetInfoCallb return } -// GetMinerSnapshots obtains list of allocations of a miner. +// GetMinerSnapshots obtains a list of miner snapshots starting from a specific round. +// Miner snapshots are historical records of the miner instance to track its change over time and serve graph requests, +// which are requests that need multiple data points, distributed over an interval of time, usually to plot them on a +// graph. +// - round: round number to start fetching snapshots +// - limit: how many miner snapshots should be fetched +// - offset: how many miner snapshots should be skipped +// - cb: info callback instance, carries the response of the GET request to the sharders func GetMinerSnapshots(round int64, limit int64, offset int64, cb GetInfoCallback) (err error) { if err = CheckConfig(); err != nil { return @@ -964,7 +1010,14 @@ func GetMinerSnapshots(round int64, limit int64, offset int64, cb GetInfoCallbac return } -// GetSharderSnapshots obtains list of allocations of a sharder. +// GetSharderSnapshots obtains a list of sharder snapshots starting from a specific round. +// Sharder snapshots are historical records of the sharder instance to track its change over time and serve graph requests, +// which are requests that need multiple data points, distributed over an interval of time, usually to plot them on a +// graph. +// - round: round number to start fetching snapshots +// - limit: how many sharder snapshots should be fetched +// - offset: how many sharder snapshots should be skipped +// - cb: info callback instance, carries the response of the GET request to the sharders func GetSharderSnapshots(round int64, limit int64, offset int64, cb GetInfoCallback) (err error) { if err = CheckConfig(); err != nil { return @@ -978,7 +1031,14 @@ func GetSharderSnapshots(round int64, limit int64, offset int64, cb GetInfoCallb return } -// GetValidatorSnapshots obtains list of allocations of a validator. +// GetValidatorSnapshots obtains list of validator snapshots from the sharders. +// Validator snapshots are historical records of the validator instance to track its change over time and serve graph requests, +// which are requests that need multiple data points, distributed over an interval of time, usually to plot them on a +// graph. +// - round: round number to start fetching snapshots +// - limit: how many validator snapshots should be fetched +// - offset: how many validator snapshots should be skipped +// - cb: info callback instance, carries the response of the GET request to the sharders func GetValidatorSnapshots(round int64, limit int64, offset int64, cb GetInfoCallback) (err error) { if err = CheckConfig(); err != nil { return @@ -992,7 +1052,14 @@ func GetValidatorSnapshots(round int64, limit int64, offset int64, cb GetInfoCal return } -// GetAuthorizerSnapshots obtains list of allocations of an authorizer. +// GetAuthorizerSnapshots obtains list of authorizers snapshots from the sharders. +// Authorizer snapshots are historical records of the authorizer instance to track its change over time and serve graph requests, +// which are requests that need multiple data points, distributed over an interval of time, usually to plot them on a +// graph. +// - round: round number to start fetching snapshots +// - limit: how many authorizer snapshots should be fetched +// - offset: how many authorizer snapshots should be skipped +// - cb: info callback instance, carries the response of the GET request to the sharders func GetAuthorizerSnapshots(round int64, limit int64, offset int64, cb GetInfoCallback) (err error) { if err = CheckConfig(); err != nil { return @@ -1006,7 +1073,14 @@ func GetAuthorizerSnapshots(round int64, limit int64, offset int64, cb GetInfoCa return } -// GetUserSnapshots replicates user aggregates from events_db. +// GetUserSnapshots replicates user snapshots from the sharders +// User snapshots are historical records of the client data to track its change over time and serve graph requests, +// which are requests that need multiple data points, distributed over an interval of time, usually to plot them on a +// graph. +// - round: round number to start fetching snapshots +// - limit: how many user snapshots should be fetched +// - offset: how many user snapshots should be skipped +// - cb: info callback instance, carries the response of the GET request to the sharders func GetUserSnapshots(round int64, limit int64, offset int64, cb GetInfoCallback) (err error) { if err = CheckConfig(); err != nil { return @@ -1069,25 +1143,35 @@ func GetStakePoolUserInfo(clientID string, offset, limit int, cb GetInfoCallback return } -// GetBlobbers obtains list of all active blobbers. +// GetStakeableBlobbers obtains list of all active blobbers that can be staked (i.e. still number of delegations < max_delegations) // # Inputs -// - cb: callback for checking result +// - cb: info callback instance, carries the response of the GET request to the sharders +// - limit: how many blobbers should be fetched +// - offset: how many blobbers should be skipped +// - active: only fetch active blobbers +func GetStakableBlobbers(cb GetInfoCallback, limit, offset int, active bool) { + getBlobbersInternal(cb, active, limit, offset, true) +} + +// GetBlobbers obtains list of all active blobbers. +// - cb: info callback instance, carries the response of the GET request to the sharders // - limit: how many blobbers should be fetched // - offset: how many blobbers should be skipped // - active: only fetch active blobbers func GetBlobbers(cb GetInfoCallback, limit, offset int, active bool) { - getBlobbersInternal(cb, active, limit, offset) + getBlobbersInternal(cb, active, limit, offset, false) } -func getBlobbersInternal(cb GetInfoCallback, active bool, limit, offset int) { +func getBlobbersInternal(cb GetInfoCallback, active bool, limit, offset int, stakable bool) { if err := CheckConfig(); err != nil { return } var url = withParams(STORAGESC_GET_BLOBBERS, Params{ - "active": strconv.FormatBool(active), - "offset": strconv.FormatInt(int64(offset), 10), - "limit": strconv.FormatInt(int64(limit), 10), + "active": strconv.FormatBool(active), + "offset": strconv.FormatInt(int64(offset), 10), + "limit": strconv.FormatInt(int64(limit), 10), + "stakable": strconv.FormatBool(stakable), }) go GetInfoFromSharders(url, OpStorageSCGetBlobbers, cb) From 572cf09e42f8cfba0c36f98e54b1ab5ba17f85b5 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 24 Aug 2024 21:36:45 +0530 Subject: [PATCH 028/319] Fix sdk build --- winsdk/sdk.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/winsdk/sdk.go b/winsdk/sdk.go index 99449dc5d..86227ae14 100644 --- a/winsdk/sdk.go +++ b/winsdk/sdk.go @@ -156,18 +156,14 @@ func InitWallet(clientJson *C.char) *C.char { l.Logger.Info("Start InitWallet") clientJs := C.GoString(clientJson) - + var w zcncrypto.Wallet err := json.Unmarshal([]byte(clientJs), &w) if err != nil { l.Logger.Error(err) return WithJSON(false, err) } - err = client.SetWallet(w) - if err != nil { - l.Logger.Error(err) - return WithJSON(false, err) - } + client.SetWallet(w) l.Logger.Info("InitWallet success") zboxApiClient.SetWallet(client.Wallet().ClientID, client.Wallet().Keys[0].PrivateKey, client.Wallet().ClientKey) From 3575b7cf9ae6ad841cb40b2cd6726971d8ee7238 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 24 Aug 2024 23:14:54 +0530 Subject: [PATCH 029/319] Fix wasm build --- core/client/set.go | 8 ++++++++ core/http/http.go | 19 +++++++++++++++++-- core/http/transport.go | 24 ------------------------ core/version/version.go | 3 +-- wasmsdk/sdk.go | 16 ++++++++-------- wasmsdk/wallet.go | 7 ++----- zboxcore/zboxutil/http.go | 5 +---- zboxcore/zboxutil/transport_wasm.go | 3 ++- 8 files changed, 39 insertions(+), 46 deletions(-) delete mode 100644 core/http/transport.go diff --git a/core/client/set.go b/core/client/set.go index 456df86bd..8ac67fcba 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -181,3 +181,11 @@ func PrivateKey() string { func ClientID() string { return client.wallet.ClientID } + +func GetWallet() *zcncrypto.Wallet { + return client.wallet +} + +func GetClient() *zcncrypto.Wallet { + return client.wallet +} diff --git a/core/http/http.go b/core/http/http.go index a3b9b638a..37d9e1a98 100644 --- a/core/http/http.go +++ b/core/http/http.go @@ -13,8 +13,23 @@ import ( "net/url" "os" "sync" + "time" ) +var DefaultTransport = &http.Transport{ + Proxy: EnvProxy.Proxy, + DialContext: (&net.Dialer{ + Timeout: 3 * time.Minute, + KeepAlive: 45 * time.Second, + DualStack: true, + }).DialContext, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 45 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + MaxIdleConnsPerHost: 25, +} + // SCRestAPIHandler is a function type to handle the response from the SC Rest API // // `response` - the response from the SC Rest API @@ -70,8 +85,8 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] q.Add(k, v) } urlObj.RawQuery = q.Encode() - client := &http.Client{Transport: DefaultTransport} - response, err := client.Get(urlObj.String()) + clientObj := &http.Client{Transport: DefaultTransport} + response, err := clientObj.Get(urlObj.String()) if err != nil { nodeClient.Sharders().Fail(sharder) return diff --git a/core/http/transport.go b/core/http/transport.go deleted file mode 100644 index 01d56dc4d..000000000 --- a/core/http/transport.go +++ /dev/null @@ -1,24 +0,0 @@ -//go:build !js && !wasm -// +build !js,!wasm - -package http - -import ( - "net" - "net/http" - "time" -) - -var DefaultTransport = &http.Transport{ - Proxy: EnvProxy.Proxy, - DialContext: (&net.Dialer{ - Timeout: 3 * time.Minute, - KeepAlive: 45 * time.Second, - DualStack: true, - }).DialContext, - MaxIdleConns: 100, - IdleConnTimeout: 90 * time.Second, - TLSHandshakeTimeout: 45 * time.Second, - ExpectContinueTimeout: 1 * time.Second, - MaxIdleConnsPerHost: 25, -} diff --git a/core/version/version.go b/core/version/version.go index b4b991d41..00af60db0 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -1,6 +1,5 @@ - //====== THIS IS AUTOGENERATED FILE. DO NOT MODIFY ======== package version -const VERSIONSTR = "v1.16.3-10-g66360b13" +const VERSIONSTR = "v1.17.0-RC3-31-g572cf09e" diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index 0df1c0942..ca05366a2 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -9,6 +9,7 @@ import ( "encoding/json" "errors" "fmt" + coreHttp "github.com/0chain/gosdk/core/http" "io" "os" "sync" @@ -19,7 +20,6 @@ import ( "github.com/0chain/gosdk/core/imageutil" "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/zboxcore/sdk" - "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/0chain/gosdk/zcncore" ) @@ -47,13 +47,13 @@ func initSDKs(chainID, blockWorker, signatureScheme string, } err = client.Init(context.Background(), conf.Config{ - BlockWorker: blockWorker, - SignatureScheme: signatureScheme, - ChainID: chainID, - MinConfirmation: minConfirmation, - MinSubmit: minSubmit, + BlockWorker: blockWorker, + SignatureScheme: signatureScheme, + ChainID: chainID, + MinConfirmation: minConfirmation, + MinSubmit: minSubmit, ConfirmationChainLength: confirmationChainLength, - SharderConsensous: sharderconsensous, + SharderConsensous: sharderconsensous, }) if err != nil { @@ -155,7 +155,7 @@ func makeSCRestAPICall(scAddress, relativePath, paramsJson string) (string, erro if err != nil { sdkLogger.Error(fmt.Sprintf("Error parsing JSON: %v", err)) } - b, err := zboxutil.MakeSCRestAPICall(scAddress, relativePath, params, nil) + b, err := coreHttp.MakeSCRestAPICall(scAddress, relativePath, params, nil) return string(b), err } diff --git a/wasmsdk/wallet.go b/wasmsdk/wallet.go index 7d0cd83ba..976727c62 100644 --- a/wasmsdk/wallet.go +++ b/wasmsdk/wallet.go @@ -6,8 +6,8 @@ package main import ( "errors" - "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/zcncrypto" ) // setWallet sets the wallet used by the client for the network transactions and the backend API requests @@ -32,10 +32,7 @@ func setWallet(clientID, publicKey, privateKey, mnemonic string) error { Mnemonic: mnemonic, Keys: keys, } - err := client.SetWallet(*w) - if err != nil { - return err - } + client.SetWallet(*w) zboxApiClient.SetWallet(clientID, privateKey, publicKey) diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index b2b5f752e..2bce17933 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - http2 "github.com/0chain/gosdk/core/http" "io" "net/http" "net/url" @@ -86,9 +85,7 @@ func GetFastHTTPClient() *fasthttp.Client { } func init() { - Client = &http.Client{ - Transport: http2.DefaultTransport, - } + Client = &http.Client{Transport: coreHttp.DefaultTransport} FastHttpClient = &fasthttp.Client{ MaxIdleConnDuration: 45 * time.Second, diff --git a/zboxcore/zboxutil/transport_wasm.go b/zboxcore/zboxutil/transport_wasm.go index 553f6d807..213c9b220 100644 --- a/zboxcore/zboxutil/transport_wasm.go +++ b/zboxcore/zboxutil/transport_wasm.go @@ -4,12 +4,13 @@ package zboxutil import ( + coreHttp "github.com/0chain/gosdk/core/http" "net/http" "time" ) var DefaultTransport = &http.Transport{ - Proxy: envProxy.Proxy, + Proxy: coreHttp.EnvProxy.Proxy, MaxIdleConns: 100, IdleConnTimeout: 60 * time.Second, From 75009fe9b38097c26c5c6a8c76d673240b22999f Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 24 Aug 2024 23:37:25 +0530 Subject: [PATCH 030/319] Fix mobile build --- core/version/version.go | 2 +- go.mod | 17 +++++++++-------- go.sum | 28 ++++++++++++++-------------- mobilesdk/sdk/sdk.go | 4 +++- zcncore/transaction.go | 17 +++++++++++++++++ zcncore/transaction_mobile.go | 15 +++++++++++++++ 6 files changed, 59 insertions(+), 24 deletions(-) diff --git a/core/version/version.go b/core/version/version.go index 00af60db0..0b49ad02a 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.0-RC3-31-g572cf09e" +const VERSIONSTR = "v1.17.0-RC3-32-g3575b7cf" diff --git a/go.mod b/go.mod index 9f85673bd..8580f176e 100644 --- a/go.mod +++ b/go.mod @@ -33,9 +33,9 @@ require ( github.com/uptrace/bunrouter v1.0.20 go.dedis.ch/kyber/v3 v3.1.0 go.uber.org/zap v1.24.0 - golang.org/x/crypto v0.17.0 - golang.org/x/image v0.14.0 - golang.org/x/sync v0.5.0 + golang.org/x/crypto v0.26.0 + golang.org/x/image v0.19.0 + golang.org/x/sync v0.8.0 google.golang.org/grpc v1.53.0 gopkg.in/cheggaaa/pb.v1 v1.0.28 gopkg.in/natefinch/lumberjack.v2 v2.2.1 @@ -47,6 +47,7 @@ require ( github.com/hack-pad/go-webworkers v0.1.0 github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d github.com/minio/sha256-simd v1.0.1 + github.com/valyala/bytebufferpool v1.0.0 github.com/ybbus/jsonrpc/v3 v3.1.5 ) @@ -78,6 +79,7 @@ require ( github.com/golang/protobuf v1.5.2 // indirect github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect github.com/google/flatbuffers v22.9.29+incompatible // indirect + github.com/google/go-cmp v0.6.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/hashicorp/go-bexpr v0.1.10 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect @@ -116,13 +118,12 @@ require ( github.com/tklauser/go-sysconf v0.3.11 // indirect github.com/tklauser/numcpus v0.6.0 // indirect github.com/urfave/cli/v2 v2.17.2-0.20221006022127-8f469abc00aa // indirect - github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.51.0 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect go.dedis.ch/fixbuf v1.0.3 // indirect go.opencensus.io v0.24.0 // indirect - golang.org/x/sys v0.15.0 // indirect - golang.org/x/text v0.14.0 // indirect + golang.org/x/sys v0.23.0 // indirect + golang.org/x/text v0.17.0 // indirect google.golang.org/protobuf v1.28.1 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/ini.v1 v1.67.0 // indirect @@ -138,9 +139,9 @@ require ( github.com/yusufpapurcu/wmi v1.2.2 // indirect go.uber.org/atomic v1.10.0 // indirect go.uber.org/multierr v1.9.0 // indirect - golang.org/x/net v0.19.0 // indirect + golang.org/x/net v0.28.0 // indirect golang.org/x/time v0.3.0 // indirect google.golang.org/genproto v0.0.0-20230216225411-c8e22ba71e44 // indirect ) -//replace github.com/ethereum/go-ethereum => github.com/certifaction/go-ethereum v1.10.3-wasm \ No newline at end of file +//replace github.com/ethereum/go-ethereum => github.com/certifaction/go-ethereum v1.10.3-wasm diff --git a/go.sum b/go.sum index d64c6f548..324d90b99 100644 --- a/go.sum +++ b/go.sum @@ -253,8 +253,8 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -601,8 +601,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= -golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= +golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -619,8 +619,8 @@ golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMk golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= -golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4= -golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= +golang.org/x/image v0.19.0 h1:D9FX4QWkLfkeqaC62SonffIIuYdOk/UE2XKUBgRIBIQ= +golang.org/x/image v0.19.0/go.mod h1:y0zrRqlQRWQ5PXaYCOMLTW2fpsxZ8Qh9I/ohnInJEys= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= @@ -680,8 +680,8 @@ golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= -golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= +golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -702,8 +702,8 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= -golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -757,8 +757,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= +golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -766,8 +766,8 @@ golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3 golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/mobilesdk/sdk/sdk.go b/mobilesdk/sdk/sdk.go index ef164bf7c..c2ecaa23a 100644 --- a/mobilesdk/sdk/sdk.go +++ b/mobilesdk/sdk/sdk.go @@ -11,10 +11,12 @@ import ( "strconv" "strings" + "context" "github.com/0chain/gosdk/core/sys" "github.com/pkg/errors" "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/util" "github.com/0chain/gosdk/core/version" l "github.com/0chain/gosdk/zboxcore/logger" @@ -67,7 +69,7 @@ func SetLogLevel(logLevel int) { // - chainConfigJson: chain config json string func Init(chainConfigJson string) error { cfg := conf.Config{} - err := json.Unmarshal([]byte(chainConfigJSON), &cfg) + err := json.Unmarshal([]byte(chainConfigJson), &cfg) if err != nil { return err } diff --git a/zcncore/transaction.go b/zcncore/transaction.go index facc2cfc3..b3a98ff2e 100644 --- a/zcncore/transaction.go +++ b/zcncore/transaction.go @@ -134,6 +134,8 @@ type TransactionCommon interface { MinerSCCollectReward(providerID string, providerType Provider) error MinerSCKill(providerID string, providerType Provider) error + StorageSCCollectReward(providerID string, providerType Provider) error + VestingUpdateConfig(*InputMap) error MinerScUpdateConfig(*InputMap) error MinerScUpdateGlobals(*InputMap) error @@ -517,6 +519,21 @@ func (t *Transaction) AddHardfork(ip *InputMap) (err error) { return } +func (t *Transaction) StorageSCCollectReward(providerId string, providerType Provider) error { + pr := &scCollectReward{ + ProviderId: providerId, + ProviderType: int(providerType), + } + err := t.createSmartContractTxn(StorageSmartContractAddress, + transaction.STORAGESC_COLLECT_REWARD, pr, 0) + if err != nil { + logging.Error(err) + return err + } + go t.setNonceAndSubmit() + return err +} + func (t *Transaction) ZCNSCUpdateGlobalConfig(ip *InputMap) (err error) { err = t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_UPDATE_GLOBAL_CONFIG, ip, 0) if err != nil { diff --git a/zcncore/transaction_mobile.go b/zcncore/transaction_mobile.go index 05c0cad0d..8cb51c6b4 100644 --- a/zcncore/transaction_mobile.go +++ b/zcncore/transaction_mobile.go @@ -497,6 +497,21 @@ func (t *Transaction) MinerSCCollectReward(providerId string, providerType int) return err } +func (t *Transaction) StorageSCCollectReward(providerId string, providerType int) error { + pr := &scCollectReward{ + ProviderId: providerId, + ProviderType: providerType, + } + err := t.createSmartContractTxn(StorageSmartContractAddress, + transaction.STORAGESC_COLLECT_REWARD, pr, 0) + if err != nil { + logging.Error(err) + return err + } + go t.setNonceAndSubmit() + return err +} + func (t *Transaction) VestingUpdateConfig(vscc InputMap) (err error) { err = t.createSmartContractTxn(VestingSmartContractAddress, From 96c7a58bb85c799eb8865d8d729693d69164a658 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 25 Aug 2024 00:37:09 +0530 Subject: [PATCH 031/319] Fix zwallet build --- zcncore/transaction.go | 64 ++++++++++++++++++++++++++++++++++++++++++ znft/example/go.mod | 4 +-- znft/example/go.sum | 2 ++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/zcncore/transaction.go b/zcncore/transaction.go index b3a98ff2e..2bca0c186 100644 --- a/zcncore/transaction.go +++ b/zcncore/transaction.go @@ -1216,3 +1216,67 @@ func (t *Transaction) ZCNSCCollectReward(providerId string, providerType Provide go func() { t.setNonceAndSubmit() }() return err } + +type VestingClientList struct { + Pools []common.Key `json:"pools"` +} + +func GetVestingClientList(clientID string, cb GetInfoCallback) (err error) { + if err = CheckConfig(); err != nil { + return + } + if clientID == "" { + clientID = client.ClientID() // if not blank + } + go GetInfoFromSharders(WithParams(GET_VESTING_CLIENT_POOLS, Params{ + "client_id": clientID, + }), 0, cb) + return +} + +type VestingDestInfo struct { + ID common.Key `json:"id"` // identifier + Wanted common.Balance `json:"wanted"` // wanted amount for entire period + Earned common.Balance `json:"earned"` // can unlock + Vested common.Balance `json:"vested"` // already vested + Last common.Timestamp `json:"last"` // last time unlocked +} + +type VestingPoolInfo struct { + ID common.Key `json:"pool_id"` // pool ID + Balance common.Balance `json:"balance"` // real pool balance + Left common.Balance `json:"left"` // owner can unlock + Description string `json:"description"` // description + StartTime common.Timestamp `json:"start_time"` // from + ExpireAt common.Timestamp `json:"expire_at"` // until + Destinations []*VestingDestInfo `json:"destinations"` // receivers + ClientID common.Key `json:"client_id"` // owner +} + +func GetVestingPoolInfo(poolID string, cb GetInfoCallback) (err error) { + if err = CheckConfig(); err != nil { + return + } + GetInfoFromSharders(WithParams(GET_VESTING_POOL_INFO, Params{ + "pool_id": poolID, + }), 0, cb) + return +} + +func GetVestingSCConfig(cb GetInfoCallback) (err error) { + if err = CheckConfig(); err != nil { + return + } + go GetInfoFromSharders(GET_VESTING_CONFIG, 0, cb) + return +} + +// faucet + +func GetFaucetSCConfig(cb GetInfoCallback) (err error) { + if err = CheckConfig(); err != nil { + return + } + go GetInfoFromSharders(GET_FAUCETSC_CONFIG, 0, cb) + return +} diff --git a/znft/example/go.mod b/znft/example/go.mod index da7233de2..7c20400ac 100644 --- a/znft/example/go.mod +++ b/znft/example/go.mod @@ -25,8 +25,8 @@ require ( github.com/tklauser/numcpus v0.6.0 // indirect github.com/tyler-smith/go-bip39 v1.1.0 // indirect github.com/yusufpapurcu/wmi v1.2.2 // indirect - golang.org/x/crypto v0.17.0 // indirect - golang.org/x/sys v0.15.0 // indirect + golang.org/x/crypto v0.26.0 // indirect + golang.org/x/sys v0.23.0 // indirect gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/znft/example/go.sum b/znft/example/go.sum index 288f8e83e..99ba4bdc5 100644 --- a/znft/example/go.sum +++ b/znft/example/go.sum @@ -136,6 +136,7 @@ golang.org/x/crypto v0.0.0-20221012134737-56aed061732a h1:NmSIgad6KjE6VvHciPZuNR golang.org/x/crypto v0.0.0-20221012134737-56aed061732a/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58= golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -161,6 +162,7 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= From aadc08338a3e62467f193cda99d3c0937959400e Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 25 Aug 2024 00:51:32 +0530 Subject: [PATCH 032/319] Repo snapshots --- .github/workflows/system_tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/system_tests.yml b/.github/workflows/system_tests.yml index a25e0cbfe..ae2b2580a 100644 --- a/.github/workflows/system_tests.yml +++ b/.github/workflows/system_tests.yml @@ -28,7 +28,7 @@ jobs: uses: 0chain/actions/.github/workflows/manual_system_tests.yml@master with: gosdk_branch: ${{ github.ref_name }} - repo_snapshots_branch: ${{ github.event.inputs.repo_snapshots_branch }} + repo_snapshots_branch: fix/refactor-zboxcore test_file_filter: ${{ github.event.inputs.test_file_filter }} skip_tests: ${{ github.event.inputs.skip_tests }} run_smoke_tests: ${{ github.event.inputs.run_smoke_tests }} From 6718317b1dde6ec37473a823b79a76af63bb77dd Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 25 Aug 2024 13:20:01 +0530 Subject: [PATCH 033/319] Revert version update --- core/version/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/version/version.go b/core/version/version.go index 0b49ad02a..9213a1e29 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.0-RC3-32-g3575b7cf" +const VERSIONSTR = "v1.16.3-10-g66360b13" From df4f9b85e41c6b0361cd20530a4077b237012a01 Mon Sep 17 00:00:00 2001 From: Jayash Satolia <73050737+Jayashsatolia403@users.noreply.github.com> Date: Sun, 8 Sep 2024 14:45:15 +0530 Subject: [PATCH 034/319] Fix/refactor zboxcore merge sprint 1.18 (#1605) * Update sprint 1.12 (#1341) * wait for repair and increase numBlocks (#1338) * reorder wait group done (#1340) --------- Co-authored-by: Ebrahim Gomaa * fix trailing whitespace (#1343) * Merge staging changes (#1346) * wait for repair and increase numBlocks (#1338) * reorder wait group done (#1340) * hotfix / remove hard coded prefix handling of encrypted upload (#1344) * remove hard coded prefix handling of encrypted upload * fix for other parts of the file --------- Co-authored-by: Hitenjain14 <57557631+Hitenjain14@users.noreply.github.com> Co-authored-by: Ebrahim Gomaa * fix upload select (#1351) * fix workdir in mobile sdk (#1345) * fix mobile workdir * set multi op batch size * set option to download to disk in wasm (#1348) * fix panic in hash chan (#1352) * Fix merge conflict in sprint-1.12 (#1354) * wait for repair and increase numBlocks (#1338) * reorder wait group done (#1340) * hotfix / remove hard coded prefix handling of encrypted upload (#1344) * remove hard coded prefix handling of encrypted upload * fix for other parts of the file * increase batch size (#1349) --------- Co-authored-by: Ebrahim Gomaa * repair in batches (#1347) * repair in batches * fix lint * fix unit test * fix batch size --------- Co-authored-by: Yury * Merge staging (#1365) * wait for repair and increase numBlocks (#1338) * reorder wait group done (#1340) * hotfix / remove hard coded prefix handling of encrypted upload (#1344) * remove hard coded prefix handling of encrypted upload * fix for other parts of the file * increase batch size (#1349) * Sprint changes (#1355) * Update sprint 1.12 (#1341) * wait for repair and increase numBlocks (#1338) * reorder wait group done (#1340) --------- Co-authored-by: Ebrahim Gomaa * fix trailing whitespace (#1343) * Merge staging changes (#1346) * wait for repair and increase numBlocks (#1338) * reorder wait group done (#1340) * hotfix / remove hard coded prefix handling of encrypted upload (#1344) * remove hard coded prefix handling of encrypted upload * fix for other parts of the file --------- Co-authored-by: Hitenjain14 <57557631+Hitenjain14@users.noreply.github.com> Co-authored-by: Ebrahim Gomaa * fix upload select (#1351) * fix workdir in mobile sdk (#1345) * fix mobile workdir * set multi op batch size * set option to download to disk in wasm (#1348) * fix panic in hash chan (#1352) * Fix merge conflict in sprint-1.12 (#1354) * wait for repair and increase numBlocks (#1338) * reorder wait group done (#1340) * hotfix / remove hard coded prefix handling of encrypted upload (#1344) * remove hard coded prefix handling of encrypted upload * fix for other parts of the file * increase batch size (#1349) --------- Co-authored-by: Ebrahim Gomaa * repair in batches (#1347) * repair in batches * fix lint * fix unit test * fix batch size --------- Co-authored-by: Yury --------- Co-authored-by: Ebrahim Gomaa Co-authored-by: peterlimg <54137706+peterlimg@users.noreply.github.com> Co-authored-by: Yury * fix repair after update (#1357) * fix err msg (#1361) * fix err msg * calc root hash once --------- Co-authored-by: Ebrahim Gomaa Co-authored-by: peterlimg <54137706+peterlimg@users.noreply.github.com> Co-authored-by: Yury * Fix stake pool stats (#1356) * wait for repair and increase numBlocks (#1338) * reorder wait group done (#1340) * hotfix / remove hard coded prefix handling of encrypted upload (#1344) * remove hard coded prefix handling of encrypted upload * fix for other parts of the file * increase batch size (#1349) * Sprint changes (#1355) * Update sprint 1.12 (#1341) * wait for repair and increase numBlocks (#1338) * reorder wait group done (#1340) --------- Co-authored-by: Ebrahim Gomaa * fix trailing whitespace (#1343) * Merge staging changes (#1346) * wait for repair and increase numBlocks (#1338) * reorder wait group done (#1340) * hotfix / remove hard coded prefix handling of encrypted upload (#1344) * remove hard coded prefix handling of encrypted upload * fix for other parts of the file --------- Co-authored-by: Hitenjain14 <57557631+Hitenjain14@users.noreply.github.com> Co-authored-by: Ebrahim Gomaa * fix upload select (#1351) * fix workdir in mobile sdk (#1345) * fix mobile workdir * set multi op batch size * set option to download to disk in wasm (#1348) * fix panic in hash chan (#1352) * Fix merge conflict in sprint-1.12 (#1354) * wait for repair and increase numBlocks (#1338) * reorder wait group done (#1340) * hotfix / remove hard coded prefix handling of encrypted upload (#1344) * remove hard coded prefix handling of encrypted upload * fix for other parts of the file * increase batch size (#1349) --------- Co-authored-by: Ebrahim Gomaa * repair in batches (#1347) * repair in batches * fix lint * fix unit test * fix batch size --------- Co-authored-by: Yury --------- Co-authored-by: Ebrahim Gomaa Co-authored-by: peterlimg <54137706+peterlimg@users.noreply.github.com> Co-authored-by: Yury * Fix stake pool stats --------- Co-authored-by: Hitenjain14 <57557631+Hitenjain14@users.noreply.github.com> Co-authored-by: Ebrahim Gomaa Co-authored-by: peterlimg <54137706+peterlimg@users.noreply.github.com> Co-authored-by: Yury * list pagination (#1368) * wait for repair and increase numBlocks (#1338) * reorder wait group done (#1340) * hotfix / remove hard coded prefix handling of encrypted upload (#1344) * remove hard coded prefix handling of encrypted upload * fix for other parts of the file * increase batch size (#1349) * add pagination in list --------- Co-authored-by: Ebrahim Gomaa * Fix sync in windows (#1370) * Add option for mimeType (#1372) * wait for repair and increase numBlocks (#1338) * reorder wait group done (#1340) * hotfix / remove hard coded prefix handling of encrypted upload (#1344) * remove hard coded prefix handling of encrypted upload * fix for other parts of the file * increase batch size (#1349) * add mimeType as option --------- Co-authored-by: Ebrahim Gomaa Co-authored-by: Yury * fix shutdown blobber (#1374) (cherry picked from commit 0fbb7023d93c1601970433089ceea0b0731ba280) * Cancel upload (#1332) * feature: added more test cases * fix: fixed existing test cases * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * feature: added more test cases * feature: added more test cases * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * feature: added mocks * fix: fixed bugs * fix: fixed mocks * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: made components internal * Add webstreaming to multiupload (#1190) * add webstreaming * fix typos * add webstreaming to wasm multiupload * fix typo * fix dup upload consensus (#1195) * Fix multiupload completed callback (#1172) * remove unnecessary print * fix delete and createdir * return major error * add webstreaming * fix typos * add webstreaming to wasm multiupload * fix typo * cleanup * fix completed callback * Feat/update methods in WinSDK (#1198) * delete dir in repair (#1196) * delete dir in repair * replace ioutil * fix unit test * fix list worker test * fix rename dir error * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * Add NFT Config contract (#1212) * Silent the zcnbridge http request when --silent (#1207) * Change the return type of downloadBlocks to []byte in wasm (#1218) * change the order of exchanges to get the ZCN prices (#1216) * Deprecate non multi-operation functions (#1214) * remove CreateDir, Rename, Copy, Move * use docker build wasm * use @v4 version of git action * Revert "use @v4 version of git action" This reverts commit fbb93f25a6ca63d6e205eac418d031bba428eb29. * Updated self hosted macos runner (#1219) * Updated self hosted macos runner * fixed runner name build-sdks.yml * remove path header, fix ws tag (#1220) * Feature/ Challenge based on rounds (#1191) * Fix * Fix * Fix * Fix * Fix * Debug * Debug * Debug * Fix * Heavy logging * Fix * More logging * Changed logging * Removed logging * Debug * Fix * Fix * Fix * expose send in wasm (#1225) * fix rename dir err (#1226) * Txnfee send (#1227) * add txnfee for send function * return txn verify output * add rename dir (#1230) * Async read (#1213) * basic timings for download * upload timings * to seconds * more timings for upload processing * display ms properly * cleanup * fix merge * fix merge * add more timing logs * fix dur to ms * add timings to read and build data * async read * range over errChan * fix unit test * rmv ctx cancel from multi upload * wait for last chunk * fix loop * rmv readChunk timing * increase buffer size * rmv timing logs * cleanup * updated go to version 1.20 * use -buildvcs=false --------- Co-authored-by: din-mukhammed Co-authored-by: Dinmukhammed Kambarov <52813950+din-mukhammed@users.noreply.github.com> Co-authored-by: shahnawaz-creator Co-authored-by: Manohar Reddy * Wasm for `move`, `rename`, `copy` (#1233) * add demo for move, rename and copy * remove debug logs --------- Co-authored-by: Yury * Feat/streaming server in winsdk (#1228) * fix move when srcPath is same as destPath (#1239) * fix(upload):fixed invalid ffmpeg command in transcode feature (#1236) * fix(upload):fixed invalid ffmpeg command in transcode feature * fix(upload): always delete transcode output file * fix(upload): fixed HideWindow issue * fix(upload): fixed HideWindow issue * fix(upload): fixed HideWindow issue * fix(upload): fixed HideWindow issue * fix(upload): fixed HideWindow issue * fix(upload): fixed HideWindow issue * fix(upload): fixed HideWindow issue * fix(devops): fixed build-windows * fix(mobilesdk): add logging for MultiUpload * fix(mobilesdk): fixed file permission issue on transcode * fixed lint error --------- Co-authored-by: dabasov * Fix web streaming - wasm (#1237) * Do not send GET http request with body * Use downloadBlocks to download in player_file * Call ListDir to get file meta * Format * Download 100 blocks each time by default * Fix downloadBlocks Track the downloaded bytes and trunk the padding 0 bytes * Update downloadBlocks to download blocks concurretly Use the same download method with downloadFileByBlocks * Add ActualFileSize in PlayListFile * Update wasm_exec.js to go 1.21.0 * Update version * Replace alloc.DownloadBlocks with DownloadBlocksToFileHandler * Add concurrency control (#1245) * control file ops concurrency * cleanup * add batch size to multi op * Fix/stream tests (#1247) * fix(upload): fixed file name issue in webstreaming * removed unused code * fix share consensus (#1246) * List optimization (#1240) * list optimization * list optimization * fix list hash * add to wg * fix list test * check consensus * fix listDir test * use once for mock calls * added sharders keep list (#1231) * added sharders keep list * added holder to zboxcore * removed duplicated sharder call * fixed lint * fixed panic * fixed panic * fixed panic * fixed panic * fixed panic * added consensus * added consensus * added consensus * refactored * refactored * refactored * chec alloc and prev alloc root (#1250) * add check for chunkNumber (#1253) * add check for chunkNumber * add err log * fix save progress * split-key handler implemented in wasm sdk * chnaging go version for cdn * bumped version * minor refactoring * bumping wasm_exec.js version * Refactoring unnecessary structs * create new conID (#1256) * debugging consoles added * fix statusbar update (#1257) * lint fixes * lint ignore * nolint: unused added * use httpdo for create connection (#1259) * Add create dir in repair (#1262) * fix delete dir err in cb * add way to create dir in repair * add desc for transaction data (#1263) * Feature: provide audit zcnswap and refactor if needed (#1205) * feature: modifies zcnswap to be consitent with zcnbridge * fix: fixed bugs * fix: removed dead code * feature: extended key store logic * fix: fixed bugs * fix: fixed conflicts * fix: removed dead code * fix: fixed bugs * Fix/remove path (#1221) * feature: added more test cases * fix: fixed existing test cases * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * feature: added more test cases * feature: added more test cases * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * feature: added mocks * fix: fixed bugs * fix: fixed mocks * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: made components internal * Add webstreaming to multiupload (#1190) * add webstreaming * fix typos * add webstreaming to wasm multiupload * fix typo * fix dup upload consensus (#1195) * Fix multiupload completed callback (#1172) * remove unnecessary print * fix delete and createdir * return major error * add webstreaming * fix typos * add webstreaming to wasm multiupload * fix typo * cleanup * fix completed callback * Feat/update methods in WinSDK (#1198) * delete dir in repair (#1196) * delete dir in repair * replace ioutil * fix unit test * fix list worker test * fix rename dir error * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * Add NFT Config contract (#1212) * Silent the zcnbridge http request when --silent (#1207) * Change the return type of downloadBlocks to []byte in wasm (#1218) * change the order of exchanges to get the ZCN prices (#1216) * Deprecate non multi-operation functions (#1214) * remove CreateDir, Rename, Copy, Move * use docker build wasm * use @v4 version of git action * Revert "use @v4 version of git action" This reverts commit fbb93f25a6ca63d6e205eac418d031bba428eb29. * Updated self hosted macos runner (#1219) * Updated self hosted macos runner * fixed runner name build-sdks.yml * remove path header, fix ws tag --------- Co-authored-by: YarikRevich Co-authored-by: Yaroslav Svitlytskyi <53532703+YarikRevich@users.noreply.github.com> Co-authored-by: Dinmukhammed Kambarov <52813950+din-mukhammed@users.noreply.github.com> Co-authored-by: Hitenjain14 <57557631+Hitenjain14@users.noreply.github.com> Co-authored-by: Lz Co-authored-by: Yaroslav Svitlytskyi Co-authored-by: peterlimg <54137706+peterlimg@users.noreply.github.com> Co-authored-by: Kishan Dhakan <42718091+Kishan-Dhakan@users.noreply.github.com> Co-authored-by: Manohar Reddy Co-authored-by: shahnawaz-creator <117025384+shahnawaz-creator@users.noreply.github.com> Co-authored-by: din-mukhammed * Revert "Fix/remove path (#1221)" (#1222) This reverts commit 464260aa3c2be34fdc0aca58582b27247528333b. * remove path header, fix ws tag (#1223) * remove path header, fix ws tag * fixed broken build --------- Co-authored-by: din-mukhammed * Updated self hosted macos runner (#1219) * Updated self hosted macos runner * fixed runner name build-sdks.yml (cherry picked from commit 1c9f143fe842e6c88fec1a6bc5fa0ced2ca84682) * fix * updated wasm binary build fix. * feature: added test case for swap logic * fix: resolved dependency conflicts * fix: fixed bugs * feature: added zcn eth rate fetch logic * Update blobber.go * Update authorizers_query.go * feature: replaced confusing param naming * fix: updated bridge initialization in wasm bridge integration * fix: replaced value amount with correct value * fix: wrong test case values * feature: switch to the usage of zcn token smart contract * fix: fixed bridge tests --------- Co-authored-by: Yaroslav Svitlytskyi Co-authored-by: Yury Co-authored-by: Dinmukhammed Kambarov <52813950+din-mukhammed@users.noreply.github.com> Co-authored-by: Hitenjain14 <57557631+Hitenjain14@users.noreply.github.com> Co-authored-by: Lz Co-authored-by: peterlimg <54137706+peterlimg@users.noreply.github.com> Co-authored-by: Kishan Dhakan <42718091+Kishan-Dhakan@users.noreply.github.com> Co-authored-by: Manohar Reddy Co-authored-by: shahnawaz-creator <117025384+shahnawaz-creator@users.noreply.github.com> Co-authored-by: din-mukhammed Co-authored-by: shahnawaz-creator * Feat/exposed cli methods in winsdk (#1248) * feat(winsdk): added CreateWallet * feat(winsdk): added RecoverWallet * feat(winsdk): added logging in CreateWallet and RecoverWallet * feat(winsdk): split InitSDK into InitZCNSDK and InitWallets * feat(winsdk): fixed zboxApiClient initialization issue * feat(winsdk): renamed init sdks * feat(winsdk): fixed init zboxApiClient issue * fix(winsdk): fixed RecoverWallet naming * fix(winsdk): fixed file name issue in GetUploadStatus for transcode feature * feat(winsdk): added GetFileContentType in winsdk * feat(winsdk): added ListAllocations * feat(winsdk): added CreateFreeAllocation * feat(winsdk): added allocation methods * fix(winsdk): added logs * feat(zboxapi): added GetFreeStorage * feat(zboxapi): also send X-App-ID-Token in header * fix(winsdk): skip TestGetFreeStorage * fix(winsdk): added getAllocationWith * fix(zboxapi): used local signHash instead of sys.SignFunc * fix(zboxapi): fixed token issue in GetFreeStorage * fix(list): exported ThumbnailHash and ThumbnailSize in ListResult * fix(0box): fixed GetFreeStorage/CreateFreeAllocation * fix(winsdk): renamed jwtToken with token in CreateFreeAllocation * fix(winsdk): renamed jwtToken with token in CreateFreeAllocation * feat(winsdk): added GetFreeMarker * feat(winsdk): added AddSharedInfo/DeleteSharedInfo/GetSharedToMe/GetSharedByMe * feat(winsdk): added CreateAuthTicket/DeleteAuthTicket * feat(winsdk): fixed GetSharedToMe/GetSharedByMe * feat(winsdk): fixed GetSharedToMe/GetSharedByMe * feat(winsdk): exported CreateAuthTicket/DeleteAuthTicket/CreateSharedInfo/DeleteSharedInfo/GetSharedByMe/GetSharedByPublic/GetSharedToMe * Fix/fail shardedr on bad code (#1264) * Fix/remove path (#1221) * feature: added more test cases * fix: fixed existing test cases * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * feature: added more test cases * feature: added more test cases * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * feature: added mocks * fix: fixed bugs * fix: fixed mocks * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: made components internal * Add webstreaming to multiupload (#1190) * add webstreaming * fix typos * add webstreaming to wasm multiupload * fix typo * fix dup upload consensus (#1195) * Fix multiupload completed callback (#1172) * remove unnecessary print * fix delete and createdir * return major error * add webstreaming * fix typos * add webstreaming to wasm multiupload * fix typo * cleanup * fix completed callback * Feat/update methods in WinSDK (#1198) * delete dir in repair (#1196) * delete dir in repair * replace ioutil * fix unit test * fix list worker test * fix rename dir error * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * Add NFT Config contract (#1212) * Silent the zcnbridge http request when --silent (#1207) * Change the return type of downloadBlocks to []byte in wasm (#1218) * change the order of exchanges to get the ZCN prices (#1216) * Deprecate non multi-operation functions (#1214) * remove CreateDir, Rename, Copy, Move * use docker build wasm * use @v4 version of git action * Revert "use @v4 version of git action" This reverts commit fbb93f25a6ca63d6e205eac418d031bba428eb29. * Updated self hosted macos runner (#1219) * Updated self hosted macos runner * fixed runner name build-sdks.yml * remove path header, fix ws tag --------- Co-authored-by: YarikRevich Co-authored-by: Yaroslav Svitlytskyi <53532703+YarikRevich@users.noreply.github.com> Co-authored-by: Dinmukhammed Kambarov <52813950+din-mukhammed@users.noreply.github.com> Co-authored-by: Hitenjain14 <57557631+Hitenjain14@users.noreply.github.com> Co-authored-by: Lz Co-authored-by: Yaroslav Svitlytskyi Co-authored-by: peterlimg <54137706+peterlimg@users.noreply.github.com> Co-authored-by: Kishan Dhakan <42718091+Kishan-Dhakan@users.noreply.github.com> Co-authored-by: Manohar Reddy Co-authored-by: shahnawaz-creator <117025384+shahnawaz-creator@users.noreply.github.com> Co-authored-by: din-mukhammed * Revert "Fix/remove path (#1221)" (#1222) This reverts commit 464260aa3c2be34fdc0aca58582b27247528333b. * remove path header, fix ws tag (#1223) * remove path header, fix ws tag * fixed broken build --------- Co-authored-by: din-mukhammed * Updated self hosted macos runner (#1219) * Updated self hosted macos runner * fixed runner name build-sdks.yml (cherry picked from commit 1c9f143fe842e6c88fec1a6bc5fa0ced2ca84682) * updated wasm binary build fix. * README grammar and formatting tweaks. * Merge remote-tracking branch 'origin/feature/sharder-keep-list' into feature/sharder-keep-list --------- Co-authored-by: YarikRevich Co-authored-by: Yaroslav Svitlytskyi <53532703+YarikRevich@users.noreply.github.com> Co-authored-by: Dinmukhammed Kambarov <52813950+din-mukhammed@users.noreply.github.com> Co-authored-by: Hitenjain14 <57557631+Hitenjain14@users.noreply.github.com> Co-authored-by: Lz Co-authored-by: Yaroslav Svitlytskyi Co-authored-by: peterlimg <54137706+peterlimg@users.noreply.github.com> Co-authored-by: Kishan Dhakan <42718091+Kishan-Dhakan@users.noreply.github.com> Co-authored-by: Manohar Reddy Co-authored-by: shahnawaz-creator <117025384+shahnawaz-creator@users.noreply.github.com> Co-authored-by: din-mukhammed Co-authored-by: shahnawaz-creator Co-authored-by: UncertainBadg3r <139782199+UncertainBadg3r@users.noreply.github.com> * Fix/roll logs (#1266) * Added lumberjack logger * Added lumberjack logger * fix:download file/thumbnail by authticket in winsdk (#1265) * feat(winsdk): added GetWalletBalance (#1267) * enabled gosdk update for sprint branches (#1268) * Revert "enabled gosdk update for sprint branches (#1268)" (#1269) This reverts commit 201b8bdc4f94b2aeb5eda3ce16f91b4327023654. * fix in progress callback (#1270) * Cleanup update terms from update allocation (#1274) * Improve download performance (#1271) * improve download performance * add read buffer size * add hash option * fix lint test * set default to true * log for data * fix read * feature: add reset nonce for bridge (#1279) * Add registerAuthorizer to wasm (#1275) * Hotfix/parse hostname error (#1241) * Fix/remove path (#1221) * feature: added more test cases * fix: fixed existing test cases * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * feature: added more test cases * feature: added more test cases * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * feature: added mocks * fix: fixed bugs * fix: fixed mocks * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: made components internal * Add webstreaming to multiupload (#1190) * add webstreaming * fix typos * add webstreaming to wasm multiupload * fix typo * fix dup upload consensus (#1195) * Fix multiupload completed callback (#1172) * remove unnecessary print * fix delete and createdir * return major error * add webstreaming * fix typos * add webstreaming to wasm multiupload * fix typo * cleanup * fix completed callback * Feat/update methods in WinSDK (#1198) * delete dir in repair (#1196) * delete dir in repair * replace ioutil * fix unit test * fix list worker test * fix rename dir error * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * Add NFT Config contract (#1212) * Silent the zcnbridge http request when --silent (#1207) * Change the return type of downloadBlocks to []byte in wasm (#1218) * change the order of exchanges to get the ZCN prices (#1216) * Deprecate non multi-operation functions (#1214) * remove CreateDir, Rename, Copy, Move * use docker build wasm * use @v4 version of git action * Revert "use @v4 version of git action" This reverts commit fbb93f25a6ca63d6e205eac418d031bba428eb29. * Updated self hosted macos runner (#1219) * Updated self hosted macos runner * fixed runner name build-sdks.yml * remove path header, fix ws tag --------- Co-authored-by: YarikRevich Co-authored-by: Yaroslav Svitlytskyi <53532703+YarikRevich@users.noreply.github.com> Co-authored-by: Dinmukhammed Kambarov <52813950+din-mukhammed@users.noreply.github.com> Co-authored-by: Hitenjain14 <57557631+Hitenjain14@users.noreply.github.com> Co-authored-by: Lz Co-authored-by: Yaroslav Svitlytskyi Co-authored-by: peterlimg <54137706+peterlimg@users.noreply.github.com> Co-authored-by: Kishan Dhakan <42718091+Kishan-Dhakan@users.noreply.github.com> Co-authored-by: Manohar Reddy Co-authored-by: shahnawaz-creator <117025384+shahnawaz-creator@users.noreply.github.com> Co-authored-by: din-mukhammed * Revert "Fix/remove path (#1221)" (#1222) This reverts commit 464260aa3c2be34fdc0aca58582b27247528333b. * remove path header, fix ws tag * remove path header, fix ws tag (#1223) * remove path header, fix ws tag * fixed broken build --------- Co-authored-by: din-mukhammed * Updated self hosted macos runner (#1219) * Updated self hosted macos runner * fixed runner name build-sdks.yml (cherry picked from commit 1c9f143fe842e6c88fec1a6bc5fa0ced2ca84682) * added logging * updated wasm binary build fix. * updated wasm binary build fix. (cherry picked from commit 257188873db1d21ccdb04eb7e5a35ff6ef6d7696) * README grammar and formatting tweaks. --------- Co-authored-by: YarikRevich Co-authored-by: Yaroslav Svitlytskyi <53532703+YarikRevich@users.noreply.github.com> Co-authored-by: Dinmukhammed Kambarov <52813950+din-mukhammed@users.noreply.github.com> Co-authored-by: Hitenjain14 <57557631+Hitenjain14@users.noreply.github.com> Co-authored-by: Lz Co-authored-by: Yaroslav Svitlytskyi Co-authored-by: peterlimg <54137706+peterlimg@users.noreply.github.com> Co-authored-by: Kishan Dhakan <42718091+Kishan-Dhakan@users.noreply.github.com> Co-authored-by: Manohar Reddy Co-authored-by: shahnawaz-creator <117025384+shahnawaz-creator@users.noreply.github.com> Co-authored-by: din-mukhammed Co-authored-by: shahnawaz-creator Co-authored-by: UncertainBadg3r <139782199+UncertainBadg3r@users.noreply.github.com> Co-authored-by: Jayash Satolia <73050737+Jayashsatolia403@users.noreply.github.com> * Adding RegisterAuthorizer method in wasmsdk (#1273) * Adding RegisterAuthorizer method in wasmsdk * Fix:wasm test fail * Adding demo for RegisterAuthorizer handler in wasm * Js and Go bridge implementation * RegisterAuthorizer handler changes and adding Async Js bridge * golangci-lint fixes * Added error handling in setSplitKey and setAuthURL * Refactoring wasm exposed handlers * Setting split key wallet before setting auth url in setAuthURL's demo * Lint Fixes after resolving conflicts * upload optimization (#1281) * add sha256 simd and blake3 * add hash chan * fix lint and unit test * fix MHash * rmv timing log * fix thumbnail hash * added logging * added logging * hotfix * hotfix * experiment * experiment * experiment * experiment * experiment * experiment * experiment * experiment * experiment * experiment * experiment * Sprint 1.11 revert (#1284) * Revert "experiment" This reverts commit 050270f52815dcd930f760f20a4c7e3e669d73fb. * Revert "experiment" This reverts commit d4821f8e28c4efce59775129b4f25eaa1df2905d. * Revert "experiment" This reverts commit 9f65b2eef30528662b59482a6c59d6a633f1ff77. * Revert "experiment" This reverts commit 6313cb8a49d211ab8a6c476ca839922be7c31288. * Revert "experiment" This reverts commit b6be0b51767f39cb53a9439c3a34fe2f828b2dfc. * Revert "experiment" This reverts commit 68d41905a3aaf1c094d50f7b418349d7c78663cc. * Revert "experiment" This reverts commit 3a23f3f33b9446cf5711f950b0cd80a40993e429. * Revert "experiment" This reverts commit 6b6578bb857c9e4351db941fc84e65fac0994c15. * Revert "experiment" This reverts commit 65a0152adc10754977a479e09cdb8d577ca8aefb. * Revert "experiment" This reverts commit 6834b058a99bba80621c756292f7f4205b24e936. * Revert "experiment" This reverts commit 8446894c579f5acd14db1b09b5966f92ca1588be. * Revert "hotfix" This reverts commit 48cf793a70f754f44c662d839bdde4e191ec2748. * Revert "hotfix" This reverts commit 895c7f0012837d2f9a0cbfdcddf47f8dbe0d13b7. * Revert "added logging" This reverts commit fafce3c9b90c0863e574a2609a202f962ecb90ac. * Revert "added logging" This reverts commit 10b6a4afbdecc1e8a201d5ac80c4d12f0b97c411. * rmv channel hasher (#1285) * Fix submit txn (#1287) * Feature: add BNT, USDC and EURC tokens for Swap (#1277) * feature: add multiple token support for swap * feature: add correct Bancor API calls * feature: added approve for swap of bancor token * fix: removed debug lines * fix: fixed swap approval * fix: fixed bug * fix: fixed bugs * change hasher (#1288) * change hasher * set max proc to 1 * set batch size to 2 * revert hasher lib changes * increase batch size to 4 * set batch size to 2 * fix build * update chunk reader * run gc * add sleep and decrease batch size * change blake3 * fix operation slice * fix batch size * increase sleep time * add memory pool * increase swg for wasm * reuse chunk reader * fix read chunk * rmv unused package * Remove sleep and GOMAXPROCS=1 * Increase batch size a little --------- Co-authored-by: peterlimg * Import eth wallet with index (#1292) * Add import account address index * Make it optional * Feat/import-account-idx (#1294) * Add import account address index * Make it optional * Add account index option * Add account bip32 option (#1295) * Fix allocation min lock (#1297) * create PRs to dependent repos when a PR is merged to sprint branches (#1283) * create PRs to dependent repos when a PR is merged to sprint branches * add branch name * move wasm-build to build-sdk.yml * cleanup sdk-release.yml * Removed padding from the CryptoJS Encrypt and Decrypt functions functions. (#1300) Co-authored-by: Manohar Reddy * Fix allocation min lock * change chunkNumber for wasm (#1304) * change numBlock for wasm * change numBlocks * revert sdk changes * Feature: implement fork per run strategy (#1299) * feature: added fork creation * fix: fixed bug * feature: improved Tenderly fork creation flow * fix parse error (#1309) * fix parse error * fix log * add swg * Fix memory usage (#1311) * fix memory usage * change hasher to sha2 256 * fix unit test * increase numBlocks * fix thumbnail hash * Fix iso header (#1314) * add ISO check * remove filename field from authticket * fix typo * log auth token * cleanup * Fix zboxcore min submit (#1315) * Fix zboxcore min submit * Fix min submit and min confirmation default configs * Fix unit tests * Fix unit tests * Revert "Fix iso header (#1314)" (#1317) This reverts commit 218d3edd1a43dce1cfd1a700adcb33fb03a883f9. * Add upload for empty file (#1316) * add upload for empty file * fix hash * check ref * fix actual size * fix empty upload (#1320) * fix read auth ticket (#1319) * allocation validation for data parity shards (#1321) * fix git action to raise PRs to update gosdk (#1313) * test gosdk update * test-gosdk-update * use binary operators instead of terinary * use github.ref * use github ref * add branch name * use quotes * prepare for PR * test work flow dispatch * test extract branch * print env * prepare for PR * feature: added custom block number during Tenderly fork creation (#1323) * Fix/retry tx on nonce error (#1322) * Revert "experiment" This reverts commit 050270f52815dcd930f760f20a4c7e3e669d73fb. * Revert "experiment" This reverts commit d4821f8e28c4efce59775129b4f25eaa1df2905d. * Revert "experiment" This reverts commit 9f65b2eef30528662b59482a6c59d6a633f1ff77. * Revert "experiment" This reverts commit 6313cb8a49d211ab8a6c476ca839922be7c31288. * Revert "experiment" This reverts commit b6be0b51767f39cb53a9439c3a34fe2f828b2dfc. * Revert "experiment" This reverts commit 68d41905a3aaf1c094d50f7b418349d7c78663cc. * Revert "experiment" This reverts commit 3a23f3f33b9446cf5711f950b0cd80a40993e429. * Revert "experiment" This reverts commit 6b6578bb857c9e4351db941fc84e65fac0994c15. * Revert "experiment" This reverts commit 65a0152adc10754977a479e09cdb8d577ca8aefb. * Revert "experiment" This reverts commit 6834b058a99bba80621c756292f7f4205b24e936. * Revert "experiment" This reverts commit 8446894c579f5acd14db1b09b5966f92ca1588be. * Revert "hotfix" This reverts commit 48cf793a70f754f44c662d839bdde4e191ec2748. * Revert "hotfix" This reverts commit 895c7f0012837d2f9a0cbfdcddf47f8dbe0d13b7. * Revert "added logging" This reverts commit fafce3c9b90c0863e574a2609a202f962ecb90ac. * Revert "added logging" This reverts commit 10b6a4afbdecc1e8a201d5ac80c4d12f0b97c411. * retry failed transaction in zbox with nonce error * Fix wm rollback (#1324) * fix wm rollback * fix rb check * version logs * fix prev version * cleanup * Cleanup free storage update allocation (#1325) * remove batch size (#1327) * fix retry op callback (#1329) * Revert "remove batch size (#1327)" (#1328) This reverts commit 310edbb4d669566acc8cfc6e2940170d2da813fa. * fix cancel upload * Add `force` param for getAllocationBlobbers wasm (#1330) * fix retry callback (#1331) * Fix fill panic (#1334) * fix fill shards panic * add log for fill err * fix err * add cancel upload to wasm * fix proxy * wait for repair and increase numBlocks (#1338) * reorder wait group done (#1340) * hotfix / remove hard coded prefix handling of encrypted upload (#1344) * remove hard coded prefix handling of encrypted upload * fix for other parts of the file * increase batch size (#1349) * Sprint changes (#1355) * Update sprint 1.12 (#1341) * wait for repair and increase numBlocks (#1338) * reorder wait group done (#1340) --------- Co-authored-by: Ebrahim Gomaa * fix trailing whitespace (#1343) * Merge staging changes (#1346) * wait for repair and increase numBlocks (#1338) * reorder wait group done (#1340) * hotfix / remove hard coded prefix handling of encrypted upload (#1344) * remove hard coded prefix handling of encrypted upload * fix for other parts of the file --------- Co-authored-by: Hitenjain14 <57557631+Hitenjain14@users.noreply.github.com> Co-authored-by: Ebrahim Gomaa * fix upload select (#1351) * fix workdir in mobile sdk (#1345) * fix mobile workdir * set multi op batch size * set option to download to disk in wasm (#1348) * fix panic in hash chan (#1352) * Fix merge conflict in sprint-1.12 (#1354) * wait for repair and increase numBlocks (#1338) * reorder wait group done (#1340) * hotfix / remove hard coded prefix handling of encrypted upload (#1344) * remove hard coded prefix handling of encrypted upload * fix for other parts of the file * increase batch size (#1349) --------- Co-authored-by: Ebrahim Gomaa * repair in batches (#1347) * repair in batches * fix lint * fix unit test * fix batch size --------- Co-authored-by: Yury --------- Co-authored-by: Ebrahim Gomaa Co-authored-by: peterlimg <54137706+peterlimg@users.noreply.github.com> Co-authored-by: Yury * fix repair after update (#1357) * use mo ctx as parent * fix wasm build * empty commit * fix ctx --------- Co-authored-by: YarikRevich Co-authored-by: Yaroslav Svitlytskyi <53532703+YarikRevich@users.noreply.github.com> Co-authored-by: Dinmukhammed Kambarov <52813950+din-mukhammed@users.noreply.github.com> Co-authored-by: Lz Co-authored-by: Yaroslav Svitlytskyi Co-authored-by: peterlimg <54137706+peterlimg@users.noreply.github.com> Co-authored-by: Kishan Dhakan <42718091+Kishan-Dhakan@users.noreply.github.com> Co-authored-by: Manohar Reddy Co-authored-by: shahnawaz-creator <117025384+shahnawaz-creator@users.noreply.github.com> Co-authored-by: Jayash Satolia <73050737+Jayashsatolia403@users.noreply.github.com> Co-authored-by: din-mukhammed Co-authored-by: shahnawaz-creator Co-authored-by: Yury Co-authored-by: Ash-KODES Co-authored-by: Yaroslav Svitlytskyi Co-authored-by: UncertainBadg3r <139782199+UncertainBadg3r@users.noreply.github.com> Co-authored-by: peterlimg Co-authored-by: Tanishq Gupta <52757132+tanishq67@users.noreply.github.com> Co-authored-by: Jayash Satolia Co-authored-by: GuruHubb Co-authored-by: Ebrahim Gomaa * Fix auth response on wasm (#1359) * Fix auth response on wasm * Fix verifyHandler * Fix auth sign wasm * Update pkg for gomobile * Remove test code * fixed lint --------- Co-authored-by: dabasov * Download buffer (#1363) * revert index changes * revert close changes * rmv data buffer * download buffer * fix write to file * rmv len check * fix decodeEC ut * fix remaining size * cleanup * fix mem chan file * fix req complete cb (#1381) * get hardfork round by name (#1385) * get hardfork round by name * fix naming * fix default hardfork round * made GetFreeAllocationBlobbers public * Add miner/sharder stake and unstake support * Add collect rewards for miner/sharder * fixed resp format * fixed resp format * [wasm] Expose minersc - stakePool/unstakePool collect rewards (#1386) * replaced coingecko url with internal price url (#1379) * Add miner/sharder stake and unstake support * Add collect rewards for miner/sharder --------- Co-authored-by: Gaurang Patel * Use zcncore transaction for storagesc to support 2fa * Remove duplicate Execute transaction in packages * Expose claim rewards to wasm * Fix collect rewards * Fix transaction output * Fix send token * 2FA file operation support * Fix 2FA copy/rename operations * Differ 2FA Sign and normal Sign * Add SplitKeysWallet to split keys and return wallet * Add methods to set split keys and persist * Add isSplit field to wallet to indicate whether the wallet is split or not * Fix send with zauth * Add zauth functions * Add zauth config for zboxcli * Resolve conflicts for setClientInfoWithSign * Update to support zauth server signing * Add zauth wasm * Update CallZauthSetup func * Add wasm methods to support zvault and zauth * Adjust zvault callback funcs * Update setWallet to introduce isSplit and peerPublicKey * Register zauth server properly * Init ZCNSDK with split option * Clean up debug logs * Fix write marker signature verify * Adjust wasm auth sign * Adjust wasm auth common sign * ScryptEncrypt key does not have to be 32, it's the derivate key from salt need to be 32. * Add passphrase requirement for auth keys store and generate * Add multiple users support zauth server * Fix wasm panic on auth error * Fix invalid signature after updating allocation * Fix errors after meging staging * Add missing functions for wasm worker mode * Add revoke and expiredAt field to split wallet for zauth * Add CallZauthRevoke * Add CallZauthDelete method * Add zauthServer address env to web worker * Add more zvault wasm methods * Remove pubkey from registerZauthServer * Fix BLE with web worker - Communicate the auth and response via web worker event data, from worker to main thread and vice verse. - Add msgType to differ the event data * Fix none BLE sign * Comment unused method * Update worker wallet with event * Fix conflicts after cherry-pick * Fix sign * fix: renamed header used for jwt session creation (#1553) * Feature: add additional zvault endpoints for WASM (#1554) * fix: renamed header used for jwt session creation * feature: added wasm functions for zvault endpoints * fix: fixed signature hash function (#1564) * feature: added shared wallets retrieval calls (#1567) * feature: migrated to use user id instead of phone number to issue jwt tokens * hotfix: fixed mobilesdk signature * fix merge errors * fix: debug * fix: debug * fix: debug * fix: debug * fix: debug * fix: debug * fix: debug * fix: debug * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed bugs * fix: fixed sign function initialization * add customMeta field for upload * add callback for repair progress in update with reapir * fix start processor for upload in wasm * initalize sub map * fix: failing dex transaction * fix: return transaction nonce if certain status code received * fix: fixed wallet info initialization * Remove unnecessary print statement * Remove unnecessary print statement * More cleanup (#1597) * remove debug print statements from NewTransaction method. * Revert "Repair progress" * fix merged changes * fix error output while executing a sc. * Fix * Fix * Fix * Fix * Fix tests * Fix lint * Fix * Empty commit --------- Co-authored-by: Hitenjain14 <57557631+Hitenjain14@users.noreply.github.com> Co-authored-by: Ebrahim Gomaa Co-authored-by: peterlimg <54137706+peterlimg@users.noreply.github.com> Co-authored-by: Yury Co-authored-by: Roshan Kumar <56060325+Roshan-Mehta@users.noreply.github.com> Co-authored-by: Manohar Reddy Co-authored-by: YarikRevich Co-authored-by: Yaroslav Svitlytskyi <53532703+YarikRevich@users.noreply.github.com> Co-authored-by: Dinmukhammed Kambarov <52813950+din-mukhammed@users.noreply.github.com> Co-authored-by: Lz Co-authored-by: Yaroslav Svitlytskyi Co-authored-by: Kishan Dhakan <42718091+Kishan-Dhakan@users.noreply.github.com> Co-authored-by: shahnawaz-creator <117025384+shahnawaz-creator@users.noreply.github.com> Co-authored-by: din-mukhammed Co-authored-by: shahnawaz-creator Co-authored-by: Ash-KODES Co-authored-by: Yaroslav Svitlytskyi Co-authored-by: UncertainBadg3r <139782199+UncertainBadg3r@users.noreply.github.com> Co-authored-by: peterlimg Co-authored-by: Tanishq Gupta <52757132+tanishq67@users.noreply.github.com> Co-authored-by: GuruHubb Co-authored-by: Gaurang Patel Co-authored-by: Hitenjain14 Co-authored-by: yash10019coder --- core/client/set.go | 3 +- core/conf/config.go | 17 +- core/conf/config_test.go | 4 + core/sys/vars.go | 14 +- core/transaction/entity.go | 5 +- core/version/version.go | 2 +- core/zcncrypto/bls0chain_herumi_test.go | 6 +- core/zcncrypto/signature_scheme.go | 47 +- mobilesdk/zboxapi/client.go | 14 +- mobilesdk/zcn/smartcontract.go | 11 +- wasmsdk/allocation.go | 5 + wasmsdk/auth_txn.go | 61 +++ wasmsdk/blobber.go | 39 +- wasmsdk/cache.go | 10 +- wasmsdk/jsbridge/func.go | 3 +- wasmsdk/jsbridge/webworker.go | 112 +++- wasmsdk/jsbridge/zcnworker.js.tpl | 44 +- wasmsdk/proxy.go | 215 +++++++- wasmsdk/sdk.go | 15 +- wasmsdk/wallet.go | 51 +- wasmsdk/zbox.go | 12 +- winsdk/sdk.go | 2 +- winsdk/zboxapi.go | 12 +- zboxapi/sdk.go | 50 +- zboxapi/sdk_test.go | 33 +- zboxcore/marker/writemarker.go | 2 +- zboxcore/sdk/allocation.go | 23 +- zboxcore/sdk/allocation_file_delete_test.go | 4 +- zboxcore/sdk/allocation_file_test.go | 4 +- zboxcore/sdk/allocation_test.go | 40 +- zboxcore/sdk/chunked_upload_blobber.go | 3 +- zboxcore/sdk/chunked_upload_process_js.go | 213 +++++--- zboxcore/sdk/commitworker.go | 7 +- zboxcore/sdk/common.go | 4 +- zboxcore/sdk/copyworker.go | 9 +- zboxcore/sdk/copyworker_test.go | 9 +- zboxcore/sdk/deleteworker.go | 8 +- zboxcore/sdk/deleteworker_test.go | 4 +- zboxcore/sdk/dirworker.go | 6 +- zboxcore/sdk/downloadworker.go | 4 +- zboxcore/sdk/filemetaworker.go | 4 +- zboxcore/sdk/filemetaworker_test.go | 4 +- zboxcore/sdk/filerefsworker.go | 5 +- zboxcore/sdk/filestatsworker.go | 2 +- zboxcore/sdk/filestatsworker_test.go | 4 +- zboxcore/sdk/listworker.go | 1 + zboxcore/sdk/listworker_test.go | 4 +- zboxcore/sdk/moveworker.go | 7 +- zboxcore/sdk/multi_operation_worker.go | 3 +- zboxcore/sdk/renameworker.go | 7 +- zboxcore/sdk/renameworker_test.go | 4 +- zboxcore/sdk/rollback.go | 8 +- zboxcore/sdk/sdk.go | 3 +- zboxcore/sdk/sharerequest.go | 2 + zboxcore/sdk/writemarker_mutex.go | 4 +- zboxcore/zboxutil/http.go | 212 +++++--- zboxcore/zboxutil/util.go | 4 +- zcncore/transaction.go | 17 +- zcncore/transaction_base.go | 12 + zcncore/transaction_mobile.go | 41 +- zcncore/transactionauth.go | 26 +- zcncore/transactionauth_base.go | 19 +- zcncore/transactionauth_mobile.go | 23 +- zcncore/wallet_base.go | 2 +- zcncore/zauth.go | 534 ++++++++++++++++++++ 65 files changed, 1637 insertions(+), 442 deletions(-) create mode 100644 zcncore/zauth.go diff --git a/core/client/set.go b/core/client/set.go index 8ac67fcba..8c5aa7e91 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -89,8 +89,9 @@ func GetClientSysKeys() []sys.KeyPair { } // SetWallet should be set before any transaction or client specific APIs -func SetWallet(w zcncrypto.Wallet) { +func SetWallet(isSplit bool, w zcncrypto.Wallet) { client.wallet = &w + client.wallet.IsSplit = isSplit } // splitKeyWallet parameter is valid only if SignatureScheme is "BLS0Chain" diff --git a/core/conf/config.go b/core/conf/config.go index 7bd1f5f2a..799364cc5 100644 --- a/core/conf/config.go +++ b/core/conf/config.go @@ -77,7 +77,11 @@ type Config struct { // ZboxAppType app type name ZboxAppType string `json:"zbox_app_type"` // SharderConsensous is consensous for when quering for SCRestAPI calls - SharderConsensous int `json:"sharder_consensous"` + SharderConsensous int `json:"sharder_consensous"` + ZauthServer string `json:"zauth_server"` + V *viper.Viper `json:"-"` + + IsSplitWallet bool `json:"is_split_wallet"` } // LoadConfigFile load and parse SDK Config from file @@ -104,7 +108,14 @@ func LoadConfigFile(file string) (Config, error) { return cfg, thrown.Throw(ErrBadParsing, err.Error()) } - return LoadConfig(v) + cfg, err = LoadConfig(v) + if err != nil { + return cfg, err + } + + cfg.V = v + + return cfg, nil } // LoadConfig load and parse config @@ -170,9 +181,9 @@ func LoadConfig(v Reader) (Config, error) { cfg.SignatureScheme = v.GetString("signature_scheme") cfg.ChainID = v.GetString("chain_id") + cfg.ZauthServer = v.GetString("zauth.server") return cfg, nil - } func isURL(s string) bool { diff --git a/core/conf/config_test.go b/core/conf/config_test.go index 3eae68b8b..77f8c51b1 100644 --- a/core/conf/config_test.go +++ b/core/conf/config_test.go @@ -12,6 +12,7 @@ func TestLoadConfig(t *testing.T) { var mockDefaultReader = func() Reader { reader := &mocks.Reader{} reader.On("GetString", "block_worker").Return("http://127.0.0.1:9091/dns") + reader.On("GetString", "zauth.server").Return("http://127.0.0.1:8090/") reader.On("GetInt", "min_submit").Return(0) reader.On("GetInt", "min_confirmation").Return(0) reader.On("GetInt", "max_txn_query").Return(0) @@ -41,6 +42,7 @@ func TestLoadConfig(t *testing.T) { reader := &mocks.Reader{} reader.On("GetString", "block_worker").Return("") + reader.On("GetString", "zauth.server").Return("") reader.On("GetInt", "min_submit").Return(0) reader.On("GetInt", "min_confirmation").Return(0) reader.On("GetInt", "max_txn_query").Return(0) @@ -85,6 +87,7 @@ func TestLoadConfig(t *testing.T) { reader := &mocks.Reader{} reader.On("GetString", "block_worker").Return("https://127.0.0.1:9091/dns") + reader.On("GetString", "zauth.server").Return("http://127.0.0.1:8090/") reader.On("GetInt", "min_submit").Return(101) reader.On("GetInt", "min_confirmation").Return(0) reader.On("GetInt", "max_txn_query").Return(0) @@ -119,6 +122,7 @@ func TestLoadConfig(t *testing.T) { reader := &mocks.Reader{} reader.On("GetString", "block_worker").Return("https://127.0.0.1:9091/dns") + reader.On("GetString", "zauth.server").Return("http://127.0.0.1:8090/") reader.On("GetInt", "min_submit").Return(0) reader.On("GetInt", "min_confirmation").Return(101) reader.On("GetInt", "max_txn_query").Return(0) diff --git a/core/sys/vars.go b/core/sys/vars.go index 43f001c41..b1b53876e 100644 --- a/core/sys/vars.go +++ b/core/sys/vars.go @@ -14,7 +14,8 @@ var ( Sleep = time.Sleep // Sign sign method. it should be initialized on different platform. - Sign SignFunc + Sign SignFunc + SignWithAuth SignFunc // Verify verify method. it should be initialized on different platform. Verify VerifyFunc @@ -23,4 +24,15 @@ var ( VerifyWith VerifyWithFunc Authorize AuthorizeFunc + + AuthCommon AuthorizeFunc ) + +// SetAuthorize sets the authorize callback function +func SetAuthorize(auth AuthorizeFunc) { + Authorize = auth +} + +func SetAuthCommon(auth AuthorizeFunc) { + AuthCommon = auth +} diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 47f0f3268..19faca165 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -261,14 +261,15 @@ func NewTransactionReceipt(t *Transaction) *TxnReceipt { return &TxnReceipt{Transaction: t} } -func (t *Transaction) VerifyTransaction(verifyHandler VerifyFunc) (bool, error) { +// VerifySigWith verify the signature with the given public key and handler +func (t *Transaction) VerifySigWith(pubkey string, verifyHandler VerifyFunc) (bool, error) { // Store the hash hash := t.Hash t.ComputeHashData() if t.Hash != hash { return false, errors.New("verify_transaction", fmt.Sprintf(`{"error":"hash_mismatch", "expected":"%v", "actual":%v"}`, t.Hash, hash)) } - return verifyHandler(t.PublicKey, t.Signature, t.Hash) + return verifyHandler(pubkey, t.Signature, t.Hash) } func SendTransactionSync(txn *Transaction, miners []string) error { diff --git a/core/version/version.go b/core/version/version.go index 9213a1e29..22d2ceb0c 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.16.3-10-g66360b13" +const VERSIONSTR = "v1.17.3-42-gea1f6917" diff --git a/core/zcncrypto/bls0chain_herumi_test.go b/core/zcncrypto/bls0chain_herumi_test.go index 6819696f5..221a8d0cc 100644 --- a/core/zcncrypto/bls0chain_herumi_test.go +++ b/core/zcncrypto/bls0chain_herumi_test.go @@ -150,12 +150,13 @@ func TestCombinedSignAndVerify(t *testing.T) { } func TestSplitKey(t *testing.T) { - primaryKeyStr := `c36f2f92b673cf057a32e8bd0ca88888e7ace40337b737e9c7459fdc4c521918` + primaryKeyStr := `872eac6370c72093535fa395ad41a08ee90c9d0d46df9461eb2515451f389d1b` sig0 := NewSignatureScheme("bls0chain") err := sig0.SetPrivateKey(primaryKeyStr) if err != nil { t.Fatalf("Set private key failed - %s", errors.Top(err)) } + data = "823bb3dc0b80a6c86922a884e63908cb9e963ef488688b41e32cbf4d84471a1f" hash := Sha3Sum256(data) signature, err := sig0.Sign(hash) if err != nil { @@ -170,15 +171,18 @@ func TestSplitKey(t *testing.T) { for i := 0; i < numSplitKeys; i++ { sigAggScheme[i] = NewSignatureScheme("bls0chain") err = sigAggScheme[i].SetPrivateKey(w.Keys[i].PrivateKey) + fmt.Println("seckey:", sigAggScheme[i].GetPrivateKey()) require.NoError(t, err) } var aggrSig string for i := 1; i < numSplitKeys; i++ { tmpSig, _ := sigAggScheme[i].Sign(hash) + fmt.Println("tmpSig:", tmpSig) aggrSig, _ = sigAggScheme[0].Add(tmpSig, hash) } if aggrSig != signature { t.Fatalf("split key signature failed") } + fmt.Println("aggrSig:", aggrSig) } diff --git a/core/zcncrypto/signature_scheme.go b/core/zcncrypto/signature_scheme.go index da806b163..6a192ac26 100644 --- a/core/zcncrypto/signature_scheme.go +++ b/core/zcncrypto/signature_scheme.go @@ -3,6 +3,8 @@ package zcncrypto import ( "encoding/json" + "fmt" + "os" "github.com/0chain/errors" "github.com/0chain/gosdk/core/encryption" @@ -20,26 +22,15 @@ type KeyPair struct { // Wallet represents client wallet information type Wallet struct { - // ClientID client unique identifier - ClientID string `json:"client_id"` - - // ClientKey client public key - ClientKey string `json:"client_key"` - - // Keys private and public key pair - Keys []KeyPair `json:"keys"` - - // Mnemonic recovery phrase of the wallet - Mnemonic string `json:"mnemonics"` - - // Version version of the wallet - Version string `json:"version"` - - // DateCreated date of wallet creation - DateCreated string `json:"date_created"` - - // Nonce nonce of the wallet - Nonce int64 `json:"nonce"` + ClientID string `json:"client_id"` + ClientKey string `json:"client_key"` + PeerPublicKey string `json:"peer_public_key"` // Peer public key exists only in split wallet + Keys []KeyPair `json:"keys"` + Mnemonic string `json:"mnemonics"` + Version string `json:"version"` + DateCreated string `json:"date_created"` + Nonce int64 `json:"nonce"` + IsSplit bool `json:"is_split"` } // SignatureScheme - an encryption scheme for signing and verifying messages @@ -96,6 +87,22 @@ func (w *Wallet) Sign(hash, scheme string) (string, error) { return sigScheme.Sign(hash) } +// SetSplitKeys sets split keys and wipes out mnemonic and original primary keys +func (w *Wallet) SetSplitKeys(sw *Wallet) { + *w = *sw +} + +func (w *Wallet) SaveTo(file string) error { + d, err := json.Marshal(w) + if err != nil { + return err + } + + fmt.Println("Saving wallet to file: ", string(d)) + + return os.WriteFile(file, d, 0644) +} + func IsMnemonicValid(mnemonic string) bool { return bip39.IsMnemonicValid(mnemonic) } diff --git a/mobilesdk/zboxapi/client.go b/mobilesdk/zboxapi/client.go index 1b7852e27..9dc4bd272 100644 --- a/mobilesdk/zboxapi/client.go +++ b/mobilesdk/zboxapi/client.go @@ -65,30 +65,30 @@ func GetCsrfToken() (string, error) { // CreateJwtSession create a jwt session for the given phone number // - phoneNumber is the phone number -func CreateJwtSession(phoneNumber string) (int64, error) { +func CreateJwtSession(userID string) (int64, error) { if zboxApiClient == nil { return 0, ErrZboxApiNotInitialized } - return zboxApiClient.CreateJwtSession(context.TODO(), phoneNumber) + return zboxApiClient.CreateJwtSession(context.TODO(), userID) } -// CreateJwtToken create a fresh jwt token for the given phone number +// CreateJwtToken create a fresh jwt token for the given user id // - phoneNumber is the phone number // - jwtSessionID is the jwt session id // - otp is the one time password -func CreateJwtToken(phoneNumber string, jwtSessionID int64, otp string) (string, error) { +func CreateJwtToken(userID string, jwtSessionID int64) (string, error) { if zboxApiClient == nil { return "", ErrZboxApiNotInitialized } - return zboxApiClient.CreateJwtToken(context.TODO(), phoneNumber, jwtSessionID, otp) + return zboxApiClient.CreateJwtToken(context.TODO(), userID, jwtSessionID) } // RefreshJwtToken refresh jwt token // - phoneNumber is the phone number for which the token is to be refreshed // - token is the token to be refreshed -func RefreshJwtToken(phoneNumber string, token string) (string, error) { +func RefreshJwtToken(userID string, token string) (string, error) { if zboxApiClient == nil { return "", ErrZboxApiNotInitialized } - return zboxApiClient.RefreshJwtToken(context.TODO(), phoneNumber, token) + return zboxApiClient.RefreshJwtToken(context.TODO(), userID, token) } diff --git a/mobilesdk/zcn/smartcontract.go b/mobilesdk/zcn/smartcontract.go index 956f45d76..9069c8756 100644 --- a/mobilesdk/zcn/smartcontract.go +++ b/mobilesdk/zcn/smartcontract.go @@ -6,6 +6,7 @@ package zcn import ( "encoding/json" "fmt" + "strconv" "sync" "github.com/0chain/gosdk/zcncore" @@ -19,13 +20,19 @@ func Faucet(methodName, jsonInput string, zcnToken float64) (string, error) { func ExecuteSmartContract(address, methodName, input string, sasToken string) (string, error) { wg := &sync.WaitGroup{} cb := &transactionCallback{wg: wg} - txn, err := zcncore.NewTransaction(cb, "0", 0) + txn, err := zcncore.NewTransaction(cb, 0, 0) if err != nil { return "", err } wg.Add(1) - err = txn.ExecuteSmartContract(address, methodName, input, sasToken) + + v, err := strconv.ParseUint(sasToken, 10, 64) + if err != nil { + return "", fmt.Errorf("invalid token value: %v, err: %v", sasToken, err) + } + + _, err = txn.ExecuteSmartContract(address, methodName, input, v) if err != nil { return "", err diff --git a/wasmsdk/allocation.go b/wasmsdk/allocation.go index af03ac4eb..b94c8511c 100644 --- a/wasmsdk/allocation.go +++ b/wasmsdk/allocation.go @@ -411,6 +411,11 @@ func unlockStakePool(providerType, fee uint64, providerID string) (int64, error) return unstake, err } +func collectRewards(providerType int, providerID string) (string, error) { + hash, _, err := sdk.CollectRewards(providerID, sdk.ProviderType(providerType)) + return hash, err +} + // getSkatePoolInfo is to get information about the stake pool for the allocation // - providerType: provider type (1: miner, 2:sharder, 3:blobber, 4:validator, 5:authorizer) // - providerID: provider id diff --git a/wasmsdk/auth_txn.go b/wasmsdk/auth_txn.go index 641b58b1f..5e2357787 100644 --- a/wasmsdk/auth_txn.go +++ b/wasmsdk/auth_txn.go @@ -8,12 +8,17 @@ import ( "syscall/js" "github.com/0chain/gosdk/core/sys" + "github.com/0chain/gosdk/wasmsdk/jsbridge" + "github.com/0chain/gosdk/zcncore" ) type AuthCallbackFunc func(msg string) string +var authMsgCallback AuthCallbackFunc var authCallback AuthCallbackFunc var authResponseC chan string +var authMsgResponseC chan string +var authMsgLock = make(chan struct{}, 1) // registerAuthorizer Register the callback function to authorize the transaction. // This function is called from JavaScript. @@ -30,6 +35,62 @@ func registerAuthorizer(this js.Value, args []js.Value) interface{} { return nil } +func registerZauthServer(serverAddr string) { + fmt.Println("registerZauthServer...") + jsbridge.SetZauthServer(serverAddr) + sys.SetAuthorize(zcncore.ZauthSignTxn(serverAddr)) + sys.SetAuthCommon(zcncore.ZauthAuthCommon(serverAddr)) +} + +// zvaultNewWallet generates new split wallet +func zvaultNewWallet(serverAddr, token string) (string, error) { + return zcncore.CallZvaultNewWalletString(serverAddr, token, "") +} + +// zvaultNewSplit generates new split wallet from existing clientID +func zvaultNewSplit(clientID, serverAddr, token string) (string, error) { + return zcncore.CallZvaultNewWalletString(serverAddr, token, clientID) +} + +func zvaultStoreKey(serverAddr, token, privateKey string) (string, error) { + return zcncore.CallZvaultStoreKeyString(serverAddr, token, privateKey) +} + +func zvaultRetrieveKeys(serverAddr, token, clientID string) (string, error) { + return zcncore.CallZvaultRetrieveKeys(serverAddr, token, clientID) +} + +func zvaultRevokeKey(serverAddr, token, clientID, publicKey string) error { + return zcncore.CallZvaultRevokeKey(serverAddr, token, clientID, publicKey) +} + +func zvaultDeletePrimaryKey(serverAddr, token, clientID string) error { + return zcncore.CallZvaultDeletePrimaryKey(serverAddr, token, clientID) +} + +func zvaultRetrieveWallets(serverAddr, token string) (string, error) { + return zcncore.CallZvaultRetrieveWallets(serverAddr, token) +} + +func zvaultRetrieveSharedWallets(serverAddr, token string) (string, error) { + return zcncore.CallZvaultRetrieveSharedWallets(serverAddr, token) +} + +func registerAuthCommon(this js.Value, args []js.Value) interface{} { + authMsgCallback = parseAuthorizerCallback(args[0]) + authMsgResponseC = make(chan string, 1) + + sys.AuthCommon = func(msg string) (string, error) { + authMsgLock <- struct{}{} + defer func() { + <-authMsgLock + }() + authMsgCallback(msg) + return <-authMsgResponseC, nil + } + return nil +} + // authResponse Publishes the response to the authorization request. // `response` is the response to the authorization request. func authResponse(response string) { diff --git a/wasmsdk/blobber.go b/wasmsdk/blobber.go index 4de6909f8..2a3253d0a 100644 --- a/wasmsdk/blobber.go +++ b/wasmsdk/blobber.go @@ -26,6 +26,8 @@ import ( "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/0chain/gosdk/zboxcore/zboxutil" + + "github.com/hack-pad/go-webworkers/worker" ) const FileOperationInsert = "insert" @@ -553,6 +555,7 @@ type BulkUploadOption struct { Md5HashFuncName string `json:"md5HashFuncName,omitempty"` MimeType string `json:"mimeType,omitempty"` MemoryStorer bool `json:"memoryStorer,omitempty"` + CustomMeta string `json:"customMeta,omitempty"` } // BulkUploadResult result of a single file upload, usually as part of bulk operations request @@ -772,6 +775,7 @@ func multiUpload(jsonBulkUploadOptions string) (MultiUploadResult, error) { MimeType: mimeType, RemoteName: fileName, RemotePath: fullRemotePath, + CustomMeta: option.CustomMeta, } numBlocks := option.NumBlocks if numBlocks <= 1 { @@ -1200,7 +1204,7 @@ func cancelDownloadDirectory(remotePath string) { downloadDirLock.Unlock() } -func startListener() error { +func startListener(respChan chan string) error { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -1208,6 +1212,7 @@ func startListener() error { if err != nil { return err } + defer fmt.Println("[web worker] exiting") safeVal, _ := safejs.ValueOf("startListener") selfWorker.PostMessage(safeVal, nil) //nolint:errcheck @@ -1217,12 +1222,32 @@ func startListener() error { } sdk.InitHasherMap() for event := range listener { - data, err := event.Data() - if err != nil { - PrintError("Error in getting data from event", err) - return err - } - sdk.ProcessEventData(data) + func(event worker.MessageEvent) { + msgType, data, err := jsbridge.GetMsgType(event) + if err != nil { + PrintError("Error in getting data from event", err) + return + } + + switch msgType { + case jsbridge.MsgTypeAuthRsp: + rsp, err := jsbridge.ParseEventDataField(data, "data") + if err != nil { + PrintError("Error in parsing data from event", err) + return + } + respChan <- rsp + case jsbridge.MsgTypeUpload: + go sdk.ProcessEventData(*data) + case jsbridge.MsgTypeUpdateWallet: + fmt.Println("received update wallet event") + if err := UpdateWalletWithEventData(data); err != nil { + PrintError("Error in updating wallet", err) + } + default: + PrintError("Unknown message type", msgType) + } + }(event) } return nil diff --git a/wasmsdk/cache.go b/wasmsdk/cache.go index f7fdae5b0..9089015b3 100644 --- a/wasmsdk/cache.go +++ b/wasmsdk/cache.go @@ -84,7 +84,15 @@ func addWebWorkers(alloc *sdk.Allocation) (err error) { respChan := make(chan error, len(alloc.Blobbers)) respRequired := 0 for _, blober := range alloc.Blobbers { - weborker, workerCreated, _ := jsbridge.NewWasmWebWorker(blober.ID, blober.Baseurl, c.ClientID, c.Keys[0].PublicKey, c.Keys[0].PrivateKey, c.Mnemonic) //nolint:errcheck + weborker, workerCreated, _ := jsbridge.NewWasmWebWorker(blober.ID, + blober.Baseurl, + c.ClientID, + c.ClientKey, + c.PeerPublicKey, + c.Keys[0].PublicKey, + c.Keys[0].PrivateKey, + c.Mnemonic, + c.IsSplit) //nolint:errcheck if workerCreated { respRequired++ go func() { diff --git a/wasmsdk/jsbridge/func.go b/wasmsdk/jsbridge/func.go index 1c35286fc..bc4f08429 100644 --- a/wasmsdk/jsbridge/func.go +++ b/wasmsdk/jsbridge/func.go @@ -31,7 +31,7 @@ func BindFunc(global js.Value, jsFuncName string, fn interface{}) error { func BindAsyncFuncs(global js.Value, fnList map[string]interface{}) { for jsFuncName, fn := range fnList { - if jsFuncName == "registerAuthorizer" || jsFuncName == "callAuth" { + if jsFuncName == "registerAuthorizer" || jsFuncName == "callAuth" || jsFuncName == "registerAuthCommon" { global.Set(jsFuncName, fn) } else { jsFunc, err := promise(fn) @@ -45,7 +45,6 @@ func BindAsyncFuncs(global js.Value, fnList map[string]interface{}) { } } - func BindFuncs(global js.Value, fnList map[string]interface{}) { for jsFuncName, fn := range fnList { diff --git a/wasmsdk/jsbridge/webworker.go b/wasmsdk/jsbridge/webworker.go index 5f33af169..d889de621 100644 --- a/wasmsdk/jsbridge/webworker.go +++ b/wasmsdk/jsbridge/webworker.go @@ -6,13 +6,23 @@ package jsbridge import ( "context" "errors" + "fmt" + "strconv" "sync" + "syscall/js" "github.com/google/uuid" "github.com/hack-pad/go-webworkers/worker" "github.com/hack-pad/safejs" ) +const ( + MsgTypeAuth = "auth" + MsgTypeAuthRsp = "auth_rsp" + MsgTypeUpload = "upload" + MsgTypeUpdateWallet = "update_wallet" +) + type WasmWebWorker struct { // Name specifies an identifying name for the DedicatedWorkerGlobalScope representing the scope of the worker, which is mainly useful for debugging purposes. // If this is not specified, `Start` will create a UUIDv4 for it and populate back. @@ -47,19 +57,30 @@ type WasmWebWorker struct { } var ( - workers = make(map[string]*WasmWebWorker) + workers = make(map[string]*WasmWebWorker) + gZauthServer string ) -func NewWasmWebWorker(blobberID, blobberURL, clientID, publicKey, privateKey, mnemonic string) (*WasmWebWorker, bool, error) { +func NewWasmWebWorker(blobberID, blobberURL, clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemonic string, isSplit bool) (*WasmWebWorker, bool, error) { created := false _, ok := workers[blobberID] if ok { return workers[blobberID], created, nil } + fmt.Println("New wasm web worker, zauth server:", gZauthServer) w := &WasmWebWorker{ - Name: blobberURL, - Env: []string{"BLOBBER_URL=" + blobberURL, "CLIENT_ID=" + clientID, "PRIVATE_KEY=" + privateKey, "MODE=worker", "PUBLIC_KEY=" + publicKey, "MNEMONIC=" + mnemonic}, + Name: blobberURL, + Env: []string{"BLOBBER_URL=" + blobberURL, + "CLIENT_ID=" + clientID, + "CLIENT_KEY=" + clientKey, + "PEER_PUBLIC_KEY=" + peerPublicKey, + "PRIVATE_KEY=" + privateKey, + "MODE=worker", + "PUBLIC_KEY=" + publicKey, + "IS_SPLIT=" + strconv.FormatBool(isSplit), + "MNEMONIC=" + mnemonic, + "ZAUTH_SERVER=" + gZauthServer}, Path: "zcn.wasm", subscribers: make(map[string]chan worker.MessageEvent), } @@ -214,3 +235,86 @@ func (ww *WasmWebWorker) Terminate() { func (ww *WasmWebWorker) Listen(ctx context.Context) (<-chan worker.MessageEvent, error) { return ww.worker.Listen(ctx) } + +func SetZauthServer(zauthServer string) { + gZauthServer = zauthServer +} + +type PostWorker interface { + PostMessage(data safejs.Value, transferables []safejs.Value) error +} + +func PostMessage(w PostWorker, msgType string, data map[string]string) error { + msgTypeUint8Array := js.Global().Get("Uint8Array").New(len(msgType)) + js.CopyBytesToJS(msgTypeUint8Array, []byte(msgType)) + + obj := js.Global().Get("Object").New() + obj.Set("msgType", msgTypeUint8Array) + + for k, v := range data { + if k == "msgType" { + return errors.New("msgType is key word reserved") + } + + dataUint8Array := js.Global().Get("Uint8Array").New(len(v)) + js.CopyBytesToJS(dataUint8Array, []byte(v)) + obj.Set(k, dataUint8Array) + } + + return w.PostMessage(safejs.Safe(obj), nil) +} + +func GetMsgType(event worker.MessageEvent) (string, *safejs.Value, error) { + data, err := event.Data() + if err != nil { + return "", nil, err + } + + mt, err := data.Get("msgType") + if err != nil { + return "", nil, err + } + msgTypeLen, err := mt.Length() + if err != nil { + return "", nil, err + } + + mstType := make([]byte, msgTypeLen) + safejs.CopyBytesToGo(mstType, mt) + + return string(mstType), &data, nil +} + +func SetMsgType(data *js.Value, msgType string) { + msgTypeUint8Array := js.Global().Get("Uint8Array").New(len(msgType)) + js.CopyBytesToJS(msgTypeUint8Array, []byte(msgType)) + data.Set("msgType", msgTypeUint8Array) +} + +func ParseEventDataField(data *safejs.Value, field string) (string, error) { + fieldUint8Array, err := data.Get(field) + if err != nil { + return "", err + } + fieldLen, err := fieldUint8Array.Length() + if err != nil { + return "", err + } + + fieldData := make([]byte, fieldLen) + safejs.CopyBytesToGo(fieldData, fieldUint8Array) + + return string(fieldData), nil +} + +func PostMessageToAllWorkers(msgType string, data map[string]string) error { + for id, worker := range workers { + fmt.Println("post message to worker", id) + err := PostMessage(worker, msgType, data) + if err != nil { + return fmt.Errorf("failed to post message to worker: %s, err: %v", id, err) + } + } + + return nil +} diff --git a/wasmsdk/jsbridge/zcnworker.js.tpl b/wasmsdk/jsbridge/zcnworker.js.tpl index 6cf03e7af..2f4d3d4ac 100644 --- a/wasmsdk/jsbridge/zcnworker.js.tpl +++ b/wasmsdk/jsbridge/zcnworker.js.tpl @@ -57,5 +57,45 @@ self.__zcn_worker_wasm__ = { } return sig.serializeToHexStr() - } -} \ No newline at end of file + }, + initProxyKeys: initProxyKeys, + verify: blsVerify, + verifyWith: blsVerifyWith, + addSignature: blsAddSignature +} + +async function initProxyKeys(publicKey, privateKey) { + const pubKey = bls.deserializeHexStrToPublicKey(publicKey) + const privKey = bls.deserializeHexStrToSecretKey(privateKey) + bls.publicKey = pubKey + bls.secretKey = privKey +} + +async function blsVerify(signature, hash) { + const bytes = hexStringToByte(hash) + const sig = bls.deserializeHexStrToSignature(signature) + return jsProxy.publicKey.verify(sig, bytes) +} + +async function blsVerifyWith(pk, signature, hash) { + const publicKey = bls.deserializeHexStrToPublicKey(pk) + const bytes = hexStringToByte(hash) + const sig = bls.deserializeHexStrToSignature(signature) + return publicKey.verify(sig, bytes) +} + +async function blsAddSignature(secretKey, signature, hash) { + const privateKey = bls.deserializeHexStrToSecretKey(secretKey) + const sig = bls.deserializeHexStrToSignature(signature) + var sig2 = privateKey.sign(hexStringToByte(hash)) + if (!sig2) { + const errMsg = + 'err: wasm blsAddSignature function failed to sign transaction' + console.warn(errMsg) + throw new Error(errMsg) + } + + sig.add(sig2) + + return sig.serializeToHexStr() +} diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index 070903d19..04d225b67 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -4,10 +4,12 @@ package main import ( + "encoding/json" "errors" "fmt" "os" "runtime/debug" + "strconv" "sync" "time" @@ -19,6 +21,8 @@ import ( "github.com/0chain/gosdk/zboxcore/sdk" "github.com/0chain/gosdk/zcncore" + "github.com/hack-pad/safejs" + "syscall/js" ) @@ -42,6 +46,7 @@ func main() { zcn := window.Get("__zcn_wasm__") if !(zcn.IsNull() || zcn.IsUndefined()) { + fmt.Println("zcn is null, set it") jsProxy := zcn.Get("jsProxy") // import functions from js object @@ -70,6 +75,46 @@ func main() { // js already has signatureScheme and keys return signFunc(hash) } + + sys.SignWithAuth = func(hash, signatureScheme string, keys []sys.KeyPair) (string, error) { + sig, err := sys.Sign(hash, signatureScheme, keys) + if err != nil { + return "", fmt.Errorf("failed to sign with split key: %v", err) + } + + data, err := json.Marshal(struct { + Hash string `json:"hash"` + Signature string `json:"signature"` + ClientID string `json:"client_id"` + }{ + Hash: hash, + Signature: sig, + ClientID: client.GetClient().ClientID, + }) + if err != nil { + return "", err + } + + if sys.AuthCommon == nil { + return "", errors.New("authCommon is not set") + } + + rsp, err := sys.AuthCommon(string(data)) + if err != nil { + return "", err + } + + var sigpk struct { + Sig string `json:"sig"` + } + + err = json.Unmarshal([]byte(rsp), &sigpk) + if err != nil { + return "", err + } + + return sigpk.Sig, nil + } } else { PrintError("__zcn_wasm__.jsProxy.sign is not installed yet") } @@ -93,7 +138,6 @@ func main() { } jsVerifyWith := jsProxy.Get("verifyWith") - if !(jsVerifyWith.IsNull() || jsVerifyWith.IsUndefined()) { verifyFuncWith := func(pk, signature, hash string) (bool, error) { result, err := jsbridge.Await(jsVerifyWith.Invoke(pk, signature, hash)) @@ -233,6 +277,9 @@ func main() { "unLockReadPool": unLockReadPool, "createReadPool": createReadPool, + // claim rewards + "collectRewards": collectRewards, + // stakepool "getSkatePoolInfo": getSkatePoolInfo, "lockStakePool": lockStakePool, @@ -275,8 +322,21 @@ func main() { "setAuthUrl": setAuthUrl, "registerAuthorizer": js.FuncOf(registerAuthorizer), + "registerAuthCommon": js.FuncOf(registerAuthCommon), "callAuth": js.FuncOf(callAuth), "authResponse": authResponse, + + // zauth + "registerZauthServer": registerZauthServer, + // zvault + "zvaultNewWallet": zvaultNewWallet, + "zvaultNewSplit": zvaultNewSplit, + "zvaultStoreKey": zvaultStoreKey, + "zvaultRetrieveKeys": zvaultRetrieveKeys, + "zvaultRevokeKey": zvaultRevokeKey, + "zvaultDeletePrimaryKey": zvaultDeletePrimaryKey, + "zvaultRetrieveWallets": zvaultRetrieveWallets, + "zvaultRetrieveSharedWallets": zvaultRetrieveSharedWallets, }) fmt.Println("__wasm_initialized__ = true;") @@ -285,9 +345,13 @@ func main() { PrintError("__zcn_wasm__.sdk is not installed yet") } + } else { + fmt.Println("zcn is not null") + fmt.Println("zcn is not null - signWithAuth:", sys.SignWithAuth) } if mode != "" { + respChan := make(chan string, 1) jsProxy := window.Get("__zcn_worker_wasm__") if !(jsProxy.IsNull() || jsProxy.IsUndefined()) { jsSign := jsProxy.Get("sign") @@ -312,17 +376,114 @@ func main() { // js already has signatureScheme and keys return signFunc(hash) } + + sys.SignWithAuth = func(hash, signatureScheme string, keys []sys.KeyPair) (string, error) { + fmt.Println("[worker] SignWithAuth pubkey:", keys[0]) + sig, err := sys.Sign(hash, signatureScheme, keys) + if err != nil { + return "", fmt.Errorf("failed to sign with split key: %v", err) + } + + data, err := json.Marshal(struct { + Hash string `json:"hash"` + Signature string `json:"signature"` + ClientID string `json:"client_id"` + }{ + Hash: hash, + Signature: sig, + ClientID: client.GetClient().ClientID, + }) + if err != nil { + return "", err + } + + if sys.AuthCommon == nil { + return "", errors.New("authCommon is not set") + } + + rsp, err := sys.AuthCommon(string(data)) + if err != nil { + return "", err + } + + var sigpk struct { + Sig string `json:"sig"` + } + + err = json.Unmarshal([]byte(rsp), &sigpk) + if err != nil { + return "", err + } + + return sigpk.Sig, nil + } + + fmt.Println("Init SignWithAuth:", sys.SignWithAuth) + } else { PrintError("__zcn_worker_wasm__.jsProxy.sign is not installed yet") } + + initProxyKeys := jsProxy.Get("initProxyKeys") + if !(initProxyKeys.IsNull() || initProxyKeys.IsUndefined()) { + gInitProxyKeys = func(publicKey, privateKey string) { + // jsProxy.Set("publicKey", bls.DeserializeHexStrToPublicKey(publicKey)) + // jsProxy.Set("secretKey", bls.DeserializeHexStrToSecretKey(privateKey)) + _, err := jsbridge.Await(initProxyKeys.Invoke(publicKey, privateKey)) + if len(err) > 0 && !err[0].IsNull() { + PrintError("initProxyKeys: ", err[0].String()) + return + } + + // return result[0].String(), nil + return + } + } + + fmt.Println("Init SignWithAuth:", sys.SignWithAuth) } else { PrintError("__zcn_worker_wasm__ is not installed yet") } - setWallet(os.Getenv("CLIENT_ID"), os.Getenv("PUBLIC_KEY"), os.Getenv("PRIVATE_KEY"), os.Getenv("MNEMONIC")) + + fmt.Println("CLIENT_ID:", os.Getenv("CLIENT_ID")) + isSplitEnv := os.Getenv("IS_SPLIT") + // convert to bool + isSplit, err := strconv.ParseBool(isSplitEnv) + if err != nil { + fmt.Println("convert isSplitEnv failed:", err) + return + } + + clientID := os.Getenv("CLIENT_ID") + clientKey := os.Getenv("CLIENT_KEY") + publicKey := os.Getenv("PUBLIC_KEY") + peerPublicKey := os.Getenv("PEER_PUBLIC_KEY") + mnemonic := os.Getenv("MNEMONIC") + privateKey := os.Getenv("PRIVATE_KEY") + zauthServer := os.Getenv("ZAUTH_SERVER") + + gInitProxyKeys(publicKey, privateKey) + + if isSplit { + sys.AuthCommon = func(msg string) (string, error) { + // send message to main thread + sendMessageToMainThread(msg) + // wait for response from main thread + rsp := <-respChan + return rsp, nil + } + + // TODO: differe the registerAuthorizer + // registerZauthServer("http://18.191.13.66:8080", publicKey) + // registerZauthServer("http://127.0.0.1:8080", publicKey) + registerZauthServer(zauthServer) + } + + setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemonic, isSplit) hideLogs() debug.SetGCPercent(40) debug.SetMemoryLimit(300 * 1024 * 1024) //300MB - err := startListener() + err = startListener(respChan) if err != nil { fmt.Println("Error starting listener", err) return @@ -337,3 +498,51 @@ func main() { jsbridge.Close() } + +var gInitProxyKeys func(publicKey, privateKey string) + +func sendMessageToMainThread(msg string) { + PrintInfo("[send to main thread]:", msg) + jsbridge.PostMessage(jsbridge.GetSelfWorker(), jsbridge.MsgTypeAuth, map[string]string{"msg": msg}) +} + +func UpdateWalletWithEventData(data *safejs.Value) error { + clientID, err := jsbridge.ParseEventDataField(data, "client_id") + if err != nil { + return err + } + clientKey, err := jsbridge.ParseEventDataField(data, "client_key") + if err != nil { + return err + } + peerPublicKey, err := jsbridge.ParseEventDataField(data, "peer_public_key") + if err != nil { + return err + } + + publicKey, err := jsbridge.ParseEventDataField(data, "public_key") + if err != nil { + return err + } + privateKey, err := jsbridge.ParseEventDataField(data, "private_key") + if err != nil { + return err + } + mnemonic, err := jsbridge.ParseEventDataField(data, "mnemonic") + if err != nil { + return err + } + isSplitStr, err := jsbridge.ParseEventDataField(data, "is_split") + if err != nil { + return err + } + + isSplit, err := strconv.ParseBool(isSplitStr) + if err != nil { + isSplit = false + } + + fmt.Println("update wallet with event data") + setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemonic, isSplit) + return nil +} diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index ca05366a2..6ea3d9635 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -36,7 +36,8 @@ var CreateObjectURL func(buf []byte, mimeType string) string // - zboxAppType is the application type of the 0box service // - sharderconsensous is the number of sharders to reach consensus func initSDKs(chainID, blockWorker, signatureScheme string, - minConfirmation, minSubmit, confirmationChainLength int, zboxHost, zboxAppType string, sharderconsensous int) error { + minConfirmation, minSubmit, confirmationChainLength int, + zboxHost, zboxAppType string, sharderconsensous int, isSplit bool) error { zboxApiClient.SetRequest(zboxHost, zboxAppType) @@ -46,6 +47,16 @@ func initSDKs(chainID, blockWorker, signatureScheme string, return err } + clientConf, err := conf.GetClientConfig() + if err != nil { + return err + } + + if !isSplit && clientConf.IsSplitWallet { + // split wallet should not be reset back, use the existing + isSplit = true + } + err = client.Init(context.Background(), conf.Config{ BlockWorker: blockWorker, SignatureScheme: signatureScheme, @@ -54,6 +65,7 @@ func initSDKs(chainID, blockWorker, signatureScheme string, MinSubmit: minSubmit, ConfirmationChainLength: confirmationChainLength, SharderConsensous: sharderconsensous, + IsSplitWallet: isSplit, }) if err != nil { @@ -127,6 +139,7 @@ func getLookupHash(allocationID string, path string) string { // createThumbnail create thumbnail of an image buffer. It supports // - png + // - jpeg // - gif // - bmp diff --git a/wasmsdk/wallet.go b/wasmsdk/wallet.go index 976727c62..6c3594653 100644 --- a/wasmsdk/wallet.go +++ b/wasmsdk/wallet.go @@ -6,19 +6,21 @@ package main import ( "errors" + "fmt" + "os" + "strconv" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/zcncrypto" + "github.com/0chain/gosdk/wasmsdk/jsbridge" ) -// setWallet sets the wallet used by the client for the network transactions and the backend API requests -// - clientID is the client id -// - publicKey is the public key of the client -// - privateKey is the private key of the client -// - mnemonic is the mnemonic of the client -func setWallet(clientID, publicKey, privateKey, mnemonic string) error { - if mnemonic == "" { +func setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemonic string, isSplit bool) error { + if mnemonic == "" && !isSplit { return errors.New("mnemonic is required") } + mode := os.Getenv("MODE") + fmt.Println("gosdk setWallet, mode:", mode, "is split:", isSplit) keys := []zcncrypto.KeyPair{ { PrivateKey: privateKey, @@ -26,15 +28,40 @@ func setWallet(clientID, publicKey, privateKey, mnemonic string) error { }, } + c := client.GetClient() + c.Mnemonic = mnemonic + c.ClientID = clientID + c.ClientKey = clientKey + c.PeerPublicKey = peerPublicKey + c.Keys = keys + c.IsSplit = isSplit + w := &zcncrypto.Wallet{ - ClientID: clientID, - ClientKey: publicKey, - Mnemonic: mnemonic, - Keys: keys, + ClientID: clientID, + ClientKey: clientKey, + PeerPublicKey: peerPublicKey, + Mnemonic: mnemonic, + Keys: keys, + IsSplit: isSplit, } - client.SetWallet(*w) + fmt.Println("set Wallet, is split:", isSplit) + client.SetWallet(isSplit, *w) zboxApiClient.SetWallet(clientID, privateKey, publicKey) + if mode == "" { // main thread, need to notify the web worker to update wallet + // notify the web worker to update wallet + if err := jsbridge.PostMessageToAllWorkers(jsbridge.MsgTypeUpdateWallet, map[string]string{ + "client_id": clientID, + "client_key": clientKey, + "peer_public_key": peerPublicKey, + "public_key": publicKey, + "private_key": privateKey, + "mnemonic": mnemonic, + "is_split": strconv.FormatBool(isSplit), + }); err != nil { + return err + } + } return nil } diff --git a/wasmsdk/zbox.go b/wasmsdk/zbox.go index 257f7660f..6410a2d1a 100644 --- a/wasmsdk/zbox.go +++ b/wasmsdk/zbox.go @@ -29,30 +29,30 @@ func getCsrfToken() (string, error) { // createJwtSession creates jwt session for the given phone number // - phoneNumber is the phone number of the user -func createJwtSession(phoneNumber string) (int64, error) { +func createJwtSession(userID string) (int64, error) { if zboxApiClient == nil { return 0, ErrZboxApiNotInitialized } - return zboxApiClient.CreateJwtSession(context.TODO(), phoneNumber) + return zboxApiClient.CreateJwtSession(context.TODO(), userID) } // createJwtToken creates jwt token for the given phone number // - phoneNumber is the phone number of the user // - jwtSessionID is the jwt session id // - otp is the one time password -func createJwtToken(phoneNumber string, jwtSessionID int64, otp string) (string, error) { +func createJwtToken(userID string, jwtSessionID int64) (string, error) { if zboxApiClient == nil { return "", ErrZboxApiNotInitialized } - return zboxApiClient.CreateJwtToken(context.TODO(), phoneNumber, jwtSessionID, otp) + return zboxApiClient.CreateJwtToken(context.TODO(), userID, jwtSessionID) } // refreshJwtToken refreshes jwt token for the given phone number // - phoneNumber is the phone number of the user // - token is the jwt token to refresh -func refreshJwtToken(phoneNumber string, token string) (string, error) { +func refreshJwtToken(userID string, token string) (string, error) { if zboxApiClient == nil { return "", ErrZboxApiNotInitialized } - return zboxApiClient.RefreshJwtToken(context.TODO(), phoneNumber, token) + return zboxApiClient.RefreshJwtToken(context.TODO(), userID, token) } diff --git a/winsdk/sdk.go b/winsdk/sdk.go index 86227ae14..643d70047 100644 --- a/winsdk/sdk.go +++ b/winsdk/sdk.go @@ -163,7 +163,7 @@ func InitWallet(clientJson *C.char) *C.char { l.Logger.Error(err) return WithJSON(false, err) } - client.SetWallet(w) + client.SetWallet(false, w) l.Logger.Info("InitWallet success") zboxApiClient.SetWallet(client.Wallet().ClientID, client.Wallet().Keys[0].PrivateKey, client.Wallet().ClientKey) diff --git a/winsdk/zboxapi.go b/winsdk/zboxapi.go index 4576d7773..149ad043b 100644 --- a/winsdk/zboxapi.go +++ b/winsdk/zboxapi.go @@ -103,7 +103,7 @@ func GetCsrfToken() *C.char { // } // //export CreateJwtSession -func CreateJwtSession(phoneNumber *C.char) *C.char { +func CreateJwtSession(userID *C.char) *C.char { defer func() { if r := recover(); r != nil { log.Error("win: crash ", r) @@ -112,7 +112,7 @@ func CreateJwtSession(phoneNumber *C.char) *C.char { if zboxApiClient == nil { return WithJSON(0, ErrZboxApiNotInitialized) } - return WithJSON(zboxApiClient.CreateJwtSession(context.TODO(), C.GoString(phoneNumber))) + return WithJSON(zboxApiClient.CreateJwtSession(context.TODO(), C.GoString(userID))) } // CreateJwtToken create a fresh jwt token @@ -124,7 +124,7 @@ func CreateJwtSession(phoneNumber *C.char) *C.char { // } // //export CreateJwtToken -func CreateJwtToken(phoneNumber *C.char, jwtSessionID int64, otp *C.char) *C.char { +func CreateJwtToken(userID *C.char, jwtSessionID int64) *C.char { defer func() { if r := recover(); r != nil { log.Error("win: crash ", r) @@ -133,7 +133,7 @@ func CreateJwtToken(phoneNumber *C.char, jwtSessionID int64, otp *C.char) *C.cha if zboxApiClient == nil { return WithJSON("", ErrZboxApiNotInitialized) } - return WithJSON(zboxApiClient.CreateJwtToken(context.TODO(), C.GoString(phoneNumber), jwtSessionID, C.GoString(otp))) + return WithJSON(zboxApiClient.CreateJwtToken(context.TODO(), C.GoString(userID), jwtSessionID)) } // RefreshJwtToken refresh jwt token @@ -145,7 +145,7 @@ func CreateJwtToken(phoneNumber *C.char, jwtSessionID int64, otp *C.char) *C.cha // } // //export RefreshJwtToken -func RefreshJwtToken(phoneNumber, token *C.char) *C.char { +func RefreshJwtToken(userID, token *C.char) *C.char { defer func() { if r := recover(); r != nil { log.Error("win: crash ", r) @@ -154,7 +154,7 @@ func RefreshJwtToken(phoneNumber, token *C.char) *C.char { if zboxApiClient == nil { return WithJSON("", ErrZboxApiNotInitialized) } - return WithJSON(zboxApiClient.RefreshJwtToken(context.TODO(), C.GoString(phoneNumber), C.GoString(token))) + return WithJSON(zboxApiClient.RefreshJwtToken(context.TODO(), C.GoString(userID), C.GoString(token))) } // GetFreeMarker create a free storage marker diff --git a/zboxapi/sdk.go b/zboxapi/sdk.go index dee51560a..6c2d61094 100644 --- a/zboxapi/sdk.go +++ b/zboxapi/sdk.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/0chain/gosdk/zcncore" "net/http" "strconv" "time" @@ -14,8 +15,6 @@ import ( "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/core/resty" - "github.com/0chain/gosdk/core/sys" - "github.com/0chain/gosdk/core/zcncrypto" ) var log logger.Logger @@ -24,27 +23,6 @@ func GetLogger() *logger.Logger { return &log } -func signHash(hash string, signatureScheme string, keys []sys.KeyPair) (string, error) { - retSignature := "" - for _, kv := range keys { - ss := zcncrypto.NewSignatureScheme(signatureScheme) - err := ss.SetPrivateKey(kv.PrivateKey) - if err != nil { - return "", err - } - - if len(retSignature) == 0 { - retSignature, err = ss.Sign(hash) - } else { - retSignature, err = ss.Add(retSignature, hash) - } - if err != nil { - return "", err - } - } - return retSignature, nil -} - type Client struct { baseUrl string appType string @@ -113,18 +91,17 @@ func (c *Client) GetCsrfToken(ctx context.Context) (string, error) { return result.Token, nil } -func (c *Client) createResty(ctx context.Context, csrfToken, phoneNumber string, headers map[string]string) (*resty.Resty, error) { +func (c *Client) createResty(ctx context.Context, csrfToken, userID string, headers map[string]string) (*resty.Resty, error) { h := make(map[string]string) h["X-App-Client-ID"] = c.clientID h["X-App-Client-Key"] = c.clientPublicKey - h["X-App-Phone-Number"] = phoneNumber + h["X-App-User-ID"] = userID if c.clientPrivateKey != "" { - data := fmt.Sprintf("%v:%v:%v", c.clientID, phoneNumber, c.clientPublicKey) + data := fmt.Sprintf("%v:%v:%v", c.clientID, userID, c.clientPublicKey) hash := encryption.Hash(data) - sign, err := signHash(hash, "bls0chain", []sys.KeyPair{{ - PrivateKey: c.clientPrivateKey, - }}) + + sign, err := zcncore.SignFn(hash) if err != nil { return nil, err } @@ -143,15 +120,15 @@ func (c *Client) createResty(ctx context.Context, csrfToken, phoneNumber string, return resty.New(resty.WithHeader(h)), nil } -// CreateJwtSession create a jwt session with phone number -func (c *Client) CreateJwtSession(ctx context.Context, phoneNumber string) (int64, error) { +// CreateJwtSession create a jwt session with user id +func (c *Client) CreateJwtSession(ctx context.Context, userID string) (int64, error) { csrfToken, err := c.GetCsrfToken(ctx) if err != nil { return 0, err } - r, err := c.createResty(ctx, csrfToken, phoneNumber, nil) + r, err := c.createResty(ctx, csrfToken, userID, nil) if err != nil { return 0, err @@ -176,17 +153,16 @@ func (c *Client) CreateJwtSession(ctx context.Context, phoneNumber string) (int6 } // CreateJwtToken create a jwt token with jwt session id and otp -func (c *Client) CreateJwtToken(ctx context.Context, phoneNumber string, jwtSessionID int64, otp string) (string, error) { +func (c *Client) CreateJwtToken(ctx context.Context, userID string, jwtSessionID int64) (string, error) { csrfToken, err := c.GetCsrfToken(ctx) if err != nil { return "", err } headers := map[string]string{ "X-JWT-Session-ID": strconv.FormatInt(jwtSessionID, 10), - "X-JWT-OTP": otp, } - r, err := c.createResty(ctx, csrfToken, phoneNumber, headers) + r, err := c.createResty(ctx, csrfToken, userID, headers) if err != nil { return "", err @@ -210,7 +186,7 @@ func (c *Client) CreateJwtToken(ctx context.Context, phoneNumber string, jwtSess } // RefreshJwtToken refresh jwt token -func (c *Client) RefreshJwtToken(ctx context.Context, phoneNumber string, token string) (string, error) { +func (c *Client) RefreshJwtToken(ctx context.Context, userID string, token string) (string, error) { csrfToken, err := c.GetCsrfToken(ctx) if err != nil { return "", err @@ -219,7 +195,7 @@ func (c *Client) RefreshJwtToken(ctx context.Context, phoneNumber string, token "X-JWT-Token": token, } - r, err := c.createResty(ctx, csrfToken, phoneNumber, headers) + r, err := c.createResty(ctx, csrfToken, userID, headers) if err != nil { return "", err diff --git a/zboxapi/sdk_test.go b/zboxapi/sdk_test.go index 2b54496ca..3dfded2bb 100644 --- a/zboxapi/sdk_test.go +++ b/zboxapi/sdk_test.go @@ -15,7 +15,8 @@ var ( ClientID = "70e1318a9709786cf975f15ca941bee73d0f422305ecd78b0f358870ec17f41d" ClientPublicKey = "4ec4b4dfb8c9ceb8fb6e84ef46e503c3445a0c6d770986a019cdbef4bc47b70dfadd5441f708f0df47df14e5cd6a0aa94ec31ca66e337692d9a92599d9456a81" ClientPrivateKey = "982801f352e886eaaf61196d83373b4cc09e9a598ffe1f49bf5adf905174cb0c" - PhoneNumber = "+16026666666" + UserID = "lWVZRhERosYtXR9MBJh5yJUtweI4" + PhoneNumber = "+917777777777" ) func TestGetCsrfToken(t *testing.T) { @@ -37,17 +38,17 @@ func TestJwtToken(t *testing.T) { c := NewClient() c.SetRequest(BaseURL, AppType) c.SetWallet(ClientID, ClientPrivateKey, ClientPublicKey) - sessionID, err := c.CreateJwtSession(context.TODO(), PhoneNumber) + sessionID, err := c.CreateJwtSession(context.TODO(), UserID) require.Nil(t, err) require.GreaterOrEqual(t, sessionID, int64(0)) - token, err := c.CreateJwtToken(context.TODO(), PhoneNumber, sessionID, "000000") //any otp works on test phone number + token, err := c.CreateJwtToken(context.TODO(), UserID, sessionID) //any otp works on test phone number require.Nil(t, err) require.NotEmpty(t, token) - refreshedToken, err := c.RefreshJwtToken(context.TODO(), PhoneNumber, token) + refreshedToken, err := c.RefreshJwtToken(context.TODO(), UserID, token) require.Nil(t, err) require.NotEmpty(t, refreshedToken) require.NotEqual(t, token, refreshedToken) @@ -59,12 +60,12 @@ func TestGetFreeStorage(t *testing.T) { c := NewClient() c.SetRequest(BaseURL, AppType) c.SetWallet(ClientID, ClientPrivateKey, ClientPublicKey) - sessionID, err := c.CreateJwtSession(context.TODO(), PhoneNumber) + sessionID, err := c.CreateJwtSession(context.TODO(), UserID) require.Nil(t, err) require.GreaterOrEqual(t, sessionID, int64(0)) - token, err := c.CreateJwtToken(context.TODO(), PhoneNumber, sessionID, "000000") //any otp works on test phone number + token, err := c.CreateJwtToken(context.TODO(), UserID, sessionID) //any otp works on test phone number require.Nil(t, err) require.NotEmpty(t, token) @@ -89,12 +90,12 @@ func TestCreateSharedInfo(t *testing.T) { c := NewClient() c.SetRequest(BaseURL, AppType) c.SetWallet(ClientID, ClientPrivateKey, ClientPublicKey) - sessionID, err := c.CreateJwtSession(context.TODO(), PhoneNumber) + sessionID, err := c.CreateJwtSession(context.TODO(), UserID) require.Nil(t, err) require.GreaterOrEqual(t, sessionID, int64(0)) - token, err := c.CreateJwtToken(context.TODO(), PhoneNumber, sessionID, "000000") //any otp works on test phone number + token, err := c.CreateJwtToken(context.TODO(), UserID, sessionID) //any otp works on test phone number require.Nil(t, err) require.NotEmpty(t, token) @@ -111,12 +112,12 @@ func TestGetSharedToMe(t *testing.T) { c := NewClient() c.SetRequest(BaseURL, AppType) c.SetWallet(ClientID, ClientPrivateKey, ClientPublicKey) - sessionID, err := c.CreateJwtSession(context.TODO(), PhoneNumber) + sessionID, err := c.CreateJwtSession(context.TODO(), UserID) require.Nil(t, err) require.GreaterOrEqual(t, sessionID, int64(0)) - token, err := c.CreateJwtToken(context.TODO(), PhoneNumber, sessionID, "000000") //any otp works on test phone number + token, err := c.CreateJwtToken(context.TODO(), UserID, sessionID) //any otp works on test phone number require.Nil(t, err) require.NotEmpty(t, token) @@ -135,12 +136,12 @@ func TestGetSharedByMe(t *testing.T) { c := NewClient() c.SetRequest(BaseURL, AppType) c.SetWallet(ClientID, ClientPrivateKey, ClientPublicKey) - sessionID, err := c.CreateJwtSession(context.TODO(), PhoneNumber) + sessionID, err := c.CreateJwtSession(context.TODO(), UserID) require.Nil(t, err) require.GreaterOrEqual(t, sessionID, int64(0)) - token, err := c.CreateJwtToken(context.TODO(), PhoneNumber, sessionID, "000000") //any otp works on test phone number + token, err := c.CreateJwtToken(context.TODO(), UserID, sessionID) //any otp works on test phone number require.Nil(t, err) require.NotEmpty(t, token) @@ -158,12 +159,12 @@ func TestGetSharedByPublic(t *testing.T) { c := NewClient() c.SetRequest(BaseURL, AppType) c.SetWallet(ClientID, ClientPrivateKey, ClientPublicKey) - sessionID, err := c.CreateJwtSession(context.TODO(), PhoneNumber) + sessionID, err := c.CreateJwtSession(context.TODO(), UserID) require.Nil(t, err) require.GreaterOrEqual(t, sessionID, int64(0)) - token, err := c.CreateJwtToken(context.TODO(), PhoneNumber, sessionID, "000000") //any otp works on test phone number + token, err := c.CreateJwtToken(context.TODO(), UserID, sessionID) //any otp works on test phone number require.Nil(t, err) require.NotEmpty(t, token) @@ -181,12 +182,12 @@ func TestDeleteSharedInfo(t *testing.T) { c := NewClient() c.SetRequest(BaseURL, AppType) c.SetWallet(ClientID, ClientPrivateKey, ClientPublicKey) - sessionID, err := c.CreateJwtSession(context.TODO(), PhoneNumber) + sessionID, err := c.CreateJwtSession(context.TODO(), UserID) require.Nil(t, err) require.GreaterOrEqual(t, sessionID, int64(0)) - token, err := c.CreateJwtToken(context.TODO(), PhoneNumber, sessionID, "000000") //any otp works on test phone number + token, err := c.CreateJwtToken(context.TODO(), UserID, sessionID) //any otp works on test phone number require.Nil(t, err) require.NotEmpty(t, token) diff --git a/zboxcore/marker/writemarker.go b/zboxcore/marker/writemarker.go index c20e1b78d..60e328c1b 100644 --- a/zboxcore/marker/writemarker.go +++ b/zboxcore/marker/writemarker.go @@ -55,7 +55,7 @@ func (wm *WriteMarker) Sign() error { func (wm *WriteMarker) VerifySignature(clientPublicKey string) error { hashData := wm.GetHashData() signatureHash := encryption.Hash(hashData) - sigOK, err := sys.Verify(wm.Signature, signatureHash) + sigOK, err := sys.VerifyWith(clientPublicKey, wm.Signature, signatureHash) if err != nil { return errors.New("write_marker_validation_failed", "Error during verifying signature. "+err.Error()) } diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 6a85df40d..bbbb13277 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -300,6 +300,7 @@ type Allocation struct { // conseususes consensusThreshold int fullconsensus int + sig string `json:"-"` } // OperationRequest represents an operation request with its related options. @@ -850,7 +851,7 @@ func (a *Allocation) GetCurrentVersion() (bool, error) { go func(blobber *blockchain.StorageNode) { defer wg.Done() - wr, err := GetWritemarker(a.ID, a.Tx, blobber.ID, blobber.Baseurl) + wr, err := GetWritemarker(a.ID, a.Tx, a.sig, blobber.ID, blobber.Baseurl) if err != nil { atomic.AddInt32(&errCnt, 1) logger.Logger.Error("error during getWritemarke", zap.Error(err)) @@ -956,6 +957,7 @@ func (a *Allocation) RepairRequired(remotepath string) (zboxutil.Uint128, zboxut listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} listReq.allocationID = a.ID listReq.allocationTx = a.Tx + listReq.sig = a.sig listReq.blobbers = a.Blobbers listReq.fullconsensus = a.fullconsensus listReq.consensusThresh = a.DataShards @@ -988,9 +990,10 @@ func (a *Allocation) DoMultiOperation(operations []OperationRequest, opts ...Mul } connectionID := zboxutil.NewConnectionId() var mo MultiOperation + mo.allocationObj = a + for i := 0; i < len(operations); { // resetting multi operation and previous paths for every batch - mo.allocationObj = a mo.operationMask = zboxutil.NewUint128(0) mo.maskMU = &sync.Mutex{} mo.connectionID = connectionID @@ -1326,6 +1329,7 @@ func (a *Allocation) generateDownloadRequest( downloadReq.allocationID = a.ID downloadReq.allocationTx = a.Tx downloadReq.allocOwnerID = a.Owner + downloadReq.sig = a.sig downloadReq.allocOwnerPubKey = a.OwnerPublicKey downloadReq.ctx, downloadReq.ctxCncl = context.WithCancel(a.ctx) downloadReq.fileHandler = fileHandler @@ -1562,6 +1566,7 @@ func (a *Allocation) ListDirFromAuthTicket(authTicket string, lookupHash string, listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} listReq.allocationID = a.ID listReq.allocationTx = a.Tx + listReq.sig = a.sig listReq.blobbers = a.Blobbers listReq.fullconsensus = a.fullconsensus listReq.consensusThresh = a.consensusThreshold @@ -1601,6 +1606,7 @@ func (a *Allocation) ListDir(path string, opts ...ListRequestOptions) (*ListResu listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} listReq.allocationID = a.ID listReq.allocationTx = a.Tx + listReq.sig = a.sig listReq.blobbers = a.Blobbers listReq.fullconsensus = a.fullconsensus listReq.consensusThresh = a.DataShards @@ -1628,6 +1634,7 @@ func (a *Allocation) getRefs(path, pathHash, authToken, offsetPath, updatedDate, oTreeReq := &ObjectTreeRequest{ allocationID: a.ID, allocationTx: a.Tx, + sig: a.sig, blobbers: a.Blobbers, authToken: authToken, pathHash: pathHash, @@ -1848,6 +1855,7 @@ func (a *Allocation) GetRecentlyAddedRefs(page int, fromDate int64, pageLimit in req := &RecentlyAddedRefRequest{ allocationID: a.ID, allocationTx: a.Tx, + sig: a.sig, blobbers: a.Blobbers, offset: offset, fromDate: fromDate, @@ -1875,6 +1883,7 @@ func (a *Allocation) GetFileMeta(path string) (*ConsolidatedFileMeta, error) { listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} listReq.allocationID = a.ID listReq.allocationTx = a.Tx + listReq.sig = a.sig listReq.blobbers = a.Blobbers listReq.fullconsensus = a.fullconsensus listReq.consensusThresh = a.consensusThreshold @@ -1993,6 +2002,7 @@ func (a *Allocation) GetFileMetaFromAuthTicket(authTicket string, lookupHash str listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} listReq.allocationID = a.ID listReq.allocationTx = a.Tx + listReq.sig = a.sig listReq.blobbers = a.Blobbers listReq.fullconsensus = a.fullconsensus listReq.consensusThresh = a.consensusThreshold @@ -2038,6 +2048,7 @@ func (a *Allocation) GetFileStats(path string) (map[string]*FileStats, error) { listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} listReq.allocationID = a.ID listReq.allocationTx = a.Tx + listReq.sig = a.sig listReq.blobbers = a.Blobbers listReq.fullconsensus = a.fullconsensus listReq.consensusThresh = a.consensusThreshold @@ -2080,6 +2091,7 @@ func (a *Allocation) deleteFile(path string, threshConsensus, fullConsensus int, req.blobbers = a.Blobbers req.allocationID = a.ID req.allocationTx = a.Tx + req.sig = a.sig req.consensus.Init(threshConsensus, fullConsensus) req.ctx, req.ctxCncl = context.WithCancel(a.ctx) req.remotefilepath = path @@ -2110,6 +2122,7 @@ func (a *Allocation) createDir(remotePath string, threshConsensus, fullConsensus allocationObj: a, allocationID: a.ID, allocationTx: a.Tx, + sig: a.sig, blobbers: a.Blobbers, mu: &sync.Mutex{}, dirMask: mask, @@ -2169,7 +2182,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, query) + httpreq, err := zboxutil.NewRevokeShareRequest(baseUrl, a.ID, a.Tx, a.sig, query) if err != nil { return err } @@ -2265,6 +2278,7 @@ func (a *Allocation) GetAuthTicket(path, filename string, expirationSeconds: expiration, allocationID: a.ID, allocationTx: a.Tx, + sig: a.sig, blobbers: a.Blobbers, ctx: a.ctx, remotefilepath: path, @@ -2331,7 +2345,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, body) + httpreq, err := zboxutil.NewShareRequest(url, a.ID, a.Tx, a.sig, body) if err != nil { return err } @@ -2733,6 +2747,7 @@ func (a *Allocation) downloadFromAuthTicket(fileHandler sys.File, authTicket str downloadReq.maskMu = &sync.Mutex{} downloadReq.allocationID = a.ID downloadReq.allocationTx = a.Tx + downloadReq.sig = a.sig downloadReq.allocOwnerID = a.Owner downloadReq.allocOwnerPubKey = a.OwnerPublicKey downloadReq.ctx, downloadReq.ctxCncl = context.WithCancel(a.ctx) diff --git a/zboxcore/sdk/allocation_file_delete_test.go b/zboxcore/sdk/allocation_file_delete_test.go index e9be2c349..38b893a0d 100644 --- a/zboxcore/sdk/allocation_file_delete_test.go +++ b/zboxcore/sdk/allocation_file_delete_test.go @@ -29,7 +29,7 @@ func TestAllocation_DeleteFile(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -88,7 +88,7 @@ func TestAllocation_deleteFile(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) diff --git a/zboxcore/sdk/allocation_file_test.go b/zboxcore/sdk/allocation_file_test.go index ed3920133..28d6dc6fe 100644 --- a/zboxcore/sdk/allocation_file_test.go +++ b/zboxcore/sdk/allocation_file_test.go @@ -41,7 +41,7 @@ func setupHttpResponses( err := json.Unmarshal([]byte(walletJSON), &wallet) require.NoError(t, err) - client.SetWallet(wallet) + client.SetWallet(false, wallet) client.SetSignatureScheme("bls0chain") for i := 0; i < numBlobbers; i++ { @@ -683,7 +683,7 @@ func TestAllocation_RepairFile(t *testing.T) { resty.CreateClient = createClient }() - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) diff --git a/zboxcore/sdk/allocation_test.go b/zboxcore/sdk/allocation_test.go index f73a5a830..a1600b176 100644 --- a/zboxcore/sdk/allocation_test.go +++ b/zboxcore/sdk/allocation_test.go @@ -401,7 +401,7 @@ func TestAllocation_GetBlobberStats(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -546,7 +546,7 @@ func TestAllocation_RepairRequired(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -693,7 +693,7 @@ func TestAllocation_DownloadFileToFileHandler(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -774,7 +774,7 @@ func TestAllocation_DownloadFile(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -821,7 +821,7 @@ func TestAllocation_DownloadFileByBlock(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -875,7 +875,7 @@ func TestAllocation_downloadFile(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -1014,7 +1014,7 @@ func TestAllocation_GetRefs(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -1058,7 +1058,7 @@ func TestAllocation_GetFileMeta(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -1175,7 +1175,7 @@ func TestAllocation_GetAuthTicketForShare(t *testing.T) { mockClient.On("Do", mock.Anything).Return(&httpResponse, nil) } - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -1380,7 +1380,7 @@ func TestAllocation_GetAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -1589,7 +1589,7 @@ func TestAllocation_ListDirFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -1642,7 +1642,7 @@ func TestAllocation_downloadFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -1867,7 +1867,7 @@ func TestAllocation_listDir(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -2000,7 +2000,7 @@ func TestAllocation_GetFileMetaFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -2048,7 +2048,7 @@ func TestAllocation_DownloadToFileHandlerFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -2083,7 +2083,7 @@ func TestAllocation_DownloadThumbnailFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -2125,7 +2125,7 @@ func TestAllocation_DownloadFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -2161,7 +2161,7 @@ func TestAllocation_DownloadFromAuthTicketByBlocks(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -2196,7 +2196,7 @@ func TestAllocation_StartRepair(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -2381,7 +2381,7 @@ func getMockAuthTicket(t *testing.T) string { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) diff --git a/zboxcore/sdk/chunked_upload_blobber.go b/zboxcore/sdk/chunked_upload_blobber.go index a40fc8118..3a4190c20 100644 --- a/zboxcore/sdk/chunked_upload_blobber.go +++ b/zboxcore/sdk/chunked_upload_blobber.go @@ -181,7 +181,6 @@ func (sb *ChunkedUploadBlobber) processCommit(ctx context.Context, su *ChunkedUp }() rootRef, latestWM, size, fileIDMeta, err := sb.processWriteMarker(ctx, su) - if err != nil { logger.Logger.Error(err) return err @@ -327,7 +326,7 @@ func (sb *ChunkedUploadBlobber) processWriteMarker( } var lR ReferencePathResult - req, err := zboxutil.NewReferencePathRequest(sb.blobber.Baseurl, su.allocationObj.ID, su.allocationObj.Tx, paths) + req, err := zboxutil.NewReferencePathRequest(sb.blobber.Baseurl, su.allocationObj.ID, su.allocationObj.Tx, su.allocationObj.sig, paths) if err != nil || len(paths) == 0 { logger.Logger.Error("Creating ref path req", err) return nil, nil, 0, nil, err diff --git a/zboxcore/sdk/chunked_upload_process_js.go b/zboxcore/sdk/chunked_upload_process_js.go index aed8cca60..ee7998f6b 100644 --- a/zboxcore/sdk/chunked_upload_process_js.go +++ b/zboxcore/sdk/chunked_upload_process_js.go @@ -19,6 +19,7 @@ import ( "github.com/0chain/errors" thrown "github.com/0chain/errors" "github.com/0chain/gosdk/constants" + "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/wasmsdk/jsbridge" "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/zboxutil" @@ -155,6 +156,7 @@ func (su *ChunkedUpload) processUpload(chunkStartIndex, chunkEndIndex int, thumbnailChunkData = thumbnailShards[pos] } obj := js.Global().Get("Object").New() + jsbridge.SetMsgType(&obj, "upload") obj.Set("fileMeta", fileMetaUint8) if formInfo.OnlyHash && su.progress.UploadMask.And(zboxutil.NewUint128(1).Lsh(pos)).Equals64(0) { //check if pos is set in upload mask in progress @@ -227,7 +229,7 @@ type FinalWorkerResult struct { ThumbnailContentHash string } -func (su *ChunkedUpload) listen(allEventChan []chan worker.MessageEvent, respChan chan error) { +func (su *ChunkedUpload) listen(allEventChan []eventChanWorker, respChan chan error) { su.consensus.Reset() var ( @@ -254,109 +256,69 @@ func (su *ChunkedUpload) listen(allEventChan []chan worker.MessageEvent, respCha blobber := su.blobbers[pos] eventChan := allEventChan[pos] - if eventChan == nil { + if eventChan.C == nil { errC := atomic.AddInt32(&errCount, 1) if errC >= int32(su.consensus.consensusThresh) { wgErrors <- thrown.New("upload_failed", "Upload failed. Worker event channel not found") } return } - event, ok := <-eventChan - if !ok { - logger.Logger.Error("chan closed from: ", blobber.blobber.Baseurl) - errC := atomic.AddInt32(&errCount, 1) - if errC >= int32(su.consensus.consensusThresh) { - if su.ctx.Err() != nil { - wgErrors <- context.Cause(su.ctx) - } else { - wgErrors <- thrown.New("upload_failed", "Upload failed. Worker event channel closed") - } - } - return - } - data, err := event.Data() - if err != nil { - errC := atomic.AddInt32(&errCount, 1) - if errC >= int32(su.consensus.consensusThresh) { - wgErrors <- thrown.New("upload_failed", "Upload failed. Error getting worker data") - } - return - } - success, err := data.Get("success") - if err != nil { - errC := atomic.AddInt32(&errCount, 1) - if errC >= int32(su.consensus.consensusThresh) { - wgErrors <- thrown.New("upload_failed", "Upload failed. Error getting worker data") - } - return - } - res, _ := success.Bool() - if !res { - //get error message - errMsg, err := data.Get("error") - if err != nil { + for { + event, ok := <-eventChan.C + if !ok { + logger.Logger.Error("chan closed from: ", blobber.blobber.Baseurl) errC := atomic.AddInt32(&errCount, 1) if errC >= int32(su.consensus.consensusThresh) { - wgErrors <- thrown.New("upload_failed", "Upload failed. Error getting worker data") + if su.ctx.Err() != nil { + wgErrors <- context.Cause(su.ctx) + } else { + wgErrors <- thrown.New("upload_failed", "Upload failed. Worker event channel closed") + } } return } - errMsgStr, _ := errMsg.String() - logger.Logger.Error("error from worker: ", errMsgStr) - errC := atomic.AddInt32(&errCount, 1) - if errC >= int32(su.consensus.consensusThresh) { - wgErrors <- thrown.New("upload_failed", fmt.Sprintf("Upload failed. %s", errMsgStr)) - } - return - } - chunkEndIndexObj, _ := data.Get("chunkEndIndex") - chunkEndIndex, _ := chunkEndIndexObj.Int() - su.updateChunkProgress(chunkEndIndex) - finalRequestObject, _ := data.Get("isFinal") - finalRequest, _ := finalRequestObject.Bool() - if finalRequest { - //get final result - finalResult, err := data.Get("finalResult") + msgType, data, err := jsbridge.GetMsgType(event) if err != nil { - logger.Logger.Error("errorGettingFinalResult") errC := atomic.AddInt32(&errCount, 1) if errC >= int32(su.consensus.consensusThresh) { - wgErrors <- thrown.New("upload_failed", "Upload failed. Error getting worker data") + wgErrors <- errors.Wrap(err, "could not get msgType") } return } - len, err := finalResult.Length() - if err != nil { - logger.Logger.Error("errorGettingFinalResultLength") - errC := atomic.AddInt32(&errCount, 1) - if errC >= int32(su.consensus.consensusThresh) { - wgErrors <- thrown.New("upload_failed", "Upload failed. Error getting worker data") + + switch msgType { + case "auth": + if err := su.processWebWorkerAuthRequest(data, eventChan); err != nil { + errC := atomic.AddInt32(&errCount, 1) + if errC >= int32(su.consensus.consensusThresh) { + wgErrors <- err + } + return } - return - } - resBuf := make([]byte, len) - safejs.CopyBytesToGo(resBuf, finalResult) - var finalResultObj FinalWorkerResult - err = json.Unmarshal(resBuf, &finalResultObj) - if err != nil { - logger.Logger.Error("errorGettingFinalResultUnmarshal") - errC := atomic.AddInt32(&errCount, 1) - if errC >= int32(su.consensus.consensusThresh) { - wgErrors <- thrown.New("upload_failed", "Upload failed. Error getting worker data") + case "upload": + //get error message + //get final result + var err error + isFinal, err = su.processWebWorkerUpload(data, blobber) + if err != nil { + errC := atomic.AddInt32(&errCount, 1) + if errC >= int32(su.consensus.consensusThresh) { + wgErrors <- err + } + } else { + uploadSuccess = true + su.consensus.Done() } return + default: + logger.Logger.Error("unknown msg type: ", msgType) } - blobber.fileRef.FixedMerkleRoot = finalResultObj.FixedMerkleRoot - blobber.fileRef.ValidationRoot = finalResultObj.ValidationRoot - blobber.fileRef.ThumbnailHash = finalResultObj.ThumbnailContentHash - isFinal = true + return } - uploadSuccess = true - su.consensus.Done() }(pos) - } + wg.Wait() close(wgErrors) for err := range wgErrors { @@ -386,6 +348,81 @@ func (su *ChunkedUpload) listen(allEventChan []chan worker.MessageEvent, respCha } } +func (su *ChunkedUpload) processWebWorkerUpload(data *safejs.Value, blobber *ChunkedUploadBlobber) (bool, error) { + var isFinal bool + success, err := data.Get("success") + if err != nil { + return false, errors.Wrap(err, "could not get 'success' field") + } + res, _ := success.Bool() + if !res { + errMsg, err := data.Get("error") + if err != nil { + return false, errors.Wrap(err, "could not get 'error' field") + } + + errMsgStr, _ := errMsg.String() + return false, fmt.Errorf("%s", errMsgStr) + } + + chunkEndIndexObj, _ := data.Get("chunkEndIndex") + chunkEndIndex, _ := chunkEndIndexObj.Int() + su.updateChunkProgress(chunkEndIndex) + finalRequestObject, _ := data.Get("isFinal") + finalRequest, _ := finalRequestObject.Bool() + if finalRequest { + finalResult, err := data.Get("finalResult") + if err != nil { + logger.Logger.Error("errorGettingFinalResult") + return false, errors.Wrap(err, "could not get 'finalResult' field") + } + + len, err := finalResult.Length() + if err != nil { + logger.Logger.Error("errorGettingFinalResultLength") + return false, errors.Wrap(err, "could not get 'finalResult' Length") + } + + resBuf := make([]byte, len) + safejs.CopyBytesToGo(resBuf, finalResult) + var finalResultObj FinalWorkerResult + err = json.Unmarshal(resBuf, &finalResultObj) + if err != nil { + logger.Logger.Error("errorGettingFinalResultUnmarshal") + return false, errors.Wrap(err, "could not unmarshal 'finalResult' obj") + } + + blobber.fileRef.FixedMerkleRoot = finalResultObj.FixedMerkleRoot + blobber.fileRef.ValidationRoot = finalResultObj.ValidationRoot + blobber.fileRef.ThumbnailHash = finalResultObj.ThumbnailContentHash + isFinal = true + } + + su.consensus.Done() + return isFinal, nil +} + +func (su *ChunkedUpload) processWebWorkerAuthRequest(data *safejs.Value, eventChan eventChanWorker) error { + authMsg, err := jsbridge.ParseEventDataField(data, "msg") + if err != nil { + return errors.Wrap(err, "could not parse 'msg' field") + } + + rsp, err := sys.AuthCommon(string(authMsg)) + if err != nil { + return errors.Wrap(err, "chunk upload authCommon failed") + } + + if err := jsbridge.PostMessage(jsbridge.GetWorker(eventChan.workerID), jsbridge.MsgTypeAuthRsp, + map[string]string{ + "data": rsp, + }); err != nil { + return errors.Wrap(err, "chunk upload postMessage failed") + } + + return nil +} + func ProcessEventData(data safejs.Value) { fileMeta, formInfo, fileShards, thumbnailChunkData, err := parseEventData(data) var remotePath string @@ -494,9 +531,12 @@ func selfPostMessage(success, isFinal bool, errMsg, remotePath string, chunkEndI obj.Set("finalResult", finalResultUint8) } } + + // msgType is upload + jsbridge.SetMsgType(&obj, jsbridge.MsgTypeUpload) + self := jsbridge.GetSelfWorker() self.PostMessage(safejs.Safe(obj), nil) //nolint:errcheck - } func parseEventData(data safejs.Value) (*FileMeta, *ChunkedUploadFormInfo, [][]byte, []byte, error) { @@ -641,14 +681,18 @@ func sendUploadRequest(dataBuffers []*bytes.Buffer, contentSlice []string, blobb return eg.Wait() } +type eventChanWorker struct { + C <-chan worker.MessageEvent + workerID string +} + func (su *ChunkedUpload) startProcessor() { su.listenChan = make(chan struct{}, su.uploadWorkers) su.processMap = make(map[int]int) su.uploadWG.Add(1) - go func() { respChan := make(chan error, 1) - allEventChan := make([]chan worker.MessageEvent, len(su.blobbers)) + allEventChan := make([]eventChanWorker, len(su.blobbers)) var pos uint64 for i := su.uploadMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { pos = uint64(i.TrailingZeros()) @@ -663,7 +707,10 @@ func (su *ChunkedUpload) startProcessor() { return } defer webWorker.UnsubscribeToEvents(su.fileMeta.RemotePath) - allEventChan[pos] = eventChan + allEventChan[pos] = eventChanWorker{ + C: eventChan, + workerID: blobber.blobber.ID, + } } } defer su.uploadWG.Done() diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index f6df4068f..5b57a12ba 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -17,9 +17,9 @@ import ( "github.com/0chain/errors" thrown "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" l "github.com/0chain/gosdk/zboxcore/logger" @@ -57,6 +57,7 @@ type CommitRequest struct { allocationID string allocationTx string connectionID string + sig string wg *sync.WaitGroup result *CommitResult timestamp int64 @@ -111,7 +112,7 @@ func (commitreq *CommitRequest) processCommit() { } var req *http.Request var lR ReferencePathResult - req, err := zboxutil.NewReferencePathRequest(commitreq.blobber.Baseurl, commitreq.allocationID, commitreq.allocationTx, paths) + req, err := zboxutil.NewReferencePathRequest(commitreq.blobber.Baseurl, commitreq.allocationID, commitreq.allocationTx, commitreq.sig, paths) if err != nil { l.Logger.Error("Creating ref path req", err) return @@ -126,7 +127,7 @@ func (commitreq *CommitRequest) processCommit() { if resp.StatusCode != http.StatusOK { l.Logger.Error("Ref path response : ", resp.StatusCode) } - resp_body, err := ioutil.ReadAll(resp.Body) + resp_body, err := io.ReadAll(resp.Body) if err != nil { l.Logger.Error("Ref path: Resp", err) return err diff --git a/zboxcore/sdk/common.go b/zboxcore/sdk/common.go index 211effe76..61eb45a00 100644 --- a/zboxcore/sdk/common.go +++ b/zboxcore/sdk/common.go @@ -19,8 +19,8 @@ import ( "github.com/0chain/gosdk/zboxcore/zboxutil" ) -func getObjectTreeFromBlobber(ctx context.Context, allocationID, allocationTx string, remoteFilePath string, blobber *blockchain.StorageNode) (fileref.RefEntity, error) { - httpreq, err := zboxutil.NewObjectTreeRequest(blobber.Baseurl, allocationID, allocationTx, remoteFilePath) +func getObjectTreeFromBlobber(ctx context.Context, allocationID, allocationTx, sig string, remoteFilePath string, blobber *blockchain.StorageNode) (fileref.RefEntity, error) { + httpreq, err := zboxutil.NewObjectTreeRequest(blobber.Baseurl, allocationID, allocationTx, sig, remoteFilePath) if err != nil { l.Logger.Error(blobber.Baseurl, "Error creating object tree request", err) return nil, err diff --git a/zboxcore/sdk/copyworker.go b/zboxcore/sdk/copyworker.go index f41ffc308..0b5e21b0b 100644 --- a/zboxcore/sdk/copyworker.go +++ b/zboxcore/sdk/copyworker.go @@ -30,6 +30,7 @@ type CopyRequest struct { allocationObj *Allocation allocationID string allocationTx string + sig string blobbers []*blockchain.StorageNode remotefilepath string destPath string @@ -43,7 +44,7 @@ type CopyRequest struct { } func (req *CopyRequest) getObjectTreeFromBlobber(blobber *blockchain.StorageNode) (fileref.RefEntity, error) { - return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.remotefilepath, blobber) + return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.sig, req.remotefilepath, blobber) } func (req *CopyRequest) copyBlobberObject( @@ -98,7 +99,7 @@ func (req *CopyRequest) copyBlobberObject( cncl context.CancelFunc ) - httpreq, err = zboxutil.NewCopyRequest(blobber.Baseurl, req.allocationID, req.allocationTx, body) + httpreq, err = zboxutil.NewCopyRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body) if err != nil { l.Logger.Error(blobber.Baseurl, "Error creating rename request", err) return @@ -257,11 +258,13 @@ func (req *CopyRequest) ProcessCopy() error { commitReq := &CommitRequest{ allocationID: req.allocationID, allocationTx: req.allocationTx, + sig: req.sig, blobber: req.blobbers[pos], connectionID: req.connectionID, wg: wg, timestamp: req.timestamp, } + commitReq.changes = append(commitReq.changes, newChange) commitReqs[c] = commitReq go AddCommitRequest(commitReq) @@ -307,6 +310,7 @@ func (co *CopyOperation) Process(allocObj *Allocation, connectionID string) ([]f allocationObj: allocObj, allocationID: allocObj.ID, allocationTx: allocObj.Tx, + sig: allocObj.sig, connectionID: connectionID, blobbers: allocObj.Blobbers, remotefilepath: co.remotefilepath, @@ -317,6 +321,7 @@ func (co *CopyOperation) Process(allocObj *Allocation, connectionID string) ([]f maskMU: co.maskMU, Consensus: Consensus{RWMutex: &sync.RWMutex{}}, } + cR.consensusThresh = co.consensusThresh cR.fullconsensus = co.fullconsensus diff --git a/zboxcore/sdk/copyworker_test.go b/zboxcore/sdk/copyworker_test.go index dc4aad0bd..be122920c 100644 --- a/zboxcore/sdk/copyworker_test.go +++ b/zboxcore/sdk/copyworker_test.go @@ -44,7 +44,7 @@ func TestCopyRequest_copyBlobberObject(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -249,7 +249,7 @@ func TestCopyRequest_ProcessCopy(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -488,10 +488,13 @@ func TestCopyRequest_ProcessCopy(t *testing.T) { maskMU: &sync.Mutex{}, connectionID: mockConnectionId, } + sig, err := client.Sign(mockAllocationTxId) + require.NoError(err) + req.sig = sig req.ctx, req.ctxCncl = context.WithCancel(context.TODO()) tt.setup(t, tt.name, tt.numBlobbers, tt.numCorrect, req) - err := req.ProcessCopy() + err = req.ProcessCopy() if tt.wantErr { require.Contains(errors.Top(err), tt.errMsg) } else { diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index fc739a07f..601ec2137 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -29,6 +29,7 @@ type DeleteRequest struct { allocationObj *Allocation allocationID string allocationTx string + sig string blobbers []*blockchain.StorageNode remotefilepath string ctx context.Context @@ -60,7 +61,7 @@ func (req *DeleteRequest) deleteBlobberFile( query.Add("connection_id", req.connectionID) query.Add("path", req.remotefilepath) - httpreq, err := zboxutil.NewDeleteRequest(blobber.Baseurl, req.allocationID, req.allocationTx, query) + httpreq, err := zboxutil.NewDeleteRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, query) if err != nil { l.Logger.Error(blobber.Baseurl, "Error creating delete request", err) return err @@ -152,7 +153,7 @@ func (req *DeleteRequest) getObjectTreeFromBlobber(pos uint64) ( }() fRefEntity, err = getObjectTreeFromBlobber( - req.ctx, req.allocationID, req.allocationTx, + req.ctx, req.allocationID, req.allocationTx, req.sig, req.remotefilepath, req.blobbers[pos]) return } @@ -287,11 +288,13 @@ func (req *DeleteRequest) ProcessDelete() (err error) { commitReq := &CommitRequest{ allocationID: req.allocationID, allocationTx: req.allocationTx, + sig: req.sig, blobber: req.blobbers[pos], connectionID: req.connectionID, wg: wg, timestamp: req.timestamp, } + commitReq.changes = append(commitReq.changes, newChange) commitReqs[c] = commitReq go AddCommitRequest(commitReq) @@ -335,6 +338,7 @@ func (dop *DeleteOperation) Process(allocObj *Allocation, connectionID string) ( allocationObj: allocObj, allocationID: allocObj.ID, allocationTx: allocObj.Tx, + sig: allocObj.sig, connectionID: connectionID, blobbers: allocObj.Blobbers, remotefilepath: dop.remotefilepath, diff --git a/zboxcore/sdk/deleteworker_test.go b/zboxcore/sdk/deleteworker_test.go index 7b0de8e5a..dbabb4062 100644 --- a/zboxcore/sdk/deleteworker_test.go +++ b/zboxcore/sdk/deleteworker_test.go @@ -41,7 +41,7 @@ func TestDeleteRequest_deleteBlobberFile(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -214,7 +214,7 @@ func TestDeleteRequest_ProcessDelete(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) diff --git a/zboxcore/sdk/dirworker.go b/zboxcore/sdk/dirworker.go index ffc1f2840..80aea731a 100644 --- a/zboxcore/sdk/dirworker.go +++ b/zboxcore/sdk/dirworker.go @@ -32,6 +32,7 @@ type DirRequest struct { allocationObj *Allocation allocationID string allocationTx string + sig string remotePath string blobbers []*blockchain.StorageNode ctx context.Context @@ -120,7 +121,7 @@ func (req *DirRequest) commitRequest(existingDirCount int) error { commitReq.allocationID = req.allocationID commitReq.allocationTx = req.allocationTx commitReq.blobber = req.blobbers[pos] - + commitReq.sig = req.sig newChange := &allocationchange.DirCreateChange{ RemotePath: req.remotePath, Uuid: uid, @@ -185,7 +186,7 @@ func (req *DirRequest) createDirInBlobber(blobber *blockchain.StorageNode, pos u } formWriter.Close() - httpreq, err := zboxutil.NewCreateDirRequest(blobber.Baseurl, req.allocationID, req.allocationTx, body) + httpreq, err := zboxutil.NewCreateDirRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body) if err != nil { l.Logger.Error(blobber.Baseurl, "Error creating dir request", err) return err, false @@ -290,6 +291,7 @@ func (dirOp *DirOperation) Process(allocObj *Allocation, connectionID string) ([ allocationID: allocObj.ID, allocationTx: allocObj.Tx, connectionID: connectionID, + sig: allocObj.sig, blobbers: allocObj.Blobbers, remotePath: dirOp.remotePath, ctx: dirOp.ctx, diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index 4f969ea12..b5f97e181 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -69,6 +69,7 @@ func WithFileCallback(cb func()) DownloadRequestOption { type DownloadRequest struct { allocationID string allocationTx string + sig string allocOwnerID string allocOwnerPubKey string blobbers []*blockchain.StorageNode @@ -281,7 +282,6 @@ func (req *DownloadRequest) downloadBlock( } c++ - } var failed int32 @@ -1100,6 +1100,7 @@ func GetFileRefFromBlobber(allocationID, blobberId, remotePath string) (fRef *fi listReq.allocationID = a.ID listReq.allocationTx = a.Tx + listReq.sig = a.sig listReq.blobbers = []*blockchain.StorageNode{ {ID: string(blobber.ID), Baseurl: blobber.BaseURL}, } @@ -1120,6 +1121,7 @@ func (req *DownloadRequest) getFileRef() (fRef *fileref.FileRef, err error) { remotefilepathhash: req.remotefilepathhash, allocationID: req.allocationID, allocationTx: req.allocationTx, + sig: req.sig, blobbers: req.blobbers, authToken: req.authTicket, Consensus: Consensus{ diff --git a/zboxcore/sdk/filemetaworker.go b/zboxcore/sdk/filemetaworker.go index 068447800..2a8595e2b 100644 --- a/zboxcore/sdk/filemetaworker.go +++ b/zboxcore/sdk/filemetaworker.go @@ -76,7 +76,7 @@ func (req *ListRequest) getFileMetaInfoFromBlobber(blobber *blockchain.StorageNo } formWriter.Close() - httpreq, err := zboxutil.NewFileMetaRequest(blobber.Baseurl, req.allocationID, req.allocationTx, body) + httpreq, err := zboxutil.NewFileMetaRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body) if err != nil { l.Logger.Error("File meta info request error: ", err.Error()) return @@ -141,7 +141,7 @@ func (req *ListRequest) getFileMetaByNameInfoFromBlobber(blobber *blockchain.Sto } } formWriter.Close() - httpreq, err := zboxutil.NewFileMetaRequest(blobber.Baseurl, req.allocationID, req.allocationTx, body) + httpreq, err := zboxutil.NewFileMetaRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body) if err != nil { l.Logger.Error("File meta info request error: ", err.Error()) return diff --git a/zboxcore/sdk/filemetaworker_test.go b/zboxcore/sdk/filemetaworker_test.go index 395abb6c4..ad0bcd005 100644 --- a/zboxcore/sdk/filemetaworker_test.go +++ b/zboxcore/sdk/filemetaworker_test.go @@ -42,7 +42,7 @@ func TestListRequest_getFileMetaInfoFromBlobber(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -203,7 +203,7 @@ func TestListRequest_getFileConsensusFromBlobbers(t *testing.T) { const mockClientId = "mock client id" const mockClientKey = "mock client key" - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) diff --git a/zboxcore/sdk/filerefsworker.go b/zboxcore/sdk/filerefsworker.go index 7a498aa61..09d8a1b98 100644 --- a/zboxcore/sdk/filerefsworker.go +++ b/zboxcore/sdk/filerefsworker.go @@ -32,6 +32,7 @@ const INVALID_PATH = "invalid_path" type ObjectTreeRequest struct { allocationID string allocationTx string + sig string blobbers []*blockchain.StorageNode authToken string pathHash string @@ -147,6 +148,7 @@ func (o *ObjectTreeRequest) getFileRefs(bUrl string, respChan chan *oTreeRespons oReq, err := zboxutil.NewRefsRequest( bUrl, o.allocationID, + o.sig, o.allocationTx, o.remotefilepath, o.pathHash, @@ -235,6 +237,7 @@ type RecentlyAddedRefRequest struct { ctx context.Context allocationID string allocationTx string + sig string blobbers []*blockchain.StorageNode fromDate int64 offset int64 @@ -311,7 +314,7 @@ func (r *RecentlyAddedRefRequest) GetRecentlyAddedRefs() (*RecentlyAddedRefResul func (r *RecentlyAddedRefRequest) getRecentlyAddedRefs(resp *RecentlyAddedRefResponse, bUrl string) { defer r.wg.Done() - req, err := zboxutil.NewRecentlyAddedRefsRequest(bUrl, r.allocationID, r.allocationTx, r.fromDate, r.offset, r.pageLimit) + req, err := zboxutil.NewRecentlyAddedRefsRequest(bUrl, r.allocationID, r.allocationTx, r.sig, r.fromDate, r.offset, r.pageLimit) if err != nil { resp.err = err return diff --git a/zboxcore/sdk/filestatsworker.go b/zboxcore/sdk/filestatsworker.go index 54c75b298..568d1cf49 100644 --- a/zboxcore/sdk/filestatsworker.go +++ b/zboxcore/sdk/filestatsworker.go @@ -70,7 +70,7 @@ func (req *ListRequest) getFileStatsInfoFromBlobber(blobber *blockchain.StorageN } formWriter.Close() - httpreq, err := zboxutil.NewFileStatsRequest(blobber.Baseurl, req.allocationID, req.allocationTx, body) + httpreq, err := zboxutil.NewFileStatsRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body) if err != nil { l.Logger.Error("File meta info request error: ", err.Error()) return diff --git a/zboxcore/sdk/filestatsworker_test.go b/zboxcore/sdk/filestatsworker_test.go index a5981f389..da621eed4 100644 --- a/zboxcore/sdk/filestatsworker_test.go +++ b/zboxcore/sdk/filestatsworker_test.go @@ -42,7 +42,7 @@ func TestListRequest_getFileStatsInfoFromBlobber(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -184,7 +184,7 @@ func TestListRequest_getFileStatsFromBlobbers(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) diff --git a/zboxcore/sdk/listworker.go b/zboxcore/sdk/listworker.go index 19a8d5fb9..220974a7e 100644 --- a/zboxcore/sdk/listworker.go +++ b/zboxcore/sdk/listworker.go @@ -25,6 +25,7 @@ const CHUNK_SIZE = 64 * 1024 type ListRequest struct { allocationID string allocationTx string + sig string blobbers []*blockchain.StorageNode remotefilepathhash string remotefilepath string diff --git a/zboxcore/sdk/listworker_test.go b/zboxcore/sdk/listworker_test.go index 0145304f1..8b98488d0 100644 --- a/zboxcore/sdk/listworker_test.go +++ b/zboxcore/sdk/listworker_test.go @@ -41,7 +41,7 @@ func TestListRequest_getListInfoFromBlobber(t *testing.T) { ) var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -214,7 +214,7 @@ func TestListRequest_GetListFromBlobbers(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) diff --git a/zboxcore/sdk/moveworker.go b/zboxcore/sdk/moveworker.go index 4cc0f3f60..a70c814ce 100644 --- a/zboxcore/sdk/moveworker.go +++ b/zboxcore/sdk/moveworker.go @@ -30,6 +30,7 @@ type MoveRequest struct { allocationObj *Allocation allocationID string allocationTx string + sig string blobbers []*blockchain.StorageNode remotefilepath string destPath string @@ -43,7 +44,7 @@ type MoveRequest struct { } func (req *MoveRequest) getObjectTreeFromBlobber(blobber *blockchain.StorageNode) (fileref.RefEntity, error) { - return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.remotefilepath, blobber) + return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.sig, req.remotefilepath, blobber) } func (req *MoveRequest) moveBlobberObject( @@ -95,7 +96,7 @@ func (req *MoveRequest) moveBlobberObject( cncl context.CancelFunc ) - httpreq, err = zboxutil.NewMoveRequest(blobber.Baseurl, req.allocationID, req.allocationTx, body) + httpreq, err = zboxutil.NewMoveRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body) if err != nil { l.Logger.Error(blobber.Baseurl, "Error creating rename request", err) return @@ -251,6 +252,7 @@ func (req *MoveRequest) ProcessMove() error { commitReq := &CommitRequest{ allocationID: req.allocationID, allocationTx: req.allocationTx, + sig: req.sig, blobber: req.blobbers[pos], connectionID: req.connectionID, wg: wg, @@ -300,6 +302,7 @@ func (mo *MoveOperation) Process(allocObj *Allocation, connectionID string) ([]f allocationObj: allocObj, allocationID: allocObj.ID, allocationTx: allocObj.Tx, + sig: allocObj.sig, connectionID: connectionID, blobbers: allocObj.Blobbers, remotefilepath: mo.remotefilepath, diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 4a0632ab2..115cb8482 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -91,7 +91,7 @@ func (mo *MultiOperation) createConnectionObj(blobberIdx int) (err error) { formWriter.Close() var httpreq *http.Request - httpreq, err = zboxutil.NewConnectionRequest(blobber.Baseurl, mo.allocationObj.ID, mo.allocationObj.Tx, body) + httpreq, err = zboxutil.NewConnectionRequest(blobber.Baseurl, mo.allocationObj.ID, mo.allocationObj.Tx, mo.allocationObj.sig, body) if err != nil { l.Logger.Error(blobber.Baseurl, "Error creating new connection request", err) return @@ -286,6 +286,7 @@ func (mo *MultiOperation) Process() error { commitReq := &CommitRequest{ allocationID: mo.allocationObj.ID, allocationTx: mo.allocationObj.Tx, + sig: mo.allocationObj.sig, blobber: mo.allocationObj.Blobbers[pos], connectionID: mo.connectionID, wg: wg, diff --git a/zboxcore/sdk/renameworker.go b/zboxcore/sdk/renameworker.go index 3d4b4889c..3260104fa 100644 --- a/zboxcore/sdk/renameworker.go +++ b/zboxcore/sdk/renameworker.go @@ -29,6 +29,7 @@ type RenameRequest struct { allocationObj *Allocation allocationID string allocationTx string + sig string blobbers []*blockchain.StorageNode remotefilepath string newName string @@ -43,7 +44,7 @@ type RenameRequest struct { } func (req *RenameRequest) getObjectTreeFromBlobber(blobber *blockchain.StorageNode) (fileref.RefEntity, error) { - return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.remotefilepath, blobber) + return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.sig, req.remotefilepath, blobber) } func (req *RenameRequest) renameBlobberObject( @@ -92,7 +93,7 @@ func (req *RenameRequest) renameBlobberObject( formWriter.Close() var httpreq *http.Request - httpreq, err = zboxutil.NewRenameRequest(blobber.Baseurl, req.allocationID, req.allocationTx, body) + httpreq, err = zboxutil.NewRenameRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body) if err != nil { l.Logger.Error(blobber.Baseurl, "Error creating rename request", err) return @@ -251,6 +252,7 @@ func (req *RenameRequest) ProcessRename() error { commitReq := &CommitRequest{ allocationID: req.allocationID, allocationTx: req.allocationTx, + sig: req.sig, blobber: req.blobbers[pos], connectionID: req.connectionID, wg: wg, @@ -306,6 +308,7 @@ func (ro *RenameOperation) Process(allocObj *Allocation, connectionID string) ([ allocationObj: allocObj, allocationID: allocObj.ID, allocationTx: allocObj.Tx, + sig: allocObj.sig, connectionID: connectionID, blobbers: allocObj.Blobbers, remotefilepath: ro.remotefilepath, diff --git a/zboxcore/sdk/renameworker_test.go b/zboxcore/sdk/renameworker_test.go index b14eaab10..986edc1fa 100644 --- a/zboxcore/sdk/renameworker_test.go +++ b/zboxcore/sdk/renameworker_test.go @@ -49,7 +49,7 @@ func TestRenameRequest_renameBlobberObject(t *testing.T) { zboxutil.Client = rawClient }() - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -279,7 +279,7 @@ func TestRenameRequest_ProcessRename(t *testing.T) { zboxutil.Client = rawClient }() - client.SetWallet(zcncrypto.Wallet{ + client.SetWallet(false, zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) diff --git a/zboxcore/sdk/rollback.go b/zboxcore/sdk/rollback.go index 39866bd3e..b5812a7ea 100644 --- a/zboxcore/sdk/rollback.go +++ b/zboxcore/sdk/rollback.go @@ -60,11 +60,11 @@ type BlobberStatus struct { Status string } -func GetWritemarker(allocID, allocTx, id, baseUrl string) (*LatestPrevWriteMarker, error) { +func GetWritemarker(allocID, allocTx, sig, id, baseUrl string) (*LatestPrevWriteMarker, error) { var lpm LatestPrevWriteMarker - req, err := zboxutil.NewWritemarkerRequest(baseUrl, allocID, allocTx) + req, err := zboxutil.NewWritemarkerRequest(baseUrl, allocID, allocTx, sig) if err != nil { return nil, err } @@ -270,7 +270,7 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { ID: blobber.ID, Status: "available", } - wr, err := GetWritemarker(a.ID, a.Tx, blobber.ID, blobber.Baseurl) + wr, err := GetWritemarker(a.ID, a.Tx, a.sig, blobber.ID, blobber.Baseurl) if err != nil { atomic.AddInt32(&errCnt, 1) markerError = err @@ -397,7 +397,7 @@ func (a *Allocation) RollbackWithMask(mask zboxutil.Uint128) { go func(blobber *blockchain.StorageNode) { defer wg.Done() - wr, err := GetWritemarker(a.ID, a.Tx, blobber.ID, blobber.Baseurl) + wr, err := GetWritemarker(a.ID, a.Tx, a.sig, blobber.ID, blobber.Baseurl) if err != nil { l.Logger.Error("error during getWritemarker", zap.Error(err)) } diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index 6a0a60f76..e5fbabbf2 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -125,7 +125,7 @@ func InitStorageSDK(walletJSON string, return err } - client.SetWallet(wallet) + client.SetWallet(false, wallet) client.SetSignatureScheme(signatureScheme) client.SetNonce(nonce) if len(fee) > 0 { @@ -994,6 +994,7 @@ func GetAllocationUpdates(allocation *Allocation) error { allocation.MovedBack = updatedAllocationObj.MovedBack allocation.MovedToValidators = updatedAllocationObj.MovedToValidators allocation.FileOptions = updatedAllocationObj.FileOptions + allocation.IsEnterprise = updatedAllocationObj.IsEnterprise return nil } diff --git a/zboxcore/sdk/sharerequest.go b/zboxcore/sdk/sharerequest.go index ad85239ac..227870446 100644 --- a/zboxcore/sdk/sharerequest.go +++ b/zboxcore/sdk/sharerequest.go @@ -17,6 +17,7 @@ import ( type ShareRequest struct { allocationID string allocationTx string + sig string remotefilepath string remotefilename string refType string @@ -33,6 +34,7 @@ func (req *ShareRequest) GetFileRef() (*fileref.FileRef, error) { remotefilepathhash: filePathHash, allocationID: req.allocationID, allocationTx: req.allocationTx, + sig: req.sig, blobbers: req.blobbers, ctx: req.ctx, Consensus: Consensus{RWMutex: &sync.RWMutex{}}, diff --git a/zboxcore/sdk/writemarker_mutex.go b/zboxcore/sdk/writemarker_mutex.go index 61e89a412..501f1f1a6 100644 --- a/zboxcore/sdk/writemarker_mutex.go +++ b/zboxcore/sdk/writemarker_mutex.go @@ -100,7 +100,7 @@ func (wmMu *WriteMarkerMutex) UnlockBlobber( var req *http.Request req, err = zboxutil.NewWriteMarkerUnLockRequest( - b.Baseurl, wmMu.allocationObj.ID, wmMu.allocationObj.Tx, connID, "") + b.Baseurl, wmMu.allocationObj.ID, wmMu.allocationObj.Tx, wmMu.allocationObj.sig, connID, "") if err != nil { return } @@ -281,7 +281,7 @@ func (wmMu *WriteMarkerMutex) lockBlobber( var req *http.Request req, err = zboxutil.NewWriteMarkerLockRequest( - b.Baseurl, wmMu.allocationObj.ID, wmMu.allocationObj.Tx, connID) + b.Baseurl, wmMu.allocationObj.ID, wmMu.allocationObj.Tx, wmMu.allocationObj.sig, connID) if err != nil { return } diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index 6f7d5cf41..1a4cebc99 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -5,8 +5,10 @@ import ( "context" "encoding/json" "io" + "net" "net/http" "net/url" + "os" "path" "strconv" "time" @@ -76,6 +78,48 @@ const ( ALLOCATION_ID_HEADER = "ALLOCATION-ID" ) +func getEnvAny(names ...string) string { + for _, n := range names { + if val := os.Getenv(n); val != "" { + return val + } + } + return "" +} + +type proxyFromEnv struct { + HTTPProxy string + HTTPSProxy string + NoProxy string + + http, https *url.URL +} + +func (pfe *proxyFromEnv) initialize() { + pfe.HTTPProxy = getEnvAny("HTTP_PROXY", "http_proxy") + pfe.HTTPSProxy = getEnvAny("HTTPS_PROXY", "https_proxy") + pfe.NoProxy = getEnvAny("NO_PROXY", "no_proxy") + + if pfe.NoProxy != "" { + return + } + + if pfe.HTTPProxy != "" { + pfe.http, _ = url.Parse(pfe.HTTPProxy) + } + if pfe.HTTPSProxy != "" { + pfe.https, _ = url.Parse(pfe.HTTPSProxy) + } +} + +func (pfe *proxyFromEnv) isLoopback(host string) (ok bool) { + host, _, _ = net.SplitHostPort(host) + if host == "localhost" { + return true + } + return net.ParseIP(host).IsLoopback() +} + func GetFastHTTPClient() *fasthttp.Client { fc, ok := FastHttpClient.(*fasthttp.Client) if ok { @@ -84,8 +128,25 @@ func GetFastHTTPClient() *fasthttp.Client { return nil } +func (pfe *proxyFromEnv) Proxy(req *http.Request) (proxy *url.URL, err error) { + if pfe.isLoopback(req.URL.Host) { + switch req.URL.Scheme { + case "http": + return pfe.http, nil + case "https": + return pfe.https, nil + default: + } + } + return http.ProxyFromEnvironment(req) +} + +var envProxy proxyFromEnv + func init() { - Client = &http.Client{Transport: coreHttp.DefaultTransport} + Client = &http.Client{ + Transport: coreHttp.DefaultTransport, + } FastHttpClient = &fasthttp.Client{ MaxIdleConnDuration: 45 * time.Second, @@ -104,7 +165,7 @@ func init() { MaxConnsPerHost: 1024, } fasthttp.SetBodySizePoolLimit(respBodyPoolLimit, respBodyPoolLimit) - coreHttp.EnvProxy.Initialize() + envProxy.initialize() log.Init(logger.DEBUG, "0box-sdk") } @@ -130,35 +191,16 @@ func setClientInfo(req *http.Request) { req.Header.Set("X-App-Client-Key", client.PublicKey()) } -func setClientInfoWithSign(req *http.Request, allocation, baseURL string) error { +func setClientInfoWithSign(req *http.Request, sig, allocation, baseURL string) error { setClientInfo(req) + req.Header.Set(CLIENT_SIGNATURE_HEADER, sig) - hashData := allocation - sign, err := client.Sign(encryption.Hash(hashData)) - if err != nil { - return err - } - req.Header.Set(CLIENT_SIGNATURE_HEADER, sign) - - hashData = allocation + baseURL - sign, err = client.Sign(encryption.Hash(hashData)) - if err != nil { - return err - } - req.Header.Set(CLIENT_SIGNATURE_HEADER_V2, sign) - return nil -} - -func setFastClientInfoWithSign(req *fasthttp.Request, allocation string) error { - req.Header.Set("X-App-Client-ID", client.ClientID()) - req.Header.Set("X-App-Client-Key", client.PublicKey()) - - sign, err := client.Sign(encryption.Hash(allocation)) + hashData := allocation + baseURL + sig2, err := client.Sign(encryption.Hash(hashData)) if err != nil { return err } - req.Header.Set(CLIENT_SIGNATURE_HEADER, sign) - + req.Header.Set(CLIENT_SIGNATURE_HEADER_V2, sig2) return nil } @@ -179,7 +221,7 @@ func NewCommitRequest(baseUrl, allocationID string, allocationTx string, body io return req, nil } -func NewReferencePathRequest(baseUrl, allocationID string, allocationTx string, paths []string) (*http.Request, error) { +func NewReferencePathRequest(baseUrl, allocationID string, allocationTx string, sig string, paths []string) (*http.Request, error) { nurl, err := joinUrl(baseUrl, REFERENCE_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -199,7 +241,7 @@ func NewReferencePathRequest(baseUrl, allocationID string, allocationTx string, return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -231,7 +273,7 @@ func NewCalculateHashRequest(baseUrl, allocationID string, allocationTx string, return req, nil } -func NewObjectTreeRequest(baseUrl, allocationID string, allocationTx string, path string) (*http.Request, error) { +func NewObjectTreeRequest(baseUrl, allocationID string, allocationTx string, sig string, path string) (*http.Request, error) { nurl, err := joinUrl(baseUrl, OBJECT_TREE_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -245,7 +287,7 @@ func NewObjectTreeRequest(baseUrl, allocationID string, allocationTx string, pat return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -254,7 +296,7 @@ func NewObjectTreeRequest(baseUrl, allocationID string, allocationTx string, pat return req, nil } -func NewRefsRequest(baseUrl, allocationID, allocationTx, path, pathHash, authToken, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int) (*http.Request, error) { +func NewRefsRequest(baseUrl, allocationID, sig, allocationTx, path, pathHash, authToken, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int) (*http.Request, error) { nUrl, err := joinUrl(baseUrl, REFS_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -278,14 +320,14 @@ func NewRefsRequest(baseUrl, allocationID, allocationTx, path, pathHash, authTok req.Header.Set(ALLOCATION_ID_HEADER, allocationID) - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationID, baseUrl); err != nil { return nil, err } return req, nil } -func NewRecentlyAddedRefsRequest(bUrl, allocID, allocTx string, fromDate, offset int64, pageLimit int) (*http.Request, error) { +func NewRecentlyAddedRefsRequest(bUrl, allocID, allocTx, sig string, fromDate, offset int64, pageLimit int) (*http.Request, error) { nUrl, err := joinUrl(bUrl, RECENT_REFS_ENDPOINT, allocID) if err != nil { return nil, err @@ -304,7 +346,7 @@ func NewRecentlyAddedRefsRequest(bUrl, allocID, allocTx string, fromDate, offset req.Header.Set(ALLOCATION_ID_HEADER, allocID) - if err := setClientInfoWithSign(req, allocTx, bUrl); err != nil { + if err = setClientInfoWithSign(req, sig, allocTx, bUrl); err != nil { return nil, err } @@ -329,7 +371,7 @@ func NewAllocationRequest(baseUrl, allocationID, allocationTx string) (*http.Req return req, nil } -func NewCollaboratorRequest(baseUrl string, allocationID string, allocationTx string, body io.Reader) (*http.Request, error) { +func NewCollaboratorRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { u, err := joinUrl(baseUrl, COLLABORATOR_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -339,7 +381,7 @@ func NewCollaboratorRequest(baseUrl string, allocationID string, allocationTx st return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -348,7 +390,7 @@ func NewCollaboratorRequest(baseUrl string, allocationID string, allocationTx st return req, nil } -func GetCollaboratorsRequest(baseUrl string, allocationID string, allocationTx string, query *url.Values) (*http.Request, error) { +func GetCollaboratorsRequest(baseUrl, allocationID, allocationTx, sig string, query *url.Values) (*http.Request, error) { u, err := joinUrl(baseUrl, COLLABORATOR_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -359,7 +401,7 @@ func GetCollaboratorsRequest(baseUrl string, allocationID string, allocationTx s return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -368,7 +410,7 @@ func GetCollaboratorsRequest(baseUrl string, allocationID string, allocationTx s return req, nil } -func DeleteCollaboratorRequest(baseUrl string, allocationID string, allocationTx string, query *url.Values) (*http.Request, error) { +func DeleteCollaboratorRequest(baseUrl, allocationID, allocationTx, sig string, query *url.Values) (*http.Request, error) { u, err := joinUrl(baseUrl, COLLABORATOR_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -380,7 +422,7 @@ func DeleteCollaboratorRequest(baseUrl string, allocationID string, allocationTx return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -389,7 +431,7 @@ func DeleteCollaboratorRequest(baseUrl string, allocationID string, allocationTx return req, nil } -func NewFileMetaRequest(baseUrl string, allocationID string, allocationTx string, body io.Reader) (*http.Request, error) { +func NewFileMetaRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { u, err := joinUrl(baseUrl, FILE_META_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -399,7 +441,7 @@ func NewFileMetaRequest(baseUrl string, allocationID string, allocationTx string return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -408,7 +450,7 @@ func NewFileMetaRequest(baseUrl string, allocationID string, allocationTx string return req, nil } -func NewFileStatsRequest(baseUrl string, allocationID string, allocationTx string, body io.Reader) (*http.Request, error) { +func NewFileStatsRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { u, err := joinUrl(baseUrl, FILE_STATS_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -418,7 +460,7 @@ func NewFileStatsRequest(baseUrl string, allocationID string, allocationTx strin return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -454,7 +496,7 @@ func NewListRequest(baseUrl, allocationID, allocationTx, path, pathHash, auth_to } // NewUploadRequestWithMethod create a http request of upload -func NewUploadRequestWithMethod(baseURL, allocationID string, allocationTx string, body io.Reader, method string) (*http.Request, error) { +func NewUploadRequestWithMethod(baseURL, allocationID, allocationTx, sig string, body io.Reader, method string) (*http.Request, error) { u, err := joinUrl(baseURL, UPLOAD_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -469,7 +511,7 @@ func NewUploadRequestWithMethod(baseURL, allocationID string, allocationTx strin } // set header: X-App-Client-Signature - if err := setClientInfoWithSign(req, allocationTx, baseURL); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseURL); err != nil { return nil, err } @@ -479,7 +521,7 @@ func NewUploadRequestWithMethod(baseURL, allocationID string, allocationTx strin } func NewWriteMarkerLockRequest( - baseURL, allocationID, allocationTx, connID string) (*http.Request, error) { + baseURL, allocationID, allocationTx, sig, connID string) (*http.Request, error) { u, err := joinUrl(baseURL, WM_LOCK_ENDPOINT, allocationTx) if err != nil { @@ -495,7 +537,7 @@ func NewWriteMarkerLockRequest( return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseURL); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseURL); err != nil { return nil, err } @@ -505,7 +547,7 @@ func NewWriteMarkerLockRequest( } func NewWriteMarkerUnLockRequest( - baseURL, allocationID, allocationTx, connID, requestTime string) (*http.Request, error) { + baseURL, allocationID, allocationTx, sig, connID, requestTime string) (*http.Request, error) { u, err := joinUrl(baseURL, WM_LOCK_ENDPOINT, allocationTx, connID) if err != nil { @@ -517,7 +559,7 @@ func NewWriteMarkerUnLockRequest( return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseURL); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseURL); err != nil { return nil, err } @@ -547,7 +589,20 @@ func NewFastUploadRequest(baseURL, allocationID string, allocationTx string, bod return req, nil } -func NewUploadRequest(baseUrl, allocationID string, allocationTx string, body io.Reader, update bool) (*http.Request, error) { +func setFastClientInfoWithSign(req *fasthttp.Request, allocation string) error { + req.Header.Set("X-App-Client-ID", client.ClientID()) + req.Header.Set("X-App-Client-Key", client.PublicKey()) + + sign, err := client.Sign(encryption.Hash(allocation)) + if err != nil { + return err + } + req.Header.Set(CLIENT_SIGNATURE_HEADER, sign) + + return nil +} + +func NewUploadRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader, update bool) (*http.Request, error) { u, err := joinUrl(baseUrl, UPLOAD_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -563,7 +618,7 @@ func NewUploadRequest(baseUrl, allocationID string, allocationTx string, body io return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -572,7 +627,7 @@ func NewUploadRequest(baseUrl, allocationID string, allocationTx string, body io return req, nil } -func NewConnectionRequest(baseUrl, allocationID string, allocationTx string, body io.Reader) (*http.Request, error) { +func NewConnectionRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { u, err := joinUrl(baseUrl, CREATE_CONNECTION_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -582,7 +637,7 @@ func NewConnectionRequest(baseUrl, allocationID string, allocationTx string, bod return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -591,7 +646,7 @@ func NewConnectionRequest(baseUrl, allocationID string, allocationTx string, bod return req, nil } -func NewRenameRequest(baseUrl, allocationID string, allocationTx string, body io.Reader) (*http.Request, error) { +func NewRenameRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { u, err := joinUrl(baseUrl, RENAME_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -603,7 +658,7 @@ func NewRenameRequest(baseUrl, allocationID string, allocationTx string, body io return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -612,7 +667,7 @@ func NewRenameRequest(baseUrl, allocationID string, allocationTx string, body io return req, nil } -func NewCopyRequest(baseUrl, allocationID string, allocationTx string, body io.Reader) (*http.Request, error) { +func NewCopyRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { u, err := joinUrl(baseUrl, COPY_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -623,7 +678,7 @@ func NewCopyRequest(baseUrl, allocationID string, allocationTx string, body io.R return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -632,7 +687,7 @@ func NewCopyRequest(baseUrl, allocationID string, allocationTx string, body io.R return req, nil } -func NewMoveRequest(baseUrl, allocationID string, allocationTx string, body io.Reader) (*http.Request, error) { +func NewMoveRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { u, err := joinUrl(baseUrl, MOVE_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -643,7 +698,7 @@ func NewMoveRequest(baseUrl, allocationID string, allocationTx string, body io.R return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -663,7 +718,13 @@ func NewDownloadRequest(baseUrl, allocationID, allocationTx string) (*http.Reque if err != nil { return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + + sig, err := client.Sign(encryption.Hash(allocationTx)) + if err != nil { + return nil, err + } + + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -708,7 +769,7 @@ func NewRedeemRequest(baseUrl, allocationID, allocationTx string) (*http.Request return req, nil } -func NewDeleteRequest(baseUrl, allocationID string, allocationTx string, query *url.Values) (*http.Request, error) { +func NewDeleteRequest(baseUrl, allocationID, allocationTx, sig string, query *url.Values) (*http.Request, error) { u, err := joinUrl(baseUrl, UPLOAD_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -720,7 +781,7 @@ func NewDeleteRequest(baseUrl, allocationID string, allocationTx string, query * return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -729,7 +790,7 @@ func NewDeleteRequest(baseUrl, allocationID string, allocationTx string, query * return req, nil } -func NewCreateDirRequest(baseUrl, allocationID string, allocationTx string, body io.Reader) (*http.Request, error) { +func NewCreateDirRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { u, err := joinUrl(baseUrl, DIR_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -740,7 +801,7 @@ func NewCreateDirRequest(baseUrl, allocationID string, allocationTx string, body return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -749,7 +810,7 @@ func NewCreateDirRequest(baseUrl, allocationID string, allocationTx string, body return req, nil } -func NewShareRequest(baseUrl, allocationID string, allocationTx string, body io.Reader) (*http.Request, error) { +func NewShareRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { u, err := joinUrl(baseUrl, SHARE_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -760,7 +821,7 @@ func NewShareRequest(baseUrl, allocationID string, allocationTx string, body io. return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -769,7 +830,7 @@ func NewShareRequest(baseUrl, allocationID string, allocationTx string, body io. return req, nil } -func NewRevokeShareRequest(baseUrl, allocationID string, allocationTx string, query *url.Values) (*http.Request, error) { +func NewRevokeShareRequest(baseUrl, allocationID, allocationTx, sig string, query *url.Values) (*http.Request, error) { u, err := joinUrl(baseUrl, SHARE_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -780,7 +841,7 @@ func NewRevokeShareRequest(baseUrl, allocationID string, allocationTx string, qu return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -789,7 +850,7 @@ func NewRevokeShareRequest(baseUrl, allocationID string, allocationTx string, qu return req, nil } -func NewWritemarkerRequest(baseUrl, allocationID, allocationTx string) (*http.Request, error) { +func NewWritemarkerRequest(baseUrl, allocationID, allocationTx, sig string) (*http.Request, error) { nurl, err := joinUrl(baseUrl, LATEST_WRITE_MARKER_ENDPOINT, allocationTx) if err != nil { @@ -801,7 +862,7 @@ func NewWritemarkerRequest(baseUrl, allocationID, allocationTx string) (*http.Re return nil, err } - if err := setClientInfoWithSign(req, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } @@ -833,23 +894,18 @@ func HttpDo(ctx context.Context, cncl context.CancelFunc, req *http.Request, f f go func() { var err error for { - // Perform the request with the context provided. var resp *http.Response resp, err = Client.Do(req.WithContext(ctx)) if errors.Is(err, io.EOF) { - // If the error is io.EOF, continue to retry indefinitely. continue } - // Call the provided callback function with the response and error. err = f(resp, err) - break // Exit the loop after a successful request or a non-EOF error. + break } - c <- err // Send the final error (or nil) back through the channel. + c <- err }() - defer cncl() // Ensure the cancellation function is deferred to release resources. - select { case <-ctx.Done(): // If the context is canceled or times out, return the context's error. diff --git a/zboxcore/zboxutil/util.go b/zboxcore/zboxutil/util.go index 47afcd7c0..9241501c4 100644 --- a/zboxcore/zboxutil/util.go +++ b/zboxcore/zboxutil/util.go @@ -312,8 +312,8 @@ const ( ) func ScryptEncrypt(key, text []byte) ([]byte, error) { - if len(key) != keySize { - return nil, errors.New("scrypt: invalid key size" + strconv.Itoa(len(key))) + if len(key) == 0 { + return nil, errors.New("scrypt: key cannot be empty") } if len(text) == 0 { return nil, errors.New("scrypt: plaintext cannot be empty") diff --git a/zcncore/transaction.go b/zcncore/transaction.go index 2bca0c186..5c4088492 100644 --- a/zcncore/transaction.go +++ b/zcncore/transaction.go @@ -831,18 +831,19 @@ func (t *Transaction) Verify() error { } txnJson := conf["txn"] - var tr map[string]json.RawMessage - if err := json.Unmarshal(txnJson, &tr); err != nil { + tt := transaction.Transaction{} + if err := json.Unmarshal(txnJson, &tt); err != nil { return } - txStatus := tr["transaction_status"] - switch string(txStatus) { - case "1": + *t.txn = tt + txStatus := tt.Status + + switch txStatus { + case 1: t.completeVerifyWithConStatus(StatusSuccess, int(Success), string(output), nil) - case "2": - txOutput := tr["transaction_output"] - t.completeVerifyWithConStatus(StatusSuccess, int(ChargeableError), string(txOutput), nil) + case 2: + t.completeVerifyWithConStatus(StatusSuccess, int(ChargeableError), tt.TransactionOutput, nil) default: t.completeVerify(StatusError, string(output), nil) } diff --git a/zcncore/transaction_base.go b/zcncore/transaction_base.go index cb043ace5..0c7163d64 100644 --- a/zcncore/transaction_base.go +++ b/zcncore/transaction_base.go @@ -269,10 +269,12 @@ func txnTypeString(t int) string { } } +// Output implements the output of transaction func (t *Transaction) Output() []byte { return []byte(t.txnOut) } +// Hash implements the hash of transaction func (t *Transaction) Hash() string { return t.txn.Hash } @@ -430,6 +432,7 @@ func newTransaction(cb TransactionCallback, txnFee uint64, nonce int64) (*Transa return t, nil } +// SetTransactionCallback implements storing the callback func (t *Transaction) SetTransactionCallback(cb TransactionCallback) error { if t.txnStatus != StatusUnknown { return errors.New("", "transaction already exists. cannot set transaction hash.") @@ -438,6 +441,7 @@ func (t *Transaction) SetTransactionCallback(cb TransactionCallback) error { return nil } +// SetTransactionNonce implements method to set the transaction nonce func (t *Transaction) SetTransactionNonce(txnNonce int64) error { if t.txnStatus != StatusUnknown { return errors.New("", "transaction already exists. cannot set transaction fee.") @@ -446,6 +450,7 @@ func (t *Transaction) SetTransactionNonce(txnNonce int64) error { return nil } +// StoreData implements store the data to blockchain func (t *Transaction) StoreData(data string) error { go func() { t.txn.TransactionType = transaction.TxnTypeData @@ -553,6 +558,8 @@ func (t *Transaction) ExecuteFaucetSCWallet(walletStr string, methodName string, return nil } +// SetTransactionHash implements verify a previous transaction status +// - hash: transaction hash func (t *Transaction) SetTransactionHash(hash string) error { if t.txnStatus != StatusUnknown { return errors.New("", "transaction already exists. cannot set transaction hash.") @@ -561,6 +568,7 @@ func (t *Transaction) SetTransactionHash(hash string) error { return nil } +// GetTransactionHash implements retrieval of hash of the submitted transaction func (t *Transaction) GetTransactionHash() string { if t.txnHash != "" { return t.txnHash @@ -770,6 +778,8 @@ func (t *Transaction) isTransactionExpired(lfbCreationTime, currentTime int64) b sys.Sleep(defaultWaitSeconds) return false } + +// GetVerifyOutput implements the verification output from sharders func (t *Transaction) GetVerifyOutput() string { if t.verifyStatus == StatusSuccess { return t.verifyOut @@ -777,6 +787,7 @@ func (t *Transaction) GetVerifyOutput() string { return "" } +// GetTransactionError implements error string in case of transaction failure func (t *Transaction) GetTransactionError() string { if t.txnStatus != StatusSuccess { return t.txnError.Error() @@ -784,6 +795,7 @@ func (t *Transaction) GetTransactionError() string { return "" } +// GetVerifyError implements error string in case of verify failure error func (t *Transaction) GetVerifyError() string { if t.verifyStatus != StatusSuccess { return t.verifyError.Error() diff --git a/zcncore/transaction_mobile.go b/zcncore/transaction_mobile.go index 8cb51c6b4..49ac0ba5b 100644 --- a/zcncore/transaction_mobile.go +++ b/zcncore/transaction_mobile.go @@ -48,10 +48,10 @@ type stakePoolRequest struct { type TransactionCommon interface { // ExecuteSmartContract implements wrapper for smart contract function - ExecuteSmartContract(address, methodName string, input string, val string) error + ExecuteSmartContract(address, methodName string, input interface{}, val uint64, feeOpts ...FeeOption) (*transaction.Transaction, error) // Send implements sending token to a given clientid - Send(toClientID string, val string, desc string) error + Send(toClientID string, val uint64, desc string) error VestingAdd(ar VestingAddRequest, value string) error @@ -329,13 +329,8 @@ func parseCoinStr(vs string) (uint64, error) { // - cb: callback for transaction state // - txnFee: Transaction fees (in SAS tokens) // - nonce: latest nonce of current wallet. please set it with 0 if you don't know the latest value -func NewTransaction(cb TransactionCallback, txnFee string, nonce int64) (TransactionScheme, error) { - v, err := parseCoinStr(txnFee) - if err != nil { - return nil, err - } - - err = CheckConfig() +func NewTransaction(cb TransactionCallback, txnFee uint64, nonce int64) (TransactionScheme, error) { + err := CheckConfig() if err != nil { return nil, err } @@ -344,28 +339,25 @@ func NewTransaction(cb TransactionCallback, txnFee string, nonce int64) (Transac return nil, errors.New("", "auth url not set") } logging.Info("New transaction interface with auth") - return newTransactionWithAuth(cb, v, nonce) + return newTransactionWithAuth(cb, txnFee, nonce) } logging.Info("New transaction interface") - t, err := newTransaction(cb, v, nonce) + t, err := newTransaction(cb, txnFee, nonce) return t, err } // ExecuteSmartContract prepare and send a smart contract transaction to the blockchain -func (t *Transaction) ExecuteSmartContract(address, methodName string, input string, val string) error { - v, err := parseCoinStr(val) +func (t *Transaction) ExecuteSmartContract(address, methodName string, input interface{}, val uint64, feeOpts ...FeeOption) (*transaction.Transaction, error) { + // t.createSmartContractTxn(address, methodName, input, val, opts...) + err := t.createSmartContractTxn(address, methodName, input, val) if err != nil { - return err - } - - err = t.createSmartContractTxn(address, methodName, json.RawMessage(input), v) - if err != nil { - return err + return nil, err } go func() { t.setNonceAndSubmit() }() - return nil + + return t.txn, nil } func (t *Transaction) setTransactionFee(fee uint64) error { @@ -377,12 +369,7 @@ func (t *Transaction) setTransactionFee(fee uint64) error { } // Send to send a transaction to a given clientID -func (t *Transaction) Send(toClientID string, val string, desc string) error { - v, err := parseCoinStr(val) - if err != nil { - return err - } - +func (t *Transaction) Send(toClientID string, val uint64, desc string) error { txnData, err := json.Marshal(SendTxnData{Note: desc}) if err != nil { return errors.New("", "Could not serialize description to transaction_data") @@ -390,7 +377,7 @@ func (t *Transaction) Send(toClientID string, val string, desc string) error { go func() { t.txn.TransactionType = transaction.TxnTypeSend t.txn.ToClientID = toClientID - t.txn.Value = v + t.txn.Value = val t.txn.TransactionData = string(txnData) t.setNonceAndSubmit() }() diff --git a/zcncore/transactionauth.go b/zcncore/transactionauth.go index 4d44cfea3..57769fcdf 100644 --- a/zcncore/transactionauth.go +++ b/zcncore/transactionauth.go @@ -7,6 +7,7 @@ import ( "encoding/json" "fmt" "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/transaction" "math" ) @@ -24,17 +25,32 @@ func (ta *TransactionWithAuth) ExecuteSmartContract(address, methodName string, } func (ta *TransactionWithAuth) Send(toClientID string, val uint64, desc string) error { - txnData, err := json.Marshal(SendTxnData{Note: desc}) + txnData, err := json.Marshal(transaction.SmartContractTxnData{Name: "transfer", InputArgs: SendTxnData{Note: desc}}) if err != nil { return errors.New("", "Could not serialize description to transaction_data") } + + clientNode, err := client.GetNode() + if err != nil { + return err + } + + ta.t.txn.TransactionType = transaction.TxnTypeSend + ta.t.txn.ToClientID = toClientID + ta.t.txn.Value = val + ta.t.txn.TransactionData = string(txnData) + if ta.t.txn.TransactionFee == 0 { + fee, err := transaction.EstimateFee(ta.t.txn, clientNode.Network().Miners, 0.2) + if err != nil { + return err + } + ta.t.txn.TransactionFee = fee + } + go func() { - ta.t.txn.TransactionType = transaction.TxnTypeSend - ta.t.txn.ToClientID = toClientID - ta.t.txn.Value = val - ta.t.txn.TransactionData = string(txnData) ta.submitTxn() }() + return nil } diff --git a/zcncore/transactionauth_base.go b/zcncore/transactionauth_base.go index a6134b71f..47d8119d1 100644 --- a/zcncore/transactionauth_base.go +++ b/zcncore/transactionauth_base.go @@ -55,15 +55,13 @@ func (ta *TransactionWithAuth) getAuthorize() (*transaction.Transaction, error) if err != nil { return nil, errors.Wrap(err, "invalid json on auth response.") } - logging.Debug(txnResp) - // Verify the signature on the result - ok, err := txnResp.VerifyTransaction(sys.VerifyWith) + // Verify the split key signed signature + ok, err := txnResp.VerifySigWith(client.PublicKey(), sys.VerifyWith) if err != nil { logging.Error("verification failed for txn from auth", err.Error()) return nil, errAuthVerifyFailed } if !ok { - ta.completeTxn(StatusAuthVerifyFailed, "", errAuthVerifyFailed) return nil, errAuthVerifyFailed } return &txnResp, nil @@ -121,21 +119,14 @@ func (ta *TransactionWithAuth) submitTxn() { ta.t.txn.TransactionNonce = nonce authTxn, err := ta.getAuthorize() if err != nil { - logging.Error("get auth error for send.", err.Error()) + logging.Error("get auth error for send, err: ", err.Error()) ta.completeTxn(StatusAuthError, "", err) return } - // Authorized by user. Give callback to app. - if ta.t.txnCb != nil { - ta.t.txnCb.OnAuthComplete(ta.t, StatusSuccess) - } + // Use the timestamp from auth and sign ta.t.txn.CreationDate = authTxn.CreationDate - err = ta.sign(authTxn.Signature) - if err != nil { - ta.completeTxn(StatusError, "", errAddSignature) - } - + ta.t.txn.Signature = authTxn.Signature ta.t.submitTxn() } diff --git a/zcncore/transactionauth_mobile.go b/zcncore/transactionauth_mobile.go index e4a0d93b9..e00158e33 100644 --- a/zcncore/transactionauth_mobile.go +++ b/zcncore/transactionauth_mobile.go @@ -9,28 +9,19 @@ import ( "github.com/0chain/gosdk/core/transaction" ) -func (ta *TransactionWithAuth) ExecuteSmartContract(address, methodName string, input string, val string) error { - v, err := parseCoinStr(val) +func (ta *TransactionWithAuth) ExecuteSmartContract(address, methodName string, + input interface{}, val uint64, feeOpts ...FeeOption) (*transaction.Transaction, error) { + err := ta.t.createSmartContractTxn(address, methodName, input, val, feeOpts...) if err != nil { - return err - } - - err = ta.t.createSmartContractTxn(address, methodName, json.RawMessage(input), v) - if err != nil { - return err + return nil, err } go func() { ta.submitTxn() }() - return nil + return ta.t.txn, nil } -func (ta *TransactionWithAuth) Send(toClientID string, val string, desc string) error { - v, err := parseCoinStr(val) - if err != nil { - return err - } - +func (ta *TransactionWithAuth) Send(toClientID string, val uint64, desc string) error { txnData, err := json.Marshal(SendTxnData{Note: desc}) if err != nil { return errors.New("", "Could not serialize description to transaction_data") @@ -38,7 +29,7 @@ func (ta *TransactionWithAuth) Send(toClientID string, val string, desc string) go func() { ta.t.txn.TransactionType = transaction.TxnTypeSend ta.t.txn.ToClientID = toClientID - ta.t.txn.Value = v + ta.t.txn.Value = val ta.t.txn.TransactionData = string(txnData) ta.submitTxn() }() diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 02b0a823e..35216b201 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -462,7 +462,7 @@ func SetWalletInfo(jsonWallet string, splitKeyWallet bool) error { return errors.New("invalid jsonWallet: " + err.Error()) } - client.SetWallet(wallet) + client.SetWallet(false, wallet) return client.SetSplitKeyWallet(splitKeyWallet) } diff --git a/zcncore/zauth.go b/zcncore/zauth.go new file mode 100644 index 000000000..c88f83ea6 --- /dev/null +++ b/zcncore/zauth.go @@ -0,0 +1,534 @@ +package zcncore + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + + "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/sys" + "github.com/pkg/errors" +) + +// SplitWallet represents wallet info for split wallet +// The client id and client key are the same as the primary wallet client id and client key +type SplitWallet struct { + ClientID string `json:"client_id"` + ClientKey string `json:"client_key"` + PublicKey string `json:"public_key"` + PrivateKey string `json:"private_key"` + PeerPublicKey string `json:"peer_public_key"` + IsRevoked bool `json:"is_revoked"` + ExpiredAt int64 `json:"expired_at"` +} + +// CallZauthSetup calls the zauth setup endpoint +func CallZauthSetup(serverAddr string, token string, splitWallet SplitWallet) error { + // Add your code here + endpoint := serverAddr + "/setup" + wData, err := json.Marshal(splitWallet) + if err != nil { + return errors.Wrap(err, "failed to marshal split wallet") + } + + req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(wData)) + if err != nil { + return errors.Wrap(err, "failed to create HTTP request") + } + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Jwt-Token", token) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return errors.Wrap(err, "failed to send HTTP request") + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + errMsg, _ := io.ReadAll(resp.Body) + if len(errMsg) > 0 { + return errors.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) + } + + return errors.Errorf("code: %d", resp.StatusCode) + } + + var rsp struct { + Result string `json:"result"` + } + if err := json.NewDecoder(resp.Body).Decode(&rsp); err != nil { + return errors.Wrap(err, "failed to decode response body") + } + + if rsp.Result != "success" { + return errors.New("failed to setup zauth server") + } + + return nil +} + +func CallZauthRevoke(serverAddr, token, clientID, publicKey string) error { + endpoint := serverAddr + "/revoke/" + clientID + endpoint += "?peer_public_key=" + publicKey + req, err := http.NewRequest("POST", endpoint, nil) + if err != nil { + return errors.Wrap(err, "failed to create HTTP request") + } + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Jwt-Token", token) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return errors.Wrap(err, "failed to send HTTP request") + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + errMsg, _ := io.ReadAll(resp.Body) + if len(errMsg) > 0 { + return errors.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) + } + + return errors.Errorf("code: %d", resp.StatusCode) + } + + var rsp struct { + Result string `json:"result"` + } + if err := json.NewDecoder(resp.Body).Decode(&rsp); err != nil { + return errors.Wrap(err, "failed to decode response body") + } + + if rsp.Result != "success" { + return errors.New("failed to setup zauth server") + } + + return nil +} + +func CallZauthDelete(serverAddr, token, clientID string) error { + endpoint := serverAddr + "/delete/" + clientID + req, err := http.NewRequest("POST", endpoint, nil) + if err != nil { + return errors.Wrap(err, "failed to create HTTP request") + } + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Jwt-Token", token) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return errors.Wrap(err, "failed to send HTTP request") + } + + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + errMsg, _ := io.ReadAll(resp.Body) + if len(errMsg) > 0 { + return errors.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) + } + + return errors.Errorf("code: %d", resp.StatusCode) + } + + var rsp struct { + Result string `json:"result"` + } + if err := json.NewDecoder(resp.Body).Decode(&rsp); err != nil { + return errors.Wrap(err, "failed to decode response body") + } + + if rsp.Result != "success" { + return errors.New("failed to setup zauth server") + } + + return nil +} + +func CallZvaultNewWalletString(serverAddr, token, clientID string) (string, error) { + // Add your code here + endpoint := serverAddr + "/generate" + if clientID != "" { + endpoint = endpoint + "/" + clientID + } + + req, err := http.NewRequest("POST", endpoint, nil) + if err != nil { + return "", errors.Wrap(err, "failed to create HTTP request") + } + + fmt.Println("new wallet endpoint:", endpoint) + fmt.Println("new wallet: serverAddr:", serverAddr) + fmt.Println("new wallet: clientID:", clientID) + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Jwt-Token", token) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return "", errors.Wrap(err, "failed to send HTTP request") + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + errMsg, _ := io.ReadAll(resp.Body) + if len(errMsg) > 0 { + return "", errors.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) + } + + return "", errors.Errorf("code: %d", resp.StatusCode) + } + + d, err := io.ReadAll(resp.Body) + if err != nil { + return "", errors.Wrap(err, "failed to read response body") + } + + return string(d), nil +} + +func CallZvaultStoreKeyString(serverAddr, token, privateKey string) (string, error) { + // Add your code here + endpoint := serverAddr + "/store" + + reqData := struct { + PrivateKey string `json:"private_key"` + }{ + PrivateKey: privateKey, + } + + var buff bytes.Buffer + + encoder := json.NewEncoder(&buff) + + err := encoder.Encode(reqData) + if err != nil { + return "", errors.Wrap(err, "failed to create HTTP request") + } + + var req *http.Request + + req, err = http.NewRequest("POST", endpoint, &buff) + if err != nil { + return "", errors.Wrap(err, "failed to create HTTP request") + } + + fmt.Println("call zvault /store:", endpoint) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Jwt-Token", token) + + fmt.Println(req) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + fmt.Println(err.Error()) + + return "", errors.Wrap(err, "failed to send HTTP request") + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + errMsg, _ := io.ReadAll(resp.Body) + if len(errMsg) > 0 { + return "", errors.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) + } + + return "", errors.Errorf("code: %d", resp.StatusCode) + } + + d, err := io.ReadAll(resp.Body) + if err != nil { + return "", errors.Wrap(err, "failed to read response body") + } + + return string(d), nil +} + +func CallZvaultRetrieveKeys(serverAddr, token, clientID string) (string, error) { + // Add your code here + endpoint := fmt.Sprintf("%s/keys/%s", serverAddr, clientID) + req, err := http.NewRequest("GET", endpoint, nil) + if err != nil { + return "", errors.Wrap(err, "failed to create HTTP request") + } + + fmt.Println("call zvault /keys:", endpoint) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Jwt-Token", token) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return "", errors.Wrap(err, "failed to send HTTP request") + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + errMsg, _ := io.ReadAll(resp.Body) + return "", fmt.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) + } + + d, err := io.ReadAll(resp.Body) + if err != nil { + return "", errors.Wrap(err, "failed to read response body") + } + + return string(d), nil +} + +func CallZvaultDeletePrimaryKey(serverAddr, token, clientID string) error { + // Add your code here + endpoint := serverAddr + "/delete/" + clientID + req, err := http.NewRequest("POST", endpoint, nil) + if err != nil { + return errors.Wrap(err, "failed to create HTTP request") + } + + fmt.Println("call zvault /delete:", endpoint) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Jwt-Token", token) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return errors.Wrap(err, "failed to send HTTP request") + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + errMsg, _ := io.ReadAll(resp.Body) + return fmt.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) + } + + _, err = io.ReadAll(resp.Body) + if err != nil { + return errors.Wrap(err, "failed to read response body") + } + + return nil +} + +func CallZvaultRevokeKey(serverAddr, token, clientID, publicKey string) error { + // Add your code here + endpoint := fmt.Sprintf("%s/revoke/%s?public_key=%s", serverAddr, clientID, publicKey) + req, err := http.NewRequest("POST", endpoint, nil) + if err != nil { + return errors.Wrap(err, "failed to create HTTP request") + } + + fmt.Println("call zvault /revoke:", endpoint) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Jwt-Token", token) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return errors.Wrap(err, "failed to send HTTP request") + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + errMsg, _ := io.ReadAll(resp.Body) + return fmt.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) + } + + _, err = io.ReadAll(resp.Body) + if err != nil { + return errors.Wrap(err, "failed to read response body") + } + + return nil +} + +func CallZvaultRetrieveWallets(serverAddr, token string) (string, error) { + // Add your code here + endpoint := fmt.Sprintf("%s/wallets", serverAddr) + req, err := http.NewRequest("GET", endpoint, nil) + if err != nil { + return "", errors.Wrap(err, "failed to create HTTP request") + } + + fmt.Println("call zvault /keys:", endpoint) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Jwt-Token", token) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return "", errors.Wrap(err, "failed to send HTTP request") + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + errMsg, _ := io.ReadAll(resp.Body) + return "", fmt.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) + } + + d, err := io.ReadAll(resp.Body) + if err != nil { + return "", errors.Wrap(err, "failed to read response body") + } + + return string(d), nil +} + +func CallZvaultRetrieveSharedWallets(serverAddr, token string) (string, error) { + // Add your code here + endpoint := fmt.Sprintf("%s/wallets/shared", serverAddr) + req, err := http.NewRequest("GET", endpoint, nil) + if err != nil { + return "", errors.Wrap(err, "failed to create HTTP request") + } + + fmt.Println("call zvault /keys:", endpoint) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Jwt-Token", token) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return "", errors.Wrap(err, "failed to send HTTP request") + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + errMsg, _ := io.ReadAll(resp.Body) + return "", fmt.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) + } + + d, err := io.ReadAll(resp.Body) + if err != nil { + return "", errors.Wrap(err, "failed to read response body") + } + + return string(d), nil +} + +// ZauthSignTxn returns a function that sends a txn signing request to the zauth server +func ZauthSignTxn(serverAddr string) sys.AuthorizeFunc { + return func(msg string) (string, error) { + fmt.Println("zvault sign txn - in sign txn...") + req, err := http.NewRequest("POST", serverAddr+"/sign/txn", bytes.NewBuffer([]byte(msg))) + if err != nil { + return "", errors.Wrap(err, "failed to create HTTP request") + } + req.Header.Set("Content-Type", "application/json") + c := client.GetClient() + pubkey := c.Keys[0].PublicKey + req.Header.Set("X-Peer-Public-Key", pubkey) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return "", errors.Wrap(err, "failed to send HTTP request") + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + rsp, err := io.ReadAll(resp.Body) + if err != nil { + return "", errors.Wrap(err, "failed to read response body") + } + + return "", errors.Errorf("unexpected status code: %d, res: %s", resp.StatusCode, string(rsp)) + } + + d, err := io.ReadAll(resp.Body) + if err != nil { + return "", errors.Wrap(err, "failed to read response body") + } + + return string(d), nil + } +} + +func ZauthAuthCommon(serverAddr string) sys.AuthorizeFunc { + return func(msg string) (string, error) { + // return func(msg string) (string, error) { + req, err := http.NewRequest("POST", serverAddr+"/sign/msg", bytes.NewBuffer([]byte(msg))) + if err != nil { + return "", errors.Wrap(err, "failed to create HTTP request") + } + + c := client.GetClient() + pubkey := c.Keys[0].PublicKey + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Peer-Public-Key", pubkey) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return "", errors.Wrap(err, "failed to send HTTP request") + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + rsp, err := io.ReadAll(resp.Body) + if err != nil { + return "", errors.Wrap(err, "failed to read response body") + } + + return "", errors.Errorf("unexpected status code: %d, res: %s", resp.StatusCode, string(rsp)) + } + + d, err := io.ReadAll(resp.Body) + if err != nil { + return "", errors.Wrap(err, "failed to read response body") + } + + return string(d), nil + } +} + +type AuthMessage struct { + Hash string `json:"hash"` + Signature string `json:"signature"` + ClientID string `json:"client_id"` +} + +type AuthResponse struct { + Sig string `json:"sig"` +} + +func ZauthSignMsg(serverAddr string) sys.SignFunc { + return func(hash string, signatureScheme string, keys []sys.KeyPair) (string, error) { + sig, err := SignWithKey(keys[0].PrivateKey, hash) + if err != nil { + return "", err + } + + data, err := json.Marshal(AuthMessage{ + Hash: hash, + Signature: sig, + ClientID: client.GetClient().ClientID, + }) + if err != nil { + return "", err + } + + // fmt.Println("auth - sys.AuthCommon:", sys.AuthCommon) + if sys.AuthCommon == nil { + return "", errors.New("authCommon is not set") + } + + rsp, err := sys.AuthCommon(string(data)) + if err != nil { + return "", err + } + + var ar AuthResponse + err = json.Unmarshal([]byte(rsp), &ar) + if err != nil { + return "", err + } + + return AddSignature(client.PrivateKey(), ar.Sig, hash) + } +} From f283e4c51c4ba8747584f517f99f1118c9f4d8a0 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 8 Sep 2024 14:50:19 +0530 Subject: [PATCH 035/319] Fix set wallet --- core/client/set.go | 3 +- winsdk/sdk.go | 2 +- zboxcore/sdk/allocation_file_delete_test.go | 4 +-- zboxcore/sdk/allocation_file_test.go | 4 +-- zboxcore/sdk/allocation_test.go | 40 ++++++++++----------- zboxcore/sdk/copyworker_test.go | 4 +-- zboxcore/sdk/deleteworker_test.go | 4 +-- zboxcore/sdk/filemetaworker_test.go | 4 +-- zboxcore/sdk/filestatsworker_test.go | 4 +-- zboxcore/sdk/listworker_test.go | 4 +-- zboxcore/sdk/renameworker_test.go | 4 +-- zboxcore/sdk/sdk.go | 2 +- zcncore/wallet_base.go | 2 +- 13 files changed, 40 insertions(+), 41 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index 8c5aa7e91..8ac67fcba 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -89,9 +89,8 @@ func GetClientSysKeys() []sys.KeyPair { } // SetWallet should be set before any transaction or client specific APIs -func SetWallet(isSplit bool, w zcncrypto.Wallet) { +func SetWallet(w zcncrypto.Wallet) { client.wallet = &w - client.wallet.IsSplit = isSplit } // splitKeyWallet parameter is valid only if SignatureScheme is "BLS0Chain" diff --git a/winsdk/sdk.go b/winsdk/sdk.go index 643d70047..86227ae14 100644 --- a/winsdk/sdk.go +++ b/winsdk/sdk.go @@ -163,7 +163,7 @@ func InitWallet(clientJson *C.char) *C.char { l.Logger.Error(err) return WithJSON(false, err) } - client.SetWallet(false, w) + client.SetWallet(w) l.Logger.Info("InitWallet success") zboxApiClient.SetWallet(client.Wallet().ClientID, client.Wallet().Keys[0].PrivateKey, client.Wallet().ClientKey) diff --git a/zboxcore/sdk/allocation_file_delete_test.go b/zboxcore/sdk/allocation_file_delete_test.go index 38b893a0d..e9be2c349 100644 --- a/zboxcore/sdk/allocation_file_delete_test.go +++ b/zboxcore/sdk/allocation_file_delete_test.go @@ -29,7 +29,7 @@ func TestAllocation_DeleteFile(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -88,7 +88,7 @@ func TestAllocation_deleteFile(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) diff --git a/zboxcore/sdk/allocation_file_test.go b/zboxcore/sdk/allocation_file_test.go index 28d6dc6fe..ed3920133 100644 --- a/zboxcore/sdk/allocation_file_test.go +++ b/zboxcore/sdk/allocation_file_test.go @@ -41,7 +41,7 @@ func setupHttpResponses( err := json.Unmarshal([]byte(walletJSON), &wallet) require.NoError(t, err) - client.SetWallet(false, wallet) + client.SetWallet(wallet) client.SetSignatureScheme("bls0chain") for i := 0; i < numBlobbers; i++ { @@ -683,7 +683,7 @@ func TestAllocation_RepairFile(t *testing.T) { resty.CreateClient = createClient }() - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) diff --git a/zboxcore/sdk/allocation_test.go b/zboxcore/sdk/allocation_test.go index a1600b176..f73a5a830 100644 --- a/zboxcore/sdk/allocation_test.go +++ b/zboxcore/sdk/allocation_test.go @@ -401,7 +401,7 @@ func TestAllocation_GetBlobberStats(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -546,7 +546,7 @@ func TestAllocation_RepairRequired(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -693,7 +693,7 @@ func TestAllocation_DownloadFileToFileHandler(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -774,7 +774,7 @@ func TestAllocation_DownloadFile(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -821,7 +821,7 @@ func TestAllocation_DownloadFileByBlock(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -875,7 +875,7 @@ func TestAllocation_downloadFile(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -1014,7 +1014,7 @@ func TestAllocation_GetRefs(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -1058,7 +1058,7 @@ func TestAllocation_GetFileMeta(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -1175,7 +1175,7 @@ func TestAllocation_GetAuthTicketForShare(t *testing.T) { mockClient.On("Do", mock.Anything).Return(&httpResponse, nil) } - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -1380,7 +1380,7 @@ func TestAllocation_GetAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -1589,7 +1589,7 @@ func TestAllocation_ListDirFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -1642,7 +1642,7 @@ func TestAllocation_downloadFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -1867,7 +1867,7 @@ func TestAllocation_listDir(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -2000,7 +2000,7 @@ func TestAllocation_GetFileMetaFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -2048,7 +2048,7 @@ func TestAllocation_DownloadToFileHandlerFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -2083,7 +2083,7 @@ func TestAllocation_DownloadThumbnailFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -2125,7 +2125,7 @@ func TestAllocation_DownloadFromAuthTicket(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -2161,7 +2161,7 @@ func TestAllocation_DownloadFromAuthTicketByBlocks(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -2196,7 +2196,7 @@ func TestAllocation_StartRepair(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -2381,7 +2381,7 @@ func getMockAuthTicket(t *testing.T) string { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) diff --git a/zboxcore/sdk/copyworker_test.go b/zboxcore/sdk/copyworker_test.go index be122920c..3baa183e1 100644 --- a/zboxcore/sdk/copyworker_test.go +++ b/zboxcore/sdk/copyworker_test.go @@ -44,7 +44,7 @@ func TestCopyRequest_copyBlobberObject(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -249,7 +249,7 @@ func TestCopyRequest_ProcessCopy(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) diff --git a/zboxcore/sdk/deleteworker_test.go b/zboxcore/sdk/deleteworker_test.go index dbabb4062..7b0de8e5a 100644 --- a/zboxcore/sdk/deleteworker_test.go +++ b/zboxcore/sdk/deleteworker_test.go @@ -41,7 +41,7 @@ func TestDeleteRequest_deleteBlobberFile(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -214,7 +214,7 @@ func TestDeleteRequest_ProcessDelete(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) diff --git a/zboxcore/sdk/filemetaworker_test.go b/zboxcore/sdk/filemetaworker_test.go index ad0bcd005..395abb6c4 100644 --- a/zboxcore/sdk/filemetaworker_test.go +++ b/zboxcore/sdk/filemetaworker_test.go @@ -42,7 +42,7 @@ func TestListRequest_getFileMetaInfoFromBlobber(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -203,7 +203,7 @@ func TestListRequest_getFileConsensusFromBlobbers(t *testing.T) { const mockClientId = "mock client id" const mockClientKey = "mock client key" - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) diff --git a/zboxcore/sdk/filestatsworker_test.go b/zboxcore/sdk/filestatsworker_test.go index da621eed4..a5981f389 100644 --- a/zboxcore/sdk/filestatsworker_test.go +++ b/zboxcore/sdk/filestatsworker_test.go @@ -42,7 +42,7 @@ func TestListRequest_getFileStatsInfoFromBlobber(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -184,7 +184,7 @@ func TestListRequest_getFileStatsFromBlobbers(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) diff --git a/zboxcore/sdk/listworker_test.go b/zboxcore/sdk/listworker_test.go index 8b98488d0..0145304f1 100644 --- a/zboxcore/sdk/listworker_test.go +++ b/zboxcore/sdk/listworker_test.go @@ -41,7 +41,7 @@ func TestListRequest_getListInfoFromBlobber(t *testing.T) { ) var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -214,7 +214,7 @@ func TestListRequest_GetListFromBlobbers(t *testing.T) { var mockClient = mocks.HttpClient{} zboxutil.Client = &mockClient - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) diff --git a/zboxcore/sdk/renameworker_test.go b/zboxcore/sdk/renameworker_test.go index 986edc1fa..b14eaab10 100644 --- a/zboxcore/sdk/renameworker_test.go +++ b/zboxcore/sdk/renameworker_test.go @@ -49,7 +49,7 @@ func TestRenameRequest_renameBlobberObject(t *testing.T) { zboxutil.Client = rawClient }() - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) @@ -279,7 +279,7 @@ func TestRenameRequest_ProcessRename(t *testing.T) { zboxutil.Client = rawClient }() - client.SetWallet(false, zcncrypto.Wallet{ + client.SetWallet(zcncrypto.Wallet{ ClientID: mockClientId, ClientKey: mockClientKey, }) diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index e5fbabbf2..f818be24d 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -125,7 +125,7 @@ func InitStorageSDK(walletJSON string, return err } - client.SetWallet(false, wallet) + client.SetWallet(wallet) client.SetSignatureScheme(signatureScheme) client.SetNonce(nonce) if len(fee) > 0 { diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 35216b201..02b0a823e 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -462,7 +462,7 @@ func SetWalletInfo(jsonWallet string, splitKeyWallet bool) error { return errors.New("invalid jsonWallet: " + err.Error()) } - client.SetWallet(false, wallet) + client.SetWallet(wallet) return client.SetSplitKeyWallet(splitKeyWallet) } From 73f77e4ce92b6561defe7b01638a27247decfba9 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 8 Sep 2024 14:51:25 +0530 Subject: [PATCH 036/319] Fix version --- core/version/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/version/version.go b/core/version/version.go index 22d2ceb0c..6680f86de 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.3-42-gea1f6917" +const VERSIONSTR = "v1.17.6-41-gf283e4c5" From 5a3358c67b45f22327c900f0fbc4316f5567dd9f Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 8 Sep 2024 14:57:38 +0530 Subject: [PATCH 037/319] Fix --- core/version/version.go | 2 +- wasmsdk/smartcontract.go | 85 +++++++++++++++++++++--- wasmsdk/wallet.go | 2 +- winsdk/zboxapi.go | 2 +- zboxcore/sdk/transaction.go | 127 ------------------------------------ 5 files changed, 80 insertions(+), 138 deletions(-) delete mode 100644 zboxcore/sdk/transaction.go diff --git a/core/version/version.go b/core/version/version.go index 6680f86de..9152075da 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.6-41-gf283e4c5" +const VERSIONSTR = "v1.17.6-42-g73f77e4c" diff --git a/wasmsdk/smartcontract.go b/wasmsdk/smartcontract.go index e77cf446b..582bea1c1 100644 --- a/wasmsdk/smartcontract.go +++ b/wasmsdk/smartcontract.go @@ -2,15 +2,15 @@ package main import ( "encoding/json" + "fmt" + "sync" "github.com/0chain/gosdk/core/transaction" - "github.com/0chain/gosdk/zboxcore/sdk" "github.com/0chain/gosdk/zcncore" ) func faucet(methodName, input string, token float64) (*transaction.Transaction, error) { - return executeSmartContract(zcncore.FaucetSmartContractAddress, - methodName, input, zcncore.ConvertToValue(token)) + return executeSmartContract(zcncore.FaucetSmartContractAddress, methodName, input, zcncore.ConvertToValue(token)) } // executeSmartContract issue a smart contract transaction @@ -19,9 +19,78 @@ func faucet(methodName, input string, token float64) (*transaction.Transaction, // - input is the input data for the method // - value is the value to be sent with the transaction func executeSmartContract(address, methodName, input string, value uint64) (*transaction.Transaction, error) { - return sdk.ExecuteSmartContract(address, - transaction.SmartContractTxnData{ - Name: methodName, - InputArgs: json.RawMessage([]byte(input)), - }, value, 0) + wg := &sync.WaitGroup{} + cb := &transactionCallback{wg: wg} + txn, err := zcncore.NewTransaction(cb, 0, 0) + if err != nil { + return nil, err + } + + wg.Add(1) + t, err := txn.ExecuteSmartContract(address, methodName, json.RawMessage([]byte(input)), value) + if err != nil { + return nil, err + + } + + wg.Wait() + + if !cb.success { + return nil, fmt.Errorf("smartcontract: %s", cb.errMsg) + } + + cb.success = false + wg.Add(1) + err = txn.Verify() + if err != nil { + return nil, err + } + + wg.Wait() + + if !cb.success { + return nil, fmt.Errorf("smartcontract: %s", cb.errMsg) + } + + switch txn.GetVerifyConfirmationStatus() { + case zcncore.ChargeableError: + return nil, fmt.Errorf("smartcontract: %s", txn.GetVerifyOutput()) + case zcncore.Success: + return t, nil + } + + return nil, fmt.Errorf("smartcontract: %v", txn.GetVerifyConfirmationStatus()) +} + +type transactionCallback struct { + wg *sync.WaitGroup + success bool + errMsg string + + txn *zcncore.Transaction +} + +func (cb *transactionCallback) OnTransactionComplete(t *zcncore.Transaction, status int) { + defer cb.wg.Done() + cb.txn = t + if status == zcncore.StatusSuccess { + cb.success = true + } else { + cb.errMsg = t.GetTransactionError() + } +} + +func (cb *transactionCallback) OnVerifyComplete(t *zcncore.Transaction, status int) { + defer cb.wg.Done() + cb.txn = t + if status == zcncore.StatusSuccess { + cb.success = true + } else { + cb.errMsg = t.GetVerifyError() + } +} + +func (cb *transactionCallback) OnAuthComplete(t *zcncore.Transaction, status int) { + cb.txn = t + fmt.Println("Authorization complete on zauth.", status) } diff --git a/wasmsdk/wallet.go b/wasmsdk/wallet.go index 6c3594653..33bc332d3 100644 --- a/wasmsdk/wallet.go +++ b/wasmsdk/wallet.go @@ -45,7 +45,7 @@ func setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemon IsSplit: isSplit, } fmt.Println("set Wallet, is split:", isSplit) - client.SetWallet(isSplit, *w) + client.SetWallet(*w) zboxApiClient.SetWallet(clientID, privateKey, publicKey) if mode == "" { // main thread, need to notify the web worker to update wallet diff --git a/winsdk/zboxapi.go b/winsdk/zboxapi.go index 149ad043b..392001a81 100644 --- a/winsdk/zboxapi.go +++ b/winsdk/zboxapi.go @@ -13,9 +13,9 @@ import ( "encoding/json" "errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/zboxapi" - "github.com/0chain/gosdk/core/client" ) var ( diff --git a/zboxcore/sdk/transaction.go b/zboxcore/sdk/transaction.go deleted file mode 100644 index aba11e63e..000000000 --- a/zboxcore/sdk/transaction.go +++ /dev/null @@ -1,127 +0,0 @@ -package sdk - -import ( - "fmt" - "sync" - - "errors" - - "github.com/0chain/gosdk/core/transaction" - l "github.com/0chain/gosdk/zboxcore/logger" - "github.com/0chain/gosdk/zcncore" -) - -type transactionCallback struct { - wg *sync.WaitGroup - success bool - errMsg string - - txn *zcncore.Transaction -} - -func (cb *transactionCallback) OnTransactionComplete(t *zcncore.Transaction, status int) { - defer cb.wg.Done() - cb.txn = t - if status == zcncore.StatusSuccess { - cb.success = true - } else { - cb.errMsg = t.GetTransactionError() - } -} - -func (cb *transactionCallback) OnVerifyComplete(t *zcncore.Transaction, status int) { - defer cb.wg.Done() - cb.txn = t - if status == zcncore.StatusSuccess { - cb.success = true - } else { - cb.errMsg = t.GetVerifyError() - } -} - -func (cb *transactionCallback) OnAuthComplete(t *zcncore.Transaction, status int) { - cb.txn = t - fmt.Println("Authorization complete on zauth.", status) -} - -// ExecuteSmartContract executes the smart contract -func ExecuteSmartContract(address string, sn transaction.SmartContractTxnData, value, fee uint64) (*transaction.Transaction, error) { - wg := &sync.WaitGroup{} - cb := &transactionCallback{wg: wg} - txn, err := zcncore.NewTransaction(cb, fee, 0) - if err != nil { - return nil, err - } - - wg.Add(1) - t, err := txn.ExecuteSmartContract(address, sn.Name, sn.InputArgs, value) - if err != nil { - return nil, err - } - - msg := fmt.Sprintf("Executing transaction '%s' with hash %s ", sn.Name, t.Hash) - l.Logger.Info(msg) - l.Logger.Info("estimated txn fee: ", t.TransactionFee) - - wg.Wait() - - if !cb.success { - return nil, fmt.Errorf("smartcontract: %s", cb.errMsg) - } - - cb.success = false - wg.Add(1) - err = txn.Verify() - if err != nil { - return nil, err - } - - wg.Wait() - - if !cb.success { - return nil, fmt.Errorf("smartcontract: %s", cb.errMsg) - } - - switch txn.GetVerifyConfirmationStatus() { - case zcncore.ChargeableError: - return t, errors.New(txn.GetVerifyOutput()) - case zcncore.Success: - return t, nil - } - - return nil, fmt.Errorf("smartcontract: %v", txn.GetVerifyConfirmationStatus()) -} - -// ExecuteSmartContractSend create send transaction to transfer tokens from the caller to target address -func ExecuteSmartContractSend(to string, tokens, fee uint64, desc string) (string, error) { - wg := &sync.WaitGroup{} - cb := &transactionCallback{wg: wg} - txn, err := zcncore.NewTransaction(cb, fee, 0) - if err != nil { - return "", err - } - - wg.Add(1) - err = txn.Send(to, tokens, desc) - if err == nil { - wg.Wait() - } else { - return "", err - } - - if cb.success { - cb.success = false - wg.Add(1) - err := txn.Verify() - if err == nil { - wg.Wait() - } else { - return "", err - } - if cb.success { - return txn.GetVerifyOutput(), nil - } - } - - return "", errors.New(cb.errMsg) -} From e92116b7df5164c599be53b84271d092acf3cd6f Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 8 Sep 2024 15:03:11 +0530 Subject: [PATCH 038/319] Fix lint --- core/client/set.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index 8ac67fcba..fce37812b 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -113,22 +113,19 @@ func SetAuthUrl(url string) error { return nil } -func SetNonce(n int64) error { +func SetNonce(n int64) { client.nonce = n - return nil } -func SetTxnFee(f uint64) error { +func SetTxnFee(f uint64) { client.txnFee = f - return nil } -func SetSignatureScheme(signatureScheme string) error { +func SetSignatureScheme(signatureScheme string) { if signatureScheme != constants.BLS0CHAIN.String() && signatureScheme != constants.ED25519.String() { - return errors.New("invalid/unsupported signature scheme") + panic("invalid/unsupported signature scheme") } client.signatureScheme = signatureScheme - return nil } func Wallet() *zcncrypto.Wallet { From 1d59ebdb89531d8371844a835d0bd97700ba729d Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 8 Sep 2024 23:17:00 +0530 Subject: [PATCH 039/319] Debug --- core/version/version.go | 2 +- mobilesdk/zcn/smartcontract.go | 102 ----------- wasmsdk/bridge.go | 10 +- wasmsdk/proxy.go | 4 - wasmsdk/smartcontract.go | 96 ----------- zcnbridge/bridge.go | 57 ++----- zcnbridge/bridge_test.go | 2 +- zcnbridge/config.go | 33 ++-- zcnbridge/transaction/functions.go | 114 ++++++------- zcnbridge/transaction/txn.go | 210 ----------------------- zcncore/functions.go | 12 ++ zcncore/sample/0Wallet.go | 261 ----------------------------- zcncore/transaction_base.go | 2 + 13 files changed, 100 insertions(+), 805 deletions(-) delete mode 100644 mobilesdk/zcn/smartcontract.go delete mode 100644 wasmsdk/smartcontract.go delete mode 100644 zcnbridge/transaction/txn.go create mode 100644 zcncore/functions.go delete mode 100644 zcncore/sample/0Wallet.go diff --git a/core/version/version.go b/core/version/version.go index 9152075da..68da08386 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.6-42-g73f77e4c" +const VERSIONSTR = "v1.17.6-44-ge92116b7" diff --git a/mobilesdk/zcn/smartcontract.go b/mobilesdk/zcn/smartcontract.go deleted file mode 100644 index 9069c8756..000000000 --- a/mobilesdk/zcn/smartcontract.go +++ /dev/null @@ -1,102 +0,0 @@ -//go:build mobile -// +build mobile - -package zcn - -import ( - "encoding/json" - "fmt" - "strconv" - "sync" - - "github.com/0chain/gosdk/zcncore" -) - -// Faucet -func Faucet(methodName, jsonInput string, zcnToken float64) (string, error) { - return ExecuteSmartContract(zcncore.FaucetSmartContractAddress, methodName, jsonInput, zcncore.ConvertToValue(zcnToken)) -} - -func ExecuteSmartContract(address, methodName, input string, sasToken string) (string, error) { - wg := &sync.WaitGroup{} - cb := &transactionCallback{wg: wg} - txn, err := zcncore.NewTransaction(cb, 0, 0) - if err != nil { - return "", err - } - - wg.Add(1) - - v, err := strconv.ParseUint(sasToken, 10, 64) - if err != nil { - return "", fmt.Errorf("invalid token value: %v, err: %v", sasToken, err) - } - - _, err = txn.ExecuteSmartContract(address, methodName, input, v) - if err != nil { - return "", err - - } - - wg.Wait() - - if !cb.success { - return "", fmt.Errorf("smartcontract: %s", cb.errMsg) - } - - cb.success = false - wg.Add(1) - err = txn.Verify() - if err != nil { - return "", err - } - - wg.Wait() - - if !cb.success { - return "", fmt.Errorf("smartcontract: %s", cb.errMsg) - } - - switch txn.GetVerifyConfirmationStatus() { - case zcncore.ChargeableError: - return "", fmt.Errorf("smartcontract: %s", txn.GetVerifyOutput()) - case zcncore.Success: - js, _ := json.Marshal(cb.txn) - return string(js), nil - } - - return "", fmt.Errorf("smartcontract: %v", txn.GetVerifyConfirmationStatus()) -} - -type transactionCallback struct { - wg *sync.WaitGroup - success bool - errMsg string - - txn *zcncore.Transaction -} - -func (cb *transactionCallback) OnTransactionComplete(t *zcncore.Transaction, status int) { - defer cb.wg.Done() - cb.txn = t - if status == zcncore.StatusSuccess { - cb.success = true - } else { - cb.errMsg = t.GetTransactionError() - } -} - -func (cb *transactionCallback) OnVerifyComplete(t *zcncore.Transaction, status int) { - defer cb.wg.Done() - cb.txn = t - if status == zcncore.StatusSuccess { - cb.success = true - } else { - cb.errMsg = t.GetVerifyError() - } -} - -func (cb *transactionCallback) OnAuthComplete(t *zcncore.Transaction, status int) { - cb.txn = t - fmt.Println("Authorization complete on zauth.", status) -} diff --git a/wasmsdk/bridge.go b/wasmsdk/bridge.go index af2d8f181..47bd7f6df 100644 --- a/wasmsdk/bridge.go +++ b/wasmsdk/bridge.go @@ -12,7 +12,6 @@ import ( "github.com/0chain/gosdk/zcnbridge" "github.com/0chain/gosdk/zcnbridge/errors" "github.com/0chain/gosdk/zcnbridge/log" - "github.com/0chain/gosdk/zcnbridge/transaction" "github.com/0chain/gosdk/zcnbridge/wallet" "github.com/0chain/gosdk/zcncore" "github.com/ethereum/go-ethereum/ethclient" @@ -47,8 +46,6 @@ func initBridge( return errors.New("wallet_error", err.Error()) } - transactionProvider := transaction.NewTransactionProvider() - keyStore := zcnbridge.NewKeyStore( path.Join(".", zcnbridge.EthereumWalletStorageDir)) @@ -63,7 +60,6 @@ func initBridge( gasLimit, consensusThreshold, ethereumClient, - transactionProvider, keyStore, ) @@ -73,17 +69,17 @@ func initBridge( // burnZCN Burns ZCN tokens and returns a hash of the burn transaction // - amount: amount of ZCN tokens to burn // - txnfee: transaction fee -func burnZCN(amount, txnfee uint64) string { //nolint +func burnZCN(amount uint64) string { //nolint if bridge == nil { return errors.New("burnZCN", "bridge is not initialized").Error() } - tx, err := bridge.BurnZCN(context.Background(), amount, txnfee) + hash, err := bridge.BurnZCN(amount) if err != nil { return errors.Wrap("burnZCN", "failed to burn ZCN tokens", err).Error() } - return tx.GetHash() + return hash } // mintZCN Mints ZCN tokens and returns a hash of the mint transaction diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index 04d225b67..ceaf2e4f8 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -292,10 +292,6 @@ func main() { "allocationRepair": allocationRepair, "repairSize": repairSize, - //smartcontract - "executeSmartContract": executeSmartContract, - "faucet": faucet, - // bridge "initBridge": initBridge, "burnZCN": burnZCN, diff --git a/wasmsdk/smartcontract.go b/wasmsdk/smartcontract.go deleted file mode 100644 index 582bea1c1..000000000 --- a/wasmsdk/smartcontract.go +++ /dev/null @@ -1,96 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "sync" - - "github.com/0chain/gosdk/core/transaction" - "github.com/0chain/gosdk/zcncore" -) - -func faucet(methodName, input string, token float64) (*transaction.Transaction, error) { - return executeSmartContract(zcncore.FaucetSmartContractAddress, methodName, input, zcncore.ConvertToValue(token)) -} - -// executeSmartContract issue a smart contract transaction -// - address is the smart contract address -// - methodName is the method name to be called -// - input is the input data for the method -// - value is the value to be sent with the transaction -func executeSmartContract(address, methodName, input string, value uint64) (*transaction.Transaction, error) { - wg := &sync.WaitGroup{} - cb := &transactionCallback{wg: wg} - txn, err := zcncore.NewTransaction(cb, 0, 0) - if err != nil { - return nil, err - } - - wg.Add(1) - t, err := txn.ExecuteSmartContract(address, methodName, json.RawMessage([]byte(input)), value) - if err != nil { - return nil, err - - } - - wg.Wait() - - if !cb.success { - return nil, fmt.Errorf("smartcontract: %s", cb.errMsg) - } - - cb.success = false - wg.Add(1) - err = txn.Verify() - if err != nil { - return nil, err - } - - wg.Wait() - - if !cb.success { - return nil, fmt.Errorf("smartcontract: %s", cb.errMsg) - } - - switch txn.GetVerifyConfirmationStatus() { - case zcncore.ChargeableError: - return nil, fmt.Errorf("smartcontract: %s", txn.GetVerifyOutput()) - case zcncore.Success: - return t, nil - } - - return nil, fmt.Errorf("smartcontract: %v", txn.GetVerifyConfirmationStatus()) -} - -type transactionCallback struct { - wg *sync.WaitGroup - success bool - errMsg string - - txn *zcncore.Transaction -} - -func (cb *transactionCallback) OnTransactionComplete(t *zcncore.Transaction, status int) { - defer cb.wg.Done() - cb.txn = t - if status == zcncore.StatusSuccess { - cb.success = true - } else { - cb.errMsg = t.GetTransactionError() - } -} - -func (cb *transactionCallback) OnVerifyComplete(t *zcncore.Transaction, status int) { - defer cb.wg.Done() - cb.txn = t - if status == zcncore.StatusSuccess { - cb.success = true - } else { - cb.errMsg = t.GetVerifyError() - } -} - -func (cb *transactionCallback) OnAuthComplete(t *zcncore.Transaction, status int) { - cb.txn = t - fmt.Println("Authorization complete on zauth.", status) -} diff --git a/zcnbridge/bridge.go b/zcnbridge/bridge.go index 3cbf6b866..de10e42d3 100644 --- a/zcnbridge/bridge.go +++ b/zcnbridge/bridge.go @@ -20,14 +20,13 @@ import ( "gopkg.in/natefinch/lumberjack.v2" "github.com/0chain/gosdk/core/logger" + coreTransaction "github.com/0chain/gosdk/core/transaction" "github.com/0chain/gosdk/zcnbridge/ethereum" "github.com/0chain/gosdk/zcnbridge/ethereum/authorizers" "github.com/0chain/gosdk/zcnbridge/ethereum/bridge" "github.com/0chain/gosdk/zcnbridge/ethereum/nftconfig" - "github.com/0chain/gosdk/zcnbridge/log" "github.com/0chain/gosdk/zcncore" - "github.com/0chain/gosdk/zcnbridge/transaction" "github.com/0chain/gosdk/zcnbridge/wallet" "github.com/0chain/gosdk/zcnbridge/zcnsc" eth "github.com/ethereum/go-ethereum" @@ -155,7 +154,7 @@ func (b *BridgeClient) RemoveEthereumAuthorizer(ctx context.Context, address com } // AddEthereumAuthorizers add bridge authorizers to the Ethereum authorizers contract -// - configDir - configuration directory +// - configDir - configuration directory func (b *BridgeClient) AddEthereumAuthorizers(configDir string) { cfg := viper.New() cfg.AddConfigPath(configDir) @@ -442,13 +441,6 @@ func (b *BridgeClient) GetTokenBalance() (*big.Int, error) { return wei, nil } -// VerifyZCNTransaction verifies 0CHain transaction -// - ctx go context instance to run the transaction -// - hash transaction hash -func (b *BridgeClient) VerifyZCNTransaction(ctx context.Context, hash string) (transaction.Transaction, error) { - return transaction.Verify(ctx, hash) -} - // SignWithEthereumChain signs the digest with Ethereum chain signer taking key from the current user key storage // - message message to sign func (b *BridgeClient) SignWithEthereumChain(message string) ([]byte, error) { @@ -631,23 +623,16 @@ func (b *BridgeClient) BurnWZCN(ctx context.Context, amountTokens uint64) (*type // - ctx go context instance to run the transaction // - payload received from authorizers func (b *BridgeClient) MintZCN(ctx context.Context, payload *zcnsc.MintPayload) (string, error) { - trx, err := b.transactionProvider.NewTransactionEntity(0) - if err != nil { - log.Logger.Fatal("failed to create new transaction", zap.Error(err)) - } - Logger.Info( "Starting MINT smart contract", zap.String("sc address", wallet.ZCNSCSmartContractAddress), zap.String("function", wallet.MintFunc), zap.Int64("mint amount", int64(payload.Amount))) - hash, err := trx.ExecuteSmartContract( - ctx, - wallet.ZCNSCSmartContractAddress, - wallet.MintFunc, - payload, - 0) + hash, _, _, _, err := coreTransaction.SmartContractTxn(wallet.ZCNSCSmartContractAddress, coreTransaction.SmartContractTxnData{ + Name: wallet.MintFunc, + InputArgs: payload, + }) if err != nil { return "", errors.Wrap(err, fmt.Sprintf("failed to execute smart contract, hash = %s", hash)) @@ -665,16 +650,10 @@ func (b *BridgeClient) MintZCN(ctx context.Context, payload *zcnsc.MintPayload) // - ctx go context instance to run the transaction // - amount amount of tokens to burn // - txnfee transaction fee -func (b *BridgeClient) BurnZCN(ctx context.Context, amount, txnfee uint64) (transaction.Transaction, error) { +func (b *BridgeClient) BurnZCN(amount uint64) (string, error) { payload := zcnsc.BurnPayload{ EthereumAddress: b.EthereumAddress, } - - trx, err := b.transactionProvider.NewTransactionEntity(txnfee) - if err != nil { - log.Logger.Fatal("failed to create new transaction", zap.Error(err)) - } - Logger.Info( "Starting BURN smart contract", zap.String("sc address", wallet.ZCNSCSmartContractAddress), @@ -682,23 +661,13 @@ func (b *BridgeClient) BurnZCN(ctx context.Context, amount, txnfee uint64) (tran zap.Uint64("burn amount", amount), ) - var hash string - hash, err = trx.ExecuteSmartContract( - ctx, - wallet.ZCNSCSmartContractAddress, - wallet.BurnFunc, - payload, - amount, - ) - + hash, _, _, _, err := coreTransaction.SmartContractTxn(wallet.ZCNSCSmartContractAddress, coreTransaction.SmartContractTxnData{ + Name: wallet.MintFunc, + InputArgs: payload, + }) if err != nil { Logger.Error("Burn ZCN transaction FAILED", zap.Error(err)) - return trx, errors.Wrap(err, fmt.Sprintf("failed to execute smart contract, hash = %s", hash)) - } - - err = trx.Verify(context.Background()) - if err != nil { - return trx, errors.Wrap(err, fmt.Sprintf("failed to verify smart contract, hash = %s", hash)) + return hash, errors.Wrap(err, fmt.Sprintf("failed to execute smart contract, hash = %s", hash)) } Logger.Info( @@ -708,7 +677,7 @@ func (b *BridgeClient) BurnZCN(ctx context.Context, amount, txnfee uint64) (tran zap.Uint64("amount", amount), ) - return trx, nil + return hash, nil } // ApproveUSDCSwap provides opportunity to approve swap operation for ERC20 tokens diff --git a/zcnbridge/bridge_test.go b/zcnbridge/bridge_test.go index 8718baffc..020d30a3d 100644 --- a/zcnbridge/bridge_test.go +++ b/zcnbridge/bridge_test.go @@ -436,7 +436,7 @@ func Test_ZCNBridge(t *testing.T) { }) t.Run("should check configuration used by BurnZCN", func(t *testing.T) { - _, err := bridgeClient.BurnZCN(context.Background(), amount, txnFee) + _, err := bridgeClient.BurnZCN(amount) require.NoError(t, err) require.True(t, tx.AssertCalled( diff --git a/zcnbridge/config.go b/zcnbridge/config.go index 09bae207a..a4e5ef87a 100644 --- a/zcnbridge/config.go +++ b/zcnbridge/config.go @@ -9,7 +9,6 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/0chain/gosdk/zcnbridge/log" - "github.com/0chain/gosdk/zcnbridge/transaction" "github.com/ethereum/go-ethereum/ethclient" "github.com/spf13/viper" @@ -49,9 +48,8 @@ type EthereumClient interface { // BridgeClient is a wrapper, which exposes Ethereum KeyStore methods used by DEX bridge. type BridgeClient struct { - keyStore KeyStore - transactionProvider transaction.TransactionProvider - ethereumClient EthereumClient + keyStore KeyStore + ethereumClient EthereumClient BridgeAddress, TokenAddress, @@ -93,21 +91,19 @@ func NewBridgeClient( gasLimit uint64, consensusThreshold float64, ethereumClient EthereumClient, - transactionProvider transaction.TransactionProvider, keyStore KeyStore) *BridgeClient { return &BridgeClient{ - BridgeAddress: bridgeAddress, - TokenAddress: tokenAddress, - AuthorizersAddress: authorizersAddress, - UniswapAddress: uniswapAddress, - EthereumAddress: ethereumAddress, - EthereumNodeURL: ethereumNodeURL, - Password: password, - GasLimit: gasLimit, - ConsensusThreshold: consensusThreshold, - ethereumClient: ethereumClient, - transactionProvider: transactionProvider, - keyStore: keyStore, + BridgeAddress: bridgeAddress, + TokenAddress: tokenAddress, + AuthorizersAddress: authorizersAddress, + UniswapAddress: uniswapAddress, + EthereumAddress: ethereumAddress, + EthereumNodeURL: ethereumNodeURL, + Password: password, + GasLimit: gasLimit, + ConsensusThreshold: consensusThreshold, + ethereumClient: ethereumClient, + keyStore: keyStore, } } @@ -148,8 +144,6 @@ func SetupBridgeClientSDK(cfg *BridgeSDKConfig) *BridgeClient { Logger.Error(err) } - transactionProvider := transaction.NewTransactionProvider() - homedir := path.Dir(chainCfg.ConfigFileUsed()) if homedir == "" { log.Logger.Fatal("err happened during home directory retrieval") @@ -168,7 +162,6 @@ func SetupBridgeClientSDK(cfg *BridgeSDKConfig) *BridgeClient { chainCfg.GetUint64("bridge.gas_limit"), chainCfg.GetFloat64("bridge.consensus_threshold"), ethereumClient, - transactionProvider, keyStore, ) } diff --git a/zcnbridge/transaction/functions.go b/zcnbridge/transaction/functions.go index 07d803e36..e2716f33d 100644 --- a/zcnbridge/transaction/functions.go +++ b/zcnbridge/transaction/functions.go @@ -2,62 +2,58 @@ package transaction // ZCNSC smart contract functions wrappers -import ( - "context" - - "github.com/0chain/gosdk/zcncore" -) - -// AddAuthorizer adds authorizer to the bridge -// - ctx is the context of the request. -// - input is the payload of the request. -func AddAuthorizer(ctx context.Context, input *zcncore.AddAuthorizerPayload) (Transaction, error) { - t, err := NewTransactionEntity(0) - if err != nil { - return nil, err - } - - scheme := t.GetScheme() - - err = scheme.ZCNSCAddAuthorizer(input) - if err != nil { - return t, err - } - - callBack := t.GetCallback() - - err = callBack.WaitCompleteCall(ctx) - t.SetHash(scheme.Hash()) - if err != nil { - return t, err - } - - return t, nil -} - -// AuthorizerHealthCheck performs health check of the authorizer -// - ctx is the context of the request. -// - input is the payload of the request. -func AuthorizerHealthCheck(ctx context.Context, input *zcncore.AuthorizerHealthCheckPayload) (Transaction, error) { - t, err := NewTransactionEntity(0) - if err != nil { - return nil, err - } - - scheme := t.GetScheme() - - err = scheme.ZCNSCAuthorizerHealthCheck(input) - if err != nil { - return t, err - } - - callBack := t.GetCallback() - - err = callBack.WaitCompleteCall(ctx) - t.SetHash(scheme.Hash()) - if err != nil { - return t, err - } - - return t, nil -} +//JAYASHTODO : Uncomment if possible or rewrite + +//// AddAuthorizer adds authorizer to the bridge +//// - ctx is the context of the request. +//// - input is the payload of the request. +//func AddAuthorizer(ctx context.Context, input *zcncore.AddAuthorizerPayload) (Transaction, error) { +// t, err := NewTransactionEntity(0) +// if err != nil { +// return nil, err +// } +// +// scheme := t.GetScheme() +// +// err = scheme.ZCNSCAddAuthorizer(input) +// if err != nil { +// return t, err +// } +// +// callBack := t.GetCallback() +// +// err = callBack.WaitCompleteCall(ctx) +// t.SetHash(scheme.Hash()) +// if err != nil { +// return t, err +// } +// +// return t, nil +//} +// +//// AuthorizerHealthCheck performs health check of the authorizer +//// - ctx is the context of the request. +//// - input is the payload of the request. +//func AuthorizerHealthCheck(ctx context.Context, input *zcncore.AuthorizerHealthCheckPayload) (Transaction, error) { +// t, err := NewTransactionEntity(0) +// if err != nil { +// return nil, err +// } +// +// scheme := t.GetScheme() +// +// err = scheme.ZCNSCAuthorizerHealthCheck(input) +// if err != nil { +// return t, err +// } +// +// callBack := t.GetCallback() +// +// err = callBack.WaitCompleteCall(ctx) +// t.SetHash(scheme.Hash()) +// if err != nil { +// return t, err +// } +// +// return t, nil +//} diff --git a/zcnbridge/transaction/txn.go b/zcnbridge/transaction/txn.go deleted file mode 100644 index 9d886f773..000000000 --- a/zcnbridge/transaction/txn.go +++ /dev/null @@ -1,210 +0,0 @@ -package transaction - -import ( - "context" - "encoding/json" - "fmt" - "strconv" - "strings" - - "github.com/0chain/gosdk/core/util" - "github.com/0chain/gosdk/zcnbridge/errors" - ctime "github.com/0chain/gosdk/zcnbridge/time" - "github.com/0chain/gosdk/zcncore" -) - -var ( - _ zcncore.TransactionCallback = (*callback)(nil) -) - -type ( - // TransactionProvider ... - TransactionProvider interface { - NewTransactionEntity(txnFee uint64) (Transaction, error) - } - - // transactionProvider ... - transactionProvider struct{} - - // Transaction interface describes transaction entity. - Transaction interface { - ExecuteSmartContract(ctx context.Context, address, funcName string, input interface{}, val uint64) (string, error) - Verify(ctx context.Context) error - GetScheme() zcncore.TransactionScheme - GetCallback() TransactionCallbackAwaitable - GetTransactionOutput() string - GetHash() string - SetHash(string) - } - - // TransactionEntity entity that encapsulates the transaction related data and metadata. - transactionEntity struct { - Hash string `json:"hash,omitempty"` - Version string `json:"version,omitempty"` - TransactionOutput string `json:"transaction_output,omitempty"` - scheme zcncore.TransactionScheme - callBack TransactionCallbackAwaitable - } -) - -type ( - verifyOutput struct { - Confirmation confirmation `json:"confirmation"` - } - - // confirmation represents the acceptance that a transaction is included into the blockchain. - confirmation struct { - Version string `json:"version"` - Hash string `json:"hash"` - BlockHash string `json:"block_hash"` - PreviousBlockHash string `json:"previous_block_hash"` - Transaction *transactionEntity `json:"txn,omitempty"` - CreationDate ctime.Timestamp `json:"creation_date"` - MinerID string `json:"miner_id"` - Round int64 `json:"round"` - Status int `json:"transaction_status"` - RoundRandomSeed int64 `json:"round_random_seed"` - MerkleTreeRoot string `json:"merkle_tree_root"` - MerkleTreePath *util.MTPath `json:"merkle_tree_path"` - ReceiptMerkleTreeRoot string `json:"receipt_merkle_tree_root"` - ReceiptMerkleTreePath *util.MTPath `json:"receipt_merkle_tree_path"` - } -) - -func NewTransactionProvider() TransactionProvider { - return &transactionProvider{} -} - -func (t *transactionProvider) NewTransactionEntity(txnFee uint64) (Transaction, error) { - return NewTransactionEntity(txnFee) -} - -// NewTransactionEntity creates Transaction with initialized fields. -// Sets version, client ID, creation date, public key and creates internal zcncore.TransactionScheme. -func NewTransactionEntity(txnFee uint64) (Transaction, error) { - txn := &transactionEntity{ - callBack: NewStatus().(*callback), - } - zcntxn, err := zcncore.NewTransaction(txn.callBack, txnFee, 0) - if err != nil { - return nil, err - } - - txn.scheme = zcntxn - - return txn, nil -} - -// ExecuteSmartContract executes function of smart contract with provided address. -// -// Returns hash of executed transaction. -func (t *transactionEntity) ExecuteSmartContract(ctx context.Context, address, funcName string, input interface{}, - val uint64) (string, error) { - const errCode = "transaction_send" - - tran, err := t.scheme.ExecuteSmartContract(address, funcName, input, val) - t.Hash = tran.Hash - - if err != nil { - msg := fmt.Sprintf("error while sending txn: %v", err) - return "", errors.New(errCode, msg) - } - - if err := t.callBack.WaitCompleteCall(ctx); err != nil { - msg := fmt.Sprintf("error while sending txn: %v", err) - return "", errors.New(errCode, msg) - } - - if len(t.scheme.GetTransactionError()) > 0 { - return "", errors.New(errCode, t.scheme.GetTransactionError()) - } - - return t.scheme.Hash(), nil -} - -func (t *transactionEntity) Verify(ctx context.Context) error { - const errCode = "transaction_verify" - - err := t.scheme.Verify() - if err != nil { - msg := fmt.Sprintf("error while verifying txn: %v; txn hash: %s", err, t.scheme.GetTransactionHash()) - return errors.New(errCode, msg) - } - - if err := t.callBack.WaitVerifyCall(ctx); err != nil { - msg := fmt.Sprintf("error while verifying txn: %v; txn hash: %s", err, t.scheme.GetTransactionHash()) - return errors.New(errCode, msg) - } - - switch t.scheme.GetVerifyConfirmationStatus() { - case zcncore.ChargeableError: - return errors.New(errCode, strings.Trim(t.scheme.GetVerifyOutput(), "\"")) - case zcncore.Success: - fmt.Println("Executed smart contract successfully with txn: ", t.scheme.GetTransactionHash()) - default: - msg := fmt.Sprint("\nExecute smart contract failed. Unknown status code: " + - strconv.Itoa(int(t.scheme.GetVerifyConfirmationStatus()))) - return errors.New(errCode, msg) - } - - vo := new(verifyOutput) - if err := json.Unmarshal([]byte(t.scheme.GetVerifyOutput()), vo); err != nil { - return errors.New(errCode, "error while unmarshalling confirmation: "+err.Error()+", json: "+t.scheme.GetVerifyOutput()) - } - - if vo.Confirmation.Transaction != nil { - t.Hash = vo.Confirmation.Transaction.GetHash() - t.TransactionOutput = vo.Confirmation.Transaction.GetTransactionOutput() - } else { - return errors.New(errCode, "got invalid confirmation (missing transaction)") - } - - return nil -} - -// GetSheme returns transaction scheme -func (t *transactionEntity) GetScheme() zcncore.TransactionScheme { - return t.scheme -} - -// GetHash returns transaction hash -func (t *transactionEntity) GetHash() string { - return t.Hash -} - -// SetHash sets transaction hash -func (t *transactionEntity) SetHash(hash string) { - t.Hash = hash -} - -// GetTransactionOutput returns transaction output -func (t *transactionEntity) GetTransactionOutput() string { - return t.TransactionOutput -} - -func (t *transactionEntity) GetCallback() TransactionCallbackAwaitable { - return t.callBack -} - -// GetVersion returns transaction version -func (t *transactionEntity) GetVersion() string { - return t.Version -} - -// Verify checks including of transaction in the blockchain. -func Verify(ctx context.Context, hash string) (Transaction, error) { - t, err := NewTransactionEntity(0) - if err != nil { - return nil, err - } - - scheme := t.GetScheme() - - if err := scheme.SetTransactionHash(hash); err != nil { - return nil, err - } - - err = t.Verify(ctx) - - return t, err -} diff --git a/zcncore/functions.go b/zcncore/functions.go new file mode 100644 index 000000000..6a368ef9b --- /dev/null +++ b/zcncore/functions.go @@ -0,0 +1,12 @@ +package zcncore + +import "github.com/0chain/gosdk/core/transaction" + +func StorageScUpdateConfig(input string) (err error) { + _, _, _, _, err = transaction.SmartContractTxn(StorageSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.STORAGESC_UPDATE_SETTINGS, + InputArgs: input, + }) + + return err +} diff --git a/zcncore/sample/0Wallet.go b/zcncore/sample/0Wallet.go deleted file mode 100644 index f903fc966..000000000 --- a/zcncore/sample/0Wallet.go +++ /dev/null @@ -1,261 +0,0 @@ -//go:build ignore -// +build ignore - -// Sample usage of the Wallet SDK - do not use. -package main - -import ( - "encoding/json" - "flag" - "fmt" - "sync" - - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/zcncore" -) - -type StatusUI struct { - i int - wg sync.WaitGroup - txnout map[string]json.RawMessage -} - -// const ChainConfig = `{ -// "miners": [ -// "http://localhost:7071", -// "http://localhost:7072", -// "http://localhost:7073" -// ], -// "sharders": [ -// "http://localhost:7171" -// ], -// "signaturescheme": "bls0chain" -// }` - -const ChainConfig = `{ - "miners": [ - "http://ohio.devi.testnet-0chain.net:7071", - "http://ohio.devi.testnet-0chain.net:7072", - "http://ohio.devi.testnet-0chain.net:7073", - "http://ohio.devi.testnet-0chain.net:7074" - ], - "sharders": [ - "http://ohio.devi.testnet-0chain.net:7171" - ], - "signaturescheme": "bls0chain" -}` - -var wallet = ` -{"client_id":"0bc96a0980170045863d826f9eb579d8144013210602e88426408e9f83c236f6", -"client_key":"a4e58c66b072d27288b650db9a476fe66a1a4f69e0f8fb11499f9ec3a579e21e5dc0298b8c5ae5baa205730d06bc04b07a31943ab3bd620e8427c15d5c413b9e", -"keys":[ - { - "public_key":"a4e58c66b072d27288b650db9a476fe66a1a4f69e0f8fb11499f9ec3a579e21e5dc0298b8c5ae5baa205730d06bc04b07a31943ab3bd620e8427c15d5c413b9e", - "private_key":"c0f3a3100241888ea9c2cc5c7300e3e510a8e7190c2c20b03f80e3937a91530d" - }], -"mnemonics":"snake mixed bird cream cotton trouble small fee finger catalog measure spoon private second canal pact unable close predict dream mask delay path inflict", -"version":"1.0", -"date_created":"2019-06-19 13:37:50.466889 -0700 PDT m=+0.023873276"}` - -func (s *StatusUI) OnWalletCreateComplete(status int, w string, err string) { - defer s.wg.Done() - if status == zcncore.StatusError { - fmt.Println("Error: ", err) - } else { - fmt.Println("Wallet:", w) - } -} - -func (s *StatusUI) OnTransactionComplete(t *zcncore.Transaction, status int) { - defer s.wg.Done() - fmt.Println("========== TxnCompleted Status: ", status, "=======") - fmt.Println(" Txn Hash:", t.GetTransactionHash()) -} - -func (s *StatusUI) OnVerifyComplete(t *zcncore.Transaction, status int) { - defer s.wg.Done() - fmt.Println("========== VerifyCompleted Status: ", status, "=======") - fmt.Println(t.GetVerifyOutput()) -} - -func (s *StatusUI) OnBalanceAvailable(status int, value int64) { - defer s.wg.Done() - fmt.Println("=========== Balance Status:", status, "Value:", value, - "Token:", zcncore.ConvertToToken(value), - "Value:", zcncore.ConvertToValue(zcncore.ConvertToToken(value))) -} - -func (zcn *StatusUI) OnAuthComplete(t *zcncore.Transaction, status int) { - fmt.Println("Authorization complete on zauth.", status) -} - -func main() { - var cmd string - flag.StringVar(&cmd, "cmd", "", "create|recover|validate|send|store|faucet|getbalance|verify") - - var mnemonic string - flag.StringVar(&mnemonic, "mnemonic", "", "Mnemonic used for wallet creation.\nMandatory for -cmd recover") - - var value uint64 - flag.Uint64Var(&value, "value", 0, "Value to send") - - var txnhash string - flag.StringVar(&txnhash, "txnhash", "", "Transaction hash to verify.\nMandatory for -cmd verify") - - var txndata string - flag.StringVar(&txndata, "txndata", "", "Data to store.\nMandatory for -cmd store") - - var toclientID string - flag.StringVar(&toclientID, "toclientID", "", "Receipient client ID.\nMandatory for -cmd send") - - flag.Parse() - - switch cmd { - case "create": - case "faucet": - case "getbalance": - case "recover": - fallthrough - case "validate": - if mnemonic == "" { - flag.Usage() - return - } - case "send": - if value == 0 && toclientID == "" { - flag.Usage() - return - } - case "verify": - if txnhash == "" { - flag.Usage() - return - } - case "store": - if txndata == "" { - flag.Usage() - return - } - default: - fmt.Println("Unsupported command:", cmd) - flag.Usage() - return - } - - cfg := conf.Config{} - if err := json.Unmarshal([]byte(ChainConfig), &cfg); err != nil { - return err - } - if err := client.Init(context.Background(), cfg); err != nil { - return err - } - - err = zcncore.SetWalletInfo(wallet, false) - if err != nil { - fmt.Println("set wallet info failed: ", err) - return - } - - s := &StatusUI{i: 1} - switch cmd { - case "create": - s.wg.Add(1) - err = zcncore.CreateWallet(s) - if err != nil { - fmt.Printf("Error create wallet: %v\n", err) - } - s.wg.Wait() - case "recover": - s.wg.Add(1) - err = zcncore.RecoverWallet(mnemonic, s) - if err != nil { - fmt.Printf("Error recover wallet %v\n", err) - } - s.wg.Wait() - case "validate": - ok := zcncore.IsMnemonicValid(mnemonic) - if ok != true { - fmt.Println("Validate mnemonic failed") - return - } - fmt.Println("**** Mnemonic is Valid ****") - case "send": - txn, err := zcncore.NewTransaction(s, 0, 0) - if err != nil { - fmt.Println(err) - return - } - s.wg.Add(1) - err = txn.Send(toclientID, value, "From 0Wallet sample app") - if err != nil { - fmt.Println("send failed: ", err) - return - } - s.wg.Wait() - s.wg.Add(1) - txn.Verify() - s.wg.Wait() - case "store": - txn, err := zcncore.NewTransaction(s, 0, 0) - if err != nil { - fmt.Println(err) - return - } - s.wg.Add(1) - err = txn.StoreData(txndata) - if err != nil { - fmt.Printf("store data failed: %v\n", err) - return - } - s.wg.Wait() - s.wg.Add(1) - txn.Verify() - s.wg.Wait() - case "faucet": - txn, err := zcncore.NewTransaction(s, 0, 0) - if err != nil { - fmt.Println(err) - return - } - s.wg.Add(1) - faucetAddress := "faucet smart contract address" - methodName := "pour" - jsonInput := "{}" - value = 0 - err = txn.ExecuteSmartContract(faucetAddress, methodName, jsonInput, value) - if err != nil { - fmt.Printf("execute faucet smart contract failed: %v\n", err) - return - } - s.wg.Wait() - s.wg.Add(1) - txn.Verify() - s.wg.Wait() - case "getbalance": - s.wg.Add(1) - err = zcncore.GetBalance(nil) - if err != nil { - fmt.Println("get balance failed: ", err) - return - } - s.wg.Wait() - case "getnonce": - s.wg.Add(1) - err = zcncore.GetNonce(nil) - if err != nil { - fmt.Println("get balance failed: ", err) - return - } - s.wg.Wait() - case "verify": - txn, err := zcncore.NewTransaction(s, 0, 0) - if err != nil { - fmt.Println(err) - return - } - txn.SetTransactionHash(txnhash) - s.wg.Add(1) - txn.Verify() - s.wg.Wait() - } -} diff --git a/zcncore/transaction_base.go b/zcncore/transaction_base.go index 0c7163d64..a9711be27 100644 --- a/zcncore/transaction_base.go +++ b/zcncore/transaction_base.go @@ -320,6 +320,8 @@ func (g getNonceCallBack) OnNonceAvailable(status int, nonce int64, info string) func (t *Transaction) setNonceAndSubmit() { t.setNonce() + + //hash, _, nonce, _, err := transaction.SmartContractTxnValueFeeWithRetry(MinerSmartContractAddress, t.txn, 0, client.TxnFee()) t.submitTxn() } From ef8a452a46110af8542b8549f62318cf1e9486d3 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 8 Sep 2024 23:38:38 +0530 Subject: [PATCH 040/319] Debug --- zcncore/functions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zcncore/functions.go b/zcncore/functions.go index 6a368ef9b..a8dff3e72 100644 --- a/zcncore/functions.go +++ b/zcncore/functions.go @@ -2,7 +2,7 @@ package zcncore import "github.com/0chain/gosdk/core/transaction" -func StorageScUpdateConfig(input string) (err error) { +func StorageScUpdateConfig(input interface{}) (err error) { _, _, _, _, err = transaction.SmartContractTxn(StorageSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.STORAGESC_UPDATE_SETTINGS, InputArgs: input, From 109905cb04f06b33fc82f3b233756b2e42e1c55c Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Tue, 10 Sep 2024 13:45:24 +0530 Subject: [PATCH 041/319] add storage version for allocation --- zboxcore/blockchain/entity.go | 5 +++-- zboxcore/sdk/allocation.go | 3 +++ zboxcore/sdk/multi_operation_worker.go | 15 ++++++++++++++- zboxcore/sdk/rollback.go | 4 ++++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/zboxcore/blockchain/entity.go b/zboxcore/blockchain/entity.go index ecfd5c5d8..8ea74175a 100644 --- a/zboxcore/blockchain/entity.go +++ b/zboxcore/blockchain/entity.go @@ -84,8 +84,9 @@ type UpdateValidationNode struct { // StorageNode represents a storage node (blobber) type StorageNode struct { - ID string `json:"id"` - Baseurl string `json:"url"` + ID string `json:"id"` + Baseurl string `json:"url"` + AllocationRoot string `json:"allocation_root"` skip uint64 `json:"-"` // skip on error } diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index bbbb13277..6ddf96543 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -273,6 +273,8 @@ type Allocation struct { IsEnterprise bool `json:"is_enterprise"` + StorageVersion int `json:"storage_version"` + // FileOptions to define file restrictions on an allocation for third-parties // default 00000000 for all crud operations suggesting only owner has the below listed abilities. // enabling option/s allows any third party to perform certain ops @@ -301,6 +303,7 @@ type Allocation struct { consensusThreshold int fullconsensus int sig string `json:"-"` + allocationRoot string `json:"-"` } // OperationRequest represents an operation request with its related options. diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 7361508c6..4b50baefd 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -27,6 +27,7 @@ import ( const ( DefaultCreateConnectionTimeOut = 45 * time.Second + StorageV2 = 1 ) var BatchSize = 6 @@ -275,11 +276,23 @@ func (mo *MultiOperation) Process() error { } logger.Logger.Debug("[checkAllocStatus]", time.Since(start).Milliseconds()) mo.Consensus.Reset() + var pos uint64 + if !mo.isRepair && mo.allocationObj.StorageVersion == StorageV2 { + for i := mo.operationMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + if mo.allocationObj.Blobbers[pos].AllocationRoot != mo.allocationObj.allocationRoot { + mo.operationMask = mo.operationMask.And(zboxutil.NewUint128(1).Lsh(pos).Not()) + } + } + } + pos = 0 activeBlobbers := mo.operationMask.CountOnes() + if activeBlobbers < mo.consensusThresh { + return errors.New("consensus_not_met", fmt.Sprintf("Active blobbers %d is less than consensus threshold %d", activeBlobbers, mo.consensusThresh)) + } commitReqs := make([]*CommitRequest, activeBlobbers) start = time.Now() wg.Add(activeBlobbers) - var pos uint64 var counter = 0 timestamp := int64(common.Now()) for i := mo.operationMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { diff --git a/zboxcore/sdk/rollback.go b/zboxcore/sdk/rollback.go index 3a2a8485d..98f56a693 100644 --- a/zboxcore/sdk/rollback.go +++ b/zboxcore/sdk/rollback.go @@ -286,6 +286,9 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { commitResult: &CommitResult{}, blobIndex: ind, } + if wr.LatestWM != nil { + blobber.AllocationRoot = wr.LatestWM.AllocationRoot + } } blobberRes[ind] = blobStatus }(blobber, ind) @@ -337,6 +340,7 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { } if len(versionMap[latestVersion]) > req || len(versionMap[prevVersion]) > req { + a.allocationRoot = versionMap[latestVersion][0].lpm.LatestWM.AllocationRoot return Commit, blobberRes, nil } From 52c12231a0d623966461632e4a16e65ca38d498b Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Tue, 10 Sep 2024 15:50:37 +0530 Subject: [PATCH 042/319] Replace transactions with functions --- zcncore/functions.go | 94 ++++++++++++++++++++++++++++++++++++- zcncore/transaction_base.go | 8 ---- 2 files changed, 93 insertions(+), 9 deletions(-) diff --git a/zcncore/functions.go b/zcncore/functions.go index a8dff3e72..a37fd2144 100644 --- a/zcncore/functions.go +++ b/zcncore/functions.go @@ -1,6 +1,98 @@ package zcncore -import "github.com/0chain/gosdk/core/transaction" +import ( + "fmt" + "github.com/0chain/gosdk/core/transaction" +) + +func MinerSCLock(providerId string, providerType Provider, lock uint64) error { + _, _, _, _, err := transaction.SmartContractTxnValue(MinerSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.MINERSC_LOCK, + InputArgs: &stakePoolRequest{ + ProviderID: providerId, + ProviderType: providerType, + }, + }, lock) + + return err +} + +func MinerSCUnlock(providerId string, providerType Provider) error { + _, _, _, _, err := transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.MINERSC_UNLOCK, + InputArgs: &stakePoolRequest{ + ProviderID: providerId, + ProviderType: providerType, + }, + }) + + return err +} + +func MinerSCCollectReward(providerId string, providerType Provider) error { + _, _, _, _, err := transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.MINERSC_COLLECT_REWARD, + InputArgs: &scCollectReward{ + ProviderId: providerId, + ProviderType: int(providerType), + }, + }) + + return err +} + +func MinerSCKill(providerId string, providerType Provider) error { + pr := &scCollectReward{ + ProviderId: providerId, + ProviderType: int(providerType), + } + var name string + switch providerType { + case ProviderMiner: + name = transaction.MINERSC_KILL_MINER + case ProviderSharder: + name = transaction.MINERSC_KILL_SHARDER + default: + return fmt.Errorf("kill provider type %v not implimented", providerType) + } + + _, _, _, _, err := transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ + Name: name, + InputArgs: pr, + }) + + return err +} + +func StorageSCCollectReward(providerId string, providerType Provider) error { + _, _, _, _, err := transaction.SmartContractTxn(StorageSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.STORAGESC_COLLECT_REWARD, + InputArgs: &scCollectReward{ + ProviderId: providerId, + ProviderType: int(providerType), + }, + }) + + return err +} + +func MinerScUpdateConfig(input interface{}) (err error) { + _, _, _, _, err = transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.MINERSC_UPDATE_SETTINGS, + InputArgs: input, + }) + + return err +} + +func MinerScUpdateGlobals(input interface{}) (err error) { + _, _, _, _, err = transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.MINERSC_UPDATE_GLOBALS, + InputArgs: input, + }) + + return err +} func StorageScUpdateConfig(input interface{}) (err error) { _, _, _, _, err = transaction.SmartContractTxn(StorageSmartContractAddress, transaction.SmartContractTxnData{ diff --git a/zcncore/transaction_base.go b/zcncore/transaction_base.go index a9711be27..4bf4de5ec 100644 --- a/zcncore/transaction_base.go +++ b/zcncore/transaction_base.go @@ -880,14 +880,6 @@ type scCollectReward struct { ProviderType int `json:"provider_type"` } -type MinerSCLock struct { - ID string `json:"id"` -} - -type MinerSCUnlock struct { - ID string `json:"id"` -} - func VerifyContentHash(metaTxnDataJSON string) (bool, error) { var metaTxnData sdk.CommitMetaResponse err := json.Unmarshal([]byte(metaTxnDataJSON), &metaTxnData) From 7f42b4817cbac723df02051482006658e625ee59 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Tue, 10 Sep 2024 15:56:32 +0530 Subject: [PATCH 043/319] Remove vesting --- core/transaction/entity.go | 8 --- zcncore/mocks/TransactionCommon.go | 28 -------- zcncore/mocks/TransactionScheme.go | 84 ------------------------ zcncore/transaction.go | 102 ----------------------------- zcncore/transaction_base.go | 73 --------------------- zcncore/transaction_mobile.go | 61 ----------------- zcncore/transactionauth.go | 24 ------- zcncore/transactionauth_base.go | 46 ------------- zcncore/transactionauth_mobile.go | 27 -------- zcncore/wallet_base.go | 12 ---- 10 files changed, 465 deletions(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 19faca165..49b186818 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -125,14 +125,6 @@ const ( ADD_FREE_ALLOCATION_ASSIGNER = "add_free_storage_assigner" - // Vesting SC - VESTING_TRIGGER = "trigger" - VESTING_STOP = "stop" - VESTING_UNLOCK = "unlock" - VESTING_ADD = "add" - VESTING_DELETE = "delete" - VESTING_UPDATE_SETTINGS = "vestingsc-update-settings" - // Storage SC STORAGESC_FINALIZE_ALLOCATION = "finalize_allocation" STORAGESC_CANCEL_ALLOCATION = "cancel_allocation" diff --git a/zcncore/mocks/TransactionCommon.go b/zcncore/mocks/TransactionCommon.go index ad0236cd7..a421df0cf 100644 --- a/zcncore/mocks/TransactionCommon.go +++ b/zcncore/mocks/TransactionCommon.go @@ -439,34 +439,6 @@ func (_m *TransactionCommon) UpdateValidatorSettings(validator *zcncore.Validato return r0 } -// VestingAdd provides a mock function with given fields: ar, value -func (_m *TransactionCommon) VestingAdd(ar *zcncore.VestingAddRequest, value uint64) error { - ret := _m.Called(ar, value) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.VestingAddRequest, uint64) error); ok { - r0 = rf(ar, value) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// VestingUpdateConfig provides a mock function with given fields: _a0 -func (_m *TransactionCommon) VestingUpdateConfig(_a0 *zcncore.InputMap) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.InputMap) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - // WritePoolLock provides a mock function with given fields: allocID, blobberID, duration, lock func (_m *TransactionCommon) WritePoolLock(allocID string, blobberID string, duration int64, lock uint64) error { ret := _m.Called(allocID, blobberID, duration, lock) diff --git a/zcncore/mocks/TransactionScheme.go b/zcncore/mocks/TransactionScheme.go index eca637f4b..371074d8c 100644 --- a/zcncore/mocks/TransactionScheme.go +++ b/zcncore/mocks/TransactionScheme.go @@ -623,90 +623,6 @@ func (_m *TransactionScheme) Verify() error { return r0 } -// VestingAdd provides a mock function with given fields: ar, value -func (_m *TransactionScheme) VestingAdd(ar *zcncore.VestingAddRequest, value uint64) error { - ret := _m.Called(ar, value) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.VestingAddRequest, uint64) error); ok { - r0 = rf(ar, value) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// VestingDelete provides a mock function with given fields: poolID -func (_m *TransactionScheme) VestingDelete(poolID string) error { - ret := _m.Called(poolID) - - var r0 error - if rf, ok := ret.Get(0).(func(string) error); ok { - r0 = rf(poolID) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// VestingStop provides a mock function with given fields: sr -func (_m *TransactionScheme) VestingStop(sr *zcncore.VestingStopRequest) error { - ret := _m.Called(sr) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.VestingStopRequest) error); ok { - r0 = rf(sr) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// VestingTrigger provides a mock function with given fields: poolID -func (_m *TransactionScheme) VestingTrigger(poolID string) error { - ret := _m.Called(poolID) - - var r0 error - if rf, ok := ret.Get(0).(func(string) error); ok { - r0 = rf(poolID) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// VestingUnlock provides a mock function with given fields: poolID -func (_m *TransactionScheme) VestingUnlock(poolID string) error { - ret := _m.Called(poolID) - - var r0 error - if rf, ok := ret.Get(0).(func(string) error); ok { - r0 = rf(poolID) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// VestingUpdateConfig provides a mock function with given fields: _a0 -func (_m *TransactionScheme) VestingUpdateConfig(_a0 *zcncore.InputMap) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.InputMap) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - // WritePoolLock provides a mock function with given fields: allocID, blobberID, duration, lock func (_m *TransactionScheme) WritePoolLock(allocID string, blobberID string, duration int64, lock uint64) error { ret := _m.Called(allocID, blobberID, duration, lock) diff --git a/zcncore/transaction.go b/zcncore/transaction.go index 5c4088492..e0bb38f40 100644 --- a/zcncore/transaction.go +++ b/zcncore/transaction.go @@ -77,14 +77,6 @@ type MinerSCNodes struct { Nodes []Node `json:"Nodes"` } -type VestingSCConfig struct { - MinLock common.Balance `json:"min_lock"` - MinDuration time.Duration `json:"min_duration"` - MaxDuration time.Duration `json:"max_duration"` - MaxDestinations int `json:"max_destinations"` - MaxDescriptionLength int `json:"max_description_length"` -} - type DelegatePool struct { Balance int64 `json:"balance"` Reward int64 `json:"reward"` @@ -127,8 +119,6 @@ type TransactionCommon interface { //RegisterMultiSig registers a group wallet and subwallets with MultisigSC RegisterMultiSig(walletstr, mswallet string) error - VestingAdd(ar *VestingAddRequest, value uint64) error - MinerSCLock(providerId string, providerType Provider, lock uint64) error MinerSCUnlock(providerId string, providerType Provider) error MinerSCCollectReward(providerID string, providerType Provider) error @@ -136,7 +126,6 @@ type TransactionCommon interface { StorageSCCollectReward(providerID string, providerType Provider) error - VestingUpdateConfig(*InputMap) error MinerScUpdateConfig(*InputMap) error MinerScUpdateGlobals(*InputMap) error StorageScUpdateConfig(*InputMap) error @@ -349,31 +338,6 @@ func (t *Transaction) SendWithSignatureHash(toClientID string, val uint64, desc return nil } -type VestingDest struct { - ID string `json:"id"` // destination ID - Amount common.Balance `json:"amount"` // amount to vest for the destination -} - -type VestingAddRequest struct { - Description string `json:"description"` // allow empty - StartTime common.Timestamp `json:"start_time"` // - Duration time.Duration `json:"duration"` // - Destinations []*VestingDest `json:"destinations"` // -} - -func (t *Transaction) VestingAdd(ar *VestingAddRequest, value uint64) ( - err error) { - - err = t.createSmartContractTxn(VestingSmartContractAddress, - transaction.VESTING_ADD, ar, value) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - func (t *Transaction) MinerSCLock(providerId string, providerType Provider, lock uint64) error { if lock > math.MaxInt64 { return errors.New("invalid_lock", "int64 overflow on lock value") @@ -446,18 +410,6 @@ func (t *Transaction) MinerSCKill(providerId string, providerType Provider) erro return err } -func (t *Transaction) VestingUpdateConfig(vscc *InputMap) (err error) { - - err = t.createSmartContractTxn(VestingSmartContractAddress, - transaction.VESTING_UPDATE_SETTINGS, vscc, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - // faucet smart contract func (t *Transaction) FaucetUpdateConfig(ip *InputMap) (err error) { @@ -1218,60 +1170,6 @@ func (t *Transaction) ZCNSCCollectReward(providerId string, providerType Provide return err } -type VestingClientList struct { - Pools []common.Key `json:"pools"` -} - -func GetVestingClientList(clientID string, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - if clientID == "" { - clientID = client.ClientID() // if not blank - } - go GetInfoFromSharders(WithParams(GET_VESTING_CLIENT_POOLS, Params{ - "client_id": clientID, - }), 0, cb) - return -} - -type VestingDestInfo struct { - ID common.Key `json:"id"` // identifier - Wanted common.Balance `json:"wanted"` // wanted amount for entire period - Earned common.Balance `json:"earned"` // can unlock - Vested common.Balance `json:"vested"` // already vested - Last common.Timestamp `json:"last"` // last time unlocked -} - -type VestingPoolInfo struct { - ID common.Key `json:"pool_id"` // pool ID - Balance common.Balance `json:"balance"` // real pool balance - Left common.Balance `json:"left"` // owner can unlock - Description string `json:"description"` // description - StartTime common.Timestamp `json:"start_time"` // from - ExpireAt common.Timestamp `json:"expire_at"` // until - Destinations []*VestingDestInfo `json:"destinations"` // receivers - ClientID common.Key `json:"client_id"` // owner -} - -func GetVestingPoolInfo(poolID string, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - GetInfoFromSharders(WithParams(GET_VESTING_POOL_INFO, Params{ - "pool_id": poolID, - }), 0, cb) - return -} - -func GetVestingSCConfig(cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - go GetInfoFromSharders(GET_VESTING_CONFIG, 0, cb) - return -} - // faucet func GetFaucetSCConfig(cb GetInfoCallback) (err error) { diff --git a/zcncore/transaction_base.go b/zcncore/transaction_base.go index 4bf4de5ec..d4b6ee497 100644 --- a/zcncore/transaction_base.go +++ b/zcncore/transaction_base.go @@ -11,7 +11,6 @@ import ( "github.com/0chain/errors" "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/node" "github.com/0chain/gosdk/core/sys" @@ -74,13 +73,6 @@ type TransactionScheme interface { // Hash Transaction status regardless of status Hash() string - // Vesting SC - - VestingTrigger(poolID string) error - VestingStop(sr *VestingStopRequest) error - VestingUnlock(poolID string) error - VestingDelete(poolID string) error - // Miner SC } @@ -810,71 +802,6 @@ func (t *Transaction) GetTransactionNonce() int64 { return t.txn.TransactionNonce } -// ========================================================================== // -// vesting pool // -// ========================================================================== // - -type vestingRequest struct { - PoolID common.Key `json:"pool_id"` -} - -func (t *Transaction) vestingPoolTxn(function string, poolID string, - value uint64) error { - - return t.createSmartContractTxn(VestingSmartContractAddress, - function, vestingRequest{PoolID: common.Key(poolID)}, value) -} - -func (t *Transaction) VestingTrigger(poolID string) (err error) { - - err = t.vestingPoolTxn(transaction.VESTING_TRIGGER, poolID, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -type VestingStopRequest struct { - PoolID string `json:"pool_id"` - Destination string `json:"destination"` -} - -func (t *Transaction) VestingStop(sr *VestingStopRequest) (err error) { - - err = t.createSmartContractTxn(VestingSmartContractAddress, - transaction.VESTING_STOP, sr, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -func (t *Transaction) VestingUnlock(poolID string) (err error) { - - err = t.vestingPoolTxn(transaction.VESTING_UNLOCK, poolID, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -func (t *Transaction) VestingDelete(poolID string) (err error) { - - err = t.vestingPoolTxn(transaction.VESTING_DELETE, poolID, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - type scCollectReward struct { ProviderId string `json:"provider_id"` ProviderType int `json:"provider_type"` diff --git a/zcncore/transaction_mobile.go b/zcncore/transaction_mobile.go index 49ac0ba5b..f4e80b649 100644 --- a/zcncore/transaction_mobile.go +++ b/zcncore/transaction_mobile.go @@ -53,14 +53,11 @@ type TransactionCommon interface { // Send implements sending token to a given clientid Send(toClientID string, val uint64, desc string) error - VestingAdd(ar VestingAddRequest, value string) error - MinerSCLock(providerId string, providerType int, lock string) error MinerSCUnlock(providerId string, providerType int) error MinerSCCollectReward(providerId string, providerType int) error StorageSCCollectReward(providerId string, providerType int) error - VestingUpdateConfig(InputMap) error MinerScUpdateConfig(InputMap) error MinerScUpdateGlobals(InputMap) error StorageScUpdateConfig(InputMap) error @@ -261,34 +258,6 @@ type AuthorizerConfig struct { Fee int64 `json:"fee"` } -type VestingDest struct { - ID string `json:"id"` // destination ID - Amount int64 `json:"amount"` // amount to vest for the destination -} - -type VestingAddRequest interface { - AddDestinations(id string, amount int64) -} - -func NewVestingAddRequest(desc string, startTime int64, duration int64) VestingAddRequest { - return &vestingAddRequest{ - Description: desc, - StartTime: startTime, - Duration: duration, - } -} - -type vestingAddRequest struct { - Description string `json:"description"` // allow empty - StartTime int64 `json:"start_time"` // - Duration int64 `json:"duration"` // - Destinations []*VestingDest `json:"destinations"` // -} - -func (vr *vestingAddRequest) AddDestinations(id string, amount int64) { - vr.Destinations = append(vr.Destinations, &VestingDest{ID: id, Amount: amount}) -} - // InputMap represents an interface of functions to add fields to a map. type InputMap interface { // AddField adds a field to the map. @@ -414,24 +383,6 @@ func (t *Transaction) SendWithSignatureHash(toClientID string, val string, desc return nil } -func (t *Transaction) VestingAdd(ar VestingAddRequest, value string) ( - err error) { - - v, err := parseCoinStr(value) - if err != nil { - return err - } - - err = t.createSmartContractTxn(VestingSmartContractAddress, - transaction.VESTING_ADD, ar, v) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - func (t *Transaction) MinerSCLock(providerId string, providerType int, lock string) error { lv, err := parseCoinStr(lock) @@ -499,18 +450,6 @@ func (t *Transaction) StorageSCCollectReward(providerId string, providerType int return err } -func (t *Transaction) VestingUpdateConfig(vscc InputMap) (err error) { - - err = t.createSmartContractTxn(VestingSmartContractAddress, - transaction.VESTING_UPDATE_SETTINGS, vscc, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - // faucet smart contract func (t *Transaction) FaucetUpdateConfig(ip InputMap) (err error) { diff --git a/zcncore/transactionauth.go b/zcncore/transactionauth.go index 57769fcdf..fa5fa11ad 100644 --- a/zcncore/transactionauth.go +++ b/zcncore/transactionauth.go @@ -54,19 +54,6 @@ func (ta *TransactionWithAuth) Send(toClientID string, val uint64, desc string) return nil } -func (ta *TransactionWithAuth) VestingAdd(ar *VestingAddRequest, - value uint64) (err error) { - - err = ta.t.createSmartContractTxn(VestingSmartContractAddress, - transaction.VESTING_ADD, ar, value) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - func (ta *TransactionWithAuth) MinerSCLock(providerId string, providerType Provider, lock uint64) error { if lock > math.MaxInt64 { return errors.New("invalid_lock", "int64 overflow on lock value") @@ -154,17 +141,6 @@ func (ta *TransactionWithAuth) StorageSCCollectReward(providerId string, provide return err } -func (ta *TransactionWithAuth) VestingUpdateConfig(ip *InputMap) (err error) { - err = ta.t.createSmartContractTxn(VestingSmartContractAddress, - transaction.VESTING_UPDATE_SETTINGS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - // faucet smart contract func (ta *TransactionWithAuth) FaucetUpdateConfig(ip *InputMap) (err error) { diff --git a/zcncore/transactionauth_base.go b/zcncore/transactionauth_base.go index 47d8119d1..5c25eecf9 100644 --- a/zcncore/transactionauth_base.go +++ b/zcncore/transactionauth_base.go @@ -199,52 +199,6 @@ func (ta *TransactionWithAuth) GetTransactionNonce() int64 { return ta.t.txn.TransactionNonce } -// ========================================================================== // -// vesting pool // -// ========================================================================== // - -func (ta *TransactionWithAuth) VestingTrigger(poolID string) (err error) { - err = ta.t.vestingPoolTxn(transaction.VESTING_TRIGGER, poolID, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) VestingStop(sr *VestingStopRequest) (err error) { - err = ta.t.createSmartContractTxn(VestingSmartContractAddress, - transaction.VESTING_STOP, sr, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) VestingUnlock(poolID string) (err error) { - - err = ta.t.vestingPoolTxn(transaction.VESTING_UNLOCK, poolID, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) VestingDelete(poolID string) (err error) { - err = ta.t.vestingPoolTxn(transaction.VESTING_DELETE, poolID, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - // // miner sc // diff --git a/zcncore/transactionauth_mobile.go b/zcncore/transactionauth_mobile.go index e00158e33..9666d10eb 100644 --- a/zcncore/transactionauth_mobile.go +++ b/zcncore/transactionauth_mobile.go @@ -36,22 +36,6 @@ func (ta *TransactionWithAuth) Send(toClientID string, val uint64, desc string) return nil } -func (ta *TransactionWithAuth) VestingAdd(ar VestingAddRequest, value string) error { - v, err := parseCoinStr(value) - if err != nil { - return err - } - - err = ta.t.createSmartContractTxn(VestingSmartContractAddress, - transaction.VESTING_ADD, ar, v) - if err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return nil -} - func (ta *TransactionWithAuth) MinerSCLock(providerId string, providerType int, lock string) error { lv, err := parseCoinStr(lock) if err != nil { @@ -118,17 +102,6 @@ func (ta *TransactionWithAuth) StorageSCCollectReward(providerId string, provide return err } -func (ta *TransactionWithAuth) VestingUpdateConfig(ip InputMap) (err error) { - err = ta.t.createSmartContractTxn(VestingSmartContractAddress, - transaction.VESTING_UPDATE_SETTINGS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - // faucet smart contract func (ta *TransactionWithAuth) FaucetUpdateConfig(ip InputMap) (err error) { diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 02b0a823e..3b994c7f8 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -38,13 +38,6 @@ const ( GET_LATEST_FINALIZED_MAGIC_BLOCK = `/v1/block/get/latest_finalized_magic_block` GET_FEE_STATS = `/v1/block/get/fee_stats` GET_CHAIN_STATS = `/v1/chain/get/stats` - // vesting SC - - VESTINGSC_PFX = `/v1/screst/` + VestingSmartContractAddress - - GET_VESTING_CONFIG = VESTINGSC_PFX + `/vesting-config` - GET_VESTING_POOL_INFO = VESTINGSC_PFX + `/getPoolInfo` - GET_VESTING_CLIENT_POOLS = VESTINGSC_PFX + `/getClientPools` // faucet sc @@ -97,7 +90,6 @@ const ( const ( StorageSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d7` - VestingSmartContractAddress = `2bba5b05949ea59c80aed3ac3474d7379d3be737e8eb5a968c52295e48333ead` FaucetSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d3` MultiSigSmartContractAddress = `27b5ef7120252b79f9dd9c05505dd28f328c80f6863ee446daede08a84d651a7` MinerSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d9` @@ -755,10 +747,6 @@ func GetIdForUrl(url string) string { return "" } -// -// vesting pool -// - type Params map[string]string func (p Params) Query() string { From 1a00969f7b07aefa2f0d988479843424a9cdaeb2 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Tue, 10 Sep 2024 16:23:44 +0530 Subject: [PATCH 044/319] Remove multi sig --- zcnbridge/wallet/status.go | 17 -- zcncore/ethwallet_base.go | 87 --------- zcncore/mocks/TransactionCommon.go | 14 -- zcncore/mocks/TransactionScheme.go | 14 -- zcncore/mswallet.go | 207 --------------------- zcncore/mswallet_base.go | 134 -------------- zcncore/mswallet_mobile.go | 282 ----------------------------- zcncore/transaction.go | 130 ------------- zcncore/transactionauth_base.go | 13 -- zcncore/wallet_base.go | 11 +- 10 files changed, 4 insertions(+), 905 deletions(-) delete mode 100644 zcncore/mswallet.go delete mode 100644 zcncore/mswallet_base.go delete mode 100644 zcncore/mswallet_mobile.go diff --git a/zcnbridge/wallet/status.go b/zcnbridge/wallet/status.go index df9b8306f..6df404714 100644 --- a/zcnbridge/wallet/status.go +++ b/zcnbridge/wallet/status.go @@ -160,23 +160,6 @@ func (zcn *ZCNStatus) OnAuthorizeSendComplete(status int, _ string, _ int64, _ s Logger.Info("Signature:", signature) } -// OnVoteComplete callback when a multisig vote is completed -// - status: status of the operation -// - proposal: proposal json string -// - err: error message -func (zcn *ZCNStatus) OnVoteComplete(status int, proposal string, err string) { - defer zcn.Wg.Done() - if status != zcncore.StatusSuccess { - zcn.Success = false - zcn.Err = errors.New(err) - zcn.walletString = "" - return - } - zcn.Success = true - zcn.Err = nil - zcn.walletString = proposal -} - //goland:noinspection ALL func PrintError(v ...interface{}) { fmt.Fprintln(os.Stderr, v...) diff --git a/zcncore/ethwallet_base.go b/zcncore/ethwallet_base.go index ee95a943c..e89e5da37 100644 --- a/zcncore/ethwallet_base.go +++ b/zcncore/ethwallet_base.go @@ -2,10 +2,8 @@ package zcncore import ( "context" - "crypto/ecdsa" "encoding/json" "fmt" - "log" "math" "math/big" "regexp" @@ -15,14 +13,10 @@ import ( "github.com/0chain/gosdk/core/tokenrate" "github.com/0chain/gosdk/core/zcncrypto" hdwallet "github.com/0chain/gosdk/zcncore/ethhdwallet" - "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/params" - "golang.org/x/crypto/sha3" ) // TODO change to real wallets @@ -222,87 +216,6 @@ func SuggestEthGasPrice() (int64, error) { return gasPrice.Int64(), nil } -// TransferEthTokens - transfer ETH tokens to multisign wallet -func TransferEthTokens(fromPrivKey string, amountTokens, gasPrice int64) (string, error) { - var client *ethclient.Client - var err error - if client, err = getEthClient(); err != nil { - return "", err - } - - privateKey, err := crypto.HexToECDSA(fromPrivKey) - if err != nil { - return "", err - } - - publicKey := privateKey.Public() - publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey) - - fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA) - nonce, err := client.PendingNonceAt(context.Background(), fromAddress) - if err != nil { - return "", err - } - - toAddress := common.HexToAddress(walletAddr) - tokenAddress := common.HexToAddress(tokenAddress) - - transferFnSignature := []byte("transfer(address,uint256)") - hash := sha3.NewLegacyKeccak256() - hash.Write(transferFnSignature) - methodID := hash.Sum(nil)[:4] - fmt.Println(hexutil.Encode(methodID)) // 0xa9059cbb - - paddedAddress := common.LeftPadBytes(toAddress.Bytes(), 32) - fmt.Println(hexutil.Encode(paddedAddress)) // 0x0000000000000000000000004592d8f8d7b001e72cb26a73e4fa1806a51ac79d - - amount := new(big.Int) - amount.SetInt64(amountTokens) - - paddedAmount := common.LeftPadBytes(amount.Bytes(), 32) - fmt.Println(hexutil.Encode(paddedAmount)) // 0x00000000000000000000000000000000000000000000003635c9adc5dea00000 - - var data []byte - data = append(data, methodID...) - data = append(data, paddedAddress...) - data = append(data, paddedAmount...) - - gasLimit, err := client.EstimateGas(context.Background(), ethereum.CallMsg{ - To: &tokenAddress, - Data: data, - }) - if err != nil { - log.Fatal(err) - } - - txData := &types.LegacyTx{ - Nonce: nonce, - GasPrice: big.NewInt(gasPrice), - Gas: gasLimit, - To: &tokenAddress, - Value: amount, - Data: data, - } - tx := types.NewTx(txData) - - chainID, err := client.ChainID(context.Background()) - if err != nil { - return "", err - } - - signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey) - if err != nil { - return "", err - } - - err = client.SendTransaction(context.Background(), signedTx) - if err != nil { - return "", err - } - - return signedTx.Hash().Hex(), nil -} - func getBalanceFromEthNode(ethAddr string) (int64, error) { if client, err := getEthClient(); err == nil { account := common.HexToAddress(ethAddr) diff --git a/zcncore/mocks/TransactionCommon.go b/zcncore/mocks/TransactionCommon.go index a421df0cf..c7d19ba81 100644 --- a/zcncore/mocks/TransactionCommon.go +++ b/zcncore/mocks/TransactionCommon.go @@ -313,20 +313,6 @@ func (_m *TransactionCommon) ReadPoolUnlock() error { return r0 } -// RegisterMultiSig provides a mock function with given fields: walletstr, mswallet -func (_m *TransactionCommon) RegisterMultiSig(walletstr string, mswallet string) error { - ret := _m.Called(walletstr, mswallet) - - var r0 error - if rf, ok := ret.Get(0).(func(string, string) error); ok { - r0 = rf(walletstr, mswallet) - } else { - r0 = ret.Error(0) - } - - return r0 -} - // Send provides a mock function with given fields: toClientID, val, desc func (_m *TransactionCommon) Send(toClientID string, val uint64, desc string) error { ret := _m.Called(toClientID, val, desc) diff --git a/zcncore/mocks/TransactionScheme.go b/zcncore/mocks/TransactionScheme.go index 371074d8c..bdfdfde40 100644 --- a/zcncore/mocks/TransactionScheme.go +++ b/zcncore/mocks/TransactionScheme.go @@ -427,20 +427,6 @@ func (_m *TransactionScheme) ReadPoolUnlock() error { return r0 } -// RegisterMultiSig provides a mock function with given fields: walletstr, mswallet -func (_m *TransactionScheme) RegisterMultiSig(walletstr string, mswallet string) error { - ret := _m.Called(walletstr, mswallet) - - var r0 error - if rf, ok := ret.Get(0).(func(string, string) error); ok { - r0 = rf(walletstr, mswallet) - } else { - r0 = ret.Error(0) - } - - return r0 -} - // Send provides a mock function with given fields: toClientID, val, desc func (_m *TransactionScheme) Send(toClientID string, val uint64, desc string) error { ret := _m.Called(toClientID, val, desc) diff --git a/zcncore/mswallet.go b/zcncore/mswallet.go deleted file mode 100644 index 517d81a37..000000000 --- a/zcncore/mswallet.go +++ /dev/null @@ -1,207 +0,0 @@ -//go:build !mobile -// +build !mobile - -package zcncore - -import ( - "encoding/json" - "fmt" - - "github.com/0chain/errors" - "github.com/0chain/gosdk/core/encryption" - "github.com/0chain/gosdk/core/zcncrypto" -) - -//MSVote -- this should mimic the type Vote defined in MultiSig SC -type MSVote struct { - ProposalID string `json:"proposal_id"` - - // Client ID in transfer is that of the multi-sig wallet, not the signer. - Transfer MSTransfer `json:"transfer"` - - Signature string `json:"signature"` -} - -//MSTransfer - a data structure to hold state transfer from one client to another -type MSTransfer struct { - ClientID string `json:"from"` - ToClientID string `json:"to"` - Amount uint64 `json:"amount"` -} - -// MultisigSCWallet --this should mimic MultisigWallet definition in MultiSig SC -type MultisigSCWallet struct { - ClientID string `json:"client_id"` - SignatureScheme string `json:"signature_scheme"` - PublicKey string `json:"public_key"` - - SignerThresholdIDs []string `json:"signer_threshold_ids"` - SignerPublicKeys []string `json:"signer_public_keys"` - - NumRequired int `json:"num_required"` -} - -// MSWallet Client data necessary for a multi-sig wallet. -type MSWallet struct { - Id int `json:"id"` - SignatureScheme string `json:"signature_scheme"` - GroupClientID string `json:"group_client_id"` - GroupKey zcncrypto.SignatureScheme `json:"group_key"` - SignerClientIDs []string `json:"sig_client_ids"` - SignerKeys []zcncrypto.SignatureScheme `json:"signer_keys"` - T int `json:"threshold"` - N int `json:"num_subkeys"` -} - -func (msw *MSWallet) UnmarshalJSON(data []byte) error { - m := &struct { - Id int `json:"id"` - SignatureScheme string `json:"signature_scheme"` - GroupClientID string `json:"group_client_id"` - SignerClientIDs []string `json:"sig_client_ids"` - T int `json:"threshold"` - N int `json:"num_subkeys"` - GroupKey interface{} `json:"group_key"` - SignerKeys interface{} `json:"signer_keys"` - }{} - - if err := json.Unmarshal(data, &m); err != nil { - return err - } - - msw.Id = m.Id - msw.SignatureScheme = m.SignatureScheme - msw.GroupClientID = m.GroupClientID - msw.SignerClientIDs = m.SignerClientIDs - msw.T = m.T - msw.N = m.N - - if m.GroupKey != nil { - groupKeyBuf, err := json.Marshal(m.GroupKey) - if err != nil { - return err - } - - ss := zcncrypto.NewSignatureScheme(m.SignatureScheme) - - if err := json.Unmarshal(groupKeyBuf, &ss); err != nil { - return err - } - - msw.GroupKey = ss - } - - signerKeys, err := zcncrypto.UnmarshalSignatureSchemes(m.SignatureScheme, m.SignerKeys) - if err != nil { - return err - } - msw.SignerKeys = signerKeys - - return nil -} - -// Marshal returns json string -func (msw *MSWallet) Marshal() (string, error) { - msws, err := json.Marshal(msw) - if err != nil { - return "", errors.New("", "Invalid Wallet") - } - return string(msws), nil -} - -//GetMultisigPayload given a multisig wallet as a string, makes a multisig wallet payload to register -func GetMultisigPayload(mswstr string) (interface{}, error) { - var msw MSWallet - err := json.Unmarshal([]byte(mswstr), &msw) - - if err != nil { - fmt.Printf("Error while creating multisig wallet from input:\n%v", mswstr) - return "", err - } - var signerThresholdIDs []string - var signerPublicKeys []string - - for _, scheme := range msw.SignerKeys { - signerThresholdIDs = append(signerThresholdIDs, scheme.GetID()) - signerPublicKeys = append(signerPublicKeys, scheme.GetPublicKey()) - } - - msscw := MultisigSCWallet{ - ClientID: msw.GroupClientID, - SignatureScheme: msw.SignatureScheme, - PublicKey: msw.GroupKey.GetPublicKey(), - - SignerThresholdIDs: signerThresholdIDs, - SignerPublicKeys: signerPublicKeys, - - NumRequired: msw.T, - } - - return msscw, nil -} - -//GetMultisigVotePayload given a multisig vote as a string, makes a multisig vote payload to register -func GetMultisigVotePayload(msvstr string) (interface{}, error) { - var msv MSVote - err := json.Unmarshal([]byte(msvstr), &msv) - - if err != nil { - fmt.Printf("Error while creating multisig wallet from input:\n%v", msvstr) - return nil, err - } - - //Marshalling and unmarshalling validates the string. Do any additional veirfication here. - - return msv, nil - -} - -// CreateMSVote create a vote for multisig -func CreateMSVote(proposal, grpClientID, signerWalletstr, toClientID string, token uint64) (string, error) { - if proposal == "" || grpClientID == "" || toClientID == "" || signerWalletstr == "" { - return "", errors.New("", "proposal or groupClient or signer wallet or toClientID cannot be empty") - } - - if token < 1 { - return "", errors.New("", "Token cannot be less than 1") - } - - signerWallet, err := getWallet(signerWalletstr) - if err != nil { - fmt.Printf("Error while parsing the signer wallet. %v", err) - return "", err - } - - //Note: Is this honored by multisig sc? - transfer := MSTransfer{ - ClientID: grpClientID, - ToClientID: toClientID, - Amount: token, - } - - buff, _ := json.Marshal(transfer) - hash := encryption.Hash(buff) - - sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) - if err := sigScheme.SetPrivateKey(signerWallet.Keys[0].PrivateKey); err != nil { - return "", err - } - - sig, err := sigScheme.Sign(hash) - if err != nil { - return "", err - } - - vote := MSVote{ - Transfer: transfer, - ProposalID: proposal, - Signature: sig, - } - - vbytes, err := json.Marshal(vote) - if err != nil { - fmt.Printf("error in marshalling vote %v", vote) - return "", err - } - return string(vbytes), nil -} diff --git a/zcncore/mswallet_base.go b/zcncore/mswallet_base.go deleted file mode 100644 index 6b9b215ca..000000000 --- a/zcncore/mswallet_base.go +++ /dev/null @@ -1,134 +0,0 @@ -//go:build !mobile -// +build !mobile - -package zcncore - -import ( - "encoding/hex" - "fmt" - "time" - - "github.com/0chain/errors" - "github.com/0chain/gosdk/constants" - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/encryption" - "github.com/0chain/gosdk/core/zcncrypto" -) - -// MSVoteCallback callback definition multisig Vote function -type MSVoteCallback interface { - OnVoteComplete(status int, proposal string, err string) -} - -// CreateMSWallet returns multisig wallet information -func CreateMSWallet(t, n int) (string, string, []string, error) { - if t < 1 || t > n { - return "", "", nil, errors.New("bls0_generate_threshold_key_shares", fmt.Sprintf("Given threshold (%d) is less than 1 or greater than numsigners (%d)", t, n)) - } - - id := 0 - if signatureScheme != constants.BLS0CHAIN.String() { - return "", "", nil, errors.New("", "encryption scheme for this blockchain is not bls0chain") - - } - - groupKey := zcncrypto.NewSignatureScheme(signatureScheme) - wallet, err := groupKey.GenerateKeys() - if err != nil { - return "", "", nil, err - } - - logging.Info(fmt.Sprintf("Wallet id: %s", wallet.ClientKey)) - - groupClientID := GetClientID(groupKey.GetPublicKey()) - //Code modified to directly use BLS0ChainThresholdScheme - signerKeys, err := zcncrypto.GenerateThresholdKeyShares(t, n, groupKey) - - if err != nil { - return "", "", nil, errors.Wrap(err, "Err in generateThresholdKeyShares") - } - var signerClientIDs []string - for _, key := range signerKeys { - signerClientIDs = append(signerClientIDs, GetClientID(key.GetPublicKey())) - } - - msw := MSWallet{ - Id: id, - SignatureScheme: signatureScheme, - GroupClientID: groupClientID, - GroupKey: groupKey, - SignerClientIDs: signerClientIDs, - SignerKeys: signerKeys, - T: t, - N: n, - } - - wallets, errw := getWallets(msw) - - if errw != nil { - return "", "", nil, errw - - } - smsw, er := msw.Marshal() - if er != nil { - return "", "", nil, er - } - return smsw, groupClientID, wallets, nil - -} - -func getWallets(msw MSWallet) ([]string, error) { - - wallets := make([]string, 0, msw.N+1) - - b0ss := msw.GroupKey - - grw, err := makeWallet(b0ss.GetPrivateKey(), b0ss.GetPublicKey(), b0ss.GetMnemonic()) - - if err != nil { - return nil, err - } - - wallets = append(wallets, grw) - - for _, signer := range msw.SignerKeys { - w, err := makeWallet(signer.GetPrivateKey(), signer.GetPublicKey(), "") - if err != nil { - return nil, err - } - wallets = append(wallets, w) - } - return wallets, nil -} - -func makeWallet(privateKey, publicKey, mnemonic string) (string, error) { - w := &zcncrypto.Wallet{} - w.Keys = make([]zcncrypto.KeyPair, 1) - w.Keys[0].PrivateKey = privateKey - w.Keys[0].PublicKey = publicKey - w.ClientID = GetClientID(publicKey) //VerifyThis - w.ClientKey = publicKey - w.Mnemonic = mnemonic - w.Version = zcncrypto.CryptoVersion - w.DateCreated = time.Now().Format(time.RFC3339) - - return w.Marshal() -} - -// GetClientID -- computes Client ID from publickey -func GetClientID(pkey string) string { - publicKeyBytes, err := hex.DecodeString(pkey) - if err != nil { - panic(err) - } - - return encryption.Hash(publicKeyBytes) -} - -func GetClientWalletKey() string { - return client.Wallet().ClientKey -} - -func GetClientWalletID() string { - return client.Wallet().ClientID -} diff --git a/zcncore/mswallet_mobile.go b/zcncore/mswallet_mobile.go deleted file mode 100644 index 8e51db735..000000000 --- a/zcncore/mswallet_mobile.go +++ /dev/null @@ -1,282 +0,0 @@ -//go:build mobile -// +build mobile - -package zcncore - -import ( - "encoding/json" - "errors" - "fmt" - "strconv" - - "github.com/0chain/gosdk/core/encryption" - "github.com/0chain/gosdk/core/zcncrypto" -) - -type MultisigSCWallet interface { - GetClientID() string - GetSignatureScheme() string - GetPublicKey() string - GetNumRequired() int - GetSignerThresholdIDs() Stringers - GetSignerPublicKeys() Stringers -} - -// Stringers wraps the methods for accessing string slice -type Stringers interface { - Len() int // return the number of string slice - Get(i int) (string, error) // get string of given index -} - -// stringSlice implements the Stringers interface -type stringSlice []string - -func (ss stringSlice) Len() int { - return len(ss) -} - -func (ss stringSlice) Get(i int) (string, error) { - if i < 0 || i >= len(ss) { - return "", errors.New("index out of bounds") - } - return ss[i], nil -} - -//GetMultisigPayload given a multisig wallet as a string, makes a multisig wallet payload to register -func GetMultisigPayload(mswstr string) (MultisigSCWallet, error) { - var msw msWallet - err := json.Unmarshal([]byte(mswstr), &msw) - if err != nil { - return nil, err - } - - var signerThresholdIDs []string - var signerPublicKeys []string - - for _, scheme := range msw.SignerKeys { - signerThresholdIDs = append(signerThresholdIDs, scheme.GetID()) - signerPublicKeys = append(signerPublicKeys, scheme.GetPublicKey()) - } - - return &multisigSCWallet{ - ClientID: msw.GroupClientID, - SignatureScheme: msw.SignatureScheme, - PublicKey: msw.GroupKey.GetPublicKey(), - - SignerThresholdIDs: signerThresholdIDs, - SignerPublicKeys: signerPublicKeys, - - NumRequired: msw.T, - }, nil -} - -type multisigSCWallet struct { - ClientID string `json:"client_id"` - SignatureScheme string `json:"signature_scheme"` - PublicKey string `json:"public_key"` - - SignerThresholdIDs []string `json:"signer_threshold_ids"` - SignerPublicKeys []string `json:"signer_public_keys"` - - NumRequired int `json:"num_required"` -} - -func (m *multisigSCWallet) GetClientID() string { - return m.ClientID -} - -func (m *multisigSCWallet) GetSignatureScheme() string { - return m.SignatureScheme -} - -func (m *multisigSCWallet) GetPublicKey() string { - return m.PublicKey -} - -func (m *multisigSCWallet) GetSignerThresholdIDs() Stringers { - return stringSlice(m.SignerThresholdIDs) -} - -func (m *multisigSCWallet) GetSignerPublicKeys() Stringers { - return stringSlice(m.SignerPublicKeys) -} - -func (m *multisigSCWallet) GetNumRequired() int { - return m.NumRequired -} - -type msWallet struct { - Id int `json:"id"` - SignatureScheme string `json:"signature_scheme"` - GroupClientID string `json:"group_client_id"` - GroupKey zcncrypto.SignatureScheme `json:"group_key"` - SignerClientIDs []string `json:"sig_client_ids"` - SignerKeys []zcncrypto.SignatureScheme `json:"signer_keys"` - T int `json:"threshold"` - N int `json:"num_subkeys"` -} - -func (msw *msWallet) UnmarshalJSON(data []byte) error { - m := &struct { - Id int `json:"id"` - SignatureScheme string `json:"signature_scheme"` - GroupClientID string `json:"group_client_id"` - SignerClientIDs []string `json:"sig_client_ids"` - T int `json:"threshold"` - N int `json:"num_subkeys"` - GroupKey interface{} `json:"group_key"` - SignerKeys interface{} `json:"signer_keys"` - }{} - - if err := json.Unmarshal(data, &m); err != nil { - return err - } - - msw.Id = m.Id - msw.SignatureScheme = m.SignatureScheme - msw.GroupClientID = m.GroupClientID - msw.SignerClientIDs = m.SignerClientIDs - msw.T = m.T - msw.N = m.N - - if m.GroupKey != nil { - groupKeyBuf, err := json.Marshal(m.GroupKey) - if err != nil { - return err - } - - ss := zcncrypto.NewSignatureScheme(m.SignatureScheme) - - if err := json.Unmarshal(groupKeyBuf, &ss); err != nil { - return err - } - - msw.GroupKey = ss - } - - signerKeys, err := zcncrypto.UnmarshalSignatureSchemes(m.SignatureScheme, m.SignerKeys) - if err != nil { - return err - } - msw.SignerKeys = signerKeys - - return nil -} - -// Marshal returns json string -func (msw *msWallet) Marshal() (string, error) { - msws, err := json.Marshal(msw) - if err != nil { - return "", errors.New("invalid wallet") - } - return string(msws), nil -} - -type MSVote interface { - GetProposalID() string - GetSignature() string - GetTransferClientID() string - GetTransferToClientID() string - GetTransferAmount() string -} - -type msVote struct { - ProposalID string `json:"proposal_id"` - - // Client ID in transfer is that of the multi-sig wallet, not the signer. - Transfer msTransfer `json:"transfer"` - - Signature string `json:"signature"` -} - -func (m *msVote) GetProposalID() string { - return m.ProposalID -} - -func (m *msVote) GetTransferClientID() string { - return m.Transfer.ClientID -} - -func (m *msVote) GetTransferToClientID() string { - return m.Transfer.ToClientID -} - -func (m *msVote) GetTransferAmount() string { - return strconv.FormatUint(m.Transfer.Amount, 10) -} - -func (m *msVote) GetSignature() string { - return m.Signature -} - -//msTransfer - a data structure to hold state transfer from one client to another -type msTransfer struct { - ClientID string `json:"from"` - ToClientID string `json:"to"` - Amount uint64 `json:"amount"` -} - -//GetMultisigVotePayload given a multisig vote as a string, makes a multisig vote payload to register -func GetMultisigVotePayload(msvstr string) (MSVote, error) { - var msv msVote - err := json.Unmarshal([]byte(msvstr), &msv) - if err != nil { - return nil, err - } - - return &msv, nil -} - -// CreateMSVote create a vote for multisig -func CreateMSVote(proposal, grpClientID, signerWalletstr, toClientID string, tokenStr string) (string, error) { - if proposal == "" || grpClientID == "" || toClientID == "" || signerWalletstr == "" { - return "", errors.New("proposal or groupClient or signer wallet or toClientID cannot be empty") - } - - token, err := strconv.ParseUint(tokenStr, 10, 64) - if err != nil { - return "", err - } - - if token < 1 { - return "", errors.New("token cannot be less than 1") - } - - signerWallet, err := getWallet(signerWalletstr) - if err != nil { - return "", err - } - - //Note: Is this honored by multisig sc? - transfer := msTransfer{ - ClientID: grpClientID, - ToClientID: toClientID, - Amount: token, - } - - buff, _ := json.Marshal(transfer) - hash := encryption.Hash(buff) - - sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) - if err := sigScheme.SetPrivateKey(signerWallet.Keys[0].PrivateKey); err != nil { - return "", err - } - - sig, err := sigScheme.Sign(hash) - if err != nil { - return "", err - } - - vote := msVote{ - Transfer: transfer, - ProposalID: proposal, - Signature: sig, - } - - vbytes, err := json.Marshal(vote) - if err != nil { - fmt.Printf("error in marshalling vote %v", vote) - return "", err - } - return string(vbytes), nil -} diff --git a/zcncore/transaction.go b/zcncore/transaction.go index e0bb38f40..580d7e4dc 100644 --- a/zcncore/transaction.go +++ b/zcncore/transaction.go @@ -16,7 +16,6 @@ import ( "github.com/0chain/gosdk/core/block" "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" - "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/node" "github.com/0chain/gosdk/core/transaction" @@ -116,9 +115,6 @@ type TransactionCommon interface { // Send implements sending token to a given clientid Send(toClientID string, val uint64, desc string) error - //RegisterMultiSig registers a group wallet and subwallets with MultisigSC - RegisterMultiSig(walletstr, mswallet string) error - MinerSCLock(providerId string, providerType Provider, lock uint64) error MinerSCUnlock(providerId string, providerType Provider) error MinerSCCollectReward(providerID string, providerType Provider) error @@ -500,132 +496,6 @@ func (t *Transaction) GetVerifyConfirmationStatus() ConfirmationStatus { return ConfirmationStatus(t.verifyConfirmationStatus) } -// RegisterMultiSig register a multisig wallet with the SC. -func (t *Transaction) RegisterMultiSig(walletstr string, mswallet string) error { - w, err := GetWallet(walletstr) - if err != nil { - fmt.Printf("Error while parsing the wallet. %v\n", err) - return err - } - - msw, err := GetMultisigPayload(mswallet) - if err != nil { - fmt.Printf("\nError in registering. %v\n", err) - return err - } - sn := transaction.SmartContractTxnData{Name: MultiSigRegisterFuncName, InputArgs: msw} - snBytes, err := json.Marshal(sn) - if err != nil { - return errors.Wrap(err, "execute multisig register failed due to invalid data.") - } - - clientNode, err := client.GetNode() - if err != nil { - return err - } - - go func() { - t.txn.TransactionType = transaction.TxnTypeSmartContract - t.txn.ToClientID = MultiSigSmartContractAddress - t.txn.TransactionData = string(snBytes) - t.txn.Value = 0 - nonce := t.txn.TransactionNonce - if nonce < 1 { - nonce = node.Cache.GetNextNonce(t.txn.ClientID) - } else { - node.Cache.Set(t.txn.ClientID, nonce) - } - t.txn.TransactionNonce = nonce - - if t.txn.TransactionFee == 0 { - fee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners, 0.2) - if err != nil { - return - } - t.txn.TransactionFee = fee - } - - err = t.txn.ComputeHashAndSignWithWallet(signWithWallet, w) - if err != nil { - return - } - t.submitTxn() - }() - return nil -} - -// NewMSTransaction new transaction object for multisig operation -func NewMSTransaction(walletstr string, cb TransactionCallback) (*Transaction, error) { - w, err := GetWallet(walletstr) - if err != nil { - fmt.Printf("Error while parsing the wallet. %v", err) - return nil, err - } - cfg, err := conf.GetClientConfig() - if err != nil { - return nil, err - } - t := &Transaction{} - t.txn = transaction.NewTransactionEntity(w.ClientID, cfg.ChainID, w.ClientKey, w.Nonce) - t.txnStatus, t.verifyStatus = StatusUnknown, StatusUnknown - t.txnCb = cb - return t, nil -} - -// RegisterVote register a multisig wallet with the SC. -func (t *Transaction) RegisterVote(signerwalletstr string, msvstr string) error { - - w, err := GetWallet(signerwalletstr) - if err != nil { - fmt.Printf("Error while parsing the wallet. %v", err) - return err - } - - msv, err := GetMultisigVotePayload(msvstr) - - if err != nil { - fmt.Printf("\nError in voting. %v\n", err) - return err - } - sn := transaction.SmartContractTxnData{Name: MultiSigVoteFuncName, InputArgs: msv} - snBytes, err := json.Marshal(sn) - if err != nil { - return errors.Wrap(err, "execute multisig vote failed due to invalid data.") - } - clientNode, err := client.GetNode() - if err != nil { - return err - } - go func() { - t.txn.TransactionType = transaction.TxnTypeSmartContract - t.txn.ToClientID = MultiSigSmartContractAddress - t.txn.TransactionData = string(snBytes) - t.txn.Value = 0 - nonce := t.txn.TransactionNonce - if nonce < 1 { - nonce = node.Cache.GetNextNonce(t.txn.ClientID) - } else { - node.Cache.Set(t.txn.ClientID, nonce) - } - t.txn.TransactionNonce = nonce - - if t.txn.TransactionFee == 0 { - fee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners, 0.2) - if err != nil { - return - } - t.txn.TransactionFee = fee - } - - err = t.txn.ComputeHashAndSignWithWallet(signWithWallet, w) - if err != nil { - return - } - t.submitTxn() - }() - return nil -} - type MinerSCDelegatePool struct { Settings StakePoolSettings `json:"settings"` } diff --git a/zcncore/transactionauth_base.go b/zcncore/transactionauth_base.go index 5c25eecf9..e356840a1 100644 --- a/zcncore/transactionauth_base.go +++ b/zcncore/transactionauth_base.go @@ -198,16 +198,3 @@ func (ta *TransactionWithAuth) Output() []byte { func (ta *TransactionWithAuth) GetTransactionNonce() int64 { return ta.t.txn.TransactionNonce } - -// -// miner sc -// - -// RegisterMultiSig register a multisig wallet with the SC. -func (ta *TransactionWithAuth) RegisterMultiSig(walletstr string, mswallet string) error { - return errors.New("", "not implemented") -} - -// -// Storage SC -// diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 3b994c7f8..7e37e0c8a 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -89,13 +89,10 @@ const ( ) const ( - StorageSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d7` - FaucetSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d3` - MultiSigSmartContractAddress = `27b5ef7120252b79f9dd9c05505dd28f328c80f6863ee446daede08a84d651a7` - MinerSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d9` - ZCNSCSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712e0` - MultiSigRegisterFuncName = "register" - MultiSigVoteFuncName = "vote" + StorageSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d7` + FaucetSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d3` + MinerSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d9` + ZCNSCSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712e0` ) // In percentage From 67737efe24f06f1ab0922007d75e0e065390a6cb Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Tue, 10 Sep 2024 16:41:38 +0530 Subject: [PATCH 045/319] Add more functions --- zcncore/functions.go | 104 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/zcncore/functions.go b/zcncore/functions.go index a37fd2144..84ef8faf6 100644 --- a/zcncore/functions.go +++ b/zcncore/functions.go @@ -102,3 +102,107 @@ func StorageScUpdateConfig(input interface{}) (err error) { return err } + +func AddHardfork(input interface{}) (err error) { + _, _, _, _, err = transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.ADD_HARDFORK, + InputArgs: input, + }) + + return err +} + +func ZCNSCUpdateGlobalConfig(input *MinerSCMinerInfo) (err error) { + _, _, _, _, err = transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.ZCNSC_UPDATE_GLOBAL_CONFIG, + InputArgs: input, + }) + + return err +} + +func MinerSCMinerSettings(input *MinerSCMinerInfo) (err error) { + _, _, _, _, err = transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.MINERSC_MINER_SETTINGS, + InputArgs: input, + }) + + return err +} + +func MinerSCSharderSettings(input *MinerSCMinerInfo) (err error) { + _, _, _, _, err = transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.MINERSC_SHARDER_SETTINGS, + InputArgs: input, + }) + + return err +} + +func MinerSCDeleteMiner(input *MinerSCMinerInfo) (err error) { + _, _, _, _, err = transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.MINERSC_MINER_DELETE, + InputArgs: input, + }) + + return err +} + +func MinerSCDeleteSharder(input *AuthorizerNode) (err error) { + _, _, _, _, err = transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.MINERSC_SHARDER_DELETE, + InputArgs: input, + }) + + return err +} + +func ZCNSCUpdateAuthorizerConfig(input *AuthorizerNode) (err error) { + _, _, _, _, err = transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.ZCNSC_UPDATE_AUTHORIZER_CONFIG, + InputArgs: input, + }) + + return err +} + +func ZCNSCAddAuthorizer(input *AddAuthorizerPayload) (err error) { + _, _, _, _, err = transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.ZCNSC_ADD_AUTHORIZER, + InputArgs: input, + }) + + return err +} + +func ZCNSCAuthorizerHealthCheck(input *AuthorizerHealthCheckPayload) (err error) { + _, _, _, _, err = transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.ZCNSC_AUTHORIZER_HEALTH_CHECK, + InputArgs: input, + }) + + return err +} + +func ZCNSCDeleteAuthorizer(input *DeleteAuthorizerPayload) (err error) { + _, _, _, _, err = transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.ZCNSC_DELETE_AUTHORIZER, + InputArgs: input, + }) + + return err +} + +func ZCNSCCollectReward(providerId string, providerType Provider) (err error) { + pr := &scCollectReward{ + ProviderId: providerId, + ProviderType: int(providerType), + } + + _, _, _, _, err = transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.ZCNSC_COLLECT_REWARD, + InputArgs: pr, + }) + + return err +} From d8efc3d75dc4ff53c074daa5366f8183765d9b24 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Tue, 10 Sep 2024 16:52:05 +0530 Subject: [PATCH 046/319] Return all values --- zcncore/functions.go | 97 ++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 58 deletions(-) diff --git a/zcncore/functions.go b/zcncore/functions.go index 84ef8faf6..8cda6baaf 100644 --- a/zcncore/functions.go +++ b/zcncore/functions.go @@ -5,8 +5,8 @@ import ( "github.com/0chain/gosdk/core/transaction" ) -func MinerSCLock(providerId string, providerType Provider, lock uint64) error { - _, _, _, _, err := transaction.SmartContractTxnValue(MinerSmartContractAddress, transaction.SmartContractTxnData{ +func MinerSCLock(providerId string, providerType Provider, lock uint64) (string, string, int64, *transaction.Transaction, error) { + return transaction.SmartContractTxnValue(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_LOCK, InputArgs: &stakePoolRequest{ ProviderID: providerId, @@ -14,11 +14,10 @@ func MinerSCLock(providerId string, providerType Provider, lock uint64) error { }, }, lock) - return err } -func MinerSCUnlock(providerId string, providerType Provider) error { - _, _, _, _, err := transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ +func MinerSCUnlock(providerId string, providerType Provider) (string, string, int64, *transaction.Transaction, error) { + return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_UNLOCK, InputArgs: &stakePoolRequest{ ProviderID: providerId, @@ -26,11 +25,10 @@ func MinerSCUnlock(providerId string, providerType Provider) error { }, }) - return err } -func MinerSCCollectReward(providerId string, providerType Provider) error { - _, _, _, _, err := transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ +func MinerSCCollectReward(providerId string, providerType Provider) (string, string, int64, *transaction.Transaction, error) { + return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_COLLECT_REWARD, InputArgs: &scCollectReward{ ProviderId: providerId, @@ -38,10 +36,9 @@ func MinerSCCollectReward(providerId string, providerType Provider) error { }, }) - return err } -func MinerSCKill(providerId string, providerType Provider) error { +func MinerSCKill(providerId string, providerType Provider) (string, string, int64, *transaction.Transaction, error) { pr := &scCollectReward{ ProviderId: providerId, ProviderType: int(providerType), @@ -53,19 +50,18 @@ func MinerSCKill(providerId string, providerType Provider) error { case ProviderSharder: name = transaction.MINERSC_KILL_SHARDER default: - return fmt.Errorf("kill provider type %v not implimented", providerType) + return "", "", -1, &transaction.Transaction{}, fmt.Errorf("kill provider type %v not implimented", providerType) } - _, _, _, _, err := transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ + return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: name, InputArgs: pr, }) - return err } -func StorageSCCollectReward(providerId string, providerType Provider) error { - _, _, _, _, err := transaction.SmartContractTxn(StorageSmartContractAddress, transaction.SmartContractTxnData{ +func StorageSCCollectReward(providerId string, providerType Provider) (string, string, int64, *transaction.Transaction, error) { + return transaction.SmartContractTxn(StorageSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.STORAGESC_COLLECT_REWARD, InputArgs: &scCollectReward{ ProviderId: providerId, @@ -73,136 +69,121 @@ func StorageSCCollectReward(providerId string, providerType Provider) error { }, }) - return err } -func MinerScUpdateConfig(input interface{}) (err error) { - _, _, _, _, err = transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ +func MinerScUpdateConfig(input interface{}) (string, string, int64, *transaction.Transaction, error) { + return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_UPDATE_SETTINGS, InputArgs: input, }) - return err } -func MinerScUpdateGlobals(input interface{}) (err error) { - _, _, _, _, err = transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ +func MinerScUpdateGlobals(input interface{}) (string, string, int64, *transaction.Transaction, error) { + return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_UPDATE_GLOBALS, InputArgs: input, }) - return err } -func StorageScUpdateConfig(input interface{}) (err error) { - _, _, _, _, err = transaction.SmartContractTxn(StorageSmartContractAddress, transaction.SmartContractTxnData{ +func StorageScUpdateConfig(input interface{}) (string, string, int64, *transaction.Transaction, error) { + return transaction.SmartContractTxn(StorageSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.STORAGESC_UPDATE_SETTINGS, InputArgs: input, }) - return err } -func AddHardfork(input interface{}) (err error) { - _, _, _, _, err = transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ +func AddHardfork(input interface{}) (string, string, int64, *transaction.Transaction, error) { + return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ADD_HARDFORK, InputArgs: input, }) - return err } -func ZCNSCUpdateGlobalConfig(input *MinerSCMinerInfo) (err error) { - _, _, _, _, err = transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ +func ZCNSCUpdateGlobalConfig(input *MinerSCMinerInfo) (string, string, int64, *transaction.Transaction, error) { + return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_UPDATE_GLOBAL_CONFIG, InputArgs: input, }) - return err } -func MinerSCMinerSettings(input *MinerSCMinerInfo) (err error) { - _, _, _, _, err = transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ +func MinerSCMinerSettings(input *MinerSCMinerInfo) (string, string, int64, *transaction.Transaction, error) { + return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_MINER_SETTINGS, InputArgs: input, }) - return err } -func MinerSCSharderSettings(input *MinerSCMinerInfo) (err error) { - _, _, _, _, err = transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ +func MinerSCSharderSettings(input *MinerSCMinerInfo) (string, string, int64, *transaction.Transaction, error) { + return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_SHARDER_SETTINGS, InputArgs: input, }) - return err } -func MinerSCDeleteMiner(input *MinerSCMinerInfo) (err error) { - _, _, _, _, err = transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ +func MinerSCDeleteMiner(input *MinerSCMinerInfo) (string, string, int64, *transaction.Transaction, error) { + return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_MINER_DELETE, InputArgs: input, }) - return err } -func MinerSCDeleteSharder(input *AuthorizerNode) (err error) { - _, _, _, _, err = transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ +func MinerSCDeleteSharder(input *AuthorizerNode) (string, string, int64, *transaction.Transaction, error) { + return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_SHARDER_DELETE, InputArgs: input, }) - return err } -func ZCNSCUpdateAuthorizerConfig(input *AuthorizerNode) (err error) { - _, _, _, _, err = transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ +func ZCNSCUpdateAuthorizerConfig(input *AuthorizerNode) (string, string, int64, *transaction.Transaction, error) { + return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_UPDATE_AUTHORIZER_CONFIG, InputArgs: input, }) - return err } -func ZCNSCAddAuthorizer(input *AddAuthorizerPayload) (err error) { - _, _, _, _, err = transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ +func ZCNSCAddAuthorizer(input *AddAuthorizerPayload) (string, string, int64, *transaction.Transaction, error) { + return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_ADD_AUTHORIZER, InputArgs: input, }) - return err } -func ZCNSCAuthorizerHealthCheck(input *AuthorizerHealthCheckPayload) (err error) { - _, _, _, _, err = transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ +func ZCNSCAuthorizerHealthCheck(input *AuthorizerHealthCheckPayload) (string, string, int64, *transaction.Transaction, error) { + return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_AUTHORIZER_HEALTH_CHECK, InputArgs: input, }) - return err } -func ZCNSCDeleteAuthorizer(input *DeleteAuthorizerPayload) (err error) { - _, _, _, _, err = transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ +func ZCNSCDeleteAuthorizer(input *DeleteAuthorizerPayload) (string, string, int64, *transaction.Transaction, error) { + return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_DELETE_AUTHORIZER, InputArgs: input, }) - return err } -func ZCNSCCollectReward(providerId string, providerType Provider) (err error) { +func ZCNSCCollectReward(providerId string, providerType Provider) (string, string, int64, *transaction.Transaction, error) { pr := &scCollectReward{ ProviderId: providerId, ProviderType: int(providerType), } - _, _, _, _, err = transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ + return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_COLLECT_REWARD, InputArgs: pr, }) - return err } From 383b5905ee2e145cf2b23356bd540b4de91a8a71 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Tue, 10 Sep 2024 16:54:47 +0530 Subject: [PATCH 047/319] Return all values --- zcncore/functions.go | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/zcncore/functions.go b/zcncore/functions.go index 8cda6baaf..5de7f80b4 100644 --- a/zcncore/functions.go +++ b/zcncore/functions.go @@ -5,7 +5,7 @@ import ( "github.com/0chain/gosdk/core/transaction" ) -func MinerSCLock(providerId string, providerType Provider, lock uint64) (string, string, int64, *transaction.Transaction, error) { +func MinerSCLock(providerId string, providerType Provider, lock uint64) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxnValue(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_LOCK, InputArgs: &stakePoolRequest{ @@ -16,7 +16,7 @@ func MinerSCLock(providerId string, providerType Provider, lock uint64) (string, } -func MinerSCUnlock(providerId string, providerType Provider) (string, string, int64, *transaction.Transaction, error) { +func MinerSCUnlock(providerId string, providerType Provider) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_UNLOCK, InputArgs: &stakePoolRequest{ @@ -27,7 +27,7 @@ func MinerSCUnlock(providerId string, providerType Provider) (string, string, in } -func MinerSCCollectReward(providerId string, providerType Provider) (string, string, int64, *transaction.Transaction, error) { +func MinerSCCollectReward(providerId string, providerType Provider) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_COLLECT_REWARD, InputArgs: &scCollectReward{ @@ -38,7 +38,7 @@ func MinerSCCollectReward(providerId string, providerType Provider) (string, str } -func MinerSCKill(providerId string, providerType Provider) (string, string, int64, *transaction.Transaction, error) { +func MinerSCKill(providerId string, providerType Provider) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { pr := &scCollectReward{ ProviderId: providerId, ProviderType: int(providerType), @@ -60,7 +60,7 @@ func MinerSCKill(providerId string, providerType Provider) (string, string, int6 } -func StorageSCCollectReward(providerId string, providerType Provider) (string, string, int64, *transaction.Transaction, error) { +func StorageSCCollectReward(providerId string, providerType Provider) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(StorageSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.STORAGESC_COLLECT_REWARD, InputArgs: &scCollectReward{ @@ -71,7 +71,7 @@ func StorageSCCollectReward(providerId string, providerType Provider) (string, s } -func MinerScUpdateConfig(input interface{}) (string, string, int64, *transaction.Transaction, error) { +func MinerScUpdateConfig(input interface{}) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_UPDATE_SETTINGS, InputArgs: input, @@ -79,7 +79,7 @@ func MinerScUpdateConfig(input interface{}) (string, string, int64, *transaction } -func MinerScUpdateGlobals(input interface{}) (string, string, int64, *transaction.Transaction, error) { +func MinerScUpdateGlobals(input interface{}) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_UPDATE_GLOBALS, InputArgs: input, @@ -87,7 +87,7 @@ func MinerScUpdateGlobals(input interface{}) (string, string, int64, *transactio } -func StorageScUpdateConfig(input interface{}) (string, string, int64, *transaction.Transaction, error) { +func StorageScUpdateConfig(input interface{}) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(StorageSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.STORAGESC_UPDATE_SETTINGS, InputArgs: input, @@ -95,7 +95,7 @@ func StorageScUpdateConfig(input interface{}) (string, string, int64, *transacti } -func AddHardfork(input interface{}) (string, string, int64, *transaction.Transaction, error) { +func AddHardfork(input interface{}) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ADD_HARDFORK, InputArgs: input, @@ -103,7 +103,7 @@ func AddHardfork(input interface{}) (string, string, int64, *transaction.Transac } -func ZCNSCUpdateGlobalConfig(input *MinerSCMinerInfo) (string, string, int64, *transaction.Transaction, error) { +func ZCNSCUpdateGlobalConfig(input *MinerSCMinerInfo) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_UPDATE_GLOBAL_CONFIG, InputArgs: input, @@ -111,7 +111,7 @@ func ZCNSCUpdateGlobalConfig(input *MinerSCMinerInfo) (string, string, int64, *t } -func MinerSCMinerSettings(input *MinerSCMinerInfo) (string, string, int64, *transaction.Transaction, error) { +func MinerSCMinerSettings(input *MinerSCMinerInfo) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_MINER_SETTINGS, InputArgs: input, @@ -119,7 +119,7 @@ func MinerSCMinerSettings(input *MinerSCMinerInfo) (string, string, int64, *tran } -func MinerSCSharderSettings(input *MinerSCMinerInfo) (string, string, int64, *transaction.Transaction, error) { +func MinerSCSharderSettings(input *MinerSCMinerInfo) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_SHARDER_SETTINGS, InputArgs: input, @@ -127,7 +127,7 @@ func MinerSCSharderSettings(input *MinerSCMinerInfo) (string, string, int64, *tr } -func MinerSCDeleteMiner(input *MinerSCMinerInfo) (string, string, int64, *transaction.Transaction, error) { +func MinerSCDeleteMiner(input *MinerSCMinerInfo) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_MINER_DELETE, InputArgs: input, @@ -135,7 +135,7 @@ func MinerSCDeleteMiner(input *MinerSCMinerInfo) (string, string, int64, *transa } -func MinerSCDeleteSharder(input *AuthorizerNode) (string, string, int64, *transaction.Transaction, error) { +func MinerSCDeleteSharder(input *AuthorizerNode) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_SHARDER_DELETE, InputArgs: input, @@ -143,7 +143,7 @@ func MinerSCDeleteSharder(input *AuthorizerNode) (string, string, int64, *transa } -func ZCNSCUpdateAuthorizerConfig(input *AuthorizerNode) (string, string, int64, *transaction.Transaction, error) { +func ZCNSCUpdateAuthorizerConfig(input *AuthorizerNode) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_UPDATE_AUTHORIZER_CONFIG, InputArgs: input, @@ -151,7 +151,7 @@ func ZCNSCUpdateAuthorizerConfig(input *AuthorizerNode) (string, string, int64, } -func ZCNSCAddAuthorizer(input *AddAuthorizerPayload) (string, string, int64, *transaction.Transaction, error) { +func ZCNSCAddAuthorizer(input *AddAuthorizerPayload) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_ADD_AUTHORIZER, InputArgs: input, @@ -159,7 +159,7 @@ func ZCNSCAddAuthorizer(input *AddAuthorizerPayload) (string, string, int64, *tr } -func ZCNSCAuthorizerHealthCheck(input *AuthorizerHealthCheckPayload) (string, string, int64, *transaction.Transaction, error) { +func ZCNSCAuthorizerHealthCheck(input *AuthorizerHealthCheckPayload) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_AUTHORIZER_HEALTH_CHECK, InputArgs: input, @@ -167,7 +167,7 @@ func ZCNSCAuthorizerHealthCheck(input *AuthorizerHealthCheckPayload) (string, st } -func ZCNSCDeleteAuthorizer(input *DeleteAuthorizerPayload) (string, string, int64, *transaction.Transaction, error) { +func ZCNSCDeleteAuthorizer(input *DeleteAuthorizerPayload) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_DELETE_AUTHORIZER, InputArgs: input, @@ -175,7 +175,7 @@ func ZCNSCDeleteAuthorizer(input *DeleteAuthorizerPayload) (string, string, int6 } -func ZCNSCCollectReward(providerId string, providerType Provider) (string, string, int64, *transaction.Transaction, error) { +func ZCNSCCollectReward(providerId string, providerType Provider) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { pr := &scCollectReward{ ProviderId: providerId, ProviderType: int(providerType), From 24db157c8cefae7bf88aef6897ff00125ab0a0ce Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Tue, 10 Sep 2024 17:04:41 +0530 Subject: [PATCH 048/319] Fix --- zcncore/functions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zcncore/functions.go b/zcncore/functions.go index 5de7f80b4..364eb108b 100644 --- a/zcncore/functions.go +++ b/zcncore/functions.go @@ -103,7 +103,7 @@ func AddHardfork(input interface{}) (hash, out string, nonce int64, txn *transac } -func ZCNSCUpdateGlobalConfig(input *MinerSCMinerInfo) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func ZCNSCUpdateGlobalConfig(input *InputMap) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_UPDATE_GLOBAL_CONFIG, InputArgs: input, From 1c713bf7af42e36d4262a1bb300802bb2fe6d196 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Tue, 10 Sep 2024 19:14:56 +0530 Subject: [PATCH 049/319] Fix --- zcnbridge/authorizers_query.go | 6 +++--- zcnbridge/bridge.go | 19 +++++++++---------- zcnbridge/bridge_test.go | 4 ++-- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/zcnbridge/authorizers_query.go b/zcnbridge/authorizers_query.go index 43f161a3b..ea8aeacaf 100644 --- a/zcnbridge/authorizers_query.go +++ b/zcnbridge/authorizers_query.go @@ -9,6 +9,7 @@ import ( "strings" "sync" + coreClient "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/zcnbridge/errors" "github.com/0chain/gosdk/zcnbridge/ethereum" @@ -16,7 +17,6 @@ import ( "github.com/0chain/gosdk/zcnbridge/log" "github.com/0chain/gosdk/zcnbridge/wallet" "github.com/0chain/gosdk/zcnbridge/zcnsc" - "github.com/0chain/gosdk/zcncore" "go.uber.org/zap" ) @@ -120,7 +120,7 @@ func (b *BridgeClient) QueryEthereumBurnEvents(startNonce string) ([]*ethereum.B var ( totalWorkers = len(authorizers) values = map[string]string{ - "clientid": zcncore.GetClientWalletID(), + "clientid": coreClient.ClientID(), "ethereumaddress": b.EthereumAddress, "startnonce": startNonce, } @@ -179,7 +179,7 @@ func (b *BridgeClient) QueryZChainMintPayload(ethBurnHash string) (*zcnsc.MintPa totalWorkers = len(authorizers) values = map[string]string{ "hash": ethBurnHash, - "clientid": zcncore.GetClientWalletID(), + "clientid": coreClient.ClientID(), } ) diff --git a/zcnbridge/bridge.go b/zcnbridge/bridge.go index de10e42d3..9e6bb6b19 100644 --- a/zcnbridge/bridge.go +++ b/zcnbridge/bridge.go @@ -19,14 +19,13 @@ import ( "gopkg.in/natefinch/lumberjack.v2" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/logger" coreTransaction "github.com/0chain/gosdk/core/transaction" "github.com/0chain/gosdk/zcnbridge/ethereum" "github.com/0chain/gosdk/zcnbridge/ethereum/authorizers" "github.com/0chain/gosdk/zcnbridge/ethereum/bridge" "github.com/0chain/gosdk/zcnbridge/ethereum/nftconfig" - "github.com/0chain/gosdk/zcncore" - "github.com/0chain/gosdk/zcnbridge/wallet" "github.com/0chain/gosdk/zcnbridge/zcnsc" eth "github.com/ethereum/go-ethereum" @@ -129,7 +128,7 @@ func (b *BridgeClient) AddEthereumAuthorizer(ctx context.Context, address common tran, err := instance.AddAuthorizers(transactOpts, address) if err != nil { msg := "failed to execute AddAuthorizers transaction to ClientID = %s with amount = %s" - return nil, errors.Wrapf(err, msg, zcncore.GetClientWalletID(), address.String()) + return nil, errors.Wrapf(err, msg, client.ClientID(), address.String()) } return tran, err @@ -147,7 +146,7 @@ func (b *BridgeClient) RemoveEthereumAuthorizer(ctx context.Context, address com tran, err := instance.RemoveAuthorizers(transactOpts, address) if err != nil { msg := "failed to execute RemoveAuthorizers transaction to ClientID = %s with amount = %s" - return nil, errors.Wrapf(err, msg, zcncore.GetClientWalletID(), address.String()) + return nil, errors.Wrapf(err, msg, client.ClientID(), address.String()) } return tran, err @@ -279,7 +278,7 @@ func (b *BridgeClient) NFTConfigSetUint256Raw(ctx context.Context, key common.Ha tran, err := instance.SetUint256(transactOpts, key, v) if err != nil { msg := "failed to execute setUint256 transaction to ClientID = %s with key = %s, value = %v" - return nil, errors.Wrapf(err, msg, zcncore.GetClientWalletID(), key, v) + return nil, errors.Wrapf(err, msg, client.ClientID(), key, v) } return tran, err @@ -332,7 +331,7 @@ func (b *BridgeClient) NFTConfigSetAddress(ctx context.Context, key, address str tran, err := instance.SetAddress(transactOpts, kkey, addr) if err != nil { msg := "failed to execute setAddress transaction to ClientID = %s with key = %s, value = %v" - return nil, errors.Wrapf(err, msg, zcncore.GetClientWalletID(), key, address) + return nil, errors.Wrapf(err, msg, client.ClientID(), key, address) } return tran, err @@ -588,7 +587,7 @@ func (b *BridgeClient) BurnWZCN(ctx context.Context, amountTokens uint64) (*type } // 1. Data Parameter (amount to burn) - clientID := DefaultClientIDEncoder(zcncore.GetClientWalletID()) + clientID := DefaultClientIDEncoder(client.ClientID()) // 2. Data Parameter (signature) amount := new(big.Int) @@ -607,12 +606,12 @@ func (b *BridgeClient) BurnWZCN(ctx context.Context, amountTokens uint64) (*type tran, err := bridgeInstance.Burn(transactOpts, amount, clientID) if err != nil { msg := "failed to execute Burn WZCN transaction to ClientID = %s with amount = %s" - return nil, errors.Wrapf(err, msg, zcncore.GetClientWalletID(), amount) + return nil, errors.Wrapf(err, msg, client.ClientID(), amount) } Logger.Info( "Posted Burn WZCN", - zap.String("clientID", zcncore.GetClientWalletID()), + zap.String("clientID", client.ClientID()), zap.Int64("amount", amount.Int64()), ) @@ -1048,7 +1047,7 @@ func (b *BridgeClient) EstimateBurnWZCNGasAmount(ctx context.Context, from, to, return 0, errors.Wrap(err, "failed to get ABI") } - clientID := DefaultClientIDEncoder(zcncore.GetClientWalletID()) + clientID := DefaultClientIDEncoder(client.ClientID()) amount := new(big.Int) amount.SetString(amountTokens, 10) diff --git a/zcnbridge/bridge_test.go b/zcnbridge/bridge_test.go index 020d30a3d..1fbc002b7 100644 --- a/zcnbridge/bridge_test.go +++ b/zcnbridge/bridge_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/hex" "encoding/json" + coreClient "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zcnbridge/ethereum/uniswapnetwork" "github.com/ethereum/go-ethereum/accounts/abi" "log" @@ -25,7 +26,6 @@ import ( transactionmocks "github.com/0chain/gosdk/zcnbridge/transaction/mocks" "github.com/0chain/gosdk/zcnbridge/wallet" "github.com/0chain/gosdk/zcnbridge/zcnsc" - "github.com/0chain/gosdk/zcncore" eth "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/keystore" @@ -397,7 +397,7 @@ func Test_ZCNBridge(t *testing.T) { rawAbi, err := binding.BridgeMetaData.GetAbi() require.NoError(t, err) - pack, err := rawAbi.Pack("burn", big.NewInt(amount), DefaultClientIDEncoder(zcncore.GetClientWalletID())) + pack, err := rawAbi.Pack("burn", big.NewInt(amount), DefaultClientIDEncoder(coreClient.ClientID())) require.NoError(t, err) require.True(t, ethereumClient.AssertCalled( From 0adf43273550bd7b4bdb5f71061d638ff902fdca Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Tue, 10 Sep 2024 19:15:16 +0530 Subject: [PATCH 050/319] Remove delete miner and sharder --- zcncore/functions.go | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/zcncore/functions.go b/zcncore/functions.go index 364eb108b..c5ccdd302 100644 --- a/zcncore/functions.go +++ b/zcncore/functions.go @@ -127,22 +127,6 @@ func MinerSCSharderSettings(input *MinerSCMinerInfo) (hash, out string, nonce in } -func MinerSCDeleteMiner(input *MinerSCMinerInfo) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { - return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ - Name: transaction.MINERSC_MINER_DELETE, - InputArgs: input, - }) - -} - -func MinerSCDeleteSharder(input *AuthorizerNode) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { - return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ - Name: transaction.MINERSC_SHARDER_DELETE, - InputArgs: input, - }) - -} - func ZCNSCUpdateAuthorizerConfig(input *AuthorizerNode) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_UPDATE_AUTHORIZER_CONFIG, From 3a18f9edb340b051845ea9f58cf173c9ddfee02c Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Tue, 10 Sep 2024 19:17:00 +0530 Subject: [PATCH 051/319] Fix --- zcnbridge/bridge.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/zcnbridge/bridge.go b/zcnbridge/bridge.go index 9e6bb6b19..d934d401b 100644 --- a/zcnbridge/bridge.go +++ b/zcnbridge/bridge.go @@ -4,6 +4,7 @@ import ( "context" "encoding/hex" "fmt" + coreClient "github.com/0chain/gosdk/core/client" "math/big" "strings" "time" @@ -19,7 +20,6 @@ import ( "gopkg.in/natefinch/lumberjack.v2" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/logger" coreTransaction "github.com/0chain/gosdk/core/transaction" "github.com/0chain/gosdk/zcnbridge/ethereum" @@ -128,7 +128,7 @@ func (b *BridgeClient) AddEthereumAuthorizer(ctx context.Context, address common tran, err := instance.AddAuthorizers(transactOpts, address) if err != nil { msg := "failed to execute AddAuthorizers transaction to ClientID = %s with amount = %s" - return nil, errors.Wrapf(err, msg, client.ClientID(), address.String()) + return nil, errors.Wrapf(err, msg, coreClient.ClientID(), address.String()) } return tran, err @@ -146,7 +146,7 @@ func (b *BridgeClient) RemoveEthereumAuthorizer(ctx context.Context, address com tran, err := instance.RemoveAuthorizers(transactOpts, address) if err != nil { msg := "failed to execute RemoveAuthorizers transaction to ClientID = %s with amount = %s" - return nil, errors.Wrapf(err, msg, client.ClientID(), address.String()) + return nil, errors.Wrapf(err, msg, coreClient.ClientID(), address.String()) } return tran, err @@ -278,7 +278,7 @@ func (b *BridgeClient) NFTConfigSetUint256Raw(ctx context.Context, key common.Ha tran, err := instance.SetUint256(transactOpts, key, v) if err != nil { msg := "failed to execute setUint256 transaction to ClientID = %s with key = %s, value = %v" - return nil, errors.Wrapf(err, msg, client.ClientID(), key, v) + return nil, errors.Wrapf(err, msg, coreClient.ClientID(), key, v) } return tran, err @@ -331,7 +331,7 @@ func (b *BridgeClient) NFTConfigSetAddress(ctx context.Context, key, address str tran, err := instance.SetAddress(transactOpts, kkey, addr) if err != nil { msg := "failed to execute setAddress transaction to ClientID = %s with key = %s, value = %v" - return nil, errors.Wrapf(err, msg, client.ClientID(), key, address) + return nil, errors.Wrapf(err, msg, coreClient.ClientID(), key, address) } return tran, err @@ -587,7 +587,7 @@ func (b *BridgeClient) BurnWZCN(ctx context.Context, amountTokens uint64) (*type } // 1. Data Parameter (amount to burn) - clientID := DefaultClientIDEncoder(client.ClientID()) + clientID := DefaultClientIDEncoder(coreClient.ClientID()) // 2. Data Parameter (signature) amount := new(big.Int) @@ -606,12 +606,12 @@ func (b *BridgeClient) BurnWZCN(ctx context.Context, amountTokens uint64) (*type tran, err := bridgeInstance.Burn(transactOpts, amount, clientID) if err != nil { msg := "failed to execute Burn WZCN transaction to ClientID = %s with amount = %s" - return nil, errors.Wrapf(err, msg, client.ClientID(), amount) + return nil, errors.Wrapf(err, msg, coreClient.ClientID(), amount) } Logger.Info( "Posted Burn WZCN", - zap.String("clientID", client.ClientID()), + zap.String("clientID", coreClient.ClientID()), zap.Int64("amount", amount.Int64()), ) @@ -1047,7 +1047,7 @@ func (b *BridgeClient) EstimateBurnWZCNGasAmount(ctx context.Context, from, to, return 0, errors.Wrap(err, "failed to get ABI") } - clientID := DefaultClientIDEncoder(client.ClientID()) + clientID := DefaultClientIDEncoder(coreClient.ClientID()) amount := new(big.Int) amount.SetString(amountTokens, 10) From 2f0771c28b3f86e19a797c8c6e4c229432a8e3d7 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Tue, 10 Sep 2024 19:20:21 +0530 Subject: [PATCH 052/319] Fix bridge burn zcn --- wasmsdk/bridge.go | 2 +- zcnbridge/bridge.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/wasmsdk/bridge.go b/wasmsdk/bridge.go index 47bd7f6df..b1c09ac00 100644 --- a/wasmsdk/bridge.go +++ b/wasmsdk/bridge.go @@ -74,7 +74,7 @@ func burnZCN(amount uint64) string { //nolint return errors.New("burnZCN", "bridge is not initialized").Error() } - hash, err := bridge.BurnZCN(amount) + hash, _, err := bridge.BurnZCN(amount) if err != nil { return errors.Wrap("burnZCN", "failed to burn ZCN tokens", err).Error() } diff --git a/zcnbridge/bridge.go b/zcnbridge/bridge.go index d934d401b..4a803a9d9 100644 --- a/zcnbridge/bridge.go +++ b/zcnbridge/bridge.go @@ -649,7 +649,7 @@ func (b *BridgeClient) MintZCN(ctx context.Context, payload *zcnsc.MintPayload) // - ctx go context instance to run the transaction // - amount amount of tokens to burn // - txnfee transaction fee -func (b *BridgeClient) BurnZCN(amount uint64) (string, error) { +func (b *BridgeClient) BurnZCN(amount uint64) (string, string, error) { payload := zcnsc.BurnPayload{ EthereumAddress: b.EthereumAddress, } @@ -660,13 +660,13 @@ func (b *BridgeClient) BurnZCN(amount uint64) (string, error) { zap.Uint64("burn amount", amount), ) - hash, _, _, _, err := coreTransaction.SmartContractTxn(wallet.ZCNSCSmartContractAddress, coreTransaction.SmartContractTxnData{ + hash, out, _, _, err := coreTransaction.SmartContractTxn(wallet.ZCNSCSmartContractAddress, coreTransaction.SmartContractTxnData{ Name: wallet.MintFunc, InputArgs: payload, }) if err != nil { Logger.Error("Burn ZCN transaction FAILED", zap.Error(err)) - return hash, errors.Wrap(err, fmt.Sprintf("failed to execute smart contract, hash = %s", hash)) + return hash, out, errors.Wrap(err, fmt.Sprintf("failed to execute smart contract, hash = %s", hash)) } Logger.Info( @@ -676,7 +676,7 @@ func (b *BridgeClient) BurnZCN(amount uint64) (string, error) { zap.Uint64("amount", amount), ) - return hash, nil + return hash, out, nil } // ApproveUSDCSwap provides opportunity to approve swap operation for ERC20 tokens From e7c4e4a784e69891a9ec3e4d60daa23d47509ddb Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Tue, 10 Sep 2024 19:41:04 +0530 Subject: [PATCH 053/319] Fix signature schemee --- zcncore/transaction_base.go | 8 ++++---- zcncore/transactionauth_base.go | 2 +- zcncore/wallet_base.go | 21 +++++---------------- zcncore/wallet_mobile.go | 2 +- 4 files changed, 11 insertions(+), 22 deletions(-) diff --git a/zcncore/transaction_base.go b/zcncore/transaction_base.go index d4b6ee497..cae7c1058 100644 --- a/zcncore/transaction_base.go +++ b/zcncore/transaction_base.go @@ -182,7 +182,7 @@ type SendTxnData struct { } func Sign(hash string) (string, error) { - sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) + sigScheme := zcncrypto.NewSignatureScheme(client.SignatureScheme()) err := sigScheme.SetPrivateKey(client.Wallet().Keys[0].PrivateKey) if err != nil { return "", err @@ -209,7 +209,7 @@ func VerifyWithKey(pubKey, signature, hash string) (bool, error) { } var SignFn = func(hash string) (string, error) { - sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) + sigScheme := zcncrypto.NewSignatureScheme(client.SignatureScheme()) err := sigScheme.SetPrivateKey(client.Wallet().Keys[0].PrivateKey) if err != nil { return "", err @@ -219,7 +219,7 @@ var SignFn = func(hash string) (string, error) { var AddSignature = func(privateKey, signature string, hash string) (string, error) { var ( - ss = zcncrypto.NewSignatureScheme(signatureScheme) + ss = zcncrypto.NewSignatureScheme(client.SignatureScheme()) err error ) @@ -238,7 +238,7 @@ func signWithWallet(hash string, wi interface{}) (string, error) { fmt.Printf("Error in casting to wallet") return "", errors.New("", "error in casting to wallet") } - sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) + sigScheme := zcncrypto.NewSignatureScheme(client.SignatureScheme()) err := sigScheme.SetPrivateKey(w.Keys[0].PrivateKey) if err != nil { return "", err diff --git a/zcncore/transactionauth_base.go b/zcncore/transactionauth_base.go index e356840a1..c044cbea2 100644 --- a/zcncore/transactionauth_base.go +++ b/zcncore/transactionauth_base.go @@ -85,7 +85,7 @@ func (ta *TransactionWithAuth) completeTxn(status int, out string, err error) { } func verifyFn(signature, msgHash, publicKey string) (bool, error) { - v := zcncrypto.NewSignatureScheme(signatureScheme) + v := zcncrypto.NewSignatureScheme(client.SignatureScheme()) err := v.SetPublicKey(publicKey) if err != nil { return false, err diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 7e37e0c8a..269843cda 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -300,20 +300,9 @@ func SetLogFile(logFile string, verbose bool) { logging.Info("******* Wallet SDK Version:", version.VERSIONSTR, " ******* (SetLogFile)") } -var signatureScheme string - -// InitSignatureScheme Use client.Init() in core/client package to initialize SDK. -// InitSignatureScheme initializes signature scheme only. -func InitSignatureScheme(scheme string) { - if scheme != constants.BLS0CHAIN.String() && scheme != constants.ED25519.String() { - panic("invalid/unsupported signature scheme") - } - signatureScheme = scheme -} - // CreateWalletOffline creates the wallet for the config signature scheme. func CreateWalletOffline() (string, error) { - sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) + sigScheme := zcncrypto.NewSignatureScheme(client.SignatureScheme()) wallet, err := sigScheme.GenerateKeys() if err != nil { return "", errors.New("failed to generate keys: " + err.Error()) @@ -330,7 +319,7 @@ func RecoverOfflineWallet(mnemonic string) (string, error) { if !zcncrypto.IsMnemonicValid(mnemonic) { return "", errors.New("Invalid mnemonic") } - sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) + sigScheme := zcncrypto.NewSignatureScheme(client.SignatureScheme()) wallet, err := sigScheme.RecoverKeys(mnemonic) if err != nil { return "", err @@ -351,7 +340,7 @@ func RecoverWallet(mnemonic string, statusCb WalletCallback) error { return errors.New("Invalid mnemonic") } go func() { - sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) + sigScheme := zcncrypto.NewSignatureScheme(client.SignatureScheme()) _, err := sigScheme.RecoverKeys(mnemonic) if err != nil { statusCb.OnWalletCreateComplete(StatusError, "", err.Error()) @@ -363,10 +352,10 @@ func RecoverWallet(mnemonic string, statusCb WalletCallback) error { // SplitKeys Split keys from the primary master key func SplitKeys(privateKey string, numSplits int) (string, error) { - if signatureScheme != constants.BLS0CHAIN.String() { + if client.SignatureScheme() != constants.BLS0CHAIN.String() { return "", errors.New("signature key doesn't support split key") } - sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) + sigScheme := zcncrypto.NewSignatureScheme(client.SignatureScheme()) err := sigScheme.SetPrivateKey(privateKey) if err != nil { return "", errors.New("set private key failed." + err.Error()) diff --git a/zcncore/wallet_mobile.go b/zcncore/wallet_mobile.go index 1eae1127d..93c3c8beb 100644 --- a/zcncore/wallet_mobile.go +++ b/zcncore/wallet_mobile.go @@ -19,7 +19,7 @@ type wallet struct { // Sign sign the given string using the wallet's private key func (w *wallet) Sign(hash string) (string, error) { - sigScheme := zcncrypto.NewSignatureScheme(signatureScheme) + sigScheme := zcncrypto.NewSignatureScheme(client.SignatureScheme()) err := sigScheme.SetPrivateKey(w.Keys[0].PrivateKey) if err != nil { return "", err From ac890d1307d755b2752d7cc53f387314ac2bceaf Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Tue, 10 Sep 2024 20:45:20 +0530 Subject: [PATCH 054/319] Fix --- core/client/set.go | 2 + .../{functions.go => execute_transactions.go} | 27 + zcncore/get_data.go | 260 ++++ zcncore/networkworker_mobile.go | 125 -- zcncore/transaction.go | 878 ----------- zcncore/transaction_base.go | 838 ----------- zcncore/transaction_mobile.go | 1339 ----------------- zcncore/transaction_query.go | 614 -------- zcncore/transaction_query_base.go | 81 - zcncore/transaction_query_mobile.go | 502 ------ zcncore/transaction_query_test.go | 250 --- zcncore/transactionauth.go | 320 ---- zcncore/transactionauth_base.go | 200 --- zcncore/transactionauth_mobile.go | 246 --- zcncore/wallet_base.go | 888 ----------- 15 files changed, 289 insertions(+), 6281 deletions(-) rename zcncore/{functions.go => execute_transactions.go} (89%) create mode 100644 zcncore/get_data.go delete mode 100644 zcncore/networkworker_mobile.go delete mode 100644 zcncore/transaction_base.go delete mode 100644 zcncore/transaction_mobile.go delete mode 100644 zcncore/transaction_query.go delete mode 100644 zcncore/transaction_query_base.go delete mode 100644 zcncore/transaction_query_mobile.go delete mode 100644 zcncore/transaction_query_test.go delete mode 100644 zcncore/transactionauth.go delete mode 100644 zcncore/transactionauth_base.go delete mode 100644 zcncore/transactionauth_mobile.go diff --git a/core/client/set.go b/core/client/set.go index fce37812b..58b0b8686 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -2,6 +2,7 @@ package client import ( "errors" + "fmt" "strings" "github.com/0chain/gosdk/constants" @@ -133,6 +134,7 @@ func Wallet() *zcncrypto.Wallet { } func SignatureScheme() string { + fmt.Println("Jayash client.signatureScheme", client.signatureScheme) return client.signatureScheme } diff --git a/zcncore/functions.go b/zcncore/execute_transactions.go similarity index 89% rename from zcncore/functions.go rename to zcncore/execute_transactions.go index c5ccdd302..5dcbe7c8a 100644 --- a/zcncore/functions.go +++ b/zcncore/execute_transactions.go @@ -5,6 +5,33 @@ import ( "github.com/0chain/gosdk/core/transaction" ) +// AuthorizerNode represents an authorizer node in the network +type AuthorizerNode struct { + ID string `json:"id"` + URL string `json:"url"` + Config *AuthorizerConfig `json:"config"` +} + +type scCollectReward struct { + ProviderId string `json:"provider_id"` + ProviderType int `json:"provider_type"` +} + +type MinerSCDelegatePool struct { + Settings StakePoolSettings `json:"settings"` +} + +// SimpleMiner represents a node in the network, miner or sharder. +type SimpleMiner struct { + ID string `json:"id"` +} + +// MinerSCMinerInfo interface for miner/sharder info functions on miner smart contract. +type MinerSCMinerInfo struct { + SimpleMiner `json:"simple_miner"` + MinerSCDelegatePool `json:"stake_pool"` +} + func MinerSCLock(providerId string, providerType Provider, lock uint64) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxnValue(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_LOCK, diff --git a/zcncore/get_data.go b/zcncore/get_data.go new file mode 100644 index 000000000..a89548f99 --- /dev/null +++ b/zcncore/get_data.go @@ -0,0 +1,260 @@ +package zcncore + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/common" + "github.com/0chain/gosdk/core/tokenrate" + "github.com/0chain/gosdk/core/util" + "github.com/0chain/gosdk/core/zcncrypto" + "net/url" + "strings" +) + +type GetClientResponse struct { + ID string `json:"id"` + Version string `json:"version"` + CreationDate int `json:"creation_date"` + PublicKey string `json:"public_key"` +} + +func GetClientDetails(clientID string) (*GetClientResponse, error) { + clientNode, err := client.GetNode() + if err != nil { + panic(err) + } + minerurl := util.GetRandom(clientNode.Network().Miners, 1)[0] + url := minerurl + GET_CLIENT + url = fmt.Sprintf("%v?id=%v", url, clientID) + req, err := util.NewHTTPGetRequest(url) + if err != nil { + logging.Error(minerurl, "new get request failed. ", err.Error()) + return nil, err + } + res, err := req.Get() + if err != nil { + logging.Error(minerurl, "send error. ", err.Error()) + return nil, err + } + + var clientDetails GetClientResponse + err = json.Unmarshal([]byte(res.Body), &clientDetails) + if err != nil { + return nil, err + } + + return &clientDetails, nil +} + +// Deprecated: Use zcncrypto.IsMnemonicValid() +// IsMnemonicValid is an utility function to check the mnemonic valid +// +// # Inputs +// - mnemonic: mnemonics +func IsMnemonicValid(mnemonic string) bool { + return zcncrypto.IsMnemonicValid(mnemonic) +} + +// SetWalletInfo should be set before any transaction or client specific APIs +// splitKeyWallet parameter is valid only if SignatureScheme is "BLS0Chain" +// +// # Inputs +// - jsonWallet: json format of wallet +// { +// "client_id":"30764bcba73216b67c36b05a17b4dd076bfdc5bb0ed84856f27622188c377269", +// "client_key":"1f495df9605a4479a7dd6e5c7a78caf9f9d54e3a40f62a3dd68ed377115fe614d8acf0c238025f67a85163b9fbf31d10fbbb4a551d1cf00119897edf18b1841c", +// "keys":[ +// {"public_key":"1f495df9605a4479a7dd6e5c7a78caf9f9d54e3a40f62a3dd68ed377115fe614d8acf0c238025f67a85163b9fbf31d10fbbb4a551d1cf00119897edf18b1841c","private_key":"41729ed8d82f782646d2d30b9719acfd236842b9b6e47fee12b7bdbd05b35122"} +// ], +// "mnemonics":"glare mistake gun joke bid spare across diagram wrap cube swear cactus cave repeat you brave few best wild lion pitch pole original wasp", +// "version":"1.0", +// "date_created":"1662534022", +// "nonce":0 +// } +// +// - splitKeyWallet: if wallet keys is split +func SetWalletInfo(jsonWallet, sigScheme string, splitKeyWallet bool) error { + wallet := zcncrypto.Wallet{} + err := json.Unmarshal([]byte(jsonWallet), &wallet) + if err != nil { + return errors.New("invalid jsonWallet: " + err.Error()) + } + + client.SetWallet(wallet) + client.SetSignatureScheme(sigScheme) + return client.SetSplitKeyWallet(splitKeyWallet) +} + +// SetAuthUrl will be called by app to set zauth URL to SDK. +// # Inputs +// - url: the url of zAuth server +func SetAuthUrl(url string) error { + return client.SetAuthUrl(url) +} + +func getWalletBalance(clientId string) (common.Balance, int64, error) { + err := checkSdkInit() + if err != nil { + return 0, 0, err + } + + cb := &walletCallback{} + cb.Add(1) + + go func() { + value, info, err := getBalanceFromSharders(clientId) + if err != nil && strings.TrimSpace(info) != `{"error":"value not present"}` { + cb.OnBalanceAvailable(StatusError, value, info) + cb.err = err + return + } + cb.OnBalanceAvailable(StatusSuccess, value, info) + }() + + cb.Wait() + + var clientState struct { + Nonce int64 `json:"nonce"` + } + err = json.Unmarshal([]byte(cb.info), &clientState) + if err != nil { + return 0, 0, err + } + + return cb.balance, clientState.Nonce, cb.err +} + +// GetBalance retrieve wallet balance from sharders +// - cb: info callback instance, carries the response of the GET request to the sharders +func GetBalance(cb GetBalanceCallback) error { + err := CheckConfig() + if err != nil { + return err + } + go func() { + value, info, err := getBalanceFromSharders(client.Wallet().ClientID) + if err != nil { + logging.Error(err) + cb.OnBalanceAvailable(StatusError, 0, info) + return + } + cb.OnBalanceAvailable(StatusSuccess, value, info) + }() + return nil +} + +//// GetMintNonce retrieve the client's latest mint nonce from sharders +//// - cb: info callback instance, carries the response of the GET request to the sharders +//func GetMintNonce(cb GetInfoCallback) error { +// err := CheckConfig() +// if err != nil { +// return err +// } +// +// go GetInfoFromSharders(withParams(GET_MINT_NONCE, Params{ +// "client_id": client.Wallet().ClientID, +// }), OpGetMintNonce, cb) +// return nil +//} +// +//// GetNotProcessedZCNBurnTickets retrieve burn tickets that are not compensated by minting +//// - ethereumAddress: ethereum address for the issuer of the burn tickets +//// - startNonce: start nonce for the burn tickets +//// - cb: info callback instance, carries the response of the GET request to the sharders +//func GetNotProcessedZCNBurnTickets(ethereumAddress, startNonce string, cb GetInfoCallback) error { +// err := CheckConfig() +// if err != nil { +// return err +// } +// +// go GetInfoFromSharders(withParams(GET_NOT_PROCESSED_BURN_TICKETS, Params{ +// "ethereum_address": ethereumAddress, +// "nonce": startNonce, +// }), OpGetNotProcessedBurnTickets, cb) +// +// return nil +//} + +func getBalanceFromSharders(clientID string) (int64, string, error) { + clientNode, err := client.GetNode() + if err != nil { + return 0, "", err + } + return clientNode.Sharders().GetBalanceFieldFromSharders(clientID, "balance") +} + +// ConvertTokenToUSD converts the ZCN tokens to USD amount +// - token: ZCN tokens amount +func ConvertTokenToUSD(token float64) (float64, error) { + zcnRate, err := getTokenUSDRate() + if err != nil { + return 0, err + } + return token * zcnRate, nil +} + +// ConvertUSDToToken converts the USD amount to ZCN tokens +// - usd: USD amount +func ConvertUSDToToken(usd float64) (float64, error) { + zcnRate, err := getTokenUSDRate() + if err != nil { + return 0, err + } + return usd * (1 / zcnRate), nil +} + +func getTokenUSDRate() (float64, error) { + return tokenrate.GetUSD(context.TODO(), "zcn") +} + +// getWallet get a wallet object from a wallet string +func getWallet(walletStr string) (*zcncrypto.Wallet, error) { + var w zcncrypto.Wallet + err := json.Unmarshal([]byte(walletStr), &w) + if err != nil { + fmt.Printf("error while parsing wallet string.\n%v\n", err) + return nil, err + } + + return &w, nil +} + +type Params map[string]string + +func (p Params) Query() string { + if len(p) == 0 { + return "" + } + var params = make(url.Values) + for k, v := range p { + params[k] = []string{v} + } + return "?" + params.Encode() +} + +func withParams(uri string, params Params) string { + return uri + params.Query() +} + +// GetBlobberSnapshots obtains list of allocations of a blobber. +// Blobber snapshots are historical records of the blobber instance to track its change over time and serve graph requests, +// which are requests that need multiple data points, distributed over an interval of time, usually to plot them on a +// graph. +// - round: round number +// - limit: how many blobber snapshots should be fetched +// - offset: how many blobber snapshots should be skipped +// - cb: info callback instance, carries the response of the GET request to the sharders +//func GetBlobberSnapshots(round int64, limit int64, offset int64) (res []byte, err error) { +// if err = CheckConfig(); err != nil { +// return +// } +// +// return coreHttp.MakeSCRestAPICall(StorageSmartContractAddress, STORAGE_GET_BLOBBER_SNAPSHOT, Params{ +// "round": strconv.FormatInt(round, 10), +// "limit": strconv.FormatInt(limit, 10), +// "offset": strconv.FormatInt(offset, 10), +// }, nil) +//} diff --git a/zcncore/networkworker_mobile.go b/zcncore/networkworker_mobile.go deleted file mode 100644 index e953356bf..000000000 --- a/zcncore/networkworker_mobile.go +++ /dev/null @@ -1,125 +0,0 @@ -//go:build mobile -// +build mobile - -package zcncore - -import ( - "context" - "encoding/json" - - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/conf" -) - -const NETWORK_ENDPOINT = "/network" - -type Network struct { - net network -} - -func NewNetwork() *Network { - return &Network{} -} - -func (net *Network) AddMiner(miner string) { - net.net.Miners = append(net.net.Miners, miner) -} - -func (net *Network) AddSharder(sharder string) { - net.net.Sharders = append(net.net.Sharders, sharder) -} - -type network struct { - Miners []string `json:"miners"` - Sharders []string `json:"sharders"` -} - -//Deprecated: Get client.Node instance to check whether network update is required and update network accordingly -func UpdateNetworkDetails() error { - nodeClient, err := client.GetNode() - if err != nil { - return err - } - shouldUpdate, network, err := nodeClient.ShouldUpdateNetwork() - if err != nil { - logging.Error("error on ShouldUpdateNetwork check: ", err) - return err - } - if shouldUpdate { - logging.Info("Updating network") - if err = nodeClient.UpdateNetwork(network); err != nil { - logging.Error("error on updating network: ", err) - return err - } - logging.Info("network updated successfully") - } - return nil -} - -//Deprecated: Get client.Node instance to check whether network update is required -func UpdateRequired(networkDetails *Network) bool { - nodeClient, err := client.GetNode() - if err != nil { - panic(err) - } - shouldUpdate, _, err := nodeClient.ShouldUpdateNetwork() - if err != nil { - logging.Error("error on ShouldUpdateNetwork check: ", err) - panic(err) - } - return shouldUpdate -} - -//Deprecated: Use client.GetNetwork() function -func GetNetworkDetails() (*Network, error) { - cfg, err := conf.GetClientConfig() - if err != nil { - return nil, err - } - network, err := client.GetNetwork(context.Background(), cfg.BlockWorker) - if err != nil { - return nil, err - } - n := NewNetwork() - n.net.Miners = network.Miners - n.net.Sharders = network.Sharders - return n, nil -} - -//Deprecated: Use client.Node instance to get its network details -func GetNetwork() *Network { - nodeClient, err := client.GetNode() - if err != nil { - panic(err) - } - n := NewNetwork() - n.net.Miners = nodeClient.Network().Miners - n.net.Sharders = nodeClient.Network().Sharders - return n -} - -//Deprecated: Use client.Node instance UpdateNetwork() method -func SetNetwork(miners []string, sharders []string) { - nodeClient, err := client.GetNode() - if err != nil { - panic(err) - } - network, err := conf.NewNetwork(miners, sharders) - if err != nil { - panic(err) - } - err = nodeClient.UpdateNetwork(network) - if err != nil { - logging.Error("error updating network: ", err) - panic(err) - } - logging.Info("network updated successfully") -} - - -//Deprecated: Use client.GetNetwork() function -func GetNetworkJSON() string { - network := GetNetwork() - networkBytes, _ := json.Marshal(network) - return string(networkBytes) -} diff --git a/zcncore/transaction.go b/zcncore/transaction.go index 580d7e4dc..68b1cc458 100644 --- a/zcncore/transaction.go +++ b/zcncore/transaction.go @@ -4,22 +4,9 @@ package zcncore import ( - "context" - "encoding/json" - "fmt" - "math" - "net/http" - "sync" "time" - "github.com/0chain/errors" - "github.com/0chain/gosdk/core/block" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" - "github.com/0chain/gosdk/core/encryption" - "github.com/0chain/gosdk/core/node" - "github.com/0chain/gosdk/core/transaction" - "github.com/0chain/gosdk/core/util" ) // Provider represents the type of provider. @@ -33,26 +20,8 @@ const ( ProviderAuthorizer ) -type TransactionVelocity = float64 - -// Transaction velocity vs cost factor -// TODO: Pass it to miner to calculate real time factor -const ( - RegularTransaction TransactionVelocity = 1.0 - FastTransaction TransactionVelocity = 1.3 - FasterTransaction TransactionVelocity = 1.6 -) - type ConfirmationStatus int -const ( - Undefined ConfirmationStatus = iota - Success - - // ChargeableError is an error that still charges the user for the transaction. - ChargeableError -) - type Miner struct { ID string `json:"id"` N2NHost string `json:"n2n_host"` @@ -109,48 +78,6 @@ type MinerSCUserPoolsInfo struct { Pools map[string][]*MinerSCDelegatePoolInfo `json:"pools"` } -type TransactionCommon interface { - // ExecuteSmartContract implements wrapper for smart contract function - ExecuteSmartContract(address, methodName string, input interface{}, val uint64, feeOpts ...FeeOption) (*transaction.Transaction, error) - // Send implements sending token to a given clientid - Send(toClientID string, val uint64, desc string) error - - MinerSCLock(providerId string, providerType Provider, lock uint64) error - MinerSCUnlock(providerId string, providerType Provider) error - MinerSCCollectReward(providerID string, providerType Provider) error - MinerSCKill(providerID string, providerType Provider) error - - StorageSCCollectReward(providerID string, providerType Provider) error - - MinerScUpdateConfig(*InputMap) error - MinerScUpdateGlobals(*InputMap) error - StorageScUpdateConfig(*InputMap) error - AddHardfork(ip *InputMap) (err error) - FaucetUpdateConfig(*InputMap) error - ZCNSCUpdateGlobalConfig(*InputMap) error - - MinerSCMinerSettings(*MinerSCMinerInfo) error - MinerSCSharderSettings(*MinerSCMinerInfo) error - MinerSCDeleteMiner(*MinerSCMinerInfo) error - MinerSCDeleteSharder(*MinerSCMinerInfo) error - - // ZCNSCUpdateAuthorizerConfig updates authorizer config by ID - ZCNSCUpdateAuthorizerConfig(*AuthorizerNode) error - // ZCNSCAddAuthorizer adds authorizer - ZCNSCAddAuthorizer(*AddAuthorizerPayload) error - - // ZCNSCAuthorizerHealthCheck provides health check for authorizer - ZCNSCAuthorizerHealthCheck(*AuthorizerHealthCheckPayload) error - - // GetVerifyConfirmationStatus implements the verification status from sharders - GetVerifyConfirmationStatus() ConfirmationStatus - - // ZCNSCDeleteAuthorizer deletes authorizer - ZCNSCDeleteAuthorizer(*DeleteAuthorizerPayload) error - - ZCNSCCollectReward(providerID string, providerType Provider) error -} - // PriceRange represents a price range allowed by user to filter blobbers. type PriceRange struct { Min common.Balance `json:"min"` @@ -244,808 +171,3 @@ type AuthorizerConfig struct { type InputMap struct { Fields map[string]string `json:"Fields"` } - -// NewTransaction new generic transaction object for any operation -// - cb: callback for transaction state -// - txnFee: Transaction fees (in SAS tokens) -// - nonce: latest nonce of current wallet. please set it with 0 if you don't know the latest value -func NewTransaction(cb TransactionCallback, txnFee uint64, nonce int64) (TransactionScheme, error) { - err := CheckConfig() - if err != nil { - return nil, err - } - if client.SplitKeyWallet() { - if client.AuthUrl() == "" { - return nil, errors.New("", "auth url not set") - } - logging.Info("New transaction interface with auth") - return newTransactionWithAuth(cb, txnFee, nonce) - } - logging.Info("New transaction interface") - return newTransaction(cb, txnFee, nonce) -} - -func (t *Transaction) ExecuteSmartContract(address, methodName string, input interface{}, val uint64, opts ...FeeOption) (*transaction.Transaction, error) { - err := t.createSmartContractTxn(address, methodName, input, val, opts...) - if err != nil { - return nil, err - } - go func() { - t.setNonceAndSubmit() - }() - return t.txn, nil -} - -func (t *Transaction) Send(toClientID string, val uint64, desc string) error { - txnData, err := json.Marshal(transaction.SmartContractTxnData{Name: "transfer", InputArgs: SendTxnData{Note: desc}}) - if err != nil { - return errors.New("", "Could not serialize description to transaction_data") - } - clientNode, err := client.GetNode() - if err != nil { - return err - } - - t.txn.TransactionType = transaction.TxnTypeSend - t.txn.ToClientID = toClientID - t.txn.Value = val - t.txn.TransactionData = string(txnData) - if t.txn.TransactionFee == 0 { - fee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners, 0.2) - if err != nil { - return err - } - t.txn.TransactionFee = fee - } - - go func() { - t.setNonceAndSubmit() - }() - return nil -} - -func (t *Transaction) SendWithSignatureHash(toClientID string, val uint64, desc string, sig string, CreationDate int64, hash string) error { - txnData, err := json.Marshal(SendTxnData{Note: desc}) - if err != nil { - return errors.New("", "Could not serialize description to transaction_data") - } - clientNode, err := client.GetNode() - if err != nil { - return err - } - t.txn.TransactionType = transaction.TxnTypeSend - t.txn.ToClientID = toClientID - t.txn.Value = val - t.txn.Hash = hash - t.txn.TransactionData = string(txnData) - t.txn.Signature = sig - t.txn.CreationDate = CreationDate - if t.txn.TransactionFee == 0 { - fee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners, 0.2) - if err != nil { - return err - } - t.txn.TransactionFee = fee - } - - go func() { - t.setNonceAndSubmit() - }() - return nil -} - -func (t *Transaction) MinerSCLock(providerId string, providerType Provider, lock uint64) error { - if lock > math.MaxInt64 { - return errors.New("invalid_lock", "int64 overflow on lock value") - } - - pr := &stakePoolRequest{ - ProviderID: providerId, - ProviderType: providerType, - } - err := t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_LOCK, pr, lock) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return err -} -func (t *Transaction) MinerSCUnlock(providerId string, providerType Provider) error { - pr := &stakePoolRequest{ - ProviderID: providerId, - ProviderType: providerType, - } - err := t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_UNLOCK, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return err -} - -func (t *Transaction) MinerSCCollectReward(providerId string, providerType Provider) error { - pr := &scCollectReward{ - ProviderId: providerId, - ProviderType: int(providerType), - } - err := t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_COLLECT_REWARD, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return err -} - -func (t *Transaction) MinerSCKill(providerId string, providerType Provider) error { - pr := &scCollectReward{ - ProviderId: providerId, - ProviderType: int(providerType), - } - var name string - switch providerType { - case ProviderMiner: - name = transaction.MINERSC_KILL_MINER - case ProviderSharder: - name = transaction.MINERSC_KILL_SHARDER - default: - return fmt.Errorf("kill provider type %v not implimented", providerType) - } - - err := t.createSmartContractTxn(MinerSmartContractAddress, name, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return err -} - -// faucet smart contract - -func (t *Transaction) FaucetUpdateConfig(ip *InputMap) (err error) { - - err = t.createSmartContractTxn(FaucetSmartContractAddress, - transaction.FAUCETSC_UPDATE_SETTINGS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -// -// miner SC -// - -func (t *Transaction) MinerScUpdateConfig(ip *InputMap) (err error) { - err = t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_UPDATE_SETTINGS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -func (t *Transaction) MinerScUpdateGlobals(ip *InputMap) (err error) { - err = t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_UPDATE_GLOBALS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -func (t *Transaction) StorageScUpdateConfig(ip *InputMap) (err error) { - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_UPDATE_SETTINGS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} -func (t *Transaction) AddHardfork(ip *InputMap) (err error) { - err = t.createSmartContractTxn(MinerSmartContractAddress, - transaction.ADD_HARDFORK, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -func (t *Transaction) StorageSCCollectReward(providerId string, providerType Provider) error { - pr := &scCollectReward{ - ProviderId: providerId, - ProviderType: int(providerType), - } - err := t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_COLLECT_REWARD, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go t.setNonceAndSubmit() - return err -} - -func (t *Transaction) ZCNSCUpdateGlobalConfig(ip *InputMap) (err error) { - err = t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_UPDATE_GLOBAL_CONFIG, ip, 0) - if err != nil { - logging.Error(err) - return - } - go t.setNonceAndSubmit() - return -} - -func (t *Transaction) GetVerifyConfirmationStatus() ConfirmationStatus { - return ConfirmationStatus(t.verifyConfirmationStatus) -} - -type MinerSCDelegatePool struct { - Settings StakePoolSettings `json:"settings"` -} - -// SimpleMiner represents a node in the network, miner or sharder. -type SimpleMiner struct { - ID string `json:"id"` -} - -// MinerSCMinerInfo interface for miner/sharder info functions on miner smart contract. -type MinerSCMinerInfo struct { - SimpleMiner `json:"simple_miner"` - MinerSCDelegatePool `json:"stake_pool"` -} - -func (t *Transaction) MinerSCMinerSettings(info *MinerSCMinerInfo) (err error) { - err = t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_MINER_SETTINGS, info, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -func (t *Transaction) MinerSCSharderSettings(info *MinerSCMinerInfo) (err error) { - err = t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_SHARDER_SETTINGS, info, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -func (t *Transaction) MinerSCDeleteMiner(info *MinerSCMinerInfo) (err error) { - err = t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_MINER_DELETE, info, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -func (t *Transaction) MinerSCDeleteSharder(info *MinerSCMinerInfo) (err error) { - err = t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_SHARDER_DELETE, info, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -// AuthorizerNode represents an authorizer node in the network -type AuthorizerNode struct { - ID string `json:"id"` - URL string `json:"url"` - Config *AuthorizerConfig `json:"config"` -} - -func (t *Transaction) ZCNSCUpdateAuthorizerConfig(ip *AuthorizerNode) (err error) { - err = t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_UPDATE_AUTHORIZER_CONFIG, ip, 0) - if err != nil { - logging.Error(err) - return - } - go t.setNonceAndSubmit() - return -} - -func (t *Transaction) Verify() error { - clientNode, err := client.GetNode() - if err != nil { - return err - } - if t.txnHash == "" && t.txnStatus == StatusUnknown { - return errors.New("", "invalid transaction. cannot be verified.") - } - if t.txnHash == "" && t.txnStatus == StatusSuccess { - h := t.GetTransactionHash() - if h == "" { - node.Cache.Evict(t.txn.ClientID) - return errors.New("", "invalid transaction. cannot be verified.") - } - } - // If transaction is verify only start from current time - if t.txn.CreationDate == 0 { - t.txn.CreationDate = int64(common.Now()) - } - - tq, err := NewTransactionQuery(clientNode.Sharders().Healthy(), clientNode.Network().Miners) - if err != nil { - logging.Error(err) - return err - } - - go func() { - - for { - - tq.Reset() - // Get transaction confirmationBlock from a random sharder - confirmBlockHeader, confirmationBlock, lfbBlockHeader, err := tq.getFastConfirmation(context.TODO(), t.txnHash) - if err != nil { - now := int64(common.Now()) - - // maybe it is a network or server error - if lfbBlockHeader == nil { - logging.Info(err, " now: ", now) - } else { - logging.Info(err, " now: ", now, ", LFB creation time:", lfbBlockHeader.CreationDate) - } - - // transaction is done or expired. it means random sharder might be outdated, try to query it from s/S sharders to confirm it - if util.MaxInt64(lfbBlockHeader.getCreationDate(now), now) >= (t.txn.CreationDate + int64(defaultTxnExpirationSeconds)) { - logging.Info("falling back to ", clientNode.GetMinShardersVerify(), " of ", len(clientNode.Network().Sharders), " Sharders") - confirmBlockHeader, confirmationBlock, lfbBlockHeader, err = tq.getConsensusConfirmation(context.TODO(), clientNode.GetMinShardersVerify(), t.txnHash) - } - - // txn not found in fast confirmation/consensus confirmation - if err != nil { - - if lfbBlockHeader == nil { - // no any valid lfb on all sharders. maybe they are network/server errors. try it again - continue - } - - // it is expired - if t.isTransactionExpired(lfbBlockHeader.getCreationDate(now), now) { - t.completeVerify(StatusError, "", errors.New("", `{"error": "verify transaction failed"}`)) - return - } - continue - } - } - - valid := validateChain(confirmBlockHeader) - if valid { - output, err := json.Marshal(confirmationBlock) - if err != nil { - t.completeVerify(StatusError, "", errors.New("", `{"error": "transaction confirmation json marshal error"`)) - return - } - confJson := confirmationBlock["confirmation"] - - var conf map[string]json.RawMessage - if err := json.Unmarshal(confJson, &conf); err != nil { - return - } - txnJson := conf["txn"] - - tt := transaction.Transaction{} - if err := json.Unmarshal(txnJson, &tt); err != nil { - return - } - - *t.txn = tt - txStatus := tt.Status - - switch txStatus { - case 1: - t.completeVerifyWithConStatus(StatusSuccess, int(Success), string(output), nil) - case 2: - t.completeVerifyWithConStatus(StatusSuccess, int(ChargeableError), tt.TransactionOutput, nil) - default: - t.completeVerify(StatusError, string(output), nil) - } - return - } - } - }() - return nil -} - -// ConvertToValue converts ZCN tokens to SAS tokens -// # Inputs -// - token: ZCN tokens -func ConvertToValue(token float64) uint64 { - return uint64(token * common.TokenUnit) -} - -func GetLatestFinalized(ctx context.Context, numSharders int) (b *block.Header, err error) { - clientNode, err := client.GetNode() - if err != nil { - return nil, err - } - var result = make(chan *util.GetResponse, numSharders) - defer close(result) - - numSharders = len(clientNode.Sharders().Healthy()) // overwrite, use all - clientNode.Sharders().QueryFromShardersContext(ctx, numSharders, GET_LATEST_FINALIZED, result) - - var ( - maxConsensus int - roundConsensus = make(map[string]int) - ) - - for i := 0; i < numSharders; i++ { - var rsp = <-result - if rsp == nil { - logging.Error("nil response") - continue - } - - logging.Debug(rsp.Url, rsp.Status) - - if rsp.StatusCode != http.StatusOK { - logging.Error(rsp.Body) - continue - } - - if err = json.Unmarshal([]byte(rsp.Body), &b); err != nil { - logging.Error("block parse error: ", err) - err = nil - continue - } - - var h = encryption.FastHash([]byte(b.Hash)) - if roundConsensus[h]++; roundConsensus[h] > maxConsensus { - maxConsensus = roundConsensus[h] - } - } - - if maxConsensus == 0 { - return nil, errors.New("", "block info not found") - } - - return -} - -// GetLatestFinalizedMagicBlock gets latest finalized magic block -// - numSharders: number of sharders -// - timeout: request timeout -func GetLatestFinalizedMagicBlock(ctx context.Context, numSharders int) (m *block.MagicBlock, err error) { - clientNode, err := client.GetNode() - if err != nil { - return nil, err - } - var result = make(chan *util.GetResponse, numSharders) - defer close(result) - - numSharders = len(clientNode.Sharders().Healthy()) // overwrite, use all - clientNode.Sharders().QueryFromShardersContext(ctx, numSharders, GET_LATEST_FINALIZED_MAGIC_BLOCK, result) - - var ( - maxConsensus int - roundConsensus = make(map[string]int) - ) - - type respObj struct { - MagicBlock *block.MagicBlock `json:"magic_block"` - } - - for i := 0; i < numSharders; i++ { - var rsp = <-result - if rsp == nil { - logging.Error("nil response") - continue - } - - logging.Debug(rsp.Url, rsp.Status) - - if rsp.StatusCode != http.StatusOK { - logging.Error(rsp.Body) - continue - } - - var respo respObj - if err = json.Unmarshal([]byte(rsp.Body), &respo); err != nil { - logging.Error(" magic block parse error: ", err) - err = nil - continue - } - - m = respo.MagicBlock - var h = encryption.FastHash([]byte(respo.MagicBlock.Hash)) - if roundConsensus[h]++; roundConsensus[h] > maxConsensus { - maxConsensus = roundConsensus[h] - } - } - - if maxConsensus == 0 { - return nil, errors.New("", "magic block info not found") - } - - return -} - -func GetChainStats(ctx context.Context) (b *block.ChainStats, err error) { - clientNode, err := client.GetNode() - if err != nil { - return nil, err - } - - var result = make(chan *util.GetResponse, 1) - defer close(result) - - var numSharders = len(clientNode.Sharders().Healthy()) // overwrite, use all - clientNode.Sharders().QueryFromShardersContext(ctx, numSharders, GET_CHAIN_STATS, result) - var rsp *util.GetResponse - for i := 0; i < numSharders; i++ { - var x = <-result - if x == nil { - logging.Error("nil response") - continue - } - if x.StatusCode != http.StatusOK { - continue - } - rsp = x - } - - if rsp == nil { - return nil, errors.New("http_request_failed", "Request failed with status not 200") - } - - if err = json.Unmarshal([]byte(rsp.Body), &b); err != nil { - return nil, err - } - return -} - -func GetFeeStats(ctx context.Context) (b *block.FeeStats, err error) { - clientNode, err := client.GetNode() - if err != nil { - return nil, err - } - var numMiners = 4 - - if numMiners > len(clientNode.Network().Miners) { - numMiners = len(clientNode.Network().Miners) - } - - var result = make(chan *util.GetResponse, numMiners) - - queryFromMinersContext(ctx, numMiners, GET_FEE_STATS, result) - var rsp *util.GetResponse - -loop: - for i := 0; i < numMiners; i++ { - select { - case x := <-result: - if x.StatusCode != http.StatusOK { - continue - } - rsp = x - if rsp != nil { - break loop - } - case <-ctx.Done(): - err = ctx.Err() - return nil, err - } - } - if rsp == nil { - return nil, errors.New("http_request_failed", "Request failed with status not 200") - } - if err = json.Unmarshal([]byte(rsp.Body), &b); err != nil { - return nil, err - } - return -} - -func GetBlockByRound(ctx context.Context, numSharders int, round int64) (b *block.Block, err error) { - clientNode, err := client.GetNode() - if err != nil { - return nil, err - } - return block.GetBlockByRound(clientNode.Sharders(), ctx, numSharders, round) -} - -func GetRoundFromSharders() (int64, error) { - clientNode, err := client.GetNode() - if err != nil { - return 0, err - } - return clientNode.Sharders().GetRoundFromSharders() -} - -func GetHardForkRound(hardFork string) (int64, error) { - nodeClient, err := client.GetNode() - if err != nil { - return 0, err - } - return nodeClient.Sharders().GetHardForkRound(hardFork) -} - -func GetMagicBlockByNumber(ctx context.Context, numSharders int, number int64) (m *block.MagicBlock, err error) { - clientNode, err := client.GetNode() - if err != nil { - return nil, err - } - var result = make(chan *util.GetResponse, numSharders) - defer close(result) - - numSharders = len(clientNode.Sharders().Healthy()) // overwrite, use all - clientNode.Sharders().QueryFromShardersContext(ctx, numSharders, - fmt.Sprintf("%smagic_block_number=%d", GET_MAGIC_BLOCK_INFO, number), - result) - - var ( - maxConsensus int - roundConsensus = make(map[string]int) - ) - - type respObj struct { - MagicBlock *block.MagicBlock `json:"magic_block"` - } - - for i := 0; i < numSharders; i++ { - var rsp = <-result - if rsp == nil { - logging.Error("nil response") - continue - } - logging.Debug(rsp.Url, rsp.Status) - - if rsp.StatusCode != http.StatusOK { - logging.Error(rsp.Body) - continue - } - - var respo respObj - if err = json.Unmarshal([]byte(rsp.Body), &respo); err != nil { - logging.Error(" magic block parse error: ", err) - err = nil - continue - } - - m = respo.MagicBlock - var h = encryption.FastHash([]byte(respo.MagicBlock.Hash)) - if roundConsensus[h]++; roundConsensus[h] > maxConsensus { - maxConsensus = roundConsensus[h] - } - } - - if maxConsensus == 0 { - return nil, errors.New("", "magic block info not found") - } - - return -} - -type NonceCache struct { - cache map[string]int64 - guard sync.Mutex -} - -func NewNonceCache() *NonceCache { - return &NonceCache{cache: make(map[string]int64)} -} - -func (nc *NonceCache) GetNextNonce(clientId string) int64 { - nc.guard.Lock() - defer nc.guard.Unlock() - if _, ok := nc.cache[clientId]; !ok { - back := &getNonceCallBack{ - nonceCh: make(chan int64), - err: nil, - } - if err := GetNonce(back); err != nil { - return 0 - } - - timeout, cancel := context.WithTimeout(context.Background(), time.Second) - defer cancel() - select { - case n := <-back.nonceCh: - if back.err != nil { - return 0 - } - nc.cache[clientId] = n - case <-timeout.Done(): - return 0 - } - } - - nc.cache[clientId] += 1 - return nc.cache[clientId] -} - -func (nc *NonceCache) Set(clientId string, nonce int64) { - nc.guard.Lock() - defer nc.guard.Unlock() - nc.cache[clientId] = nonce -} - -func (nc *NonceCache) Evict(clientId string) { - nc.guard.Lock() - defer nc.guard.Unlock() - delete(nc.cache, clientId) -} - -func (t *Transaction) ZCNSCAddAuthorizer(ip *AddAuthorizerPayload) (err error) { - err = t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_ADD_AUTHORIZER, ip, 0) - if err != nil { - logging.Error(err) - return - } - go t.setNonceAndSubmit() - return -} - -func (t *Transaction) ZCNSCAuthorizerHealthCheck(ip *AuthorizerHealthCheckPayload) (err error) { - err = t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_AUTHORIZER_HEALTH_CHECK, ip, 0) - if err != nil { - logging.Error(err) - return - } - go t.setNonceAndSubmit() - return -} - -func (t *Transaction) ZCNSCDeleteAuthorizer(ip *DeleteAuthorizerPayload) (err error) { - err = t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_DELETE_AUTHORIZER, ip, 0) - if err != nil { - logging.Error(err) - return - } - go t.setNonceAndSubmit() - return -} - -func (t *Transaction) ZCNSCCollectReward(providerId string, providerType Provider) error { - pr := &scCollectReward{ - ProviderId: providerId, - ProviderType: int(providerType), - } - err := t.createSmartContractTxn(ZCNSCSmartContractAddress, - transaction.ZCNSC_COLLECT_REWARD, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return err -} - -// faucet - -func GetFaucetSCConfig(cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - go GetInfoFromSharders(GET_FAUCETSC_CONFIG, 0, cb) - return -} diff --git a/zcncore/transaction_base.go b/zcncore/transaction_base.go deleted file mode 100644 index cae7c1058..000000000 --- a/zcncore/transaction_base.go +++ /dev/null @@ -1,838 +0,0 @@ -package zcncore - -import ( - "context" - "encoding/json" - stdErrors "errors" - "fmt" - "net/http" - "sync/atomic" - "time" - - "github.com/0chain/errors" - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/conf" - "github.com/0chain/gosdk/core/node" - "github.com/0chain/gosdk/core/sys" - "github.com/0chain/gosdk/zboxcore/logger" - "go.uber.org/zap" - - "github.com/0chain/gosdk/core/encryption" - "github.com/0chain/gosdk/core/transaction" - "github.com/0chain/gosdk/core/util" - "github.com/0chain/gosdk/core/zcncrypto" - "github.com/0chain/gosdk/zboxcore/sdk" -) - -// compiler time check -var ( - _ TransactionScheme = (*Transaction)(nil) - _ TransactionScheme = (*TransactionWithAuth)(nil) -) - -var ( - errNetwork = errors.New("", "network error. host not reachable") - errUserRejected = errors.New("", "rejected by user") - errAuthVerifyFailed = errors.New("", "verification failed for auth response") - errAuthTimeout = errors.New("", "auth timed out") - errAddSignature = errors.New("", "error adding signature") -) - -// TransactionScheme implements few methods for block chain. -// -// Note: to be buildable on MacOSX all arguments should have names. -type TransactionScheme interface { - TransactionCommon - // SetTransactionCallback implements storing the callback - // used to call after the transaction or verification is completed - SetTransactionCallback(cb TransactionCallback) error - // StoreData implements store the data to blockchain - StoreData(data string) error - // ExecuteFaucetSCWallet implements the `Faucet Smart contract` for a given wallet - ExecuteFaucetSCWallet(walletStr string, methodName string, input []byte) error - // GetTransactionHash implements retrieval of hash of the submitted transaction - GetTransactionHash() string - // SetTransactionHash implements verify a previous transaction status - SetTransactionHash(hash string) error - // SetTransactionNonce implements method to set the transaction nonce - SetTransactionNonce(txnNonce int64) error - // Verify implements verify the transaction - Verify() error - // GetVerifyOutput implements the verification output from sharders - GetVerifyOutput() string - // GetTransactionError implements error string in case of transaction failure - GetTransactionError() string - // GetVerifyError implements error string in case of verify failure error - GetVerifyError() string - // GetTransactionNonce returns nonce - GetTransactionNonce() int64 - - // Output of transaction. - Output() []byte - - // Hash Transaction status regardless of status - Hash() string - - // Miner SC -} - -// TransactionCallback needs to be implemented by the caller for transaction related APIs -type TransactionCallback interface { - OnTransactionComplete(t *Transaction, status int) - OnVerifyComplete(t *Transaction, status int) - OnAuthComplete(t *Transaction, status int) -} - -type ChainConfig struct { - ChainID string `json:"chain_id,omitempty"` - BlockWorker string `json:"block_worker"` - Miners []string `json:"miners"` - Sharders []string `json:"sharders"` - SignatureScheme string `json:"signature_scheme"` - MinSubmit int `json:"min_submit"` - MinConfirmation int `json:"min_confirmation"` - ConfirmationChainLength int `json:"confirmation_chain_length"` - EthNode string `json:"eth_node"` - SharderConsensous int `json:"sharder_consensous"` -} - -// Deprecated: Use client.Init() in core/client package -// InitZCNSDK initializes the SDK with miner, sharder and signature scheme provided. -func InitZCNSDK(blockWorker string, signscheme string, configs ...func(*ChainConfig) error) error { - if signscheme != "ed25519" && signscheme != "bls0chain" { - return errors.New("", "invalid/unsupported signature scheme") - } - - chainCfg := &ChainConfig{} - for _, conf := range configs { - err := conf(chainCfg) - if err != nil { - return errors.Wrap(err, "invalid/unsupported options.") - } - } - cfg := conf.Config{ - BlockWorker: blockWorker, - SignatureScheme: signscheme, - MinSubmit: chainCfg.MinSubmit, - MinConfirmation: chainCfg.MinConfirmation, - ConfirmationChainLength: chainCfg.ConfirmationChainLength, - ChainID: chainCfg.ChainID, - EthereumNode: chainCfg.EthNode, - SharderConsensous: chainCfg.SharderConsensous, - } - return client.Init(context.Background(), cfg) -} - -/*Confirmation - a data structure that provides the confirmation that a transaction is included into the block chain */ -type confirmation struct { - Version string `json:"version"` - Hash string `json:"hash"` - BlockHash string `json:"block_hash"` - PreviousBlockHash string `json:"previous_block_hash"` - Transaction *transaction.Transaction `json:"txn,omitempty"` - CreationDate int64 `json:"creation_date,omitempty"` - MinerID string `json:"miner_id"` - Round int64 `json:"round"` - Status int `json:"transaction_status" msgpack:"sot"` - RoundRandomSeed int64 `json:"round_random_seed"` - StateChangesCount int `json:"state_changes_count"` - MerkleTreeRoot string `json:"merkle_tree_root"` - MerkleTreePath *util.MTPath `json:"merkle_tree_path"` - ReceiptMerkleTreeRoot string `json:"receipt_merkle_tree_root"` - ReceiptMerkleTreePath *util.MTPath `json:"receipt_merkle_tree_path"` -} - -type blockHeader struct { - Version string `json:"version,omitempty"` - CreationDate int64 `json:"creation_date,omitempty"` - Hash string `json:"hash,omitempty"` - MinerId string `json:"miner_id,omitempty"` - Round int64 `json:"round,omitempty"` - RoundRandomSeed int64 `json:"round_random_seed,omitempty"` - StateChangesCount int `json:"state_changes_count"` - MerkleTreeRoot string `json:"merkle_tree_root,omitempty"` - StateHash string `json:"state_hash,omitempty"` - ReceiptMerkleTreeRoot string `json:"receipt_merkle_tree_root,omitempty"` - NumTxns int64 `json:"num_txns,omitempty"` -} - -func (bh *blockHeader) getCreationDate(defaultTime int64) int64 { - if bh == nil { - return defaultTime - } - - return bh.CreationDate -} - -type Transaction struct { - txn *transaction.Transaction - txnOut string - txnHash string - txnStatus int - txnError error - txnCb TransactionCallback - verifyStatus int - verifyConfirmationStatus int - verifyOut string - verifyError error -} - -type SendTxnData struct { - Note string `json:"note"` -} - -func Sign(hash string) (string, error) { - sigScheme := zcncrypto.NewSignatureScheme(client.SignatureScheme()) - err := sigScheme.SetPrivateKey(client.Wallet().Keys[0].PrivateKey) - if err != nil { - return "", err - } - return sigScheme.Sign(hash) -} - -func SignWithKey(privateKey, hash string) (string, error) { - sigScheme := zcncrypto.NewSignatureScheme("bls0chain") - err := sigScheme.SetPrivateKey(privateKey) - if err != nil { - return "", err - } - return sigScheme.Sign(hash) -} - -func VerifyWithKey(pubKey, signature, hash string) (bool, error) { - sigScheme := zcncrypto.NewSignatureScheme("bls0chain") - err := sigScheme.SetPublicKey(pubKey) - if err != nil { - return false, err - } - return sigScheme.Verify(signature, hash) -} - -var SignFn = func(hash string) (string, error) { - sigScheme := zcncrypto.NewSignatureScheme(client.SignatureScheme()) - err := sigScheme.SetPrivateKey(client.Wallet().Keys[0].PrivateKey) - if err != nil { - return "", err - } - return sigScheme.Sign(hash) -} - -var AddSignature = func(privateKey, signature string, hash string) (string, error) { - var ( - ss = zcncrypto.NewSignatureScheme(client.SignatureScheme()) - err error - ) - - err = ss.SetPrivateKey(privateKey) - if err != nil { - return "", err - } - - return ss.Add(signature, hash) -} - -func signWithWallet(hash string, wi interface{}) (string, error) { - w, ok := wi.(*zcncrypto.Wallet) - - if !ok { - fmt.Printf("Error in casting to wallet") - return "", errors.New("", "error in casting to wallet") - } - sigScheme := zcncrypto.NewSignatureScheme(client.SignatureScheme()) - err := sigScheme.SetPrivateKey(w.Keys[0].PrivateKey) - if err != nil { - return "", err - } - return sigScheme.Sign(hash) -} - -func txnTypeString(t int) string { - switch t { - case transaction.TxnTypeSend: - return "send" - case transaction.TxnTypeLockIn: - return "lock-in" - case transaction.TxnTypeData: - return "data" - case transaction.TxnTypeSmartContract: - return "smart contract" - default: - return "unknown" - } -} - -// Output implements the output of transaction -func (t *Transaction) Output() []byte { - return []byte(t.txnOut) -} - -// Hash implements the hash of transaction -func (t *Transaction) Hash() string { - return t.txn.Hash -} - -func (t *Transaction) completeTxn(status int, out string, err error) { - t.txnStatus = status - t.txnOut = out - t.txnError = err - if t.txnCb != nil { - t.txnCb.OnTransactionComplete(t, t.txnStatus) - } -} - -func (t *Transaction) completeVerify(status int, out string, err error) { - t.completeVerifyWithConStatus(status, 0, out, err) -} - -func (t *Transaction) completeVerifyWithConStatus(status int, conStatus int, out string, err error) { - t.verifyStatus = status - t.verifyConfirmationStatus = conStatus - t.verifyOut = out - t.verifyError = err - if status == StatusError { - node.Cache.Evict(t.txn.ClientID) - } - if t.txnCb != nil { - t.txnCb.OnVerifyComplete(t, t.verifyStatus) - } -} - -type getNonceCallBack struct { - nonceCh chan int64 - err error -} - -func (g getNonceCallBack) OnNonceAvailable(status int, nonce int64, info string) { - if status != StatusSuccess { - g.err = errors.New("get_nonce", "failed respond nonce") //nolint - } - - g.nonceCh <- nonce -} - -func (t *Transaction) setNonceAndSubmit() { - t.setNonce() - - //hash, _, nonce, _, err := transaction.SmartContractTxnValueFeeWithRetry(MinerSmartContractAddress, t.txn, 0, client.TxnFee()) - t.submitTxn() -} - -func (t *Transaction) setNonce() { - nonce := t.txn.TransactionNonce - if nonce < 1 { - nonce = node.Cache.GetNextNonce(t.txn.ClientID) - } else { - node.Cache.Set(t.txn.ClientID, nonce) - } - t.txn.TransactionNonce = nonce -} - -func (t *Transaction) submitTxn() { - // Clear the status, in case transaction object reused - t.txnStatus = StatusUnknown - t.txnOut = "" - t.txnError = nil - - // If Signature is not passed compute signature - if t.txn.Signature == "" { - err := t.txn.ComputeHashAndSign(SignFn) - if err != nil { - t.completeTxn(StatusError, "", err) - node.Cache.Evict(t.txn.ClientID) - return - } - } - - nodeClient, err := client.GetNode() - if err != nil { - t.completeTxn(StatusError, "", err) - node.Cache.Evict(t.txn.ClientID) - return - } - - var ( - randomMiners = nodeClient.GetStableMiners() - minersN = len(randomMiners) - failedCount int32 - failC = make(chan struct{}) - resultC = make(chan *util.PostResponse, minersN) - ) - - for _, miner := range randomMiners { - go func(minerurl string) { - url := minerurl + PUT_TRANSACTION - logging.Info("Submitting ", txnTypeString(t.txn.TransactionType), " transaction to ", minerurl, " with JSON ", string(t.txn.DebugJSON())) - req, err := util.NewHTTPPostRequest(url, t.txn) - if err != nil { - logging.Error(minerurl, " new post request failed. ", err.Error()) - - if int(atomic.AddInt32(&failedCount, 1)) == minersN { - close(failC) - } - return - } - - res, err := req.Post() - if err != nil { - logging.Error(minerurl, " submit transaction error. ", err.Error()) - if int(atomic.AddInt32(&failedCount, 1)) == minersN { - close(failC) - } - return - } - - if res.StatusCode != http.StatusOK { - logging.Error(minerurl, " submit transaction failed with status code ", res.StatusCode) - if int(atomic.AddInt32(&failedCount, 1)) == minersN { - resultC <- res - } - return - } - - resultC <- res - }(miner) - } - - select { - case <-failC: - logging.Error("failed to submit transaction") - t.completeTxn(StatusError, "", fmt.Errorf("failed to submit transaction to all miners")) - node.Cache.Evict(t.txn.ClientID) - nodeClient.ResetStableMiners() - return - case ret := <-resultC: - logging.Debug("finish txn submitting, ", ret.Url, ", Status: ", ret.Status, ", output:", ret.Body) - if ret.StatusCode == http.StatusOK { - t.completeTxn(StatusSuccess, ret.Body, nil) - } else { - t.completeTxn(StatusError, "", fmt.Errorf("submit transaction failed. %s", ret.Body)) - node.Cache.Evict(t.txn.ClientID) - } - } -} - -func newTransaction(cb TransactionCallback, txnFee uint64, nonce int64) (*Transaction, error) { - cfg, err := conf.GetClientConfig() - if err != nil { - return nil, err - } - - t := &Transaction{} - t.txn = transaction.NewTransactionEntity(client.Wallet().ClientID, cfg.ChainID, client.Wallet().ClientKey, nonce) - t.txnStatus, t.verifyStatus = StatusUnknown, StatusUnknown - t.txnCb = cb - t.txn.TransactionNonce = nonce - t.txn.TransactionFee = txnFee - return t, nil -} - -// SetTransactionCallback implements storing the callback -func (t *Transaction) SetTransactionCallback(cb TransactionCallback) error { - if t.txnStatus != StatusUnknown { - return errors.New("", "transaction already exists. cannot set transaction hash.") - } - t.txnCb = cb - return nil -} - -// SetTransactionNonce implements method to set the transaction nonce -func (t *Transaction) SetTransactionNonce(txnNonce int64) error { - if t.txnStatus != StatusUnknown { - return errors.New("", "transaction already exists. cannot set transaction fee.") - } - t.txn.TransactionNonce = txnNonce - return nil -} - -// StoreData implements store the data to blockchain -func (t *Transaction) StoreData(data string) error { - go func() { - t.txn.TransactionType = transaction.TxnTypeData - t.txn.TransactionData = data - t.setNonceAndSubmit() - }() - return nil -} - -type TxnFeeOption struct { - // stop estimate txn fee, usually if txn fee was 0, the createSmartContractTxn method would - // estimate the txn fee by calling API from 0chain network. With this option, we could force - // the txn to have zero fee for those exempt transactions. - noEstimateFee bool -} - -// FeeOption represents txn fee related option type -type FeeOption func(*TxnFeeOption) - -// WithNoEstimateFee would prevent txn fee estimation from remote -func WithNoEstimateFee() FeeOption { - return func(o *TxnFeeOption) { - o.noEstimateFee = true - } -} - -func (t *Transaction) createSmartContractTxn(address, methodName string, input interface{}, value uint64, opts ...FeeOption) error { - sn := transaction.SmartContractTxnData{Name: methodName, InputArgs: input} - snBytes, err := json.Marshal(sn) - if err != nil { - return errors.Wrap(err, "create smart contract failed due to invalid data") - } - - t.txn.TransactionType = transaction.TxnTypeSmartContract - t.txn.ToClientID = address - t.txn.TransactionData = string(snBytes) - t.txn.Value = value - - if t.txn.TransactionFee > 0 { - return nil - } - - tf := &TxnFeeOption{} - for _, opt := range opts { - opt(tf) - } - - if tf.noEstimateFee { - return nil - } - - clientNode, err := client.GetNode() - if err != nil { - return err - } - - // TODO: check if transaction is exempt to avoid unnecessary fee estimation - minFee, err := transaction.EstimateFee(t.txn, clientNode.Network().Miners, 0.2) - if err != nil { - logger.Logger.Error("failed estimate txn fee", - zap.Any("txn", t.txn.Hash), - zap.Error(err)) - return err - } - - t.txn.TransactionFee = minFee - - return nil -} - -func (t *Transaction) createFaucetSCWallet(walletStr string, methodName string, input []byte) (*zcncrypto.Wallet, error) { - w, err := getWallet(walletStr) - if err != nil { - fmt.Printf("Error while parsing the wallet. %v\n", err) - return nil, err - } - err = t.createSmartContractTxn(FaucetSmartContractAddress, methodName, input, 0) - if err != nil { - return nil, err - } - return w, nil -} - -// ExecuteFaucetSCWallet implements the Faucet Smart contract for a given wallet -func (t *Transaction) ExecuteFaucetSCWallet(walletStr string, methodName string, input []byte) error { - w, err := t.createFaucetSCWallet(walletStr, methodName, input) - if err != nil { - return err - } - go func() { - nonce := t.txn.TransactionNonce - if nonce < 1 { - nonce = node.Cache.GetNextNonce(t.txn.ClientID) - } else { - node.Cache.Set(t.txn.ClientID, nonce) - } - t.txn.TransactionNonce = nonce - err = t.txn.ComputeHashAndSignWithWallet(signWithWallet, w) - if err != nil { - return - } - fmt.Printf("submitted transaction\n") - t.submitTxn() - }() - return nil -} - -// SetTransactionHash implements verify a previous transaction status -// - hash: transaction hash -func (t *Transaction) SetTransactionHash(hash string) error { - if t.txnStatus != StatusUnknown { - return errors.New("", "transaction already exists. cannot set transaction hash.") - } - t.txnHash = hash - return nil -} - -// GetTransactionHash implements retrieval of hash of the submitted transaction -func (t *Transaction) GetTransactionHash() string { - if t.txnHash != "" { - return t.txnHash - } - if t.txnStatus != StatusSuccess { - return "" - } - var txnout map[string]json.RawMessage - err := json.Unmarshal([]byte(t.txnOut), &txnout) - if err != nil { - fmt.Println("Error in parsing", err) - } - var entity map[string]interface{} - err = json.Unmarshal(txnout["entity"], &entity) - if err != nil { - logging.Error("json unmarshal error on GetTransactionHash()") - return t.txnHash - } - if hash, ok := entity["hash"].(string); ok { - t.txnHash = hash - } - return t.txnHash -} - -func queryFromMinersContext(ctx context.Context, numMiners int, query string, result chan *util.GetResponse) { - nodeClient, err := client.GetNode() - if err != nil { - panic(err) - } - randomMiners := util.Shuffle(nodeClient.Network().Miners)[:numMiners] - for _, miner := range randomMiners { - go func(minerurl string) { - logging.Info("Query from ", minerurl+query) - url := fmt.Sprintf("%v%v", minerurl, query) - req, err := util.NewHTTPGetRequestContext(ctx, url) - if err != nil { - logging.Error(minerurl, " new get request failed. ", err.Error()) - return - } - res, err := req.Get() - if err != nil { - logging.Error(minerurl, " get error. ", err.Error()) - } - result <- res - }(miner) - } - -} - -func getBlockHeaderFromTransactionConfirmation(txnHash string, cfmBlock map[string]json.RawMessage) (*blockHeader, error) { - block := &blockHeader{} - if cfmBytes, ok := cfmBlock["confirmation"]; ok { - var cfm confirmation - err := json.Unmarshal(cfmBytes, &cfm) - if err != nil { - return nil, errors.Wrap(err, "txn confirmation parse error.") - } - if cfm.Transaction == nil { - return nil, fmt.Errorf("missing transaction %s in block confirmation", txnHash) - } - if txnHash != cfm.Transaction.Hash { - return nil, fmt.Errorf("invalid transaction hash. Expected: %s. Received: %s", txnHash, cfm.Transaction.Hash) - } - if !util.VerifyMerklePath(cfm.Transaction.Hash, cfm.MerkleTreePath, cfm.MerkleTreeRoot) { - return nil, errors.New("", "txn merkle validation failed.") - } - txnRcpt := transaction.NewTransactionReceipt(cfm.Transaction) - if !util.VerifyMerklePath(txnRcpt.GetHash(), cfm.ReceiptMerkleTreePath, cfm.ReceiptMerkleTreeRoot) { - return nil, errors.New("", "txn receipt cmerkle validation failed.") - } - prevBlockHash := cfm.PreviousBlockHash - block.MinerId = cfm.MinerID - block.Hash = cfm.BlockHash - block.CreationDate = cfm.CreationDate - block.Round = cfm.Round - block.RoundRandomSeed = cfm.RoundRandomSeed - block.StateChangesCount = cfm.StateChangesCount - block.MerkleTreeRoot = cfm.MerkleTreeRoot - block.ReceiptMerkleTreeRoot = cfm.ReceiptMerkleTreeRoot - // Verify the block - if isBlockExtends(prevBlockHash, block) { - return block, nil - } - - return nil, errors.New("", "block hash verification failed in confirmation") - } - - return nil, errors.New("", "txn confirmation not found.") -} - -func getBlockInfoByRound(round int64, content string) (*blockHeader, error) { - nodeClient, err := client.GetNode() - if err != nil { - return nil, err - } - numSharders := len(nodeClient.Sharders().Healthy()) // overwrite, use all - resultC := make(chan *util.GetResponse, numSharders) - nodeClient.Sharders().QueryFromSharders(numSharders, fmt.Sprintf("%vround=%v&content=%v", GET_BLOCK_INFO, round, content), resultC) - var ( - maxConsensus int - roundConsensus = make(map[string]int) - waitTime = time.NewTimer(10 * time.Second) - failedCount int - ) - - type blockRound struct { - Header blockHeader `json:"header"` - } - - for i := 0; i < numSharders; i++ { - select { - case <-waitTime.C: - return nil, stdErrors.New("failed to get block info by round with consensus, timeout") - case rsp := <-resultC: - if rsp == nil { - logging.Error("nil response") - continue - } - logging.Debug(rsp.Url, rsp.Status) - if failedCount*100/numSharders > 100-consensusThresh { - return nil, stdErrors.New("failed to get block info by round with consensus, too many failures") - } - - if rsp.StatusCode != http.StatusOK { - logging.Debug(rsp.Url, "no round confirmation. Resp:", rsp.Body) - failedCount++ - continue - } - - var br blockRound - err := json.Unmarshal([]byte(rsp.Body), &br) - if err != nil { - logging.Error("round info parse error. ", err) - failedCount++ - continue - } - - if len(br.Header.Hash) == 0 { - failedCount++ - continue - } - - h := br.Header.Hash - roundConsensus[h]++ - if roundConsensus[h] > maxConsensus { - maxConsensus = roundConsensus[h] - if maxConsensus*100/numSharders >= consensusThresh { - return &br.Header, nil - } - } - } - } - - return nil, stdErrors.New("failed to get block info by round with consensus") -} - -func isBlockExtends(prevHash string, block *blockHeader) bool { - data := fmt.Sprintf("%v:%v:%v:%v:%v:%v:%v:%v", block.MinerId, prevHash, block.CreationDate, block.Round, - block.RoundRandomSeed, block.StateChangesCount, block.MerkleTreeRoot, block.ReceiptMerkleTreeRoot) - h := encryption.Hash(data) - return block.Hash == h -} - -func validateChain(confirmBlock *blockHeader) bool { - confirmRound := confirmBlock.Round - logging.Debug("Confirmation round: ", confirmRound) - currentBlockHash := confirmBlock.Hash - round := confirmRound + 1 - cfg, _ := conf.GetClientConfig() - nodeClient, _ := client.GetNode() - for { - nextBlock, err := getBlockInfoByRound(round, "header") - if err != nil { - logging.Info(err, " after a second falling thru to ", nodeClient.GetMinShardersVerify(), "of ", len(nodeClient.Network().Sharders), "Sharders", len(nodeClient.Sharders().Healthy()), "Healthy sharders") - sys.Sleep(1 * time.Second) - nextBlock, err = getBlockInfoByRound(round, "header") - if err != nil { - logging.Error(err, " block chain stalled. waiting", defaultWaitSeconds, "...") - sys.Sleep(defaultWaitSeconds) - continue - } - } - if isBlockExtends(currentBlockHash, nextBlock) { - currentBlockHash = nextBlock.Hash - round++ - } - if (round > confirmRound) && (round-confirmRound < int64(cfg.ConfirmationChainLength)) { - continue - } - if round < confirmRound { - return false - } - // Validation success - break - } - return true -} -func (t *Transaction) isTransactionExpired(lfbCreationTime, currentTime int64) bool { - // latest finalized block zero implies no response. use currentTime as lfb - if lfbCreationTime == 0 { - lfbCreationTime = currentTime - } - if util.MinInt64(lfbCreationTime, currentTime) > (t.txn.CreationDate + int64(defaultTxnExpirationSeconds)) { - return true - } - // Wait for next retry - sys.Sleep(defaultWaitSeconds) - return false -} - -// GetVerifyOutput implements the verification output from sharders -func (t *Transaction) GetVerifyOutput() string { - if t.verifyStatus == StatusSuccess { - return t.verifyOut - } - return "" -} - -// GetTransactionError implements error string in case of transaction failure -func (t *Transaction) GetTransactionError() string { - if t.txnStatus != StatusSuccess { - return t.txnError.Error() - } - return "" -} - -// GetVerifyError implements error string in case of verify failure error -func (t *Transaction) GetVerifyError() string { - if t.verifyStatus != StatusSuccess { - return t.verifyError.Error() - } - return "" -} - -// GetTransactionNonce returns nonce -func (t *Transaction) GetTransactionNonce() int64 { - return t.txn.TransactionNonce -} - -type scCollectReward struct { - ProviderId string `json:"provider_id"` - ProviderType int `json:"provider_type"` -} - -func VerifyContentHash(metaTxnDataJSON string) (bool, error) { - var metaTxnData sdk.CommitMetaResponse - err := json.Unmarshal([]byte(metaTxnDataJSON), &metaTxnData) - if err != nil { - return false, errors.New("metaTxnData_decode_error", "Unable to decode metaTxnData json") - } - - nodeClient, err := client.GetNode() - if err != nil { - return false, err - } - t, err := transaction.VerifyTransaction(metaTxnData.TxnID, nodeClient.Sharders().Healthy()) - if err != nil { - return false, errors.New("fetch_txm_details", "Unable to fetch txn details") - } - - var metaOperation sdk.CommitMetaData - err = json.Unmarshal([]byte(t.TransactionData), &metaOperation) - if err != nil { - logging.Error("Unmarshal of transaction data to fileMeta failed, Maybe not a commit meta txn :", t.Hash) - return false, nil - } - - return metaOperation.MetaData.Hash == metaTxnData.MetaData.Hash, nil -} - -// -// Storage SC transactions -// diff --git a/zcncore/transaction_mobile.go b/zcncore/transaction_mobile.go deleted file mode 100644 index f4e80b649..000000000 --- a/zcncore/transaction_mobile.go +++ /dev/null @@ -1,1339 +0,0 @@ -//go:build mobile -// +build mobile - -package zcncore - -import ( - "context" - "encoding/json" - stderrors "errors" - "fmt" - "net/http" - "strconv" - "time" - - "github.com/0chain/errors" - "github.com/0chain/gosdk/core/block" - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/common" - "github.com/0chain/gosdk/core/encryption" - "github.com/0chain/gosdk/core/node" - "github.com/0chain/gosdk/core/transaction" - "github.com/0chain/gosdk/core/util" -) - -const ( - Undefined int = iota - Success - - // ChargeableError is an error that still charges the user for the transaction. - ChargeableError -) - -// Provider represents the type of provider. -type Provider int - -const ( - ProviderMiner Provider = iota + 1 - ProviderSharder - ProviderBlobber - ProviderValidator - ProviderAuthorizer -) - -type stakePoolRequest struct { - ProviderType int `json:"provider_type,omitempty"` - ProviderID string `json:"provider_id,omitempty"` -} - -type TransactionCommon interface { - // ExecuteSmartContract implements wrapper for smart contract function - ExecuteSmartContract(address, methodName string, input interface{}, val uint64, feeOpts ...FeeOption) (*transaction.Transaction, error) - - // Send implements sending token to a given clientid - Send(toClientID string, val uint64, desc string) error - - MinerSCLock(providerId string, providerType int, lock string) error - MinerSCUnlock(providerId string, providerType int) error - MinerSCCollectReward(providerId string, providerType int) error - StorageSCCollectReward(providerId string, providerType int) error - - MinerScUpdateConfig(InputMap) error - MinerScUpdateGlobals(InputMap) error - StorageScUpdateConfig(InputMap) error - FaucetUpdateConfig(InputMap) error - ZCNSCUpdateGlobalConfig(InputMap) error - - MinerSCMinerSettings(MinerSCMinerInfo) error - MinerSCSharderSettings(MinerSCMinerInfo) error - MinerSCDeleteMiner(MinerSCMinerInfo) error - MinerSCDeleteSharder(MinerSCMinerInfo) error - - // ZCNSCUpdateAuthorizerConfig updates authorizer config by ID - ZCNSCUpdateAuthorizerConfig(AuthorizerNode) error - // ZCNSCAddAuthorizer adds authorizer - ZCNSCAddAuthorizer(AddAuthorizerPayload) error - - GetVerifyConfirmationStatus() int -} - -// priceRange represents a price range allowed by user to filter blobbers. -type priceRange struct { - Min int64 `json:"min"` - Max int64 `json:"max"` -} - -// createAllocationRequest is information to create allocation. -type createAllocationRequest struct { - 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"` - Blobbers []string `json:"blobbers"` - ReadPriceRange priceRange `json:"read_price_range"` - WritePriceRange priceRange `json:"write_price_range"` -} - -type CreateAllocationRequest struct { - DataShards int - ParityShards int - Size int64 - Expiration int64 - Owner string - OwnerPublicKey string - ReadPriceMin int64 - ReadPriceMax int64 - WritePriceMin int64 - WritePriceMax int64 - - blobbers []string -} - -func (car *CreateAllocationRequest) AddBlobber(blobber string) { - car.blobbers = append(car.blobbers, blobber) -} - -func (car *CreateAllocationRequest) toCreateAllocationSCInput() *createAllocationRequest { - return &createAllocationRequest{ - DataShards: car.DataShards, - ParityShards: car.ParityShards, - Size: car.Size, - Expiration: car.Expiration, - Owner: car.Owner, - OwnerPublicKey: car.OwnerPublicKey, - Blobbers: car.blobbers, - ReadPriceRange: priceRange{Min: car.ReadPriceMin, Max: car.ReadPriceMax}, - WritePriceRange: priceRange{Min: car.WritePriceMin, Max: car.WritePriceMax}, - } -} - -type StakePoolSettings struct { - DelegateWallet string `json:"delegate_wallet"` - NumDelegates int `json:"num_delegates"` - ServiceCharge float64 `json:"service_charge"` -} - -type Terms struct { - ReadPrice int64 `json:"read_price"` // tokens / GB - WritePrice int64 `json:"write_price"` // tokens / GB - MaxOfferDuration int64 `json:"max_offer_duration"` -} - -type Blobber interface { - SetTerms(readPrice int64, writePrice int64, minLockDemand float64, maxOfferDuration int64) - SetStakePoolSettings(delegateWallet string, numDelegates int, serviceCharge float64) - SetAvailable(bool) -} - -func NewBlobber(id, baseUrl string, capacity, allocated, lastHealthCheck int64) Blobber { - return &blobber{ - ID: id, - BaseURL: baseUrl, - Capacity: capacity, - Allocated: allocated, - LastHealthCheck: lastHealthCheck, - } -} - -type blobber struct { - ID string `json:"id"` - BaseURL string `json:"url"` - Capacity int64 `json:"capacity"` - Allocated int64 `json:"allocated"` - LastHealthCheck int64 `json:"last_health_check"` - Terms Terms `json:"terms"` - StakePoolSettings StakePoolSettings `json:"stake_pool_settings"` - NotAvailable bool `json:"not_available"` -} - -func (b *blobber) SetStakePoolSettings(delegateWallet string, numDelegates int, serviceCharge float64) { - b.StakePoolSettings = StakePoolSettings{ - DelegateWallet: delegateWallet, - NumDelegates: numDelegates, - ServiceCharge: serviceCharge, - } -} - -func (b *blobber) SetTerms(readPrice int64, writePrice int64, minLockDemand float64, maxOfferDuration int64) { - b.Terms = Terms{ - ReadPrice: readPrice, - WritePrice: writePrice, - MaxOfferDuration: maxOfferDuration, - } -} - -func (b *blobber) SetAvailable(availability bool) { - b.NotAvailable = availability -} - -type Validator interface { - SetStakePoolSettings(delegateWallet string, numDelegates int, serviceCharge float64) -} - -func NewValidator(id string, baseUrl string) Validator { - return &validator{ - ID: common.Key(id), - BaseURL: baseUrl, - } -} - -type validator struct { - ID common.Key `json:"id"` - BaseURL string `json:"url"` - StakePoolSettings StakePoolSettings `json:"stake_pool_settings"` -} - -func (v *validator) SetStakePoolSettings(delegateWallet string, numDelegates int, serviceCharge float64) { - v.StakePoolSettings = StakePoolSettings{ - DelegateWallet: delegateWallet, - NumDelegates: numDelegates, - ServiceCharge: serviceCharge, - } -} - -// AddAuthorizerPayload is the interface gathering the functions to add a new authorizer. -type AddAuthorizerPayload interface { - // SetStakePoolSettings sets the stake pool settings for the authorizer. - SetStakePoolSettings(delegateWallet string, numDelegates int, serviceCharge float64) -} - -// NewAddAuthorizerPayload creates a new AddAuthorizerPayload concrete instance. -func NewAddAuthorizerPayload(pubKey, url string) AddAuthorizerPayload { - return &addAuthorizerPayload{ - PublicKey: pubKey, - URL: url, - } -} - -type addAuthorizerPayload struct { - PublicKey string `json:"public_key"` - URL string `json:"url"` - StakePoolSettings AuthorizerStakePoolSettings `json:"stake_pool_settings"` // Used to initially create stake pool -} - -// SetStakePoolSettings sets the stake pool settings for the authorizer. -func (a *addAuthorizerPayload) SetStakePoolSettings(delegateWallet string, numDelegates int, serviceCharge float64) { - a.StakePoolSettings = AuthorizerStakePoolSettings{ - DelegateWallet: delegateWallet, - NumDelegates: numDelegates, - ServiceCharge: serviceCharge, - } -} - -type AuthorizerHealthCheckPayload struct { - ID string `json:"id"` // authorizer ID -} - -// AuthorizerStakePoolSettings represents configuration of an authorizer stake pool. -type AuthorizerStakePoolSettings struct { - DelegateWallet string `json:"delegate_wallet"` - NumDelegates int `json:"num_delegates"` - ServiceCharge float64 `json:"service_charge"` -} - -// AuthorizerConfig represents configuration of an authorizer node. -type AuthorizerConfig struct { - Fee int64 `json:"fee"` -} - -// InputMap represents an interface of functions to add fields to a map. -type InputMap interface { - // AddField adds a field to the map. - // - key: field key - // - value: field value - AddField(key, value string) -} - -type inputMap struct { - Fields map[string]string `json:"fields"` -} - -// NewInputMap creates a new InputMap concrete instance. -func NewInputMap() InputMap { - return &inputMap{ - Fields: make(map[string]string), - } -} - -func (im *inputMap) AddField(key, value string) { - im.Fields[key] = value -} - -func parseCoinStr(vs string) (uint64, error) { - if vs == "" { - return 0, nil - } - - v, err := strconv.ParseUint(vs, 10, 64) - if err != nil { - return 0, fmt.Errorf("invalid token value: %v, err: %v", vs, err) - } - - return v, nil -} - -// NewTransaction new generic transaction object for any operation -// - cb: callback for transaction state -// - txnFee: Transaction fees (in SAS tokens) -// - nonce: latest nonce of current wallet. please set it with 0 if you don't know the latest value -func NewTransaction(cb TransactionCallback, txnFee uint64, nonce int64) (TransactionScheme, error) { - err := CheckConfig() - if err != nil { - return nil, err - } - if client.SplitKeyWallet() { - if client.AuthUrl() == "" { - return nil, errors.New("", "auth url not set") - } - logging.Info("New transaction interface with auth") - return newTransactionWithAuth(cb, txnFee, nonce) - } - logging.Info("New transaction interface") - t, err := newTransaction(cb, txnFee, nonce) - return t, err -} - -// ExecuteSmartContract prepare and send a smart contract transaction to the blockchain -func (t *Transaction) ExecuteSmartContract(address, methodName string, input interface{}, val uint64, feeOpts ...FeeOption) (*transaction.Transaction, error) { - // t.createSmartContractTxn(address, methodName, input, val, opts...) - err := t.createSmartContractTxn(address, methodName, input, val) - if err != nil { - return nil, err - } - go func() { - t.setNonceAndSubmit() - }() - - return t.txn, nil -} - -func (t *Transaction) setTransactionFee(fee uint64) error { - if t.txnStatus != StatusUnknown { - return errors.New("", "transaction already exists. cannot set transaction fee.") - } - t.txn.TransactionFee = fee - return nil -} - -// Send to send a transaction to a given clientID -func (t *Transaction) Send(toClientID string, val uint64, desc string) error { - txnData, err := json.Marshal(SendTxnData{Note: desc}) - if err != nil { - return errors.New("", "Could not serialize description to transaction_data") - } - go func() { - t.txn.TransactionType = transaction.TxnTypeSend - t.txn.ToClientID = toClientID - t.txn.Value = val - t.txn.TransactionData = string(txnData) - t.setNonceAndSubmit() - }() - return nil -} - -// SendWithSignatureHash to send a transaction to a given clientID with a signature hash -// - toClientID: client ID in the To field of the transaction -// - val: amount of tokens to send -// - desc: description of the transaction -// - sig: signature hash -// - CreationDate: creation date of the transaction -// - hash: hash of the transaction -func (t *Transaction) SendWithSignatureHash(toClientID string, val string, desc string, sig string, CreationDate int64, hash string) error { - v, err := parseCoinStr(val) - if err != nil { - return err - } - - txnData, err := json.Marshal(SendTxnData{Note: desc}) - if err != nil { - return errors.New("", "Could not serialize description to transaction_data") - } - go func() { - t.txn.TransactionType = transaction.TxnTypeSend - t.txn.ToClientID = toClientID - t.txn.Value = v - t.txn.Hash = hash - t.txn.TransactionData = string(txnData) - t.txn.Signature = sig - t.txn.CreationDate = CreationDate - t.setNonceAndSubmit() - }() - return nil -} - -func (t *Transaction) MinerSCLock(providerId string, providerType int, lock string) error { - - lv, err := parseCoinStr(lock) - if err != nil { - return err - } - - pr := stakePoolRequest{ - ProviderType: providerType, - ProviderID: providerId, - } - err = t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_LOCK, pr, lv) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return err -} - -func (t *Transaction) MinerSCUnlock(providerId string, providerType int) error { - pr := &stakePoolRequest{ - ProviderID: providerId, - ProviderType: providerType, - } - err := t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_UNLOCK, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return err -} - -func (t *Transaction) MinerSCCollectReward(providerId string, providerType int) error { - pr := &scCollectReward{ - ProviderId: providerId, - ProviderType: providerType, - } - - err := t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_COLLECT_REWARD, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { t.setNonceAndSubmit() }() - return err -} - -func (t *Transaction) StorageSCCollectReward(providerId string, providerType int) error { - pr := &scCollectReward{ - ProviderId: providerId, - ProviderType: providerType, - } - err := t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_COLLECT_REWARD, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go t.setNonceAndSubmit() - return err -} - -// faucet smart contract - -func (t *Transaction) FaucetUpdateConfig(ip InputMap) (err error) { - - err = t.createSmartContractTxn(FaucetSmartContractAddress, - transaction.FAUCETSC_UPDATE_SETTINGS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -// -// miner SC -// - -func (t *Transaction) MinerScUpdateConfig(ip InputMap) (err error) { - err = t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_UPDATE_SETTINGS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -func (t *Transaction) MinerScUpdateGlobals(ip InputMap) (err error) { - err = t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_UPDATE_GLOBALS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -func (t *Transaction) StorageScUpdateConfig(ip InputMap) (err error) { - err = t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_UPDATE_SETTINGS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -func (t *Transaction) ZCNSCUpdateGlobalConfig(ip InputMap) (err error) { - err = t.createSmartContractTxn(ZCNSCSmartContractAddress, - transaction.ZCNSC_UPDATE_GLOBAL_CONFIG, ip, 0) - if err != nil { - logging.Error(err) - return - } - go t.setNonceAndSubmit() - return -} - -func (t *Transaction) GetVerifyConfirmationStatus() int { - return int(t.verifyConfirmationStatus) -} - -// MinerSCMinerInfo interface for miner info functions on miner smart contract. -type MinerSCMinerInfo interface { - // GetID returns the ID of the miner - GetID() string -} - -// NewMinerSCMinerInfo creates a new miner info. -// - id: miner ID -// - delegateWallet: delegate wallet -// - minStake: minimum stake -// - maxStake: maximum stake -// - numDelegates: number of delegates -// - serviceCharge: service charge -func NewMinerSCMinerInfo(id string, delegateWallet string, - minStake int64, maxStake int64, numDelegates int, serviceCharge float64) MinerSCMinerInfo { - return &minerSCMinerInfo{ - simpleMiner: simpleMiner{ID: id}, - minerSCDelegatePool: minerSCDelegatePool{ - Settings: StakePoolSettings{ - DelegateWallet: delegateWallet, - NumDelegates: numDelegates, - ServiceCharge: serviceCharge, - }, - }, - } -} - -type minerSCMinerInfo struct { - simpleMiner `json:"simple_miner"` - minerSCDelegatePool `json:"stake_pool"` -} - -func (mi *minerSCMinerInfo) GetID() string { - return mi.ID -} - -type minerSCDelegatePool struct { - Settings StakePoolSettings `json:"settings"` -} - -type simpleMiner struct { - ID string `json:"id"` -} - -func (t *Transaction) MinerSCMinerSettings(info MinerSCMinerInfo) (err error) { - err = t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_MINER_SETTINGS, info, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -func (t *Transaction) MinerSCSharderSettings(info MinerSCMinerInfo) (err error) { - err = t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_SHARDER_SETTINGS, info, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -func (t *Transaction) MinerSCDeleteMiner(info MinerSCMinerInfo) (err error) { - err = t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_MINER_DELETE, info, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -func (t *Transaction) MinerSCDeleteSharder(info MinerSCMinerInfo) (err error) { - err = t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_SHARDER_DELETE, info, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.setNonceAndSubmit() }() - return -} - -// AuthorizerNode interface for authorizer node functions. -type AuthorizerNode interface { - // GetID returns the ID of the authorizer node. - GetID() string -} - -// NewAuthorizerNode creates a new authorizer node. -func NewAuthorizerNode(id string, fee int64) AuthorizerNode { - return &authorizerNode{ - ID: id, - Config: &AuthorizerConfig{Fee: fee}, - } -} - -type authorizerNode struct { - ID string `json:"id"` - Config *AuthorizerConfig `json:"config"` -} - -func (a *authorizerNode) GetID() string { - return a.ID -} - -func (t *Transaction) ZCNSCUpdateAuthorizerConfig(ip AuthorizerNode) (err error) { - err = t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_UPDATE_AUTHORIZER_CONFIG, ip, 0) - if err != nil { - logging.Error(err) - return - } - go t.setNonceAndSubmit() - return -} - -func (t *Transaction) Verify() error { - if t.txnHash == "" && t.txnStatus == StatusUnknown { - return errors.New("", "invalid transaction. cannot be verified.") - } - if t.txnHash == "" && t.txnStatus == StatusSuccess { - h := t.GetTransactionHash() - if h == "" { - node.Cache.Evict(t.txn.ClientID) - return errors.New("", "invalid transaction. cannot be verified.") - } - } - // If transaction is verify only start from current time - if t.txn.CreationDate == 0 { - t.txn.CreationDate = int64(common.Now()) - } - - nodeClient, err := client.GetNode() - if err != nil { - return err - } - - tq, err := newTransactionQuery(nodeClient.Sharders().Healthy()) - if err != nil { - logging.Error(err) - return err - } - - go func() { - - for { - - tq.Reset() - // Get transaction confirmationBlock from a random sharder - confirmBlockHeader, confirmationBlock, lfbBlockHeader, err := tq.getFastConfirmation(t.txnHash, nil) - - if err != nil { - now := int64(common.Now()) - - // maybe it is a network or server error - if lfbBlockHeader == nil { - logging.Info(err, " now: ", now) - } else { - logging.Info(err, " now: ", now, ", LFB creation time:", lfbBlockHeader.CreationDate) - } - - // transaction is done or expired. it means random sharder might be outdated, try to query it from s/S sharders to confirm it - if util.MaxInt64(lfbBlockHeader.getCreationDate(now), now) >= (t.txn.CreationDate + int64(defaultTxnExpirationSeconds)) { - logging.Info("falling back to ", nodeClient.GetMinShardersVerify(), " of ", len(nodeClient.Network().Sharders), " Sharders", len(nodeClient.Sharders().Healthy()), "Healthy sharders") - confirmBlockHeader, confirmationBlock, lfbBlockHeader, err = tq.getConsensusConfirmation(nodeClient.GetMinShardersVerify(), t.txnHash, nil) - } - - // txn not found in fast confirmation/consensus confirmation - if err != nil { - - if lfbBlockHeader == nil { - // no any valid lfb on all sharders. maybe they are network/server errors. try it again - continue - } - - // it is expired - if t.isTransactionExpired(lfbBlockHeader.getCreationDate(now), now) { - t.completeVerify(StatusError, "", errors.New("", `{"error": "verify transaction failed"}`)) - return - } - continue - } - - } - - valid := validateChain(confirmBlockHeader) - if valid { - output, err := json.Marshal(confirmationBlock) - if err != nil { - t.completeVerify(StatusError, "", errors.New("", `{"error": "transaction confirmation json marshal error"`)) - return - } - confJson := confirmationBlock["confirmation"] - - var conf map[string]json.RawMessage - if err := json.Unmarshal(confJson, &conf); err != nil { - return - } - txnJson := conf["txn"] - - var tr map[string]json.RawMessage - if err := json.Unmarshal(txnJson, &tr); err != nil { - return - } - - txStatus := tr["transaction_status"] - switch string(txStatus) { - case "1": - t.completeVerifyWithConStatus(StatusSuccess, Success, string(output), nil) - case "2": - txOutput := tr["transaction_output"] - t.completeVerifyWithConStatus(StatusSuccess, ChargeableError, string(txOutput), nil) - default: - t.completeVerify(StatusError, string(output), nil) - } - return - } - } - }() - return nil -} - -func (t *Transaction) ZCNSCAddAuthorizer(ip AddAuthorizerPayload) (err error) { - err = t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_ADD_AUTHORIZER, ip, 0) - if err != nil { - logging.Error(err) - return - } - go t.setNonceAndSubmit() - return -} - -// EstimateFee estimates transaction fee -func (t *Transaction) EstimateFee(reqPercent float32) (int64, error) { - nodeClient, err := client.GetNode() - if err != nil { - return 0, err - } - fee, err := transaction.EstimateFee(t.txn, nodeClient.Network().Miners, reqPercent) - return int64(fee), err -} - -// ConvertTokenToSAS converts ZCN tokens to SAS tokens -// # Inputs -// - token: ZCN tokens -func ConvertTokenToSAS(token float64) uint64 { - return uint64(token * common.TokenUnit) -} - -// ConvertToValue converts ZCN tokens to SAS tokens with string format -// - token: ZCN tokens -func ConvertToValue(token float64) string { - return strconv.FormatUint(ConvertTokenToSAS(token), 10) -} - -func makeTimeoutContext(tm RequestTimeout) (context.Context, func()) { - - if tm != nil && tm.Get() > 0 { - return context.WithTimeout(context.Background(), time.Millisecond*time.Duration(tm.Get())) - - } - return context.Background(), func() {} - -} - -func GetLatestFinalized(numSharders int, timeout RequestTimeout) (b *BlockHeader, err error) { - nodeClient, err := client.GetNode() - if err != nil { - return nil, err - } - var result = make(chan *util.GetResponse, numSharders) - defer close(result) - - ctx, cancel := makeTimeoutContext(timeout) - defer cancel() - - numSharders = len(nodeClient.Sharders().Healthy()) // overwrite, use all - nodeClient.Sharders().QueryFromShardersContext(ctx, numSharders, GET_LATEST_FINALIZED, result) - - var ( - maxConsensus int - roundConsensus = make(map[string]int) - ) - - for i := 0; i < numSharders; i++ { - var rsp = <-result - if rsp == nil { - logging.Error("nil response") - continue - } - - logging.Debug(rsp.Url, rsp.Status) - - if rsp.StatusCode != http.StatusOK { - logging.Error(rsp.Body) - continue - } - - if err = json.Unmarshal([]byte(rsp.Body), &b); err != nil { - logging.Error("block parse error: ", err) - err = nil - continue - } - - var h = encryption.FastHash([]byte(b.Hash)) - if roundConsensus[h]++; roundConsensus[h] > maxConsensus { - maxConsensus = roundConsensus[h] - } - } - - if maxConsensus == 0 { - return nil, errors.New("", "block info not found") - } - - return -} - -// GetLatestFinalizedMagicBlock gets latest finalized magic block -// - numSharders: number of sharders -// - timeout: request timeout -func GetLatestFinalizedMagicBlock(numSharders int, timeout RequestTimeout) ([]byte, error) { - nodeClient, err := client.GetNode() - if err != nil { - return nil, err - } - var result = make(chan *util.GetResponse, numSharders) - defer close(result) - - ctx, cancel := makeTimeoutContext(timeout) - defer cancel() - - numSharders = len(nodeClient.Sharders().Healthy()) // overwrite, use all - nodeClient.Sharders().QueryFromShardersContext(ctx, numSharders, GET_LATEST_FINALIZED_MAGIC_BLOCK, result) - - var ( - maxConsensus int - roundConsensus = make(map[string]int) - m *block.MagicBlock - ) - - type respObj struct { - MagicBlock *block.MagicBlock `json:"magic_block"` - } - - for i := 0; i < numSharders; i++ { - var rsp = <-result - if rsp == nil { - logging.Error("nil response") - continue - } - - logging.Debug(rsp.Url, rsp.Status) - - if rsp.StatusCode != http.StatusOK { - logging.Error(rsp.Body) - continue - } - - var respo respObj - if err = json.Unmarshal([]byte(rsp.Body), &respo); err != nil { - logging.Error(" magic block parse error: ", err) - err = nil - continue - } - - m = respo.MagicBlock - var h = encryption.FastHash([]byte(respo.MagicBlock.Hash)) - if roundConsensus[h]++; roundConsensus[h] > maxConsensus { - maxConsensus = roundConsensus[h] - } - } - - if maxConsensus == 0 { - return nil, errors.New("", "magic block info not found") - } - - if m != nil { - return json.Marshal(m) - } - - return nil, err -} - -// GetChainStats gets chain stats with time out -// timeout in milliseconds -func GetChainStats(timeout RequestTimeout) ([]byte, error) { - nodeClient, err := client.GetNode() - if err != nil { - return nil, err - } - var result = make(chan *util.GetResponse, 1) - defer close(result) - - ctx, cancel := makeTimeoutContext(timeout) - defer cancel() - - var ( - b *block.ChainStats - ) - - var numSharders = len(nodeClient.Sharders().Healthy()) // overwrite, use all - nodeClient.Sharders().QueryFromShardersContext(ctx, numSharders, GET_CHAIN_STATS, result) - var rsp *util.GetResponse - for i := 0; i < numSharders; i++ { - var x = <-result - if x == nil { - logging.Error("nil response") - continue - } - if x.StatusCode != http.StatusOK { - continue - } - rsp = x - } - - if rsp == nil { - return nil, errors.New("http_request_failed", "Request failed with status not 200") - } - - if err = json.Unmarshal([]byte(rsp.Body), &b); err != nil { - return nil, err - } - - return []byte(rsp.Body), nil -} - -func GetFeeStats(timeout RequestTimeout) ([]byte, error) { - nodeClient, err := client.GetNode() - if err != nil { - return nil, err - } - - var numMiners = 4 - - if numMiners > len(nodeClient.Network().Miners) { - numMiners = len(nodeClient.Network().Miners) - } - - var result = make(chan *util.GetResponse, numMiners) - - ctx, cancel := makeTimeoutContext(timeout) - defer cancel() - - var ( - b *block.FeeStats - ) - - queryFromMinersContext(ctx, numMiners, GET_FEE_STATS, result) - var rsp *util.GetResponse - -loop: - for i := 0; i < numMiners; i++ { - select { - case x := <-result: - if x.StatusCode != http.StatusOK { - continue - } - rsp = x - if rsp != nil { - break loop - } - case <-ctx.Done(): - return nil, ctx.Err() - } - } - if rsp == nil { - return nil, errors.New("http_request_failed", "Request failed with status not 200") - } - - if err = json.Unmarshal([]byte(rsp.Body), &b); err != nil { - return nil, err - } - - return []byte(rsp.Body), nil -} - -type BlockHeader struct { - Version string `json:"version,omitempty"` - CreationDate int64 `json:"creation_date,omitempty"` - Hash string `json:"hash,omitempty"` - MinerID string `json:"miner_id,omitempty"` - Round int64 `json:"round,omitempty"` - RoundRandomSeed int64 `json:"round_random_seed,omitempty"` - MerkleTreeRoot string `json:"merkle_tree_root,omitempty"` - StateHash string `json:"state_hash,omitempty"` - ReceiptMerkleTreeRoot string `json:"receipt_merkle_tree_root,omitempty"` - NumTxns int64 `json:"num_txns,omitempty"` -} - -type Block struct { - MinerID string `json:"miner_id"` - Round int64 `json:"round"` - RoundRandomSeed int64 `json:"round_random_seed"` - RoundTimeoutCount int `json:"round_timeout_count"` - - Hash string `json:"hash"` - Signature string `json:"signature"` - ChainID string `json:"chain_id"` - ChainWeight float64 `json:"chain_weight"` - RunningTxnCount int64 `json:"running_txn_count"` - - Version string `json:"version"` - CreationDate int64 `json:"creation_date"` - - MagicBlockHash string `json:"magic_block_hash"` - PrevHash string `json:"prev_hash"` - - ClientStateHash string `json:"state_hash"` - - // unexported fields - header *BlockHeader `json:"-"` - txns []*TransactionMobile `json:"transactions,omitempty"` -} - -func (b *Block) GetHeader() *BlockHeader { - return b.header -} - -type IterTxnFunc func(idx int, txn *TransactionMobile) - -type Transactions struct { - txns []*TransactionMobile -} - -func (tm *Transactions) Len() int { - return len(tm.txns) -} - -func (tm *Transactions) Get(idx int) (*TransactionMobile, error) { - if idx < 0 && idx >= len(tm.txns) { - return nil, stderrors.New("index out of bounds") - } - - return tm.txns[idx], nil -} - -func (b *Block) GetTxns() *Transactions { - return &Transactions{ - txns: b.txns, - } -} - -func toMobileBlock(b *block.Block) *Block { - lb := &Block{ - header: &BlockHeader{ - Version: b.Header.Version, - CreationDate: b.Header.CreationDate, - Hash: b.Header.Hash, - MinerID: b.Header.MinerID, - Round: b.Header.Round, - RoundRandomSeed: b.Header.RoundRandomSeed, - MerkleTreeRoot: b.Header.MerkleTreeRoot, - StateHash: b.Header.StateHash, - ReceiptMerkleTreeRoot: b.Header.ReceiptMerkleTreeRoot, - NumTxns: b.Header.NumTxns, - }, - MinerID: string(b.MinerID), - Round: b.Round, - RoundRandomSeed: b.RoundRandomSeed, - RoundTimeoutCount: b.RoundTimeoutCount, - - Hash: string(b.Hash), - Signature: b.Signature, - ChainID: string(b.ChainID), - ChainWeight: b.ChainWeight, - RunningTxnCount: b.RunningTxnCount, - - Version: b.Version, - CreationDate: int64(b.CreationDate), - - MagicBlockHash: b.MagicBlockHash, - PrevHash: b.PrevHash, - - ClientStateHash: string(b.ClientStateHash), - } - - lb.txns = make([]*TransactionMobile, len(b.Txns)) - for i, txn := range b.Txns { - lb.txns[i] = &TransactionMobile{ - Hash: txn.Hash, - Version: txn.Version, - ClientID: txn.ClientID, - PublicKey: txn.PublicKey, - ToClientID: txn.ToClientID, - ChainID: txn.ChainID, - TransactionData: txn.TransactionData, - Signature: txn.Signature, - CreationDate: txn.CreationDate, - TransactionType: txn.TransactionType, - TransactionOutput: txn.TransactionOutput, - TransactionNonce: txn.TransactionNonce, - OutputHash: txn.OutputHash, - Status: txn.Status, - Value: strconv.FormatUint(txn.Value, 10), - TransactionFee: strconv.FormatUint(txn.TransactionFee, 10), - } - } - - return lb -} - -// TransactionMobile entity that encapsulates the transaction related data and meta data -type TransactionMobile struct { - Hash string `json:"hash,omitempty"` - Version string `json:"version,omitempty"` - ClientID string `json:"client_id,omitempty"` - PublicKey string `json:"public_key,omitempty"` - ToClientID string `json:"to_client_id,omitempty"` - ChainID string `json:"chain_id,omitempty"` - TransactionData string `json:"transaction_data"` - Value string `json:"transaction_value"` - Signature string `json:"signature,omitempty"` - CreationDate int64 `json:"creation_date,omitempty"` - TransactionType int `json:"transaction_type"` - TransactionOutput string `json:"transaction_output,omitempty"` - TransactionFee string `json:"transaction_fee"` - TransactionNonce int64 `json:"transaction_nonce"` - OutputHash string `json:"txn_output_hash"` - Status int `json:"transaction_status"` -} - -// RequestTimeout will be used for setting requests with timeout -type RequestTimeout interface { - Set(int64) // milliseconds - Get() int64 // milliseconds -} - -type timeoutCtx struct { - millisecond int64 -} - -func NewRequestTimeout(timeout int64) RequestTimeout { - return &timeoutCtx{millisecond: timeout} -} - -func (t *timeoutCtx) Set(tm int64) { - t.millisecond = tm -} - -func (t *timeoutCtx) Get() int64 { - return t.millisecond -} - -func GetBlockByRound(numSharders int, round int64, timeout RequestTimeout) (b *Block, err error) { - nodeClient, err := client.GetNode() - if err != nil { - return nil, err - } - var result = make(chan *util.GetResponse, numSharders) - defer close(result) - - ctx, cancel := makeTimeoutContext(timeout) - defer cancel() - - numSharders = len(nodeClient.Sharders().Healthy()) // overwrite, use all - nodeClient.Sharders().QueryFromShardersContext(ctx, numSharders, - fmt.Sprintf("%sround=%d&content=full,header", GET_BLOCK_INFO, round), - result) - - var ( - maxConsensus int - roundConsensus = make(map[string]int) - ) - - type respObj struct { - Block *block.Block `json:"block"` - Header *block.Header `json:"header"` - } - - for i := 0; i < numSharders; i++ { - var rsp = <-result - if rsp == nil { - logging.Error("nil response") - continue - } - logging.Debug(rsp.Url, rsp.Status) - - if rsp.StatusCode != http.StatusOK { - logging.Error(rsp.Body) - continue - } - - var respo respObj - if err = json.Unmarshal([]byte(rsp.Body), &respo); err != nil { - logging.Error("block parse error: ", err) - err = nil - continue - } - - if respo.Block == nil { - logging.Debug(rsp.Url, "no block in response:", rsp.Body) - continue - } - - if respo.Header == nil { - logging.Debug(rsp.Url, "no block header in response:", rsp.Body) - continue - } - - if respo.Header.Hash != string(respo.Block.Hash) { - logging.Debug(rsp.Url, "header and block hash mismatch:", rsp.Body) - continue - } - - b = toMobileBlock(respo.Block) - - b.header = &BlockHeader{ - Version: respo.Header.Version, - CreationDate: respo.Header.CreationDate, - Hash: respo.Header.Hash, - MinerID: respo.Header.MinerID, - Round: respo.Header.Round, - RoundRandomSeed: respo.Header.RoundRandomSeed, - MerkleTreeRoot: respo.Header.MerkleTreeRoot, - StateHash: respo.Header.StateHash, - ReceiptMerkleTreeRoot: respo.Header.ReceiptMerkleTreeRoot, - NumTxns: respo.Header.NumTxns, - } - - var h = encryption.FastHash([]byte(b.Hash)) - if roundConsensus[h]++; roundConsensus[h] > maxConsensus { - maxConsensus = roundConsensus[h] - } - } - - if maxConsensus == 0 { - return nil, errors.New("", "round info not found") - } - - return -} - -func GetMagicBlockByNumber(numSharders int, number int64, timeout RequestTimeout) ([]byte, error) { - nodeClient, err := client.GetNode() - if err != nil { - return nil, err - } - var result = make(chan *util.GetResponse, numSharders) - defer close(result) - - ctx, cancel := makeTimeoutContext(timeout) - defer cancel() - - numSharders = len(nodeClient.Sharders().Healthy()) // overwrite, use all - nodeClient.Sharders().QueryFromShardersContext(ctx, numSharders, - fmt.Sprintf("%smagic_block_number=%d", GET_MAGIC_BLOCK_INFO, number), - result) - - var ( - maxConsensus int - roundConsensus = make(map[string]int) - ret []byte - ) - - type respObj struct { - MagicBlock *block.MagicBlock `json:"magic_block"` - } - - for i := 0; i < numSharders; i++ { - var rsp = <-result - if rsp == nil { - logging.Error("nil response") - continue - } - - logging.Debug(rsp.Url, rsp.Status) - - if rsp.StatusCode != http.StatusOK { - logging.Error(rsp.Body) - continue - } - - var respo respObj - if err = json.Unmarshal([]byte(rsp.Body), &respo); err != nil { - logging.Error(" magic block parse error: ", err) - err = nil - continue - } - - ret = []byte(rsp.Body) - var h = encryption.FastHash([]byte(respo.MagicBlock.Hash)) - if roundConsensus[h]++; roundConsensus[h] > maxConsensus { - maxConsensus = roundConsensus[h] - } - } - - if maxConsensus == 0 { - return nil, errors.New("", "magic block info not found") - } - - if err != nil { - return nil, err - } - - return ret, nil -} - -// GetFeesTable get fee tables -func GetFeesTable(reqPercent float32) (string, error) { - nodeClient, err := client.GetNode() - if err != nil { - return "", err - } - - fees, err := transaction.GetFeesTable(nodeClient.Network().Miners, reqPercent) - if err != nil { - return "", err - } - - js, err := json.Marshal(fees) - if err != nil { - return "", err - } - - return string(js), nil -} diff --git a/zcncore/transaction_query.go b/zcncore/transaction_query.go deleted file mode 100644 index 38d065d75..000000000 --- a/zcncore/transaction_query.go +++ /dev/null @@ -1,614 +0,0 @@ -//go:build !mobile -// +build !mobile - -package zcncore - -import ( - "context" - "encoding/json" - "errors" - stderrors "errors" - "net/http" - "strconv" - "strings" - "sync" - "time" - - thrown "github.com/0chain/errors" - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/resty" - "github.com/0chain/gosdk/core/util" -) - -var ( - ErrNoAvailableSharders = errors.New("zcn: no available sharders") - ErrNoEnoughSharders = errors.New("zcn: sharders is not enough") - ErrNoEnoughOnlineSharders = errors.New("zcn: online sharders is not enough") - ErrInvalidNumSharder = errors.New("zcn: number of sharders is invalid") - ErrNoOnlineSharders = errors.New("zcn: no any online sharder") - ErrSharderOffline = errors.New("zcn: sharder is offline") - ErrInvalidConsensus = errors.New("zcn: invalid consensus") - ErrTransactionNotFound = errors.New("zcn: transaction not found") - ErrTransactionNotConfirmed = errors.New("zcn: transaction not confirmed") - ErrNoAvailableMiners = errors.New("zcn: no available miners") -) - -const ( - SharderEndpointHealthCheck = "/v1/healthcheck" -) - -type QueryResult struct { - Content []byte - StatusCode int - Error error -} - -// QueryResultHandle handle query response, return true if it is a consensus-result -type QueryResultHandle func(result QueryResult) bool - -type TransactionQuery struct { - sync.RWMutex - max int - sharders []string - miners []string - numShardersToBatch int - - selected map[string]interface{} - offline map[string]interface{} -} - -func NewTransactionQuery(sharders []string, miners []string) (*TransactionQuery, error) { - - if len(sharders) == 0 { - return nil, ErrNoAvailableSharders - } - - tq := &TransactionQuery{ - max: len(sharders), - sharders: sharders, - numShardersToBatch: 3, - } - tq.selected = make(map[string]interface{}) - tq.offline = make(map[string]interface{}) - - return tq, nil -} - -func (tq *TransactionQuery) Reset() { - tq.selected = make(map[string]interface{}) - tq.offline = make(map[string]interface{}) -} - -// validate validate data and input -func (tq *TransactionQuery) validate(num int) error { - if tq == nil || tq.max == 0 { - return ErrNoAvailableSharders - } - - if num < 1 { - return ErrInvalidNumSharder - } - - if num > tq.max { - return ErrNoEnoughSharders - } - - if num > (tq.max - len(tq.offline)) { - return ErrNoEnoughOnlineSharders - } - - return nil - -} - -// buildUrl build url with host and parts -func (tq *TransactionQuery) buildUrl(host string, parts ...string) string { - var sb strings.Builder - - sb.WriteString(strings.TrimSuffix(host, "/")) - - for _, it := range parts { - sb.WriteString(it) - } - - return sb.String() -} - -// checkSharderHealth checks the health of a sharder (denoted by host) and returns if it is healthy -// or ErrNoOnlineSharders if no sharders are healthy/up at the moment. -func (tq *TransactionQuery) checkSharderHealth(ctx context.Context, host string) error { - tq.RLock() - _, ok := tq.offline[host] - tq.RUnlock() - if ok { - return ErrSharderOffline - } - - // check health - ctx, cancel := context.WithTimeout(ctx, 5*time.Second) - defer cancel() - r := resty.New() - requestUrl := tq.buildUrl(host, SharderEndpointHealthCheck) - logging.Info("zcn: check health ", requestUrl) - r.DoGet(ctx, requestUrl) - r.Then(func(req *http.Request, resp *http.Response, respBody []byte, cf context.CancelFunc, err error) error { - if err != nil { - return err - } - - // 5xx: it is a server error, not client error - if resp.StatusCode >= http.StatusInternalServerError { - return thrown.Throw(ErrSharderOffline, resp.Status) - } - - return nil - }) - errs := r.Wait() - - if len(errs) > 0 { - if errors.Is(errs[0], context.DeadlineExceeded) { - return context.DeadlineExceeded - } - tq.Lock() - tq.offline[host] = true - tq.Unlock() - return ErrSharderOffline - } - return nil -} - -// getRandomSharder returns a random healthy sharder -func (tq *TransactionQuery) getRandomSharder(ctx context.Context) (string, error) { - if tq.sharders == nil || len(tq.sharders) == 0 { - return "", ErrNoAvailableMiners - } - - shuffledSharders := util.Shuffle(tq.sharders) - - return shuffledSharders[0], nil -} - -//nolint:unused -func (tq *TransactionQuery) getRandomSharderWithHealthcheck(ctx context.Context) (string, error) { - ctx, cancel := context.WithCancel(ctx) - defer cancel() - shuffledSharders := util.Shuffle(tq.sharders) - for i := 0; i < len(shuffledSharders); i += tq.numShardersToBatch { - var mu sync.Mutex - done := false - errCh := make(chan error, tq.numShardersToBatch) - successCh := make(chan string) - last := i + tq.numShardersToBatch - 1 - - if last > len(shuffledSharders)-1 { - last = len(shuffledSharders) - 1 - } - numShardersOffline := 0 - for j := i; j <= last; j++ { - sharder := shuffledSharders[j] - go func(sharder string) { - err := tq.checkSharderHealth(ctx, sharder) - if err != nil { - errCh <- err - } else { - mu.Lock() - if !done { - successCh <- sharder - done = true - } - mu.Unlock() - } - }(sharder) - } - innerLoop: - for { - select { - case e := <-errCh: - switch e { - case ErrSharderOffline: - tq.RLock() - if len(tq.offline) >= tq.max { - tq.RUnlock() - return "", ErrNoOnlineSharders - } - tq.RUnlock() - numShardersOffline++ - if numShardersOffline >= tq.numShardersToBatch { - break innerLoop - } - case context.DeadlineExceeded: - return "", e - } - case s := <-successCh: - return s, nil - case <-ctx.Done(): - if ctx.Err() == context.DeadlineExceeded { - return "", context.DeadlineExceeded - } - } - } - } - return "", ErrNoOnlineSharders -} - -// getRandomMiner returns a random miner -func (tq *TransactionQuery) getRandomMiner(ctx context.Context) (string, error) { - - if tq.miners == nil || len(tq.miners) == 0 { - return "", ErrNoAvailableMiners - } - - shuffledMiners := util.Shuffle(tq.miners) - - return shuffledMiners[0], nil -} - -// FromAll query transaction from all sharders whatever it is selected or offline in previous queires, and return consensus result -func (tq *TransactionQuery) FromAll(ctx context.Context, query string, handle QueryResultHandle) error { - if tq == nil || tq.max == 0 { - return ErrNoAvailableSharders - } - - urls := make([]string, 0, tq.max) - for _, host := range tq.sharders { - urls = append(urls, tq.buildUrl(host, query)) - } - - r := resty.New() - r.DoGet(ctx, urls...). - Then(func(req *http.Request, resp *http.Response, respBody []byte, cf context.CancelFunc, err error) error { - res := QueryResult{ - Content: respBody, - Error: err, - StatusCode: http.StatusBadRequest, - } - - if resp != nil { - res.StatusCode = resp.StatusCode - - logging.Debug(req.URL.String() + " " + resp.Status) - logging.Debug(string(respBody)) - } else { - logging.Debug(req.URL.String()) - - } - - if handle != nil { - if handle(res) { - - cf() - } - } - - return nil - }) - - r.Wait() - - return nil -} - -func (tq *TransactionQuery) GetInfo(ctx context.Context, query string) (*QueryResult, error) { - - consensuses := make(map[int]int) - var maxConsensus int - var consensusesResp QueryResult - // {host}{query} - err := tq.FromAll(ctx, query, - func(qr QueryResult) bool { - //ignore response if it is network error - if qr.StatusCode >= 500 { - return false - } - - consensuses[qr.StatusCode]++ - if consensuses[qr.StatusCode] > maxConsensus { - maxConsensus = consensuses[qr.StatusCode] - consensusesResp = qr - } - - // If number of 200's is equal to number of some other status codes, use 200's. - if qr.StatusCode == http.StatusOK && consensuses[qr.StatusCode] == maxConsensus { - maxConsensus = consensuses[qr.StatusCode] - consensusesResp = qr - } - - return false - - }) - - if err != nil { - return nil, err - } - - if maxConsensus == 0 { - return nil, stderrors.New("zcn: query not found") - } - - rate := maxConsensus * 100 / tq.max - if rate < consensusThresh { - return nil, ErrInvalidConsensus - } - - if consensusesResp.StatusCode != http.StatusOK { - return nil, stderrors.New(string(consensusesResp.Content)) - } - - return &consensusesResp, nil -} - -// FromAny queries transaction from any sharder that is not selected in previous queries. -// use any used sharder if there is not any unused sharder -func (tq *TransactionQuery) FromAny(ctx context.Context, query string, provider Provider) (QueryResult, error) { - - res := QueryResult{ - StatusCode: http.StatusBadRequest, - } - - err := tq.validate(1) - - if err != nil { - return res, err - } - - var host string - - // host, err := tq.getRandomSharder(ctx) - - switch provider { - case ProviderMiner: - host, err = tq.getRandomMiner(ctx) - case ProviderSharder: - host, err = tq.getRandomSharder(ctx) - } - - if err != nil { - return res, err - } - - r := resty.New() - requestUrl := tq.buildUrl(host, query) - - logging.Debug("GET", requestUrl) - - r.DoGet(ctx, requestUrl). - Then(func(req *http.Request, resp *http.Response, respBody []byte, cf context.CancelFunc, err error) error { - res.Error = err - if err != nil { - return err - } - - res.Content = respBody - logging.Debug(string(respBody)) - - if resp != nil { - res.StatusCode = resp.StatusCode - } - - return nil - }) - - errs := r.Wait() - - if len(errs) > 0 { - return res, errs[0] - } - - return res, nil - -} - -func (tq *TransactionQuery) getConsensusConfirmation(ctx context.Context, numSharders int, txnHash string) (*blockHeader, map[string]json.RawMessage, *blockHeader, error) { - maxConfirmation := int(0) - txnConfirmations := make(map[string]int) - var confirmationBlockHeader *blockHeader - var confirmationBlock map[string]json.RawMessage - var lfbBlockHeader *blockHeader - maxLfbBlockHeader := int(0) - lfbBlockHeaders := make(map[string]int) - - // {host}/v1/transaction/get/confirmation?hash={txnHash}&content=lfb - err := tq.FromAll(ctx, - tq.buildUrl("", TXN_VERIFY_URL, txnHash, "&content=lfb"), - func(qr QueryResult) bool { - if qr.StatusCode != http.StatusOK { - return false - } - - var cfmBlock map[string]json.RawMessage - err := json.Unmarshal([]byte(qr.Content), &cfmBlock) - if err != nil { - logging.Error("txn confirmation parse error", err) - return false - } - - // parse `confirmation` section as block header - cfmBlockHeader, err := getBlockHeaderFromTransactionConfirmation(txnHash, cfmBlock) - if err != nil { - logging.Error("txn confirmation parse header error", err) - - // parse `latest_finalized_block` section - if lfbRaw, ok := cfmBlock["latest_finalized_block"]; ok { - var lfb blockHeader - err := json.Unmarshal([]byte(lfbRaw), &lfb) - if err != nil { - logging.Error("round info parse error.", err) - return false - } - - lfbBlockHeaders[lfb.Hash]++ - if lfbBlockHeaders[lfb.Hash] > maxLfbBlockHeader { - maxLfbBlockHeader = lfbBlockHeaders[lfb.Hash] - lfbBlockHeader = &lfb - } - } - - return false - } - - txnConfirmations[cfmBlockHeader.Hash]++ - if txnConfirmations[cfmBlockHeader.Hash] > maxConfirmation { - maxConfirmation = txnConfirmations[cfmBlockHeader.Hash] - - if maxConfirmation >= numSharders { - confirmationBlockHeader = cfmBlockHeader - confirmationBlock = cfmBlock - - // it is consensus by enough sharders, and latest_finalized_block is valid - // return true to cancel other requests - return true - } - } - - return false - - }) - - if err != nil { - return nil, nil, lfbBlockHeader, err - } - - if maxConfirmation == 0 { - return nil, nil, lfbBlockHeader, stderrors.New("zcn: transaction not found") - } - - if maxConfirmation < numSharders { - return nil, nil, lfbBlockHeader, ErrInvalidConsensus - } - - return confirmationBlockHeader, confirmationBlock, lfbBlockHeader, nil -} - -// getFastConfirmation get txn confirmation from a random online sharder -func (tq *TransactionQuery) getFastConfirmation(ctx context.Context, txnHash string) (*blockHeader, map[string]json.RawMessage, *blockHeader, error) { - var confirmationBlockHeader *blockHeader - var confirmationBlock map[string]json.RawMessage - var lfbBlockHeader blockHeader - - // {host}/v1/transaction/get/confirmation?hash={txnHash}&content=lfb - result, err := tq.FromAny(ctx, tq.buildUrl("", TXN_VERIFY_URL, txnHash, "&content=lfb"), ProviderSharder) - if err != nil { - return nil, nil, nil, err - } - - if result.StatusCode == http.StatusOK { - - err = json.Unmarshal(result.Content, &confirmationBlock) - if err != nil { - logging.Error("txn confirmation parse error", err) - return nil, nil, nil, err - } - - // parse `confirmation` section as block header - confirmationBlockHeader, err = getBlockHeaderFromTransactionConfirmation(txnHash, confirmationBlock) - if err == nil { - return confirmationBlockHeader, confirmationBlock, nil, nil - } - - logging.Error("txn confirmation parse header error", err) - - // parse `latest_finalized_block` section - lfbRaw, ok := confirmationBlock["latest_finalized_block"] - if !ok { - return confirmationBlockHeader, confirmationBlock, nil, err - } - - err = json.Unmarshal([]byte(lfbRaw), &lfbBlockHeader) - if err == nil { - return confirmationBlockHeader, confirmationBlock, &lfbBlockHeader, ErrTransactionNotConfirmed - } - - logging.Error("round info parse error.", err) - return nil, nil, nil, err - - } - - return nil, nil, nil, thrown.Throw(ErrTransactionNotFound, strconv.Itoa(result.StatusCode)) -} - -func GetInfoFromSharders(urlSuffix string, op int, cb GetInfoCallback) { - nodeClient, err := client.GetNode() - if err != nil { - cb.OnInfoAvailable(op, StatusError, "", err.Error()) - return - } - tq, err := NewTransactionQuery(util.Shuffle(nodeClient.Sharders().Healthy()), []string{}) - if err != nil { - cb.OnInfoAvailable(op, StatusError, "", err.Error()) - return - } - - qr, err := tq.GetInfo(context.TODO(), urlSuffix) - if err != nil { - if qr != nil && op == OpGetMintNonce { - logging.Debug("OpGetMintNonce QueryResult error", "; Content = ", qr.Content, "; Error = ", qr.Error.Error(), "; StatusCode = ", qr.StatusCode) - cb.OnInfoAvailable(op, qr.StatusCode, "", qr.Error.Error()) - return - } - cb.OnInfoAvailable(op, StatusError, "", err.Error()) - return - } - - cb.OnInfoAvailable(op, StatusSuccess, string(qr.Content), "") -} - -func GetInfoFromAnySharder(urlSuffix string, op int, cb GetInfoCallback) { - nodeClient, err := client.GetNode() - if err != nil { - cb.OnInfoAvailable(op, StatusError, "", err.Error()) - return - } - - tq, err := NewTransactionQuery(util.Shuffle(nodeClient.Sharders().Healthy()), []string{}) - if err != nil { - cb.OnInfoAvailable(op, StatusError, "", err.Error()) - return - } - - qr, err := tq.FromAny(context.TODO(), urlSuffix, ProviderSharder) - if err != nil { - cb.OnInfoAvailable(op, StatusError, "", err.Error()) - return - } - - cb.OnInfoAvailable(op, StatusSuccess, string(qr.Content), "") -} - -func GetInfoFromAnyMiner(urlSuffix string, op int, cb getInfoCallback) { - nodeClient, err := client.GetNode() - if err != nil { - cb.OnInfoAvailable(op, StatusError, "", err.Error()) - return - } - - tq, err := NewTransactionQuery([]string{}, util.Shuffle(nodeClient.Network().Miners)) - - if err != nil { - cb.OnInfoAvailable(op, StatusError, "", err.Error()) - return - } - qr, err := tq.FromAny(context.TODO(), urlSuffix, ProviderMiner) - - if err != nil { - cb.OnInfoAvailable(op, StatusError, "", err.Error()) - return - } - cb.OnInfoAvailable(op, StatusSuccess, string(qr.Content), "") -} - -func GetEvents(cb GetInfoCallback, filters map[string]string) (err error) { - if err = CheckConfig(); err != nil { - return - } - go GetInfoFromSharders(WithParams(GET_MINERSC_EVENTS, Params{ - "block_number": filters["block_number"], - "tx_hash": filters["tx_hash"], - "type": filters["type"], - "tag": filters["tag"], - }), 0, cb) - return -} - -func WithParams(uri string, params Params) string { - return withParams(uri, params) -} diff --git a/zcncore/transaction_query_base.go b/zcncore/transaction_query_base.go deleted file mode 100644 index 74eb52630..000000000 --- a/zcncore/transaction_query_base.go +++ /dev/null @@ -1,81 +0,0 @@ -package zcncore - -import ( - "encoding/json" - stderrors "errors" - - thrown "github.com/0chain/errors" -) - -// GetUserLockedTotal get total token user locked -// # Inputs -// - clientID wallet id -func GetUserLockedTotal(clientID string) (int64, error) { - - err := checkSdkInit() - if err != nil { - return 0, err - } - - var url = withParams(STORAGESC_GET_USER_LOCKED_TOTAL, Params{ - "client_id": clientID, - }) - cb := createGetInfoCallback() - go GetInfoFromSharders(url, OpStorageSCGetStakePoolInfo, cb) - info, err := cb.Wait() - if err != nil { - return 0, err - } - - result := make(map[string]int64) - - err = json.Unmarshal([]byte(info), &result) - if err != nil { - return 0, thrown.Throw(err, "invalid json format") - } - - total, ok := result["total"] - if ok { - return total, nil - } - - return 0, stderrors.New("invalid result") - -} - -func createGetInfoCallback() *getInfoCallback { - return &getInfoCallback{ - callback: make(chan bool), - } -} - -type getInfoCallback struct { - callback chan bool - status int - info string - err string -} - -func (cb *getInfoCallback) OnInfoAvailable(op int, status int, info string, err string) { - - // if status == StatusSuccess then info is valid - // is status != StatusSuccess then err will give the reason - - cb.status = status - if status == StatusSuccess { - cb.info = info - } else { - cb.err = err - } - - cb.callback <- true -} - -func (cb *getInfoCallback) Wait() (string, error) { - <-cb.callback - if cb.err == "" { - return cb.info, nil - } - - return "", stderrors.New(cb.err) -} diff --git a/zcncore/transaction_query_mobile.go b/zcncore/transaction_query_mobile.go deleted file mode 100644 index c2a81532d..000000000 --- a/zcncore/transaction_query_mobile.go +++ /dev/null @@ -1,502 +0,0 @@ -//go:build mobile -// +build mobile - -package zcncore - -import ( - "context" - "encoding/json" - "errors" - stderrors "errors" - "math/rand" - "net/http" - "strconv" - "strings" - "time" - - thrown "github.com/0chain/errors" - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/resty" - "github.com/0chain/gosdk/core/util" -) - -var ( - ErrNoAvailableSharders = errors.New("zcn: no available sharders") - ErrNoEnoughSharders = errors.New("zcn: sharders is not enough") - ErrNoEnoughOnlineSharders = errors.New("zcn: online sharders is not enough") - ErrInvalidNumSharder = errors.New("zcn: number of sharders is invalid") - ErrNoOnlineSharders = errors.New("zcn: no any online sharder") - ErrSharderOffline = errors.New("zcn: sharder is offline") - ErrInvalidConsensus = errors.New("zcn: invalid consensus") - ErrTransactionNotFound = errors.New("zcn: transaction not found") - ErrTransactionNotConfirmed = errors.New("zcn: transaction not confirmed") -) - -const ( - SharderEndpointHealthCheck = "/_health_check" -) - -type QueryResult struct { - Content []byte - StatusCode int - Error error -} - -// queryResultHandle handle query response, return true if it is a consensus-result -type queryResultHandle func(result QueryResult) bool - -type transactionQuery struct { - max int - sharders []string - - selected map[string]interface{} - offline map[string]interface{} -} - -func (tq *transactionQuery) Reset() { - tq.selected = make(map[string]interface{}) - tq.offline = make(map[string]interface{}) -} - -// validate validate data and input -func (tq *transactionQuery) validate(num int) error { - if tq == nil || tq.max == 0 { - return ErrNoAvailableSharders - } - - if num < 1 { - return ErrInvalidNumSharder - } - - if num > tq.max { - return ErrNoEnoughSharders - } - - if num > (tq.max - len(tq.offline)) { - return ErrNoEnoughOnlineSharders - } - - return nil - -} - -// buildUrl build url with host and parts -func (tq *transactionQuery) buildUrl(host string, parts ...string) string { - var sb strings.Builder - - sb.WriteString(strings.TrimSuffix(host, "/")) - - for _, it := range parts { - sb.WriteString(it) - } - - return sb.String() -} - -// checkHealth check health -func (tq *transactionQuery) checkHealth(ctx context.Context, host string) error { - - _, ok := tq.offline[host] - if ok { - return ErrSharderOffline - } - - // check health - r := resty.New() - requestUrl := tq.buildUrl(host, SharderEndpointHealthCheck) - logging.Info("zcn: check health ", requestUrl) - r.DoGet(ctx, requestUrl) - r.Then(func(req *http.Request, resp *http.Response, respBody []byte, cf context.CancelFunc, err error) error { - if err != nil { - return err - } - - // 5xx: it is a server error, not client error - if resp.StatusCode >= http.StatusInternalServerError { - return thrown.Throw(ErrSharderOffline, resp.Status) - } - - return nil - }) - errs := r.Wait() - - if len(errs) > 0 { - tq.offline[host] = true - - if len(tq.offline) >= tq.max { - return ErrNoOnlineSharders - } - } - - return nil -} - -// randOne random one health sharder -func (tq *transactionQuery) randOne(ctx context.Context) (string, error) { - - randGen := rand.New(rand.NewSource(time.Now().UnixNano())) - for { - - // reset selected if all sharders were selected - if len(tq.selected) >= tq.max { - tq.selected = make(map[string]interface{}) - } - - i := randGen.Intn(len(tq.sharders)) - host := tq.sharders[i] - - _, ok := tq.selected[host] - - // it was selected, try next - if ok { - continue - } - - tq.selected[host] = true - - err := tq.checkHealth(ctx, host) - - if err != nil { - if errors.Is(err, ErrNoOnlineSharders) { - return "", err - } - - // it is offline, try next one - continue - } - - return host, nil - } -} - -func newTransactionQuery(sharders []string) (*transactionQuery, error) { - - if len(sharders) == 0 { - return nil, ErrNoAvailableSharders - } - - tq := &transactionQuery{ - max: len(sharders), - sharders: sharders, - } - tq.selected = make(map[string]interface{}) - tq.offline = make(map[string]interface{}) - - return tq, nil -} - -// fromAll query transaction from all sharders whatever it is selected or offline in previous queires, and return consensus result -func (tq *transactionQuery) fromAll(query string, handle queryResultHandle, timeout RequestTimeout) error { - if tq == nil || tq.max == 0 { - return ErrNoAvailableSharders - } - - ctx, cancel := makeTimeoutContext(timeout) - defer cancel() - - urls := make([]string, 0, tq.max) - for _, host := range tq.sharders { - urls = append(urls, tq.buildUrl(host, query)) - } - - r := resty.New() - r.DoGet(ctx, urls...). - Then(func(req *http.Request, resp *http.Response, respBody []byte, cf context.CancelFunc, err error) error { - res := QueryResult{ - Content: respBody, - Error: err, - StatusCode: http.StatusBadRequest, - } - - if resp != nil { - res.StatusCode = resp.StatusCode - - logging.Debug(req.URL.String() + " " + resp.Status) - logging.Debug(string(respBody)) - } else { - logging.Debug(req.URL.String()) - - } - - if handle != nil { - if handle(res) { - - cf() - } - } - - return nil - }) - - r.Wait() - - return nil -} - -// fromAny query transaction from any sharder that is not selected in previous queires. use any used sharder if there is not any unused sharder -func (tq *transactionQuery) fromAny(query string, timeout RequestTimeout) (QueryResult, error) { - res := QueryResult{ - StatusCode: http.StatusBadRequest, - } - - ctx, cancel := makeTimeoutContext(timeout) - defer cancel() - - err := tq.validate(1) - - if err != nil { - return res, err - } - - host, err := tq.randOne(ctx) - - if err != nil { - return res, err - } - - r := resty.New() - requestUrl := tq.buildUrl(host, query) - - logging.Debug("GET", requestUrl) - - r.DoGet(ctx, requestUrl). - Then(func(req *http.Request, resp *http.Response, respBody []byte, cf context.CancelFunc, err error) error { - res.Error = err - if err != nil { - return err - } - - res.Content = respBody - logging.Debug(string(respBody)) - - if resp != nil { - res.StatusCode = resp.StatusCode - } - - return nil - }) - - errs := r.Wait() - - if len(errs) > 0 { - return res, errs[0] - } - - return res, nil - -} - -func (tq *transactionQuery) getInfo(query string, timeout RequestTimeout) (*QueryResult, error) { - - consensuses := make(map[int]int) - var maxConsensus int - var consensusesResp QueryResult - // {host}{query} - - err := tq.fromAll(query, - func(qr QueryResult) bool { - //ignore response if it is network error - if qr.StatusCode >= 500 { - return false - } - - consensuses[qr.StatusCode]++ - if consensuses[qr.StatusCode] >= maxConsensus { - maxConsensus = consensuses[qr.StatusCode] - consensusesResp = qr - } - - return false - - }, timeout) - - if err != nil { - return nil, err - } - - if maxConsensus == 0 { - return nil, stderrors.New("zcn: query not found") - } - - rate := float32(maxConsensus*100) / float32(tq.max) - if rate < consensusThresh { - return nil, ErrInvalidConsensus - } - - if consensusesResp.StatusCode != http.StatusOK { - return nil, stderrors.New(string(consensusesResp.Content)) - } - - return &consensusesResp, nil -} - -func (tq *transactionQuery) getConsensusConfirmation(numSharders int, txnHash string, timeout RequestTimeout) (*blockHeader, map[string]json.RawMessage, *blockHeader, error) { - var maxConfirmation int - txnConfirmations := make(map[string]int) - var confirmationBlockHeader *blockHeader - var confirmationBlock map[string]json.RawMessage - var lfbBlockHeader *blockHeader - maxLfbBlockHeader := int(0) - lfbBlockHeaders := make(map[string]int) - - // {host}/v1/transaction/get/confirmation?hash={txnHash}&content=lfb - err := tq.fromAll(tq.buildUrl("", TXN_VERIFY_URL, txnHash, "&content=lfb"), - func(qr QueryResult) bool { - if qr.StatusCode != http.StatusOK { - return false - } - - var cfmBlock map[string]json.RawMessage - err := json.Unmarshal([]byte(qr.Content), &cfmBlock) - if err != nil { - logging.Error("txn confirmation parse error", err) - return false - } - - // parse `confirmation` section as block header - cfmBlockHeader, err := getBlockHeaderFromTransactionConfirmation(txnHash, cfmBlock) - if err != nil { - logging.Error("txn confirmation parse header error", err) - - // parse `latest_finalized_block` section - if lfbRaw, ok := cfmBlock["latest_finalized_block"]; ok { - var lfb blockHeader - err := json.Unmarshal([]byte(lfbRaw), &lfb) - if err != nil { - logging.Error("round info parse error.", err) - return false - } - - lfbBlockHeaders[lfb.Hash]++ - if lfbBlockHeaders[lfb.Hash] > maxLfbBlockHeader { - maxLfbBlockHeader = lfbBlockHeaders[lfb.Hash] - lfbBlockHeader = &lfb - } - } - - return false - } - - txnConfirmations[cfmBlockHeader.Hash]++ - if txnConfirmations[cfmBlockHeader.Hash] > maxConfirmation { - maxConfirmation = txnConfirmations[cfmBlockHeader.Hash] - - if maxConfirmation >= numSharders { - confirmationBlockHeader = cfmBlockHeader - confirmationBlock = cfmBlock - - // it is consensus by enough sharders, and latest_finalized_block is valid - // return true to cancel other requests - return true - } - } - - return false - - }, timeout) - - if err != nil { - return nil, nil, lfbBlockHeader, err - } - - if maxConfirmation == 0 { - return nil, nil, lfbBlockHeader, stderrors.New("zcn: transaction not found") - } - - if maxConfirmation < numSharders { - return nil, nil, lfbBlockHeader, ErrInvalidConsensus - } - - return confirmationBlockHeader, confirmationBlock, lfbBlockHeader, nil -} - -// getFastConfirmation get txn confirmation from a random online sharder -func (tq *transactionQuery) getFastConfirmation(txnHash string, timeout RequestTimeout) (*blockHeader, map[string]json.RawMessage, *blockHeader, error) { - var confirmationBlockHeader *blockHeader - var confirmationBlock map[string]json.RawMessage - var lfbBlockHeader blockHeader - - // {host}/v1/transaction/get/confirmation?hash={txnHash}&content=lfb - result, err := tq.fromAny(tq.buildUrl("", TXN_VERIFY_URL, txnHash, "&content=lfb"), timeout) - if err != nil { - return nil, nil, nil, err - } - - if result.StatusCode == http.StatusOK { - - err = json.Unmarshal(result.Content, &confirmationBlock) - if err != nil { - logging.Error("txn confirmation parse error", err) - return nil, nil, nil, err - } - - // parse `confirmation` section as block header - confirmationBlockHeader, err = getBlockHeaderFromTransactionConfirmation(txnHash, confirmationBlock) - if err == nil { - return confirmationBlockHeader, confirmationBlock, nil, nil - } - - logging.Error("txn confirmation parse header error", err) - - // parse `latest_finalized_block` section - lfbRaw, ok := confirmationBlock["latest_finalized_block"] - if !ok { - return confirmationBlockHeader, confirmationBlock, nil, err - } - - err = json.Unmarshal([]byte(lfbRaw), &lfbBlockHeader) - if err == nil { - return confirmationBlockHeader, confirmationBlock, &lfbBlockHeader, ErrTransactionNotConfirmed - } - - logging.Error("round info parse error.", err) - return nil, nil, nil, err - - } - - return nil, nil, nil, thrown.Throw(ErrTransactionNotFound, strconv.Itoa(result.StatusCode)) -} - -func GetInfoFromSharders(urlSuffix string, op int, cb GetInfoCallback) { - nodeClient, err := client.GetNode() - if err != nil { - cb.OnInfoAvailable(op, StatusError, "", err.Error()) - return - } - - tq, err := newTransactionQuery(util.Shuffle(nodeClient.Sharders().Healthy())) - if err != nil { - cb.OnInfoAvailable(op, StatusError, "", err.Error()) - return - } - - qr, err := tq.getInfo(urlSuffix, nil) - if err != nil { - cb.OnInfoAvailable(op, StatusError, "", err.Error()) - return - } - - cb.OnInfoAvailable(op, StatusSuccess, string(qr.Content), "") -} - -func GetInfoFromAnySharder(urlSuffix string, op int, cb GetInfoCallback) { - nodeClient, err := client.GetNode() - if err != nil { - cb.OnInfoAvailable(op, StatusError, "", err.Error()) - return - } - - tq, err := newTransactionQuery(util.Shuffle(nodeClient.Sharders().Healthy())) - if err != nil { - cb.OnInfoAvailable(op, StatusError, "", err.Error()) - return - } - - qr, err := tq.fromAny(urlSuffix, nil) - if err != nil { - cb.OnInfoAvailable(op, StatusError, "", err.Error()) - return - } - - cb.OnInfoAvailable(op, StatusSuccess, string(qr.Content), "") -} diff --git a/zcncore/transaction_query_test.go b/zcncore/transaction_query_test.go deleted file mode 100644 index 1bf8da5fb..000000000 --- a/zcncore/transaction_query_test.go +++ /dev/null @@ -1,250 +0,0 @@ -package zcncore - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "log" - "math/rand" - "net" - "net/http" - "os" - "testing" - "time" - - "github.com/stretchr/testify/require" -) - -const ( - keyServerAddr = "serverAddr" - addrPrefix = "http://localhost" -) - -var tq *TransactionQuery -var numSharders int -var avgTimeToFindSharder float32 -var maxTimePerIteration float32 -var sharders []string - -type SharderHealthStatus struct { - Host string `json:"host"` - HealthStatus string `json:"health"` -} - -func TestMain(m *testing.M) { - numSharders = 10 - sharders = make([]string, 0) - for i := 0; i < numSharders; i++ { - port := fmt.Sprintf(":600%d", i) - sharders = append(sharders, addrPrefix+port) - } - startMockSharderServers(sharders) - // wait for 2s for all servers to start - time.Sleep(2 * time.Second) - exitVal := m.Run() - os.Exit(exitVal) -} - -func TestGetRandomSharder(t *testing.T) { - var err error - tq, err = NewTransactionQuery(sharders, []string{}) - if err != nil { - t.Fatalf("Failed to create new transaction query: %v", err) - } - - for _, tc := range []struct { - name string - onlineSharders []string - expectedErr error - setupContext func(ctx context.Context) context.Context - }{ - { - name: "context deadline exceeded", - onlineSharders: []string{"http://localhost:6009"}, - expectedErr: context.DeadlineExceeded, - setupContext: func(ct context.Context) context.Context { - ctx, cancel := context.WithTimeout(ct, 100*time.Microsecond) - go func() { - <-ctx.Done() - cancel() - }() - return ctx - }, - }, - { - name: "all sharders online", - onlineSharders: sharders, - expectedErr: nil, - }, - { - name: "only one sharder online", - onlineSharders: []string{"http://localhost:6000"}, - expectedErr: nil, - }, - { - name: "few sharders online", - onlineSharders: []string{"http://localhost:6001", "http://localhost:6006", "http://localhost:6009"}, - expectedErr: nil, - }, - { - name: "all sharders offline", - onlineSharders: []string{}, - expectedErr: ErrNoOnlineSharders, - }, - } { - t.Run(tc.name, func(t *testing.T) { - tq.Reset() - - for _, s := range sharders { - if !contains(tc.onlineSharders, s) { - tq.Lock() - tq.offline[s] = true - tq.Unlock() - } - } - ctx := context.Background() - if tc.setupContext != nil { - ctx = tc.setupContext(ctx) - } - sharder, err := tq.getRandomSharderWithHealthcheck(ctx) - if tc.expectedErr == nil { - require.NoError(t, err) - require.Subset(t, tc.onlineSharders, []string{sharder}) - } else { - require.EqualError(t, err, tc.expectedErr.Error()) - } - }) - } -} - -// Maybe replace this with the standard go benchmark later on -func TestGetRandomSharderAndBenchmark(t *testing.T) { - var err error - tq, err = NewTransactionQuery(sharders, []string{}) - if err != nil { - t.Fatalf("Failed to create new transaction query: %v", err) - } - - done := make(chan struct{}) - go startAndStopShardersRandomly(done) - fetchRandomSharderAndBenchmark(t) - close(done) -} - -func startMockSharderServers(sharders []string) { - for i := range sharders { - url := fmt.Sprintf(":600%d", i) - go func(url string) { - ctx, cancel := context.WithCancel(context.Background()) - mx := http.NewServeMux() - mx.HandleFunc(SharderEndpointHealthCheck, getSharderHealth) - httpServer := &http.Server{ - Addr: url, - Handler: mx, - BaseContext: func(l net.Listener) context.Context { - ctx := context.WithValue(ctx, keyServerAddr, url) // nolint - return ctx - }, - } - log.Printf("Starting sharder server at: %v", url) - err := httpServer.ListenAndServe() - if errors.Is(err, http.ErrServerClosed) { - log.Printf("server %v closed\n", httpServer.Addr) - } else if err != nil { - log.Printf("error listening for server one: %s\n", err) - } - cancel() - }(url) - } -} - -func getSharderHealth(w http.ResponseWriter, req *http.Request) { - ctx := req.Context() - sharderHost := ctx.Value(keyServerAddr).(string) - tq.RLock() - _, ok := tq.offline[sharderHost] - tq.RUnlock() - if ok { - errorAny(w, 404, fmt.Sprintf("sharder %v is offline", sharderHost)) - } else { - healthStatus := &SharderHealthStatus{ - Host: sharderHost, - HealthStatus: "healthy", - } - err := json.NewEncoder(w).Encode(healthStatus) - if err != nil { - errorAny(w, http.StatusInternalServerError, "failed to encode json") - } - } -} - -func startAndStopShardersRandomly(done chan struct{}) { - for { - select { - case <-time.After(5 * time.Millisecond): - tq.Lock() - // mark a random sharder offline every 5ms - randGen := rand.New(rand.NewSource(time.Now().UnixNano())) - randomSharder := tq.sharders[randGen.Intn(numSharders)] - tq.offline[randomSharder] = true - tq.Unlock() - - case <-time.After(3 * time.Millisecond): - tq.Lock() - // mark a random sharder online every 3ms - randGen := rand.New(rand.NewSource(time.Now().UnixNano())) - randomSharder := tq.sharders[randGen.Intn(numSharders)] - delete(tq.offline, randomSharder) - tq.Unlock() - - case <-time.After(5 * time.Second): - //Randomly mark all sharders online every 5s - tq.Lock() - tq.Reset() - tq.Unlock() - case <-done: - return - } - } -} - -func fetchRandomSharderAndBenchmark(t *testing.T) { - numIterations := 5 - for i := 0; i < numIterations; i++ { - // Sleep for sometime to have some random sharders started and stopped - time.Sleep(20 * time.Millisecond) - ctx := context.Background() - start := time.Now() - _, err := tq.getRandomSharderWithHealthcheck(ctx) - if err != nil { - t.Fatalf("Failed to get a random sharder err: %v", err) - } - end := float32(time.Since(start) / time.Microsecond) - if end > maxTimePerIteration { - maxTimePerIteration = end - } - avgTimeToFindSharder += end - - } - avgTimeToFindSharder = (avgTimeToFindSharder / float32(numIterations)) / 1000 - maxTimePerIteration /= 1000 - t.Logf("Average time to find a random sharder: %vms and max time for an iteration: %vms", avgTimeToFindSharder, maxTimePerIteration) -} - -func errorAny(w http.ResponseWriter, status int, msg string) { - httpMsg := fmt.Sprintf("%d %s", status, http.StatusText(status)) - if msg != "" { - httpMsg = fmt.Sprintf("%s - %s", httpMsg, msg) - } - http.Error(w, httpMsg, status) -} - -func contains(list []string, e string) bool { - for _, l := range list { - if l == e { - return true - } - } - return false -} diff --git a/zcncore/transactionauth.go b/zcncore/transactionauth.go deleted file mode 100644 index fa5fa11ad..000000000 --- a/zcncore/transactionauth.go +++ /dev/null @@ -1,320 +0,0 @@ -//go:build !mobile -// +build !mobile - -package zcncore - -import ( - "encoding/json" - "fmt" - "github.com/0chain/errors" - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/transaction" - "math" -) - -func (ta *TransactionWithAuth) ExecuteSmartContract(address, methodName string, - input interface{}, val uint64, feeOpts ...FeeOption) (*transaction.Transaction, error) { - err := ta.t.createSmartContractTxn(address, methodName, input, val, feeOpts...) - if err != nil { - return nil, err - } - go func() { - ta.submitTxn() - }() - return ta.t.txn, nil -} - -func (ta *TransactionWithAuth) Send(toClientID string, val uint64, desc string) error { - txnData, err := json.Marshal(transaction.SmartContractTxnData{Name: "transfer", InputArgs: SendTxnData{Note: desc}}) - if err != nil { - return errors.New("", "Could not serialize description to transaction_data") - } - - clientNode, err := client.GetNode() - if err != nil { - return err - } - - ta.t.txn.TransactionType = transaction.TxnTypeSend - ta.t.txn.ToClientID = toClientID - ta.t.txn.Value = val - ta.t.txn.TransactionData = string(txnData) - if ta.t.txn.TransactionFee == 0 { - fee, err := transaction.EstimateFee(ta.t.txn, clientNode.Network().Miners, 0.2) - if err != nil { - return err - } - ta.t.txn.TransactionFee = fee - } - - go func() { - ta.submitTxn() - }() - - return nil -} - -func (ta *TransactionWithAuth) MinerSCLock(providerId string, providerType Provider, lock uint64) error { - if lock > math.MaxInt64 { - return errors.New("invalid_lock", "int64 overflow on lock value") - } - - pr := &stakePoolRequest{ - ProviderID: providerId, - ProviderType: providerType, - } - err := ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_LOCK, pr, lock) - if err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return nil -} - -func (ta *TransactionWithAuth) MinerSCUnlock(providerId string, providerType Provider) error { - pr := &stakePoolRequest{ - ProviderID: providerId, - ProviderType: providerType, - } - err := ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_LOCK, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return err -} - -func (ta *TransactionWithAuth) MinerSCCollectReward(providerId string, providerType Provider) error { - pr := &scCollectReward{ - ProviderId: providerId, - ProviderType: int(providerType), - } - err := ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_COLLECT_REWARD, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go ta.submitTxn() - return err -} - -func (ta *TransactionWithAuth) MinerSCKill(providerId string, providerType Provider) error { - pr := &scCollectReward{ - ProviderId: providerId, - ProviderType: int(providerType), - } - var name string - switch providerType { - case ProviderMiner: - name = transaction.MINERSC_KILL_MINER - case ProviderSharder: - name = transaction.MINERSC_KILL_SHARDER - default: - return fmt.Errorf("kill provider type %v not implimented", providerType) - } - err := ta.t.createSmartContractTxn(MinerSmartContractAddress, name, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go ta.submitTxn() - return err -} - -func (ta *TransactionWithAuth) StorageSCCollectReward(providerId string, providerType Provider) error { - pr := &scCollectReward{ - ProviderId: providerId, - ProviderType: int(providerType), - } - err := ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_COLLECT_REWARD, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return err -} - -// faucet smart contract - -func (ta *TransactionWithAuth) FaucetUpdateConfig(ip *InputMap) (err error) { - err = ta.t.createSmartContractTxn(FaucetSmartContractAddress, - transaction.FAUCETSC_UPDATE_SETTINGS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) MinerScUpdateConfig(ip *InputMap) (err error) { - err = ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_UPDATE_SETTINGS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) MinerScUpdateGlobals(ip *InputMap) (err error) { - err = ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_UPDATE_GLOBALS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) StorageScUpdateConfig(ip *InputMap) (err error) { - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_UPDATE_SETTINGS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (t *TransactionWithAuth) AddHardfork(ip *InputMap) (err error) { - err = t.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.ADD_HARDFORK, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { t.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) ZCNSCUpdateGlobalConfig(ip *InputMap) (err error) { - err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_UPDATE_GLOBAL_CONFIG, ip, 0) - if err != nil { - logging.Error(err) - return - } - go ta.submitTxn() - return -} - -func (ta *TransactionWithAuth) GetVerifyConfirmationStatus() ConfirmationStatus { - return ta.t.GetVerifyConfirmationStatus() //nolint -} - -func (ta *TransactionWithAuth) MinerSCMinerSettings(info *MinerSCMinerInfo) ( - err error) { - - err = ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_MINER_SETTINGS, info, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) MinerSCSharderSettings(info *MinerSCMinerInfo) ( - err error) { - - err = ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_SHARDER_SETTINGS, info, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) MinerSCDeleteMiner(info *MinerSCMinerInfo) ( - err error) { - - err = ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_MINER_DELETE, info, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) MinerSCDeleteSharder(info *MinerSCMinerInfo) ( - err error) { - - err = ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_SHARDER_DELETE, info, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) ZCNSCUpdateAuthorizerConfig(ip *AuthorizerNode) (err error) { - err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_UPDATE_AUTHORIZER_CONFIG, ip, 0) - if err != nil { - logging.Error(err) - return - } - go ta.submitTxn() - return -} - -func (ta *TransactionWithAuth) ZCNSCAddAuthorizer(ip *AddAuthorizerPayload) (err error) { - err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_ADD_AUTHORIZER, ip, 0) - if err != nil { - logging.Error(err) - return - } - go ta.submitTxn() - return -} - -func (ta *TransactionWithAuth) ZCNSCAuthorizerHealthCheck(ip *AuthorizerHealthCheckPayload) (err error) { - err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_AUTHORIZER_HEALTH_CHECK, ip, 0) - if err != nil { - logging.Error(err) - return - } - go ta.t.setNonceAndSubmit() - return -} - -func (ta *TransactionWithAuth) ZCNSCDeleteAuthorizer(ip *DeleteAuthorizerPayload) (err error) { - err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_DELETE_AUTHORIZER, ip, 0) - if err != nil { - logging.Error(err) - return - } - go ta.submitTxn() - return -} - -func (ta *TransactionWithAuth) ZCNSCCollectReward(providerId string, providerType Provider) error { - pr := &scCollectReward{ - ProviderId: providerId, - ProviderType: int(providerType), - } - err := ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, - transaction.ZCNSC_COLLECT_REWARD, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { ta.t.setNonceAndSubmit() }() - return err -} diff --git a/zcncore/transactionauth_base.go b/zcncore/transactionauth_base.go deleted file mode 100644 index c044cbea2..000000000 --- a/zcncore/transactionauth_base.go +++ /dev/null @@ -1,200 +0,0 @@ -package zcncore - -import ( - "encoding/json" - - "github.com/0chain/errors" - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/node" - "github.com/0chain/gosdk/core/sys" - "github.com/0chain/gosdk/core/transaction" - "github.com/0chain/gosdk/core/zcncrypto" -) - -type TransactionWithAuth struct { - t *Transaction -} - -func (ta *TransactionWithAuth) Hash() string { - return ta.t.txnHash -} - -func (ta *TransactionWithAuth) SetTransactionNonce(txnNonce int64) error { - return ta.t.SetTransactionNonce(txnNonce) -} - -func newTransactionWithAuth(cb TransactionCallback, txnFee uint64, nonce int64) (*TransactionWithAuth, error) { - ta := &TransactionWithAuth{} - var err error - ta.t, err = newTransaction(cb, txnFee, nonce) - return ta, err -} - -func (ta *TransactionWithAuth) getAuthorize() (*transaction.Transaction, error) { - ta.t.txn.PublicKey = client.Wallet().ClientKey - err := ta.t.txn.ComputeHashAndSign(SignFn) - if err != nil { - return nil, errors.Wrap(err, "signing error.") - } - - jsonByte, err := json.Marshal(ta.t.txn) - if err != nil { - return nil, err - } - - if sys.Authorize == nil { - return nil, errors.New("not_initialized", "no authorize func is set, define it in native code and set in sys") - } - authorize, err := sys.Authorize(string(jsonByte)) - if err != nil { - return nil, err - } - - var txnResp transaction.Transaction - err = json.Unmarshal([]byte(authorize), &txnResp) - if err != nil { - return nil, errors.Wrap(err, "invalid json on auth response.") - } - // Verify the split key signed signature - ok, err := txnResp.VerifySigWith(client.PublicKey(), sys.VerifyWith) - if err != nil { - logging.Error("verification failed for txn from auth", err.Error()) - return nil, errAuthVerifyFailed - } - if !ok { - return nil, errAuthVerifyFailed - } - return &txnResp, nil -} - -func (ta *TransactionWithAuth) completeTxn(status int, out string, err error) { - // do error code translation - if status != StatusSuccess { - switch err { - case errNetwork: - status = StatusNetworkError - case errUserRejected: - status = StatusRejectedByUser - case errAuthVerifyFailed: - status = StatusAuthVerifyFailed - case errAuthTimeout: - status = StatusAuthTimeout - } - } - ta.t.completeTxn(status, out, err) //nolint -} - -func verifyFn(signature, msgHash, publicKey string) (bool, error) { - v := zcncrypto.NewSignatureScheme(client.SignatureScheme()) - err := v.SetPublicKey(publicKey) - if err != nil { - return false, err - } - - ok, err := v.Verify(signature, msgHash) - if err != nil || !ok { - return false, errors.New("", `{"error": "signature_mismatch"}`) - } - return true, nil -} - -func (ta *TransactionWithAuth) sign(otherSig string) error { - ta.t.txn.ComputeHashData() - - sig, err := AddSignature(client.Wallet().Keys[0].PrivateKey, otherSig, ta.t.txn.Hash) - if err != nil { - return err - } - ta.t.txn.Signature = sig - return nil -} - -func (ta *TransactionWithAuth) submitTxn() { - nonce := ta.t.txn.TransactionNonce - if nonce < 1 { - nonce = node.Cache.GetNextNonce(ta.t.txn.ClientID) - } else { - node.Cache.Set(ta.t.txn.ClientID, nonce) - } - ta.t.txn.TransactionNonce = nonce - authTxn, err := ta.getAuthorize() - if err != nil { - logging.Error("get auth error for send, err: ", err.Error()) - ta.completeTxn(StatusAuthError, "", err) - return - } - - // Use the timestamp from auth and sign - ta.t.txn.CreationDate = authTxn.CreationDate - ta.t.txn.Signature = authTxn.Signature - ta.t.submitTxn() -} - -func (ta *TransactionWithAuth) StoreData(data string) error { - go func() { - ta.t.txn.TransactionType = transaction.TxnTypeData - ta.t.txn.TransactionData = data - ta.submitTxn() - }() - return nil -} - -// ExecuteFaucetSCWallet impements the Faucet Smart contract for a given wallet -func (ta *TransactionWithAuth) ExecuteFaucetSCWallet(walletStr string, methodName string, input []byte) error { - w, err := ta.t.createFaucetSCWallet(walletStr, methodName, input) - if err != nil { - return err - } - go func() { - nonce := ta.t.txn.TransactionNonce - if nonce < 1 { - nonce = node.Cache.GetNextNonce(ta.t.txn.ClientID) - } else { - node.Cache.Set(ta.t.txn.ClientID, nonce) - } - ta.t.txn.TransactionNonce = nonce - err = ta.t.txn.ComputeHashAndSignWithWallet(signWithWallet, w) - if err != nil { - return - } - ta.submitTxn() - }() - return nil -} - -func (ta *TransactionWithAuth) SetTransactionCallback(cb TransactionCallback) error { - return ta.t.SetTransactionCallback(cb) -} - -func (ta *TransactionWithAuth) SetTransactionHash(hash string) error { - return ta.t.SetTransactionHash(hash) -} - -func (ta *TransactionWithAuth) GetTransactionHash() string { - return ta.t.GetTransactionHash() -} - -func (ta *TransactionWithAuth) Verify() error { - return ta.t.Verify() -} - -func (ta *TransactionWithAuth) GetVerifyOutput() string { - return ta.t.GetVerifyOutput() -} - -func (ta *TransactionWithAuth) GetTransactionError() string { - return ta.t.GetTransactionError() -} - -func (ta *TransactionWithAuth) GetVerifyError() string { - return ta.t.GetVerifyError() -} - -func (ta *TransactionWithAuth) Output() []byte { - return []byte(ta.t.txnOut) -} - -// GetTransactionNonce returns nonce -func (ta *TransactionWithAuth) GetTransactionNonce() int64 { - return ta.t.txn.TransactionNonce -} diff --git a/zcncore/transactionauth_mobile.go b/zcncore/transactionauth_mobile.go deleted file mode 100644 index 9666d10eb..000000000 --- a/zcncore/transactionauth_mobile.go +++ /dev/null @@ -1,246 +0,0 @@ -//go:build mobile -// +build mobile - -package zcncore - -import ( - "encoding/json" - "github.com/0chain/errors" - "github.com/0chain/gosdk/core/transaction" -) - -func (ta *TransactionWithAuth) ExecuteSmartContract(address, methodName string, - input interface{}, val uint64, feeOpts ...FeeOption) (*transaction.Transaction, error) { - err := ta.t.createSmartContractTxn(address, methodName, input, val, feeOpts...) - if err != nil { - return nil, err - } - go func() { - ta.submitTxn() - }() - return ta.t.txn, nil -} - -func (ta *TransactionWithAuth) Send(toClientID string, val uint64, desc string) error { - txnData, err := json.Marshal(SendTxnData{Note: desc}) - if err != nil { - return errors.New("", "Could not serialize description to transaction_data") - } - go func() { - ta.t.txn.TransactionType = transaction.TxnTypeSend - ta.t.txn.ToClientID = toClientID - ta.t.txn.Value = val - ta.t.txn.TransactionData = string(txnData) - ta.submitTxn() - }() - return nil -} - -func (ta *TransactionWithAuth) MinerSCLock(providerId string, providerType int, lock string) error { - lv, err := parseCoinStr(lock) - if err != nil { - return err - } - - pr := stakePoolRequest{ - ProviderType: providerType, - ProviderID: providerId, - } - - err = ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_LOCK, &pr, lv) - if err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return nil -} - -func (ta *TransactionWithAuth) MinerSCUnlock(providerId string, providerType int) error { - pr := &stakePoolRequest{ - ProviderID: providerId, - ProviderType: providerType, - } - err := ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_LOCK, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return err -} - -func (ta *TransactionWithAuth) MinerSCCollectReward(providerId string, providerType int) error { - pr := &scCollectReward{ - ProviderId: providerId, - ProviderType: providerType, - } - err := ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_COLLECT_REWARD, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go ta.submitTxn() - return err -} - -func (ta *TransactionWithAuth) StorageSCCollectReward(providerId string, providerType int) error { - pr := &scCollectReward{ - ProviderId: providerId, - ProviderType: providerType, - } - err := ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_COLLECT_REWARD, pr, 0) - if err != nil { - logging.Error(err) - return err - } - go func() { ta.submitTxn() }() - return err -} - -// faucet smart contract - -func (ta *TransactionWithAuth) FaucetUpdateConfig(ip InputMap) (err error) { - err = ta.t.createSmartContractTxn(FaucetSmartContractAddress, - transaction.FAUCETSC_UPDATE_SETTINGS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) MinerScUpdateConfig(ip InputMap) (err error) { - err = ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_UPDATE_SETTINGS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) MinerScUpdateGlobals(ip InputMap) (err error) { - err = ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_UPDATE_GLOBALS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) StorageScUpdateConfig(ip InputMap) (err error) { - err = ta.t.createSmartContractTxn(StorageSmartContractAddress, - transaction.STORAGESC_UPDATE_SETTINGS, ip, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) ZCNSCUpdateGlobalConfig(ip InputMap) (err error) { - err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, - transaction.ZCNSC_UPDATE_GLOBAL_CONFIG, ip, 0) - if err != nil { - logging.Error(err) - return - } - go ta.submitTxn() - return -} - -func (ta *TransactionWithAuth) GetVerifyConfirmationStatus() int { - return ta.t.GetVerifyConfirmationStatus() -} - -func (ta *TransactionWithAuth) MinerSCMinerSettings(info MinerSCMinerInfo) ( - err error) { - - err = ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_MINER_SETTINGS, info, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) MinerSCSharderSettings(info MinerSCMinerInfo) ( - err error) { - - err = ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_SHARDER_SETTINGS, info, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) MinerSCDeleteMiner(info MinerSCMinerInfo) ( - err error) { - - err = ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_MINER_DELETE, info, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) MinerSCDeleteSharder(info MinerSCMinerInfo) ( - err error) { - - err = ta.t.createSmartContractTxn(MinerSmartContractAddress, - transaction.MINERSC_SHARDER_DELETE, info, 0) - if err != nil { - logging.Error(err) - return - } - go func() { ta.submitTxn() }() - return -} - -func (ta *TransactionWithAuth) ZCNSCUpdateAuthorizerConfig(ip AuthorizerNode) (err error) { - err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_UPDATE_AUTHORIZER_CONFIG, ip, 0) - if err != nil { - logging.Error(err) - return - } - go ta.submitTxn() - return -} - -func (ta *TransactionWithAuth) ZCNSCAddAuthorizer(ip AddAuthorizerPayload) (err error) { - err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_ADD_AUTHORIZER, ip, 0) - if err != nil { - logging.Error(err) - return - } - go ta.submitTxn() - return -} - -func (ta *TransactionWithAuth) ZCNSCAuthorizerHealthCheck(ip *AuthorizerHealthCheckPayload) (err error) { - err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_AUTHORIZER_HEALTH_CHECK, ip, 0) - if err != nil { - logging.Error(err) - return - } - go ta.t.setNonceAndSubmit() - return -} diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 269843cda..7fe34e6ee 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -1,25 +1,14 @@ package zcncore import ( - "context" "encoding/hex" - "encoding/json" - "fmt" - "net/http" - "net/url" - "strconv" - "strings" - "sync" "time" "errors" "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/logger" - "github.com/0chain/gosdk/core/tokenrate" - "github.com/0chain/gosdk/core/util" "github.com/0chain/gosdk/core/version" "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zboxcore/encryption" @@ -371,883 +360,6 @@ func SplitKeys(privateKey string, numSplits int) (string, error) { return wStr, nil } -type GetClientResponse struct { - ID string `json:"id"` - Version string `json:"version"` - CreationDate int `json:"creation_date"` - PublicKey string `json:"public_key"` -} - -func GetClientDetails(clientID string) (*GetClientResponse, error) { - clientNode, err := client.GetNode() - if err != nil { - panic(err) - } - minerurl := util.GetRandom(clientNode.Network().Miners, 1)[0] - url := minerurl + GET_CLIENT - url = fmt.Sprintf("%v?id=%v", url, clientID) - req, err := util.NewHTTPGetRequest(url) - if err != nil { - logging.Error(minerurl, "new get request failed. ", err.Error()) - return nil, err - } - res, err := req.Get() - if err != nil { - logging.Error(minerurl, "send error. ", err.Error()) - return nil, err - } - - var clientDetails GetClientResponse - err = json.Unmarshal([]byte(res.Body), &clientDetails) - if err != nil { - return nil, err - } - - return &clientDetails, nil -} - -// Deprecated: Use zcncrypto.IsMnemonicValid() -// IsMnemonicValid is an utility function to check the mnemonic valid -// -// # Inputs -// - mnemonic: mnemonics -func IsMnemonicValid(mnemonic string) bool { - return zcncrypto.IsMnemonicValid(mnemonic) -} - -// SetWalletInfo should be set before any transaction or client specific APIs -// splitKeyWallet parameter is valid only if SignatureScheme is "BLS0Chain" -// -// # Inputs -// - jsonWallet: json format of wallet -// { -// "client_id":"30764bcba73216b67c36b05a17b4dd076bfdc5bb0ed84856f27622188c377269", -// "client_key":"1f495df9605a4479a7dd6e5c7a78caf9f9d54e3a40f62a3dd68ed377115fe614d8acf0c238025f67a85163b9fbf31d10fbbb4a551d1cf00119897edf18b1841c", -// "keys":[ -// {"public_key":"1f495df9605a4479a7dd6e5c7a78caf9f9d54e3a40f62a3dd68ed377115fe614d8acf0c238025f67a85163b9fbf31d10fbbb4a551d1cf00119897edf18b1841c","private_key":"41729ed8d82f782646d2d30b9719acfd236842b9b6e47fee12b7bdbd05b35122"} -// ], -// "mnemonics":"glare mistake gun joke bid spare across diagram wrap cube swear cactus cave repeat you brave few best wild lion pitch pole original wasp", -// "version":"1.0", -// "date_created":"1662534022", -// "nonce":0 -// } -// -// - splitKeyWallet: if wallet keys is split -func SetWalletInfo(jsonWallet string, splitKeyWallet bool) error { - wallet := zcncrypto.Wallet{} - err := json.Unmarshal([]byte(jsonWallet), &wallet) - if err != nil { - return errors.New("invalid jsonWallet: " + err.Error()) - } - - client.SetWallet(wallet) - return client.SetSplitKeyWallet(splitKeyWallet) -} - -// SetAuthUrl will be called by app to set zauth URL to SDK. -// # Inputs -// - url: the url of zAuth server -func SetAuthUrl(url string) error { - return client.SetAuthUrl(url) -} - -func getWalletBalance(clientId string) (common.Balance, int64, error) { - err := checkSdkInit() - if err != nil { - return 0, 0, err - } - - cb := &walletCallback{} - cb.Add(1) - - go func() { - value, info, err := getBalanceFromSharders(clientId) - if err != nil && strings.TrimSpace(info) != `{"error":"value not present"}` { - cb.OnBalanceAvailable(StatusError, value, info) - cb.err = err - return - } - cb.OnBalanceAvailable(StatusSuccess, value, info) - }() - - cb.Wait() - - var clientState struct { - Nonce int64 `json:"nonce"` - } - err = json.Unmarshal([]byte(cb.info), &clientState) - if err != nil { - return 0, 0, err - } - - return cb.balance, clientState.Nonce, cb.err -} - -// GetBalance retrieve wallet balance from sharders -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetBalance(cb GetBalanceCallback) error { - err := CheckConfig() - if err != nil { - return err - } - go func() { - value, info, err := getBalanceFromSharders(client.Wallet().ClientID) - if err != nil { - logging.Error(err) - cb.OnBalanceAvailable(StatusError, 0, info) - return - } - cb.OnBalanceAvailable(StatusSuccess, value, info) - }() - return nil -} - -// GetMintNonce retrieve the client's latest mint nonce from sharders -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetMintNonce(cb GetInfoCallback) error { - err := CheckConfig() - if err != nil { - return err - } - - go GetInfoFromSharders(withParams(GET_MINT_NONCE, Params{ - "client_id": client.Wallet().ClientID, - }), OpGetMintNonce, cb) - return nil -} - -// GetNotProcessedZCNBurnTickets retrieve burn tickets that are not compensated by minting -// - ethereumAddress: ethereum address for the issuer of the burn tickets -// - startNonce: start nonce for the burn tickets -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetNotProcessedZCNBurnTickets(ethereumAddress, startNonce string, cb GetInfoCallback) error { - err := CheckConfig() - if err != nil { - return err - } - - go GetInfoFromSharders(withParams(GET_NOT_PROCESSED_BURN_TICKETS, Params{ - "ethereum_address": ethereumAddress, - "nonce": startNonce, - }), OpGetNotProcessedBurnTickets, cb) - - return nil -} - -// GetNonce retrieve wallet nonce from sharders -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetNonce(cb GetNonceCallback) error { - if cb == nil { - cb = &GetNonceCallbackStub{} - } - - err := CheckConfig() - if err != nil { - return err - } - - go func() { - value, info, err := getNonceFromSharders(client.Wallet().ClientID) - if err != nil { - logging.Error(err) - cb.OnNonceAvailable(StatusError, 0, info) - return - } - - cb.OnNonceAvailable(StatusSuccess, value, info) - }() - - return nil -} - -// GetWalletBalance retrieve wallet nonce from sharders -// - clientID: client id -func GetWalletNonce(clientID string) (int64, error) { - cb := &GetNonceCallbackStub{} - - err := CheckConfig() - if err != nil { - return 0, err - } - wait := &sync.WaitGroup{} - wait.Add(1) - go func() { - defer wait.Done() - value, info, err := getNonceFromSharders(clientID) - if err != nil { - logging.Error(err) - cb.OnNonceAvailable(StatusError, 0, info) - return - } - cb.OnNonceAvailable(StatusSuccess, value, info) - }() - - wait.Wait() - - if cb.status == StatusSuccess { - return cb.nonce, nil - } - - return 0, errors.New(cb.info) -} - -// GetBalanceWallet retreives wallet balance from sharders -// - walletStr: wallet string -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetBalanceWallet(walletStr string, cb GetBalanceCallback) error { - w, err := getWallet(walletStr) - if err != nil { - fmt.Printf("Error while parsing the wallet. %v\n", err) - return err - } - - go func() { - value, info, err := getBalanceFromSharders(w.ClientID) - if err != nil { - logging.Error(err) - cb.OnBalanceAvailable(StatusError, 0, info) - return - } - cb.OnBalanceAvailable(StatusSuccess, value, info) - }() - return nil -} - -func getBalanceFromSharders(clientID string) (int64, string, error) { - clientNode, err := client.GetNode() - if err != nil { - return 0, "", err - } - return clientNode.Sharders().GetBalanceFieldFromSharders(clientID, "balance") -} - -func getNonceFromSharders(clientID string) (int64, string, error) { - clientNode, err := client.GetNode() - if err != nil { - return 0, "", err - } - return clientNode.Sharders().GetBalanceFieldFromSharders(clientID, "nonce") -} - -// ConvertToToken converts the SAS tokens to ZCN tokens -// - token: SAS tokens amount -func ConvertToToken(token int64) float64 { - return float64(token) / float64(common.TokenUnit) -} - -// ConvertTokenToUSD converts the ZCN tokens to USD amount -// - token: ZCN tokens amount -func ConvertTokenToUSD(token float64) (float64, error) { - zcnRate, err := getTokenUSDRate() - if err != nil { - return 0, err - } - return token * zcnRate, nil -} - -// ConvertUSDToToken converts the USD amount to ZCN tokens -// - usd: USD amount -func ConvertUSDToToken(usd float64) (float64, error) { - zcnRate, err := getTokenUSDRate() - if err != nil { - return 0, err - } - return usd * (1 / zcnRate), nil -} - -func getTokenUSDRate() (float64, error) { - return tokenrate.GetUSD(context.TODO(), "zcn") -} - -// getWallet get a wallet object from a wallet string -func getWallet(walletStr string) (*zcncrypto.Wallet, error) { - var w zcncrypto.Wallet - err := json.Unmarshal([]byte(walletStr), &w) - if err != nil { - fmt.Printf("error while parsing wallet string.\n%v\n", err) - return nil, err - } - - return &w, nil -} - -// GetWalletClientID extract wallet client id from wallet string -// - walletStr: wallet string to get client id -func GetWalletClientID(walletStr string) (string, error) { - w, err := getWallet(walletStr) - if err != nil { - return "", err - } - return w.ClientID, nil -} - -// GetZcnUSDInfo returns USD value for ZCN token by tokenrate -func GetZcnUSDInfo() (float64, error) { - return tokenrate.GetUSD(context.TODO(), "zcn") -} - -// SetupAuth prepare auth app with clientid, key and a set of public, private key and local publickey -// which is running on PC/Mac. -func SetupAuth(authHost, clientID, clientKey, publicKey, privateKey, localPublicKey string, cb AuthCallback) error { - go func() { - authHost = strings.TrimRight(authHost, "/") - data := map[string]string{"client_id": clientID, "client_key": clientKey, "public_key": publicKey, "private_key": privateKey, "peer_public_key": localPublicKey} - req, err := util.NewHTTPPostRequest(authHost+"/setup", data) - if err != nil { - logging.Error("new post request failed. ", err.Error()) - return - } - res, err := req.Post() - if err != nil { - logging.Error(authHost+"send error. ", err.Error()) - } - if res.StatusCode != http.StatusOK { - cb.OnSetupComplete(StatusError, res.Body) - return - } - cb.OnSetupComplete(StatusSuccess, "") - }() - return nil -} - -// GetIdForUrl retrieve the ID of the network node (miner/sharder) given its url. -// - url: url of the node. -func GetIdForUrl(url string) string { - url = strings.TrimRight(url, "/") - url = fmt.Sprintf("%v/_nh/whoami", url) - req, err := util.NewHTTPGetRequest(url) - if err != nil { - logging.Error(url, "new get request failed. ", err.Error()) - return "" - } - res, err := req.Get() - if err != nil { - logging.Error(url, "get error. ", err.Error()) - return "" - } - - s := strings.Split(res.Body, ",") - if len(s) >= 3 { - return s[3] - } - return "" -} - -type Params map[string]string - -func (p Params) Query() string { - if len(p) == 0 { - return "" - } - var params = make(url.Values) - for k, v := range p { - params[k] = []string{v} - } - return "?" + params.Encode() -} - -// -// miner SC -// - -// GetMiners obtains list of all active miners. -// - cb: info callback instance, carries the response of the GET request to the sharders -// - limit: how many miners should be fetched -// - offset: how many miners should be skipped -// - active: retrieve only active miners -// - stakable: retreive only stakable miners -func GetMiners(cb GetInfoCallback, limit, offset int, active bool, stakable bool) { - getMinersInternal(cb, active, stakable, limit, offset) -} - -func getMinersInternal(cb GetInfoCallback, active, stakable bool, limit, offset int) { - if err := CheckConfig(); err != nil { - return - } - - var url = withParams(GET_MINERSC_MINERS, Params{ - "active": strconv.FormatBool(active), - "stakable": strconv.FormatBool(stakable), - "offset": strconv.FormatInt(int64(offset), 10), - "limit": strconv.FormatInt(int64(limit), 10), - }) - - go GetInfoFromSharders(url, 0, cb) -} - -// GetSharders obtains a list of sharders given the following parameters. -// - cb: info callback instance, carries the response of the GET request to the sharders -// - limit: how many sharders should be fetched -// - offset: how many sharders should be skipped -// - active: retrieve only active sharders -// - stakable: retrieve only sharders that can be staked -func GetSharders(cb GetInfoCallback, limit, offset int, active, stakable bool) { - getShardersInternal(cb, active, stakable, limit, offset) -} - -func getShardersInternal(cb GetInfoCallback, active, stakable bool, limit, offset int) { - if err := CheckConfig(); err != nil { - return - } - - var url = withParams(GET_MINERSC_SHARDERS, Params{ - "active": strconv.FormatBool(active), - "stakable": strconv.FormatBool(stakable), - "offset": strconv.FormatInt(int64(offset), 10), - "limit": strconv.FormatInt(int64(limit), 10), - }) - - go GetInfoFromSharders(url, 0, cb) -} - -func withParams(uri string, params Params) string { - return uri + params.Query() -} - -// GetMinerSCNodeInfo get miner information from sharders -// - id: the id of miner -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetMinerSCNodeInfo(id string, cb GetInfoCallback) (err error) { - - if err = CheckConfig(); err != nil { - return - } - - go GetInfoFromSharders(withParams(GET_MINERSC_NODE, Params{ - "id": id, - }), 0, cb) - return -} - -// GetMinerSCNodePool get miner smart contract node pool -// - id: the id of miner -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetMinerSCNodePool(id string, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - go GetInfoFromSharders(withParams(GET_MINERSC_POOL, Params{ - "id": id, - "pool_id": client.Wallet().ClientID, - }), 0, cb) - - return -} - -// GetMinerSCUserInfo retrieve user stake pools for the providers related to the Miner SC (miners/sharders). -// - clientID: user's wallet id -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetMinerSCUserInfo(clientID string, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - if clientID == "" { - clientID = client.Wallet().ClientID - } - go GetInfoFromSharders(withParams(GET_MINERSC_USER, Params{ - "client_id": clientID, - }), 0, cb) - - return -} - -// GetMinerSCConfig get miner SC configuration -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetMinerSCConfig(cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - go GetInfoFromSharders(GET_MINERSC_CONFIG, 0, cb) - return -} - -// GetMinerSCGlobals get miner SC globals -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetMinerSCGlobals(cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - go GetInfoFromSharders(GET_MINERSC_GLOBALS, 0, cb) - return -} - -// -// Storage SC -// - -// GetStorageSCConfig obtains Storage SC configurations. -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetStorageSCConfig(cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - go GetInfoFromSharders(STORAGESC_GET_SC_CONFIG, OpStorageSCGetConfig, cb) - return -} - -// GetChallengePoolInfo obtains challenge pool information for an allocation. -func GetChallengePoolInfo(allocID string, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - var url = withParams(STORAGESC_GET_CHALLENGE_POOL_INFO, Params{ - "allocation_id": allocID, - }) - go GetInfoFromSharders(url, OpStorageSCGetChallengePoolInfo, cb) - return -} - -// GetAllocation obtains allocation information. -func GetAllocation(allocID string, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - var url = withParams(STORAGESC_GET_ALLOCATION, Params{ - "allocation": allocID, - }) - go GetInfoFromSharders(url, OpStorageSCGetAllocation, cb) - return -} - -// GetAllocations obtains list of allocations of a user. -func GetAllocations(clientID string, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - if clientID == "" { - clientID = client.Wallet().ClientID - } - var url = withParams(STORAGESC_GET_ALLOCATIONS, Params{ - "client": clientID, - }) - go GetInfoFromSharders(url, OpStorageSCGetAllocations, cb) - return -} - -// GetSnapshots obtains list of global snapshots, given an initial round and a limit. -// Global snapshots are historical records of some aggregate data related -// to the network (like total staked amount and total reward amount). -// - round: round number to start fetching snapshots -// - limit: how many snapshots should be fetched -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetSnapshots(round int64, limit int64, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - var url = withParams(STORAGE_GET_SNAPSHOT, Params{ - "round": strconv.FormatInt(round, 10), - "limit": strconv.FormatInt(limit, 10), - }) - go GetInfoFromAnySharder(url, OpStorageSCGetSnapshots, cb) - return -} - -// GetBlobberSnapshots obtains list of allocations of a blobber. -// Blobber snapshots are historical records of the blobber instance to track its change over time and serve graph requests, -// which are requests that need multiple data points, distributed over an interval of time, usually to plot them on a -// graph. -// - round: round number -// - limit: how many blobber snapshots should be fetched -// - offset: how many blobber snapshots should be skipped -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetBlobberSnapshots(round int64, limit int64, offset int64, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - var url = withParams(STORAGE_GET_BLOBBER_SNAPSHOT, Params{ - "round": strconv.FormatInt(round, 10), - "limit": strconv.FormatInt(limit, 10), - "offset": strconv.FormatInt(offset, 10), - }) - go GetInfoFromAnySharder(url, OpStorageSCGetBlobberSnapshots, cb) - return -} - -// GetMinerSnapshots obtains a list of miner snapshots starting from a specific round. -// Miner snapshots are historical records of the miner instance to track its change over time and serve graph requests, -// which are requests that need multiple data points, distributed over an interval of time, usually to plot them on a -// graph. -// - round: round number to start fetching snapshots -// - limit: how many miner snapshots should be fetched -// - offset: how many miner snapshots should be skipped -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetMinerSnapshots(round int64, limit int64, offset int64, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - var url = withParams(STORAGE_GET_MINER_SNAPSHOT, Params{ - "round": strconv.FormatInt(round, 10), - "limit": strconv.FormatInt(limit, 10), - "offset": strconv.FormatInt(offset, 10), - }) - go GetInfoFromAnySharder(url, OpStorageSCGetMinerSnapshots, cb) - return -} - -// GetSharderSnapshots obtains a list of sharder snapshots starting from a specific round. -// Sharder snapshots are historical records of the sharder instance to track its change over time and serve graph requests, -// which are requests that need multiple data points, distributed over an interval of time, usually to plot them on a -// graph. -// - round: round number to start fetching snapshots -// - limit: how many sharder snapshots should be fetched -// - offset: how many sharder snapshots should be skipped -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetSharderSnapshots(round int64, limit int64, offset int64, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - var url = withParams(STORAGE_GET_SHARDER_SNAPSHOT, Params{ - "round": strconv.FormatInt(round, 10), - "limit": strconv.FormatInt(limit, 10), - "offset": strconv.FormatInt(offset, 10), - }) - go GetInfoFromAnySharder(url, OpStorageSCGetSharderSnapshots, cb) - return -} - -// GetValidatorSnapshots obtains list of validator snapshots from the sharders. -// Validator snapshots are historical records of the validator instance to track its change over time and serve graph requests, -// which are requests that need multiple data points, distributed over an interval of time, usually to plot them on a -// graph. -// - round: round number to start fetching snapshots -// - limit: how many validator snapshots should be fetched -// - offset: how many validator snapshots should be skipped -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetValidatorSnapshots(round int64, limit int64, offset int64, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - var url = withParams(STORAGE_GET_VALIDATOR_SNAPSHOT, Params{ - "round": strconv.FormatInt(round, 10), - "limit": strconv.FormatInt(limit, 10), - "offset": strconv.FormatInt(offset, 10), - }) - go GetInfoFromAnySharder(url, OpStorageSCGetValidatorSnapshots, cb) - return -} - -// GetAuthorizerSnapshots obtains list of authorizers snapshots from the sharders. -// Authorizer snapshots are historical records of the authorizer instance to track its change over time and serve graph requests, -// which are requests that need multiple data points, distributed over an interval of time, usually to plot them on a -// graph. -// - round: round number to start fetching snapshots -// - limit: how many authorizer snapshots should be fetched -// - offset: how many authorizer snapshots should be skipped -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetAuthorizerSnapshots(round int64, limit int64, offset int64, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - var url = withParams(STORAGE_GET_AUTHORIZER_SNAPSHOT, Params{ - "round": strconv.FormatInt(round, 10), - "limit": strconv.FormatInt(limit, 10), - "offset": strconv.FormatInt(offset, 10), - }) - go GetInfoFromAnySharder(url, OpStorageSCGetAuthorizerSnapshots, cb) - return -} - -// GetUserSnapshots replicates user snapshots from the sharders -// User snapshots are historical records of the client data to track its change over time and serve graph requests, -// which are requests that need multiple data points, distributed over an interval of time, usually to plot them on a -// graph. -// - round: round number to start fetching snapshots -// - limit: how many user snapshots should be fetched -// - offset: how many user snapshots should be skipped -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetUserSnapshots(round int64, limit int64, offset int64, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - var url = withParams(STORAGE_GET_USER_SNAPSHOT, Params{ - "round": strconv.FormatInt(round, 10), - "limit": strconv.FormatInt(limit, 10), - "offset": strconv.FormatInt(offset, 10), - }) - go GetInfoFromAnySharder(url, OpStorageSCGetUserSnapshots, cb) - return -} - -// GetReadPoolInfo obtains information about read pool of a user. -func GetReadPoolInfo(clientID string, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - if clientID == "" { - clientID = client.Wallet().ClientID - } - var url = withParams(STORAGESC_GET_READ_POOL_INFO, Params{ - "client_id": clientID, - }) - go GetInfoFromSharders(url, OpStorageSCGetReadPoolInfo, cb) - return -} - -// GetStakePoolInfo obtains information about stake pool of a blobber and -// related validator. -func GetStakePoolInfo(blobberID string, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - var url = withParams(STORAGESC_GET_STAKE_POOL_INFO, Params{ - "blobber_id": blobberID, - }) - go GetInfoFromSharders(url, OpStorageSCGetStakePoolInfo, cb) - return -} - -// GetStakePoolUserInfo for a user. -// # Inputs -// - clientID: the id of wallet -// - cb: callback for checking result -func GetStakePoolUserInfo(clientID string, offset, limit int, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - if clientID == "" { - clientID = client.Wallet().ClientID - } - - var url = withParams(STORAGESC_GET_STAKE_POOL_USER_INFO, Params{ - "client_id": clientID, - "offset": strconv.FormatInt(int64(offset), 10), - "limit": strconv.FormatInt(int64(limit), 10), - }) - go GetInfoFromSharders(url, OpStorageSCGetStakePoolInfo, cb) - return -} - -// GetStakeableBlobbers obtains list of all active blobbers that can be staked (i.e. still number of delegations < max_delegations) -// # Inputs -// - cb: info callback instance, carries the response of the GET request to the sharders -// - limit: how many blobbers should be fetched -// - offset: how many blobbers should be skipped -// - active: only fetch active blobbers -func GetStakableBlobbers(cb GetInfoCallback, limit, offset int, active bool) { - getBlobbersInternal(cb, active, limit, offset, true) -} - -// GetBlobbers obtains list of all active blobbers. -// - cb: info callback instance, carries the response of the GET request to the sharders -// - limit: how many blobbers should be fetched -// - offset: how many blobbers should be skipped -// - active: only fetch active blobbers -func GetBlobbers(cb GetInfoCallback, limit, offset int, active bool) { - getBlobbersInternal(cb, active, limit, offset, false) -} - -func getBlobbersInternal(cb GetInfoCallback, active bool, limit, offset int, stakable bool) { - if err := CheckConfig(); err != nil { - return - } - - var url = withParams(STORAGESC_GET_BLOBBERS, Params{ - "active": strconv.FormatBool(active), - "offset": strconv.FormatInt(int64(offset), 10), - "limit": strconv.FormatInt(int64(limit), 10), - "stakable": strconv.FormatBool(stakable), - }) - - go GetInfoFromSharders(url, OpStorageSCGetBlobbers, cb) -} - -// GetBlobber obtains blobber information. -// - blobberID: blobber id -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetBlobber(blobberID string, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - var url = withParams(STORAGESC_GET_BLOBBER, Params{ - "blobber_id": blobberID, - }) - go GetInfoFromSharders(url, OpStorageSCGetBlobber, cb) - return -} - -// GetValidator obtains validator information. -// - validatorID: validator id -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetValidator(validatorID string, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - var url = withParams(STORAGESC_GET_VALIDATOR, Params{ - "validator_id": validatorID, - }) - go GetInfoFromSharders(url, OpStorageSCGetBlobber, cb) - return -} - -// GetAuthorizer obtains authorizer information from the sharders. -// - authorizerID: authorizer id -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetAuthorizer(authorizerID string, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - var url = withParams(GET_AUTHORIZER, Params{ - "id": authorizerID, - }) - go GetInfoFromSharders(url, OpStorageSCGetBlobber, cb) - return -} - -// GetMinerSharder obtains miner sharder information from the sharders. -// - id: miner sharder id -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetMinerSharder(id string, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - var url = withParams(GET_MINERSC_NODE, Params{ - "id": id, - }) - go GetInfoFromSharders(url, OpStorageSCGetBlobber, cb) - return -} - -// GetTransactions query transactions from sharders -// # Inputs -// - toClient: receiver -// - fromClient: sender -// - block_hash: block hash -// - sort: desc or asc -// - limit: how many transactions should be fetched -// - offset: how many transactions should be skipped -// - cb: callback to get result -func GetTransactions(toClient, fromClient, block_hash, sort string, limit, offset int, cb GetInfoCallback) (err error) { - if err = CheckConfig(); err != nil { - return - } - - params := Params{} - if toClient != "" { - params["to_client_id"] = toClient - } - if fromClient != "" { - params["client_id"] = fromClient - } - if block_hash != "" { - params["block_hash"] = block_hash - } - if sort != "" { - params["sort"] = sort - } - if limit != 0 { - l := strconv.Itoa(limit) - params["limit"] = l - } - if offset != 0 { - o := strconv.Itoa(offset) - params["offset"] = o - } - - var u = withParams(STORAGESC_GET_TRANSACTIONS, params) - go GetInfoFromSharders(u, OpStorageSCGetTransactions, cb) - return -} - func Encrypt(key, text string) (string, error) { keyBytes := []byte(key) textBytes := []byte(text) From af35418560983eb789ea164c0aacf1b1c68ca466 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 11 Sep 2024 00:15:11 +0530 Subject: [PATCH 055/319] Fix --- core/block/block.go | 80 ------- core/{node => client}/cache.go | 12 +- core/client/init_node.go | 13 +- core/client/node.go | 43 ++++ core/node/node.go | 369 --------------------------------- core/node/node_test.go | 56 ----- core/transaction/entity.go | 11 +- core/transaction/get_data.go | 138 ++++++++++++ zboxcore/sdk/allocation.go | 9 +- zboxcore/sdk/sdk.go | 33 --- zcnbridge/http/rest.go | 191 ----------------- zcnbridge/rest.go | 53 +++-- zcnbridge/wallet/status.go | 172 --------------- zcncore/get_data.go | 176 ++++++++++------ zcncore/wallet.go | 9 +- zcncore/wallet_base.go | 103 ++++----- 16 files changed, 382 insertions(+), 1086 deletions(-) rename core/{node => client}/cache.go (78%) create mode 100644 core/client/node.go delete mode 100644 core/node/node.go delete mode 100644 core/node/node_test.go create mode 100644 core/transaction/get_data.go delete mode 100644 zcnbridge/http/rest.go delete mode 100644 zcnbridge/wallet/status.go diff --git a/core/block/block.go b/core/block/block.go index 8a73e780f..385d4ab46 100644 --- a/core/block/block.go +++ b/core/block/block.go @@ -5,16 +5,7 @@ package block import ( - "context" - "encoding/json" "fmt" - "github.com/0chain/gosdk/core/logger" - "github.com/0chain/gosdk/core/node" - "github.com/0chain/gosdk/core/util" - "net/http" - - "github.com/0chain/errors" - "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/transaction" @@ -102,74 +93,3 @@ type FeeStats struct { MinFees common.Balance `json:"min_fees"` MeanFees common.Balance `json:"mean_fees"` } - -func GetBlockByRound(h *node.NodeHolder, ctx context.Context, numSharders int, round int64) (b *Block, err error) { - - var result = make(chan *util.GetResponse, numSharders) - defer close(result) - - numSharders = len(h.Healthy()) // overwrite, use all - h.QueryFromShardersContext(ctx, numSharders, - fmt.Sprintf("%sround=%d&content=full,header", GET_BLOCK_INFO, round), - result) - - var ( - maxConsensus int - roundConsensus = make(map[string]int) - ) - - type respObj struct { - Block *Block `json:"block"` - Header *Header `json:"header"` - } - - for i := 0; i < numSharders; i++ { - var rsp = <-result - if rsp == nil { - logger.Log.Error("nil response") - continue - } - logger.Log.Debug(rsp.Url, rsp.Status) - - if rsp.StatusCode != http.StatusOK { - logger.Log.Error(rsp.Body) - continue - } - - var respo respObj - if err = json.Unmarshal([]byte(rsp.Body), &respo); err != nil { - logger.Log.Error("block parse error: ", err) - err = nil - continue - } - - if respo.Block == nil { - logger.Log.Debug(rsp.Url, "no block in response:", rsp.Body) - continue - } - - if respo.Header == nil { - logger.Log.Debug(rsp.Url, "no block header in response:", rsp.Body) - continue - } - - if respo.Header.Hash != string(respo.Block.Hash) { - logger.Log.Debug(rsp.Url, "header and block hash mismatch:", rsp.Body) - continue - } - - b = respo.Block - b.Header = respo.Header - - var h = encryption.FastHash([]byte(b.Hash)) - if roundConsensus[h]++; roundConsensus[h] > maxConsensus { - maxConsensus = roundConsensus[h] - } - } - - if maxConsensus == 0 { - return nil, errors.New("", "round info not found") - } - - return -} diff --git a/core/node/cache.go b/core/client/cache.go similarity index 78% rename from core/node/cache.go rename to core/client/cache.go index 3e19d424d..1ccc8aaf1 100644 --- a/core/node/cache.go +++ b/core/client/cache.go @@ -1,6 +1,7 @@ -package node +package client import ( + "github.com/0chain/gosdk/core/transaction" "sync" ) @@ -29,11 +30,12 @@ func (nc *NonceCache) GetNextNonce(clientId string) int64 { nc.guard.Lock() defer nc.guard.Unlock() if _, ok := nc.cache[clientId]; !ok { - nonce, _, err := nc.sharders.GetNonceFromSharders(clientId) - if err != nil { - nonce = 0 + bal, err := transaction.GetBalance(clientId) + if err != nil || bal == nil { + nc.cache[clientId] = 0 + } else { + nc.cache[clientId] = bal.Nonce } - nc.cache[clientId] = nonce } nc.cache[clientId] += 1 diff --git a/core/client/init_node.go b/core/client/init_node.go index e782cb6b8..b218d9e1c 100644 --- a/core/client/init_node.go +++ b/core/client/init_node.go @@ -13,7 +13,6 @@ import ( "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/logger" - "github.com/0chain/gosdk/core/node" "github.com/0chain/gosdk/core/util" "go.uber.org/zap" ) @@ -32,7 +31,7 @@ func init() { // Use client.GetNode() to get its instance after Init is called. type Node struct { stableMiners []string - sharders *node.NodeHolder + sharders *NodeHolder network *conf.Network clientCtx context.Context @@ -68,7 +67,7 @@ func (n *Node) GetMinShardersVerify() int { } // Sharders Returns NodeHolder instance -func (n *Node) Sharders() *node.NodeHolder { +func (n *Node) Sharders() *NodeHolder { n.networkGuard.RLock() defer n.networkGuard.RUnlock() return n.sharders @@ -111,8 +110,8 @@ func (n *Node) UpdateNetwork(network *conf.Network) error { return err } n.network = network - n.sharders = node.NewHolder(n.network.Sharders, util.MinInt(len(n.network.Sharders), util.MaxInt(cfg.SharderConsensous, conf.DefaultSharderConsensous))) - node.InitCache(n.sharders) + n.sharders = NewHolder(n.network.Sharders, util.MinInt(len(n.network.Sharders), util.MaxInt(cfg.SharderConsensous, conf.DefaultSharderConsensous))) + InitCache(n.sharders) return nil } @@ -134,7 +133,7 @@ func Init(ctx context.Context, cfg conf.Config) error { } reqMiners := util.MaxInt(1, int(math.Ceil(float64(cfg.MinSubmit)*float64(len(network.Miners))/100))) - sharders := node.NewHolder(network.Sharders, util.MinInt(len(network.Sharders), util.MaxInt(cfg.SharderConsensous, conf.DefaultSharderConsensous))) + sharders := NewHolder(network.Sharders, util.MinInt(len(network.Sharders), util.MaxInt(cfg.SharderConsensous, conf.DefaultSharderConsensous))) nodeClient = &Node{ stableMiners: util.GetRandom(network.Miners, reqMiners), sharders: sharders, @@ -144,7 +143,7 @@ func Init(ctx context.Context, cfg conf.Config) error { //init packages conf.InitClientConfig(&cfg) - node.InitCache(nodeClient.sharders) + InitCache(nodeClient.sharders) // update Network periodically go func() { diff --git a/core/client/node.go b/core/client/node.go new file mode 100644 index 000000000..3b76fd97b --- /dev/null +++ b/core/client/node.go @@ -0,0 +1,43 @@ +package client + +import "sync" + +func (h *NodeHolder) Healthy() (res []string) { + h.guard.Lock() + defer h.guard.Unlock() + + return h.nodes[:h.consensus] +} + +type NodeHolder struct { + consensus int + guard sync.Mutex + stats map[string]*NodeStruct + nodes []string +} + +func NewHolder(nodes []string, consensus int) *NodeHolder { + if len(nodes) < consensus { + panic("consensus is not correct") + } + holder := NodeHolder{consensus: consensus, stats: make(map[string]*NodeStruct)} + + for _, n := range nodes { + holder.nodes = append(holder.nodes, n) + holder.stats[n] = NewNode(n) + } + return &holder +} +func NewNode(id string) *NodeStruct { + return &NodeStruct{ + id: id, + weight: 1, + stats: []int{1}, + } +} + +type NodeStruct struct { + id string + weight int64 + stats []int +} diff --git a/core/node/node.go b/core/node/node.go deleted file mode 100644 index 9f5f6f35c..000000000 --- a/core/node/node.go +++ /dev/null @@ -1,369 +0,0 @@ -// Provides functions and data structures to interact with the system nodes in the context of the blockchain network. -package node - -import ( - "context" - "encoding/json" - stdErrors "errors" - "fmt" - "net/http" - "sort" - "strconv" - "strings" - "sync" - "time" - - "github.com/0chain/errors" - "github.com/0chain/gosdk/core/util" - "github.com/0chain/gosdk/zboxcore/logger" - "github.com/ethereum/go-ethereum/common/math" -) - -const statSize = 20 -const defaultTimeout = 5 * time.Second - -type NodeHolder struct { - consensus int - guard sync.Mutex - stats map[string]*Node - nodes []string -} - -type Node struct { - id string - weight int64 - stats []int -} - -func NewHolder(nodes []string, consensus int) *NodeHolder { - if len(nodes) < consensus { - panic("consensus is not correct") - } - holder := NodeHolder{consensus: consensus, stats: make(map[string]*Node)} - - for _, n := range nodes { - holder.nodes = append(holder.nodes, n) - holder.stats[n] = NewNode(n) - } - return &holder -} - -func NewNode(id string) *Node { - return &Node{ - id: id, - weight: 1, - stats: []int{1}, - } -} - -func (h *NodeHolder) Success(id string) { - h.guard.Lock() - defer h.guard.Unlock() - h.adjustNode(id, 1) -} - -func (h *NodeHolder) Fail(id string) { - h.guard.Lock() - defer h.guard.Unlock() - h.adjustNode(id, -1) -} - -func (h *NodeHolder) adjustNode(id string, res int) { - n := NewNode(id) - nodes := h.nodes - if node, ok := h.stats[id]; ok { - for i, v := range nodes { - if v == id { - nodes = append(nodes[:i], nodes[i+1:]...) - break - } - } - - sourceStats := node.stats - sourceStats = append(sourceStats, res) - if len(sourceStats) > statSize { - sourceStats = sourceStats[1:] - } - node.stats = sourceStats - - w := int64(0) - for i, s := range sourceStats { - w += int64(i+1) * int64(s) - } - node.weight = w - - n = node - } - - i := sort.Search(len(nodes), func(i int) bool { - return h.stats[nodes[i]].weight < n.weight - }) - h.nodes = append(nodes[:i], append([]string{n.id}, nodes[i:]...)...) -} - -func (h *NodeHolder) Healthy() (res []string) { - h.guard.Lock() - defer h.guard.Unlock() - - return h.nodes[:h.consensus] -} - -func (h *NodeHolder) All() (res []string) { - h.guard.Lock() - defer h.guard.Unlock() - - return h.nodes -} - -const consensusThresh = 25 -const ( - GET_BALANCE = `/v1/client/get/balance?client_id=` - CURRENT_ROUND = "/v1/current-round" - GET_HARDFORK_ROUND = `/v1/screst/6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d9/hardfork?name=` -) - -func (h *NodeHolder) GetNonceFromSharders(clientID string) (int64, string, error) { - return h.GetBalanceFieldFromSharders(clientID, "nonce") -} - -func (h *NodeHolder) GetBalanceFieldFromSharders(clientID, name string) (int64, string, error) { - result := make(chan *util.GetResponse) - defer close(result) - // getMinShardersVerify - numSharders := len(h.Healthy()) - h.QueryFromSharders(numSharders, fmt.Sprintf("%v%v", GET_BALANCE, clientID), result) - - consensusMaps := util.NewHttpConsensusMaps(consensusThresh) - - for i := 0; i < numSharders; i++ { - rsp := <-result - if rsp == nil { - logger.Logger.Error("nil response") - continue - } - - logger.Logger.Debug(rsp.Url, rsp.Status) - if rsp.StatusCode != http.StatusOK { - logger.Logger.Error(rsp.Body) - - } else { - logger.Logger.Debug(rsp.Body) - } - - if err := consensusMaps.Add(rsp.StatusCode, rsp.Body); err != nil { - logger.Logger.Error(rsp.Body) - } - } - - rate := consensusMaps.MaxConsensus * 100 / numSharders - if rate < consensusThresh { - if strings.TrimSpace(consensusMaps.WinError) == `{"error":"value not present"}` { - return 0, consensusMaps.WinError, nil - } - return 0, consensusMaps.WinError, errors.New("", "get balance failed. consensus not reached") - } - - winValue, ok := consensusMaps.GetValue(name) - if ok { - winBalance, err := strconv.ParseInt(string(winValue), 10, 64) - if err != nil { - return 0, "", fmt.Errorf("get balance failed. %w", err) - } - - return winBalance, consensusMaps.WinInfo, nil - } - - return 0, consensusMaps.WinInfo, errors.New("", "get balance failed. balance field is missed") -} - -func (h *NodeHolder) QueryFromSharders(numSharders int, query string, - result chan *util.GetResponse) { - - h.QueryFromShardersContext(context.Background(), numSharders, query, result) -} - -func (h *NodeHolder) QueryFromShardersContext(ctx context.Context, numSharders int, - query string, result chan *util.GetResponse) { - - sharders := h.Healthy() - - for _, sharder := range util.Shuffle(sharders)[:numSharders] { - go func(sharderurl string) { - logger.Logger.Info("Query from ", sharderurl+query) - url := fmt.Sprintf("%v%v", sharderurl, query) - timeout, cancelFunc := context.WithTimeout(ctx, defaultTimeout) - defer cancelFunc() - - req, err := util.NewHTTPGetRequestContext(timeout, url) - if err != nil { - logger.Logger.Error(sharderurl, " new get request failed. ", err.Error()) - h.Fail(sharderurl) - result <- nil - return - } - res, err := req.Get() - if err != nil { - logger.Logger.Error(sharderurl, " get error. ", err.Error()) - } - - if res.StatusCode > http.StatusBadRequest { - h.Fail(sharderurl) - } else { - h.Success(sharderurl) - } - - result <- res - }(sharder) - } -} - -func (h *NodeHolder) GetRoundFromSharders() (int64, error) { - - sharders := h.Healthy() - if len(sharders) == 0 { - return 0, stdErrors.New("get round failed. no sharders") - } - - result := make(chan *util.GetResponse, len(sharders)) - - var numSharders = len(sharders) - // use 5 sharders to get round - if numSharders > 5 { - numSharders = 5 - } - - h.QueryFromSharders(numSharders, fmt.Sprintf("%v", CURRENT_ROUND), result) - - const consensusThresh = float32(25.0) - - var rounds []int64 - - consensus := int64(0) - roundMap := make(map[int64]int64) - - round := int64(0) - - waitTimeC := time.After(10 * time.Second) - for i := 0; i < numSharders; i++ { - select { - case <-waitTimeC: - return 0, stdErrors.New("get round failed. consensus not reached") - case rsp := <-result: - if rsp == nil { - logger.Logger.Error("nil response") - continue - } - if rsp.StatusCode != http.StatusOK { - continue - } - - var respRound int64 - err := json.Unmarshal([]byte(rsp.Body), &respRound) - - if err != nil { - continue - } - - rounds = append(rounds, respRound) - - sort.Slice(rounds, func(i, j int) bool { - return false - }) - - medianRound := rounds[len(rounds)/2] - - roundMap[medianRound]++ - - if roundMap[medianRound] > consensus { - - consensus = roundMap[medianRound] - round = medianRound - rate := consensus * 100 / int64(numSharders) - - if rate >= int64(consensusThresh) { - return round, nil - } - } - } - } - - return round, nil -} - -func (h *NodeHolder) GetHardForkRound(hardFork string) (int64, error) { - sharders := h.Healthy() - if len(sharders) == 0 { - return 0, stdErrors.New("get round failed. no sharders") - } - - result := make(chan *util.GetResponse, len(sharders)) - - var numSharders = len(sharders) - // use 5 sharders to get round - if numSharders > 5 { - numSharders = 5 - } - - h.QueryFromSharders(numSharders, fmt.Sprintf("%s%s", GET_HARDFORK_ROUND, hardFork), result) - - const consensusThresh = float32(25.0) - - var rounds []int64 - - consensus := int64(0) - roundMap := make(map[int64]int64) - // If error then set it to max int64 - round := int64(math.MaxInt64) - - waitTimeC := time.After(10 * time.Second) - for i := 0; i < numSharders; i++ { - select { - case <-waitTimeC: - return 0, stdErrors.New("get round failed. consensus not reached") - case rsp := <-result: - if rsp == nil { - logger.Logger.Error("nil response") - continue - } - if rsp.StatusCode != http.StatusOK { - continue - } - - var respRound int64 - var objmap map[string]string - err := json.Unmarshal([]byte(rsp.Body), &objmap) - if err != nil { - continue - } - - str := string(objmap["round"]) - respRound, err = strconv.ParseInt(str, 10, 64) - if err != nil { - continue - } - - rounds = append(rounds, respRound) - - sort.Slice(rounds, func(i, j int) bool { - return false - }) - - medianRound := rounds[len(rounds)/2] - - roundMap[medianRound]++ - - if roundMap[medianRound] > consensus { - - consensus = roundMap[medianRound] - round = medianRound - rate := consensus * 100 / int64(numSharders) - - if rate >= int64(consensusThresh) { - return round, nil - } - } - } - } - - return round, nil -} diff --git a/core/node/node_test.go b/core/node/node_test.go deleted file mode 100644 index e880b5bf9..000000000 --- a/core/node/node_test.go +++ /dev/null @@ -1,56 +0,0 @@ -package node - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestNodeHolder_Success(t *testing.T) { - type fields struct { - nodes []string - consensus int - } - type args struct { - id string - } - type res struct { - res []string - } - tests := []struct { - name string - fields fields - args args - res res - }{ - {name: "init", fields: struct { - nodes []string - consensus int - }{nodes: []string{"1", "2", "3", "4", "5"}, consensus: 5}, args: struct{ id string }{id: "1"}, - res: struct{ res []string }{res: []string{"1", "2", "3", "4", "5"}}}, - {name: "pull up", fields: struct { - nodes []string - consensus int - }{nodes: []string{"1", "2", "3", "4", "5"}, consensus: 5}, args: struct{ id string }{id: "5"}, - res: struct{ res []string }{res: []string{"5", "1", "2", "3", "4"}}}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - h := NewHolder(tt.fields.nodes, tt.fields.consensus) - h.Success(tt.args.id) - - assert.Equal(t, tt.res.res, h.Healthy()) - }) - } -} - -//func TestNodeHolder_GetHardForkRound(t *testing.T) { -// holder := NewHolder([]string{"https://dev2.zus.network/sharder01", -// "https://dev3.zus.network/sharder01", "https://dev1.zus.network/sharder01"}, 2) -// round, err := holder.GetHardForkRound("apollo") -// if err != nil { -// t.Error(err) -// } -// -// assert.Equal(t, 206000, round) -//} diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 49b186818..294d3e833 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -7,7 +7,6 @@ import ( "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/logger" - "github.com/0chain/gosdk/core/node" "github.com/0chain/gosdk/core/sys" "go.uber.org/zap" "net/http" @@ -24,6 +23,10 @@ import ( var Logger logger.Logger +const STORAGE_SCADDRESS = "6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d7" +const MINERSC_SCADDRESS = "6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d9" +const ZCNSC_SCADDRESS = "6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712e0" + const TXN_SUBMIT_URL = "v1/transaction/put" const TXN_VERIFY_URL = "v1/transaction/get/confirmation?hash=" const BLOCK_BY_ROUND_URL = "v1/screst/6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d7/block?round=" @@ -544,7 +547,7 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, } if txn.TransactionNonce == 0 { - txn.TransactionNonce = node.Cache.GetNextNonce(txn.ClientID) + txn.TransactionNonce = client.Cache.GetNextNonce(txn.ClientID) } if err = txn.ComputeHashAndSign(client.Sign); err != nil { @@ -558,7 +561,7 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, err = SendTransactionSync(txn, nodeClient.GetStableMiners()) if err != nil { Logger.Info("transaction submission failed", zap.Error(err)) - node.Cache.Evict(txn.ClientID) + client.Cache.Evict(txn.ClientID) nodeClient.ResetStableMiners() return } @@ -581,7 +584,7 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, if err != nil { Logger.Error("Error verifying the transaction", err.Error(), txn.Hash) - node.Cache.Evict(txn.ClientID) + client.Cache.Evict(txn.ClientID) return } diff --git a/core/transaction/get_data.go b/core/transaction/get_data.go new file mode 100644 index 000000000..8bc03058c --- /dev/null +++ b/core/transaction/get_data.go @@ -0,0 +1,138 @@ +package transaction + +import ( + "encoding/json" + "github.com/0chain/errors" + coreHttp "github.com/0chain/gosdk/core/http" +) + +const ( + StorageSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d7` + FaucetSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d3` + MinerSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d9` + ZCNSCSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712e0` +) +const ( + GET_CLIENT = `/v1/client/get` + PUT_TRANSACTION = `/v1/transaction/put` + GET_BLOCK_INFO = `/v1/block/get?` + GET_MAGIC_BLOCK_INFO = `/v1/block/magic/get?` + GET_LATEST_FINALIZED = `/v1/block/get/latest_finalized` + GET_LATEST_FINALIZED_MAGIC_BLOCK = `/v1/block/get/latest_finalized_magic_block` + GET_FEE_STATS = `/v1/block/get/fee_stats` + GET_CHAIN_STATS = `/v1/chain/get/stats` + + // zcn sc + ZCNSC_PFX = `/v1/screst/` + ZCNSCSmartContractAddress + GET_MINT_NONCE = ZCNSC_PFX + `/v1/mint_nonce` + GET_NOT_PROCESSED_BURN_TICKETS = ZCNSC_PFX + `/v1/not_processed_burn_tickets` + GET_AUTHORIZER = ZCNSC_PFX + `/getAuthorizer` + + // miner SC + + MINERSC_PFX = `/v1/screst/` + MinerSmartContractAddress + GET_MINERSC_NODE = MINERSC_PFX + "/nodeStat" + GET_MINERSC_POOL = MINERSC_PFX + "/nodePoolStat" + GET_MINERSC_CONFIG = MINERSC_PFX + "/configs" + GET_MINERSC_GLOBALS = MINERSC_PFX + "/globalSettings" + GET_MINERSC_USER = MINERSC_PFX + "/getUserPools" + GET_MINERSC_MINERS = MINERSC_PFX + "/getMinerList" + GET_MINERSC_SHARDERS = MINERSC_PFX + "/getSharderList" + GET_MINERSC_EVENTS = MINERSC_PFX + "/getEvents" + + // storage SC + + STORAGESC_PFX = "/v1/screst/" + StorageSmartContractAddress + + STORAGESC_GET_SC_CONFIG = STORAGESC_PFX + "/storage-config" + STORAGESC_GET_CHALLENGE_POOL_INFO = STORAGESC_PFX + "/getChallengePoolStat" + STORAGESC_GET_ALLOCATION = STORAGESC_PFX + "/allocation" + STORAGESC_GET_ALLOCATIONS = STORAGESC_PFX + "/allocations" + STORAGESC_GET_READ_POOL_INFO = STORAGESC_PFX + "/getReadPoolStat" + STORAGESC_GET_STAKE_POOL_INFO = STORAGESC_PFX + "/getStakePoolStat" + STORAGESC_GET_STAKE_POOL_USER_INFO = STORAGESC_PFX + "/getUserStakePoolStat" + STORAGESC_GET_USER_LOCKED_TOTAL = STORAGESC_PFX + "/getUserLockedTotal" + STORAGESC_GET_BLOBBERS = STORAGESC_PFX + "/getblobbers" + STORAGESC_GET_BLOBBER = STORAGESC_PFX + "/getBlobber" + STORAGESC_GET_VALIDATOR = STORAGESC_PFX + "/get_validator" + STORAGESC_GET_TRANSACTIONS = STORAGESC_PFX + "/transactions" + + STORAGE_GET_SNAPSHOT = STORAGESC_PFX + "/replicate-snapshots" + STORAGE_GET_BLOBBER_SNAPSHOT = STORAGESC_PFX + "/replicate-blobber-aggregates" + STORAGE_GET_MINER_SNAPSHOT = STORAGESC_PFX + "/replicate-miner-aggregates" + STORAGE_GET_SHARDER_SNAPSHOT = STORAGESC_PFX + "/replicate-sharder-aggregates" + STORAGE_GET_AUTHORIZER_SNAPSHOT = STORAGESC_PFX + "/replicate-authorizer-aggregates" + STORAGE_GET_VALIDATOR_SNAPSHOT = STORAGESC_PFX + "/replicate-validator-aggregates" + STORAGE_GET_USER_SNAPSHOT = STORAGESC_PFX + "/replicate-user-aggregates" + + GET_BALANCE = "/client/get/balance" +) + +// +// storage SC configurations and blobbers +// + +type InputMap struct { + Fields map[string]string `json:"fields"` +} + +// GetStorageSCConfig retrieves storage SC configurations. +func GetConfig(configType string) (conf *InputMap, err error) { + var ( + scAddress string + relativePath string + b []byte + ) + + if configType == "storage_sc_config" { + scAddress = StorageSmartContractAddress + relativePath = STORAGESC_GET_SC_CONFIG + } else if configType == "miner_sc_globals" { + scAddress = MinerSmartContractAddress + relativePath = GET_MINERSC_GLOBALS + } + + b, err = coreHttp.MakeSCRestAPICall(scAddress, relativePath, nil, + nil) + if err != nil { + return nil, errors.Wrap(err, "error requesting storage SC configs:") + } + if len(b) == 0 { + return nil, errors.New("", "empty response") + } + + conf = new(InputMap) + conf.Fields = make(map[string]string) + if err = json.Unmarshal(b, conf); err != nil { + return nil, errors.Wrap(err, "error decoding response:") + } + + return +} + +func GetBalance(clientId string) (*GetBalanceResponse, error) { + var ( + balance GetBalanceResponse + err error + res []byte + ) + + if res, err = coreHttp.MakeSCRestAPICall("", GET_BALANCE, map[string]string{ + "client_id": clientId, + }, nil); err != nil { + return nil, err + } + + if err = json.Unmarshal(res, &balance); err != nil { + return nil, err + } + + return &balance, nil +} + +type GetBalanceResponse struct { + Txn string `json:"txn"` + Round int64 `json:"round"` + Balance int64 `json:"balance"` + Nonce int64 `json:"nonce"` +} diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index bbbb13277..20de41bc9 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -6,6 +6,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + "github.com/0chain/gosdk/core/transaction" "io" "io/ioutil" "math" @@ -355,16 +356,12 @@ func (a *Allocation) SetCheckStatus(checkStatus bool) { } func getPriceRange(name string) (PriceRange, error) { - conf, err := GetStorageSCConfig() + conf, err := transaction.GetConfig("storage_sc_config") if err != nil { return PriceRange{}, err } f := conf.Fields[name] - fStr, ok := f.(string) - if !ok { - return PriceRange{}, fmt.Errorf("type is wrong") - } - mrp, err := strconv.ParseFloat(fStr, 64) + mrp, err := strconv.ParseFloat(f, 64) if err != nil { return PriceRange{}, err } diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index f818be24d..8720ddc24 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -582,39 +582,6 @@ func GetMptData(key string) ([]byte, error) { return b, nil } -// -// storage SC configurations and blobbers -// - -type InputMap struct { - Fields map[string]interface{} `json:"fields"` -} - -// GetStorageSCConfig retrieves storage SC configurations. -func GetStorageSCConfig() (conf *InputMap, err error) { - if !sdkInitialized { - return nil, sdkNotInitialized - } - - var b []byte - b, err = coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/storage-config", nil, - nil) - if err != nil { - return nil, errors.Wrap(err, "error requesting storage SC configs:") - } - if len(b) == 0 { - return nil, errors.New("", "empty response") - } - - conf = new(InputMap) - conf.Fields = make(map[string]interface{}) - if err = json.Unmarshal(b, conf); err != nil { - return nil, errors.Wrap(err, "rror decoding response:") - } - - return -} - // Blobber type represents blobber information. type Blobber struct { // ID of the blobber diff --git a/zcnbridge/http/rest.go b/zcnbridge/http/rest.go deleted file mode 100644 index 31e2b593f..000000000 --- a/zcnbridge/http/rest.go +++ /dev/null @@ -1,191 +0,0 @@ -package http - -import ( - "crypto/sha1" - "encoding/hex" - "fmt" - "io" - "io/ioutil" - "net/http" - "net/url" - "sync" - - "go.uber.org/zap" - "gopkg.in/natefinch/lumberjack.v2" - - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/logger" - "github.com/0chain/gosdk/core/util" - "github.com/0chain/gosdk/zcnbridge/errors" - "github.com/0chain/gosdk/zcncore" -) - -const ( - // SCRestAPIPrefix represents base URL path to execute smart contract rest points. - SCRestAPIPrefix = "v1/screst/" - RestPrefix = SCRestAPIPrefix + zcncore.ZCNSCSmartContractAddress - PathGetAuthorizerNodes = "/getAuthorizerNodes?active=%t" - PathGetGlobalConfig = "/getGlobalConfig" - PathGetAuthorizer = "/getAuthorizer" -) - -type Params map[string]string - -var Logger logger.Logger -var defaultLogLevel = logger.DEBUG -var logVerbose = true - -func init() { - Logger.Init(defaultLogLevel, "zcnbridge-http-sdk") - - Logger.SetLevel(logger.DEBUG) - ioWriter := &lumberjack.Logger{ - Filename: "bridge.log", - MaxSize: 100, // MB - MaxBackups: 5, // number of backups - MaxAge: 28, //days - LocalTime: false, - Compress: false, // disabled by default - } - Logger.SetLogFile(ioWriter, true) -} - -func SetLogFile(logFile string, verbose bool) { - Logger.Init(defaultLogLevel, "zcnbridge-sdk") - Logger.SetLevel(logger.DEBUG) - - ioWriter := &lumberjack.Logger{ - Filename: logFile, - MaxSize: 100, // MB - MaxBackups: 5, // number of backups - MaxAge: 28, //days - LocalTime: false, - Compress: false, // disabled by default - } - logVerbose = verbose - Logger.SetLogFile(ioWriter, logVerbose) -} - -// MakeSCRestAPICall calls smart contract with provided address -// and makes retryable request to smart contract resource with provided relative path using params. -func MakeSCRestAPICall(opCode int, relativePath string, params Params, cb zcncore.GetInfoCallback) { - var ( - resMaxCounterBody []byte - hashMaxCounter int - msg string - hashCounters = make(map[string]int) - sharders = extractSharders() - ) - - type queryResult struct { - hash string - body []byte - } - - results := make(chan *queryResult, len(sharders)) - defer close(results) - - var client = NewRetryableClient(logVerbose) - - wg := &sync.WaitGroup{} - for _, sharder := range sharders { - wg.Add(1) - go func(sharderUrl string) { - defer wg.Done() - - var u = makeURL(params, sharderUrl, relativePath) - Logger.Info("Query ", u.String()) - resp, err := client.Get(u.String()) - if err != nil { - Logger.Error("MakeSCRestAPICall - failed to get response from", zap.String("URL", sharderUrl), zap.Any("error", err)) - return - } - if resp.StatusCode != http.StatusInternalServerError { - //goland:noinspection ALL - defer resp.Body.Close() - } - - if err != nil { - Logger.Error("MakeSCRestAPICall - failed to get response from", zap.String("URL", sharderUrl), zap.Any("error", err)) - return - } - - if resp.StatusCode != http.StatusOK { - Logger.Error("MakeSCRestAPICall - error getting response from", zap.String("URL", sharderUrl), zap.Any("error", err)) - return - } - - Logger.Info("MakeSCRestAPICall successful query") - - hash, body, err := hashAndBytesOfReader(resp.Body) - if err != nil { - Logger.Error("MakeSCRestAPICall - error while reading response body", zap.String("URL", sharderUrl), zap.Any("error", err)) - return - } - - Logger.Info("MakeSCRestAPICall push body to results: ", string(body)) - - results <- &queryResult{hash: hash, body: body} - }(sharder) - } - - Logger.Info("MakeSCRestAPICall waiting for response from all sharders") - wg.Wait() - Logger.Info("MakeSCRestAPICall closing results") - - select { - case result := <-results: - Logger.Debug("request_sharders", zap.String("received result", result.hash), zap.String("received body", string(result.body))) - hashCounters[result.hash]++ - if hashCounters[result.hash] > hashMaxCounter { - hashMaxCounter = hashCounters[result.hash] - resMaxCounterBody = result.body - } - default: - } - - if hashMaxCounter == 0 { - err := errors.New("request_sharders", "no valid responses, last err: "+msg) - cb.OnInfoAvailable(opCode, zcncore.StatusError, "", err.Error()) - Logger.Error(err) - return - } - - cb.OnInfoAvailable(opCode, zcncore.StatusSuccess, string(resMaxCounterBody), "") -} - -// hashAndBytesOfReader computes hash of readers data and returns hash encoded to hex and bytes of reader data. -// If error occurs while reading data from reader, it returns non nil error. -func hashAndBytesOfReader(r io.Reader) (string, []byte, error) { - h := sha1.New() - teeReader := io.TeeReader(r, h) - readerBytes, err := ioutil.ReadAll(teeReader) - if err != nil { - return "", nil, err - } - - return hex.EncodeToString(h.Sum(nil)), readerBytes, nil -} - -// extractSharders returns string slice of randomly ordered sharders existing in the current network. -func extractSharders() []string { - nodeClient, err := client.GetNode() - if err != nil { - panic(err) - } - sharders := nodeClient.Sharders().Healthy() - return util.GetRandom(sharders, len(sharders)) -} - -// makeURL creates url.URL to make smart contract request to sharder. -func makeURL(params Params, baseURL, relativePath string) *url.URL { - uString := fmt.Sprintf("%v/%v%v", baseURL, RestPrefix, relativePath) - u, _ := url.Parse(uString) - q := u.Query() - for k, v := range params { - q.Add(k, v) - } - u.RawQuery = q.Encode() - - return u -} diff --git a/zcnbridge/rest.go b/zcnbridge/rest.go index 866d9beb5..5d6d261ab 100644 --- a/zcnbridge/rest.go +++ b/zcnbridge/rest.go @@ -1,16 +1,23 @@ package zcnbridge import ( + "encoding/json" "fmt" + coreHttp "github.com/0chain/gosdk/core/http" "github.com/0chain/gosdk/core/common" - "github.com/0chain/gosdk/zcnbridge/http" - "github.com/0chain/gosdk/zcnbridge/wallet" "github.com/0chain/gosdk/zcncore" ) -// Models +const ( + // SCRestAPIPrefix represents base URL path to execute smart contract rest points. + SCRestAPIPrefix = "v1/screst/" + RestPrefix = SCRestAPIPrefix + zcncore.ZCNSCSmartContractAddress + PathGetAuthorizerNodes = "/getAuthorizerNodes?active=%t" + PathGetGlobalConfig = "/getGlobalConfig" + PathGetAuthorizer = "/getAuthorizer" +) // AuthorizerResponse represents the response of the request to get authorizer info from the sharders. type AuthorizerResponse struct { @@ -52,17 +59,16 @@ type AuthorizerNode struct { func getAuthorizers(active bool) ([]*AuthorizerNode, error) { var ( authorizers = new(AuthorizerNodesResponse) - cb = wallet.NewZCNStatus(authorizers) err error + res []byte ) - cb.Begin() - - if err = GetAuthorizers(active, cb); err != nil { + if res, err = GetAuthorizers(active); err != nil { return nil, err } - if err = cb.Wait(); err != nil { + err = json.Unmarshal(res, authorizers) + if err != nil { return nil, err } @@ -77,43 +83,34 @@ func getAuthorizers(active bool) ([]*AuthorizerNode, error) { // GetAuthorizer returned authorizer information from Züs Blockchain by the ID // - id is the authorizer ID // - cb is the callback function to handle the response asynchronously -func GetAuthorizer(id string, cb zcncore.GetInfoCallback) (err error) { +func GetAuthorizer(id string) (res []byte, err error) { err = zcncore.CheckConfig() if err != nil { - return err + return nil, err } - go http.MakeSCRestAPICall( - zcncore.OpZCNSCGetAuthorizer, - http.PathGetAuthorizer, - http.Params{ - "id": id, - }, - cb, - ) - - return + return coreHttp.MakeSCRestAPICall(zcncore.ZCNSCSmartContractAddress, PathGetAuthorizer, zcncore.Params{ + "id": id, + }, nil) } // GetAuthorizers Returns all or only active authorizers // - active is the flag to get only active authorizers // - cb is the callback function to handle the response asynchronously -func GetAuthorizers(active bool, cb zcncore.GetInfoCallback) (err error) { +func GetAuthorizers(active bool) (res []byte, err error) { err = zcncore.CheckConfig() if err != nil { - return err + return nil, err } - go http.MakeSCRestAPICall(zcncore.OpZCNSCGetAuthorizerNodes, fmt.Sprintf(http.PathGetAuthorizerNodes, active), nil, cb) - return + return coreHttp.MakeSCRestAPICall(zcncore.ZCNSCSmartContractAddress, fmt.Sprintf(PathGetAuthorizerNodes, active), nil, nil) } // GetGlobalConfig Returns global config // - cb is the callback function to handle the response asynchronously -func GetGlobalConfig(cb zcncore.GetInfoCallback) (err error) { +func GetGlobalConfig() (res []byte, err error) { err = zcncore.CheckConfig() if err != nil { - return err + return nil, err } - go http.MakeSCRestAPICall(zcncore.OpZCNSCGetGlobalConfig, http.PathGetGlobalConfig, nil, cb) - return + return coreHttp.MakeSCRestAPICall(zcncore.ZCNSCSmartContractAddress, PathGetGlobalConfig, nil, nil) } diff --git a/zcnbridge/wallet/status.go b/zcnbridge/wallet/status.go deleted file mode 100644 index 6df404714..000000000 --- a/zcnbridge/wallet/status.go +++ /dev/null @@ -1,172 +0,0 @@ -package wallet - -import ( - "encoding/json" - "errors" - "fmt" - "net/http" - "os" - "sync" - - "github.com/0chain/gosdk/zcncore" -) - -// ZCNStatus represents the status of a ZCN operation. -type ZCNStatus struct { - walletString string - balance int64 - value interface{} - Wg *sync.WaitGroup - Success bool - Err error -} - -// NewZCNStatus creates a new ZCNStatus instance. -// - value: value to be stored in the ZCNStatus instance -func NewZCNStatus(value interface{}) (zcns *ZCNStatus) { - return &ZCNStatus{ - Wg: new(sync.WaitGroup), - value: value, - } -} - -// Begin starts the wait group -func (zcn *ZCNStatus) Begin() { - zcn.Wg.Add(1) -} - -// Wait waits for the wait group to finish -func (zcn *ZCNStatus) Wait() error { - zcn.Wg.Wait() - return zcn.Err -} - -// OnBalanceAvailable callback when balance is available -// - status: status of the operation -// - value: balance value -// - third parameter is not used, it is kept for compatibility -func (zcn *ZCNStatus) OnBalanceAvailable(status int, value int64, _ string) { - defer zcn.Wg.Done() - if status == zcncore.StatusSuccess { - zcn.Success = true - } else { - zcn.Success = false - } - zcn.balance = value -} - -// OnTransactionComplete callback when a transaction is completed -// - t: transaction object -// - status: status of the transaction -func (zcn *ZCNStatus) OnTransactionComplete(t *zcncore.Transaction, status int) { - defer zcn.Wg.Done() - if status == zcncore.StatusSuccess { - zcn.Success = true - } else { - zcn.Err = errors.New(t.GetTransactionError()) - } -} - -// OnVerifyComplete callback when a transaction is verified -// - t: transaction object -// - status: status of the transaction -func (zcn *ZCNStatus) OnVerifyComplete(t *zcncore.Transaction, status int) { - defer zcn.Wg.Done() - if status == zcncore.StatusSuccess { - zcn.Success = true - } else { - zcn.Err = errors.New(t.GetVerifyError()) - } -} - -// OnTransferComplete callback when a transfer is completed. Not used in this implementation -func (zcn *ZCNStatus) OnAuthComplete(_ *zcncore.Transaction, status int) { - Logger.Info("Authorization complete with status: ", status) -} - -// OnWalletCreateComplete callback when a wallet is created -// - status: status of the operation -// - wallet: wallet json string -func (zcn *ZCNStatus) OnWalletCreateComplete(status int, wallet string, err string) { - defer zcn.Wg.Done() - if status != zcncore.StatusSuccess { - zcn.Success = false - zcn.Err = errors.New(err) - zcn.walletString = "" - return - } - zcn.Success = true - zcn.Err = nil - zcn.walletString = wallet -} - -// OnInfoAvailable callback when information is available -// - op`: operation type (check `zcncore.Op* constants) -// - status: status of the operation -// - info: information represneted as a string -// - err: error message -func (zcn *ZCNStatus) OnInfoAvailable(op int, status int, info string, err string) { - defer zcn.Wg.Done() - - // If status is 400 for OpGetMintNonce, mintNonce is considered as 0 - if op == zcncore.OpGetMintNonce && status == http.StatusBadRequest { - zcn.Err = nil - zcn.Success = true - return - } - - if status != zcncore.StatusSuccess { - zcn.Err = errors.New(err) - zcn.Success = false - return - } - - if info == "" || info == "{}" { - zcn.Err = errors.New("empty response") - zcn.Success = false - return - } - - var errm error - if errm = json.Unmarshal([]byte(info), zcn.value); errm != nil { - zcn.Err = fmt.Errorf("decoding response: %v", errm) - zcn.Success = false - return - } - - zcn.Err = nil - zcn.Success = true -} - -// OnSetupComplete callback when setup is completed. -// Paramters are not used in this implementation, -// just kept for compatibility. -func (zcn *ZCNStatus) OnSetupComplete(_ int, _ string) { - defer zcn.Wg.Done() -} - -// OnAuthorizeSendComplete callback when authorization is completed -// - status: status of the operation -// - 2nd parameter is not used, it is kept for compatibility -// - 3rd parameter is not used, it is kept for compatibility -// - 4th parameter is not used, it is kept for compatibility -// - creationDate: timestamp of the creation date -// - signature: signature of the operation -func (zcn *ZCNStatus) OnAuthorizeSendComplete(status int, _ string, _ int64, _ string, creationDate int64, signature string) { - defer zcn.Wg.Done() - - Logger.Info("Status: ", status) - Logger.Info("Timestamp:", creationDate) - Logger.Info("Signature:", signature) -} - -//goland:noinspection ALL -func PrintError(v ...interface{}) { - fmt.Fprintln(os.Stderr, v...) -} - -//goland:noinspection ALL -func ExitWithError(v ...interface{}) { - fmt.Fprintln(os.Stderr, v...) - os.Exit(1) -} diff --git a/zcncore/get_data.go b/zcncore/get_data.go index a89548f99..56be171fb 100644 --- a/zcncore/get_data.go +++ b/zcncore/get_data.go @@ -5,13 +5,14 @@ import ( "encoding/json" "errors" "fmt" + "github.com/0chain/gosdk/core/block" "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/common" + coreHttp "github.com/0chain/gosdk/core/http" "github.com/0chain/gosdk/core/tokenrate" "github.com/0chain/gosdk/core/util" "github.com/0chain/gosdk/core/zcncrypto" "net/url" - "strings" + "strconv" ) type GetClientResponse struct { @@ -95,57 +96,6 @@ func SetAuthUrl(url string) error { return client.SetAuthUrl(url) } -func getWalletBalance(clientId string) (common.Balance, int64, error) { - err := checkSdkInit() - if err != nil { - return 0, 0, err - } - - cb := &walletCallback{} - cb.Add(1) - - go func() { - value, info, err := getBalanceFromSharders(clientId) - if err != nil && strings.TrimSpace(info) != `{"error":"value not present"}` { - cb.OnBalanceAvailable(StatusError, value, info) - cb.err = err - return - } - cb.OnBalanceAvailable(StatusSuccess, value, info) - }() - - cb.Wait() - - var clientState struct { - Nonce int64 `json:"nonce"` - } - err = json.Unmarshal([]byte(cb.info), &clientState) - if err != nil { - return 0, 0, err - } - - return cb.balance, clientState.Nonce, cb.err -} - -// GetBalance retrieve wallet balance from sharders -// - cb: info callback instance, carries the response of the GET request to the sharders -func GetBalance(cb GetBalanceCallback) error { - err := CheckConfig() - if err != nil { - return err - } - go func() { - value, info, err := getBalanceFromSharders(client.Wallet().ClientID) - if err != nil { - logging.Error(err) - cb.OnBalanceAvailable(StatusError, 0, info) - return - } - cb.OnBalanceAvailable(StatusSuccess, value, info) - }() - return nil -} - //// GetMintNonce retrieve the client's latest mint nonce from sharders //// - cb: info callback instance, carries the response of the GET request to the sharders //func GetMintNonce(cb GetInfoCallback) error { @@ -178,14 +128,6 @@ func GetBalance(cb GetBalanceCallback) error { // return nil //} -func getBalanceFromSharders(clientID string) (int64, string, error) { - clientNode, err := client.GetNode() - if err != nil { - return 0, "", err - } - return clientNode.Sharders().GetBalanceFieldFromSharders(clientID, "balance") -} - // ConvertTokenToUSD converts the ZCN tokens to USD amount // - token: ZCN tokens amount func ConvertTokenToUSD(token float64) (float64, error) { @@ -258,3 +200,115 @@ func withParams(uri string, params Params) string { // "offset": strconv.FormatInt(offset, 10), // }, nil) //} + +// GetMinerSCNodeInfo get miner information from sharders +// - id: the id of miner +// - cb: info callback instance, carries the response of the GET request to the sharders +func GetMinerSCNodeInfo(id string) ([]byte, error) { + if err := CheckConfig(); err != nil { + return nil, err + } + + return coreHttp.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_NODE, Params{ + "id": id, + }, nil) +} + +// GetMintNonce retrieve the client's latest mint nonce from sharders +// - cb: info callback instance, carries the response of the GET request to the sharders +func GetMintNonce() ([]byte, error) { + err := CheckConfig() + if err != nil { + return nil, err + } + + return coreHttp.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINT_NONCE, Params{ + "client_id": client.ClientID(), + }, nil) +} + +func GetMiners(active, stakable bool, limit, offset int) ([]byte, error) { + if err := CheckConfig(); err != nil { + return nil, err + } + + return coreHttp.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_MINERS, Params{ + "active": strconv.FormatBool(active), + "stakable": strconv.FormatBool(stakable), + "offset": strconv.FormatInt(int64(offset), 10), + "limit": strconv.FormatInt(int64(limit), 10), + }, nil) +} + +func GetSharders(active, stakable bool, limit, offset int) ([]byte, error) { + if err := CheckConfig(); err != nil { + return nil, err + } + + return coreHttp.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_SHARDERS, Params{ + "active": strconv.FormatBool(active), + "stakable": strconv.FormatBool(stakable), + "offset": strconv.FormatInt(int64(offset), 10), + "limit": strconv.FormatInt(int64(limit), 10), + }, nil) +} + +// GetLatestFinalizedMagicBlock gets latest finalized magic block +// - numSharders: number of sharders +// - timeout: request timeout +func GetLatestFinalizedMagicBlock(ctx context.Context, numSharders int) (m *block.MagicBlock, err error) { + res, err := coreHttp.MakeSCRestAPICall(MinerSmartContractAddress, GET_LATEST_FINALIZED_MAGIC_BLOCK, nil, nil) + if err != nil { + return nil, err + } + + err = json.Unmarshal(res, &m) + if err != nil { + return nil, err + } + + return m, nil +} + +// GetMinerSCUserInfo retrieve user stake pools for the providers related to the Miner SC (miners/sharders). +// - clientID: user's wallet id +func GetMinerSCUserInfo(clientID string) ([]byte, error) { + if err := CheckConfig(); err != nil { + return nil, err + } + if clientID == "" { + clientID = client.ClientID() + } + + return coreHttp.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_USER, Params{ + "client_id": clientID, + }, nil) +} + +// GetMinerSCNodePool get miner smart contract node pool +// - id: the id of miner +func GetMinerSCNodePool(id string) ([]byte, error) { + if err := CheckConfig(); err != nil { + return nil, err + } + + return coreHttp.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_POOL, Params{ + "id": id, + "pool_id": client.ClientID(), + }, nil) +} + +// GetNotProcessedZCNBurnTickets retrieve burn tickets that are not compensated by minting +// - ethereumAddress: ethereum address for the issuer of the burn tickets +// - startNonce: start nonce for the burn tickets +// - cb: info callback instance, carries the response of the GET request to the sharders +func GetNotProcessedZCNBurnTickets(ethereumAddress, startNonce string) ([]byte, error) { + err := CheckConfig() + if err != nil { + return nil, err + } + return coreHttp.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_POOL, Params{ + "ethereum_address": ethereumAddress, + "nonce": startNonce, + }, nil) +} diff --git a/zcncore/wallet.go b/zcncore/wallet.go index b0d436545..f564819e1 100644 --- a/zcncore/wallet.go +++ b/zcncore/wallet.go @@ -4,7 +4,6 @@ package zcncore import ( - "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/zcncrypto" ) @@ -14,13 +13,7 @@ func GetWallet(walletStr string) (*zcncrypto.Wallet, error) { return getWallet(walletStr) } -// GetWalletBalance retrieve wallet balance from sharders -// - id: client id -func GetWalletBalance(clientId string) (common.Balance, int64, error) { - return getWalletBalance(clientId) -} - -//Deprecated: use Sign() method in zcncrypto.Wallet +// Deprecated: use Sign() method in zcncrypto.Wallet func SignWith0Wallet(hash string, w *zcncrypto.Wallet) (string, error) { cfg, err := conf.GetClientConfig() if err != nil { diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 7fe34e6ee..7cb92d670 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -2,6 +2,7 @@ package zcncore import ( "encoding/hex" + "github.com/0chain/gosdk/core/common" "time" "errors" @@ -123,72 +124,6 @@ func CloseLog() { const TOKEN_UNIT int64 = 1e10 -const ( - OpGetTokenLockConfig int = iota - OpGetLockedTokens - OpGetUserPools - OpGetUserPoolDetail - OpGetNotProcessedBurnTickets - OpGetMintNonce - // storage SC ops - // OpStorageSCGetConfig Get global storage SC config - OpStorageSCGetConfig - - // OpStorageSCGetChallengePoolInfo Get challenge pool info - OpStorageSCGetChallengePoolInfo - - // OpStorageSCGetAllocation Get allocation info - OpStorageSCGetAllocation - - // OpStorageSCGetAllocations Get all allocations - OpStorageSCGetAllocations - - // OpStorageSCGetReadPoolInfo Get read pool info - OpStorageSCGetReadPoolInfo - - // OpStorageSCGetStakePoolInfo Get stake pool info - OpStorageSCGetStakePoolInfo - - // OpStorageSCGetStakePoolUserInfo Get blobbers - OpStorageSCGetBlobbers - - // OpStorageSCGetBlobber Get blobber information - OpStorageSCGetBlobber - - // OpStorageSCGetValidator Get transaction info - OpStorageSCGetTransactions - - // OpStorageSCGetSnapshots Get global snapshots - OpStorageSCGetSnapshots - - // OpStorageSCGetBlobberSnapshots Get blobber snapshots - OpStorageSCGetBlobberSnapshots - - // OpStorageSCGetMinerSnapshots Get miner snapshots - OpStorageSCGetMinerSnapshots - - // OpStorageSCGetSharderSnapshots Get sharder snapshots - OpStorageSCGetSharderSnapshots - - // OpStorageSCGetAuthorizerSnapshots Get authorizer snapshots - OpStorageSCGetAuthorizerSnapshots - - // OpStorageSCGetValidatorSnapshots Get validator snapshots - OpStorageSCGetValidatorSnapshots - - // OpStorageSCGetUserSnapshots Get user snapshots - OpStorageSCGetUserSnapshots - - // OpStorageSCGetUserLockedTotal Get global configuration - OpZCNSCGetGlobalConfig - - // OpZCNSCGetMintNonce Get authorizer information - OpZCNSCGetAuthorizer - - // OpZCNSCGetAuthorizerNodes Get authorizer nodes - OpZCNSCGetAuthorizerNodes -) - // WalletCallback needs to be implemented for wallet creation. type WalletCallback interface { OnWalletCreateComplete(status int, wallet string, err string) @@ -413,3 +348,39 @@ func GetPublicEncryptionKey(mnemonic string) (string, error) { } return encScheme.GetPublicKey() } + +// ConvertToValue converts ZCN tokens to SAS tokens +// # Inputs +// - token: ZCN tokens +func ConvertToValue(token float64) uint64 { + return uint64(token * common.TokenUnit) +} + +func SignWithKey(privateKey, hash string) (string, error) { + sigScheme := zcncrypto.NewSignatureScheme("bls0chain") + err := sigScheme.SetPrivateKey(privateKey) + if err != nil { + return "", err + } + return sigScheme.Sign(hash) +} + +var AddSignature = func(privateKey, signature string, hash string) (string, error) { + var ( + ss = zcncrypto.NewSignatureScheme(client.SignatureScheme()) + err error + ) + + err = ss.SetPrivateKey(privateKey) + if err != nil { + return "", err + } + + return ss.Add(signature, hash) +} + +// ConvertToToken converts the SAS tokens to ZCN tokens +// - token: SAS tokens amount +func ConvertToToken(token int64) float64 { + return float64(token) / float64(common.TokenUnit) +} From d9c35e2049a7b97b99576b3963efafaf0362632d Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 11 Sep 2024 00:54:46 +0530 Subject: [PATCH 056/319] Fix --- core/client/cache.go | 3 +- core/{http => client}/http.go | 33 ++++++++++-- core/client/node.go | 83 +++++++++++++++++++++++++---- core/transaction/get_data.go | 31 +---------- wasmsdk/sdk.go | 3 +- zboxcore/sdk/sdk.go | 33 ++++++------ zboxcore/zboxutil/http.go | 3 +- zboxcore/zboxutil/http_test.go | 2 +- zboxcore/zboxutil/transport_wasm.go | 2 +- zcnbridge/http/client.go | 4 +- zcnbridge/rest.go | 3 +- zcncore/get_data.go | 17 +++--- 12 files changed, 135 insertions(+), 82 deletions(-) rename core/{http => client}/http.go (89%) diff --git a/core/client/cache.go b/core/client/cache.go index 1ccc8aaf1..5652a3cdb 100644 --- a/core/client/cache.go +++ b/core/client/cache.go @@ -1,7 +1,6 @@ package client import ( - "github.com/0chain/gosdk/core/transaction" "sync" ) @@ -30,7 +29,7 @@ func (nc *NonceCache) GetNextNonce(clientId string) int64 { nc.guard.Lock() defer nc.guard.Unlock() if _, ok := nc.cache[clientId]; !ok { - bal, err := transaction.GetBalance(clientId) + bal, err := GetBalance(clientId) if err != nil || bal == nil { nc.cache[clientId] = 0 } else { diff --git a/core/http/http.go b/core/client/http.go similarity index 89% rename from core/http/http.go rename to core/client/http.go index 37d9e1a98..ed11fc1b3 100644 --- a/core/http/http.go +++ b/core/client/http.go @@ -1,10 +1,9 @@ -package http +package client import ( "encoding/json" "fmt" "github.com/0chain/errors" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/logger" "io/ioutil" @@ -51,7 +50,7 @@ const consensusThresh = float32(25.0) // - params is the query parameters // - handler is the handler function to handle the response func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string, handler SCRestAPIHandler) ([]byte, error) { - nodeClient, err := client.GetNode() + nodeClient, err := GetNode() if err != nil { return nil, err } @@ -217,3 +216,31 @@ func getEnvAny(names ...string) string { } return "" } + +func GetBalance(clientId string) (*GetBalanceResponse, error) { + const GET_BALANCE = "/client/get/balance" + var ( + balance GetBalanceResponse + err error + res []byte + ) + + if res, err = MakeSCRestAPICall("", GET_BALANCE, map[string]string{ + "client_id": clientId, + }, nil); err != nil { + return nil, err + } + + if err = json.Unmarshal(res, &balance); err != nil { + return nil, err + } + + return &balance, nil +} + +type GetBalanceResponse struct { + Txn string `json:"txn"` + Round int64 `json:"round"` + Balance int64 `json:"balance"` + Nonce int64 `json:"nonce"` +} diff --git a/core/client/node.go b/core/client/node.go index 3b76fd97b..c56ac9e63 100644 --- a/core/client/node.go +++ b/core/client/node.go @@ -1,13 +1,14 @@ +// Provides functions and data structures to interact with the system nodes in the context of the blockchain network. package client -import "sync" +import ( + "sort" + "sync" + "time" +) -func (h *NodeHolder) Healthy() (res []string) { - h.guard.Lock() - defer h.guard.Unlock() - - return h.nodes[:h.consensus] -} +const statSize = 20 +const defaultTimeout = 5 * time.Second type NodeHolder struct { consensus int @@ -16,6 +17,12 @@ type NodeHolder struct { nodes []string } +type NodeStruct struct { + id string + weight int64 + stats []int +} + func NewHolder(nodes []string, consensus int) *NodeHolder { if len(nodes) < consensus { panic("consensus is not correct") @@ -28,6 +35,7 @@ func NewHolder(nodes []string, consensus int) *NodeHolder { } return &holder } + func NewNode(id string) *NodeStruct { return &NodeStruct{ id: id, @@ -36,8 +44,61 @@ func NewNode(id string) *NodeStruct { } } -type NodeStruct struct { - id string - weight int64 - stats []int +func (h *NodeHolder) Success(id string) { + h.guard.Lock() + defer h.guard.Unlock() + h.adjustNode(id, 1) +} + +func (h *NodeHolder) Fail(id string) { + h.guard.Lock() + defer h.guard.Unlock() + h.adjustNode(id, -1) +} + +func (h *NodeHolder) adjustNode(id string, res int) { + n := NewNode(id) + nodes := h.nodes + if node, ok := h.stats[id]; ok { + for i, v := range nodes { + if v == id { + nodes = append(nodes[:i], nodes[i+1:]...) + break + } + } + + sourceStats := node.stats + sourceStats = append(sourceStats, res) + if len(sourceStats) > statSize { + sourceStats = sourceStats[1:] + } + node.stats = sourceStats + + w := int64(0) + for i, s := range sourceStats { + w += int64(i+1) * int64(s) + } + node.weight = w + + n = node + } + + i := sort.Search(len(nodes), func(i int) bool { + return h.stats[nodes[i]].weight < n.weight + }) + h.nodes = append(nodes[:i], append([]string{n.id}, nodes[i:]...)...) +} + +func (h *NodeHolder) Healthy() (res []string) { + h.guard.Lock() + defer h.guard.Unlock() + + return h.nodes[:h.consensus] +} + +func (h *NodeHolder) All() (res []string) { + h.guard.Lock() + defer h.guard.Unlock() + + return h.nodes } diff --git a/core/transaction/get_data.go b/core/transaction/get_data.go index 8bc03058c..66061e631 100644 --- a/core/transaction/get_data.go +++ b/core/transaction/get_data.go @@ -3,7 +3,7 @@ package transaction import ( "encoding/json" "github.com/0chain/errors" - coreHttp "github.com/0chain/gosdk/core/http" + coreHttp "github.com/0chain/gosdk/core/client" ) const ( @@ -64,8 +64,6 @@ const ( STORAGE_GET_AUTHORIZER_SNAPSHOT = STORAGESC_PFX + "/replicate-authorizer-aggregates" STORAGE_GET_VALIDATOR_SNAPSHOT = STORAGESC_PFX + "/replicate-validator-aggregates" STORAGE_GET_USER_SNAPSHOT = STORAGESC_PFX + "/replicate-user-aggregates" - - GET_BALANCE = "/client/get/balance" ) // @@ -109,30 +107,3 @@ func GetConfig(configType string) (conf *InputMap, err error) { return } - -func GetBalance(clientId string) (*GetBalanceResponse, error) { - var ( - balance GetBalanceResponse - err error - res []byte - ) - - if res, err = coreHttp.MakeSCRestAPICall("", GET_BALANCE, map[string]string{ - "client_id": clientId, - }, nil); err != nil { - return nil, err - } - - if err = json.Unmarshal(res, &balance); err != nil { - return nil, err - } - - return &balance, nil -} - -type GetBalanceResponse struct { - Txn string `json:"txn"` - Round int64 `json:"round"` - Balance int64 `json:"balance"` - Nonce int64 `json:"nonce"` -} diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index 6ea3d9635..befd6fdad 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -9,7 +9,6 @@ import ( "encoding/json" "errors" "fmt" - coreHttp "github.com/0chain/gosdk/core/http" "io" "os" "sync" @@ -168,7 +167,7 @@ func makeSCRestAPICall(scAddress, relativePath, paramsJson string) (string, erro if err != nil { sdkLogger.Error(fmt.Sprintf("Error parsing JSON: %v", err)) } - b, err := coreHttp.MakeSCRestAPICall(scAddress, relativePath, params, nil) + b, err := client.MakeSCRestAPICall(scAddress, relativePath, params, nil) return string(b), err } diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index 8720ddc24..9e872a0ab 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -8,7 +8,6 @@ import ( "github.com/0chain/common/core/currency" "github.com/0chain/errors" "github.com/0chain/gosdk/core/conf" - coreHttp "github.com/0chain/gosdk/core/http" "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/core/zcncrypto" "gopkg.in/natefinch/lumberjack.v2" @@ -184,7 +183,7 @@ func GetReadPoolInfo(clientID string) (info *ReadPool, err error) { } var b []byte - b, err = coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getReadPoolStat", + b, err = client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getReadPoolStat", map[string]string{"client_id": clientID}, nil) if err != nil { return nil, errors.Wrap(err, "error requesting read pool info") @@ -290,7 +289,7 @@ func GetStakePoolInfo(providerType ProviderType, providerID string) (info *Stake } var b []byte - b, err = coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getStakePoolStat", + b, err = client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getStakePoolStat", map[string]string{"provider_type": strconv.Itoa(int(providerType)), "provider_id": providerID}, nil) if err != nil { return nil, errors.Wrap(err, "error requesting stake pool info:") @@ -331,7 +330,7 @@ func GetStakePoolUserInfo(clientID string, offset, limit int) (info *StakePoolUs "offset": strconv.FormatInt(int64(offset), 10), "limit": strconv.FormatInt(int64(limit), 10), } - b, err = coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, + b, err = client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getUserStakePoolStat", params, nil) if err != nil { return nil, errors.Wrap(err, "error requesting stake pool user info:") @@ -543,7 +542,7 @@ func GetChallengePoolInfo(allocID string) (info *ChallengePoolInfo, err error) { } var b []byte - b, err = coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, + b, err = client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getChallengePoolStat", map[string]string{"allocation_id": allocID}, nil) if err != nil { @@ -568,7 +567,7 @@ func GetMptData(key string) ([]byte, error) { } var b []byte - b, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, + b, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/get_mpt_key", map[string]string{"key": key}, nil, ) @@ -739,7 +738,7 @@ func getBlobbersInternal(active, stakable bool, limit, offset int) (bs []*Blobbe offset, strconv.FormatBool(stakable), ) - b, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, url, nil, nil) + b, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, url, nil, nil) var wrap nodes if err != nil { return nil, errors.Wrap(err, "error requesting blobbers:") @@ -797,7 +796,7 @@ func GetBlobber(blobberID string) (blob *Blobber, err error) { return nil, sdkNotInitialized } var b []byte - b, err = coreHttp.MakeSCRestAPICall( + b, err = client.MakeSCRestAPICall( STORAGE_SCADDRESS, "/getBlobber", map[string]string{"blobber_id": blobberID}, @@ -822,7 +821,7 @@ func GetValidator(validatorID string) (validator *Validator, err error) { return nil, sdkNotInitialized } var b []byte - b, err = coreHttp.MakeSCRestAPICall( + b, err = client.MakeSCRestAPICall( STORAGE_SCADDRESS, "/get_validator", map[string]string{"validator_id": validatorID}, @@ -847,7 +846,7 @@ func GetValidators(stakable bool) (validators []*Validator, err error) { return nil, sdkNotInitialized } var b []byte - b, err = coreHttp.MakeSCRestAPICall( + b, err = client.MakeSCRestAPICall( STORAGE_SCADDRESS, "/validators", map[string]string{ @@ -911,7 +910,7 @@ func GetAllocation(allocationID string) (*Allocation, error) { } params := make(map[string]string) params["allocation"] = allocationID - allocationBytes, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params, nil) + allocationBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params, nil) if err != nil { return nil, errors.New("allocation_fetch_error", "Error fetching the allocation."+err.Error()) } @@ -932,7 +931,7 @@ func GetAllocationUpdates(allocation *Allocation) error { params := make(map[string]string) params["allocation"] = allocation.ID - allocationBytes, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params, nil) + allocationBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params, nil) if err != nil { return errors.New("allocation_fetch_error", "Error fetching the allocation."+err.Error()) } @@ -985,7 +984,7 @@ func getAllocationsInternal(clientID string, limit, offset int) ([]*Allocation, params["client"] = clientID params["limit"] = fmt.Sprint(limit) params["offset"] = fmt.Sprint(offset) - allocationsBytes, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocations", params, nil) + allocationsBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocations", params, nil) if err != nil { return nil, errors.New("allocations_fetch_error", "Error fetching the allocations."+err.Error()) } @@ -1166,7 +1165,7 @@ func GetAllocationBlobbers( params["force"] = strconv.FormatBool(force[0]) } - allocBlobber, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/alloc_blobbers", params, nil) + allocBlobber, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/alloc_blobbers", params, nil) if err != nil { return nil, err } @@ -1251,7 +1250,7 @@ func GetBlobberIds(blobberUrls []string) ([]string, error) { params := make(map[string]string) params["blobber_urls"] = string(urlsStr) - idsStr, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/blobber_ids", params, nil) + idsStr, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/blobber_ids", params, nil) if err != nil { return nil, err } @@ -1276,7 +1275,7 @@ func GetFreeAllocationBlobbers(request map[string]interface{}) ([]string, error) params := make(map[string]string) params["free_allocation_data"] = string(data) - allocBlobber, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/free_alloc_blobbers", params, nil) + allocBlobber, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/free_alloc_blobbers", params, nil) if err != nil { return nil, err } @@ -1779,7 +1778,7 @@ func GetUpdateAllocationMinLock( params := make(map[string]string) params["data"] = string(data) - responseBytes, err := coreHttp.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation-update-min-lock", params, nil) + responseBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation-update-min-lock", params, nil) if err != nil { return 0, errors.Wrap(err, "failed to request allocation update min lock") } diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index 1a4cebc99..3f4759ba4 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -16,7 +16,6 @@ import ( "github.com/0chain/errors" "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/encryption" - coreHttp "github.com/0chain/gosdk/core/http" "github.com/0chain/gosdk/core/logger" "github.com/hitenjain14/fasthttp" ) @@ -145,7 +144,7 @@ var envProxy proxyFromEnv func init() { Client = &http.Client{ - Transport: coreHttp.DefaultTransport, + Transport: client.DefaultTransport, } FastHttpClient = &fasthttp.Client{ diff --git a/zboxcore/zboxutil/http_test.go b/zboxcore/zboxutil/http_test.go index 27da8395a..750cd2afd 100644 --- a/zboxcore/zboxutil/http_test.go +++ b/zboxcore/zboxutil/http_test.go @@ -1,7 +1,7 @@ package zboxutil import ( - coreHttp "github.com/0chain/gosdk/core/http" + coreHttp "github.com/0chain/gosdk/core/client" "testing" "github.com/stretchr/testify/assert" diff --git a/zboxcore/zboxutil/transport_wasm.go b/zboxcore/zboxutil/transport_wasm.go index 213c9b220..f0a14af95 100644 --- a/zboxcore/zboxutil/transport_wasm.go +++ b/zboxcore/zboxutil/transport_wasm.go @@ -4,7 +4,7 @@ package zboxutil import ( - coreHttp "github.com/0chain/gosdk/core/http" + coreHttp "github.com/0chain/gosdk/core/client" "net/http" "time" ) diff --git a/zcnbridge/http/client.go b/zcnbridge/http/client.go index 99d7c33bc..0687a2c45 100644 --- a/zcnbridge/http/client.go +++ b/zcnbridge/http/client.go @@ -1,7 +1,7 @@ package http import ( - http2 "github.com/0chain/gosdk/core/http" + http2 "github.com/0chain/gosdk/core/client" "net/http" "time" @@ -32,7 +32,7 @@ func CleanClient() *http.Client { func NewRetryableClient(verbose bool) *retryablehttp.Client { client := retryablehttp.NewClient() client.HTTPClient = &http.Client{ - Transport: http2.DefaultTransport, + Transport: client.DefaultTransport, } if !verbose { diff --git a/zcnbridge/rest.go b/zcnbridge/rest.go index 5d6d261ab..22075a59c 100644 --- a/zcnbridge/rest.go +++ b/zcnbridge/rest.go @@ -3,8 +3,7 @@ package zcnbridge import ( "encoding/json" "fmt" - coreHttp "github.com/0chain/gosdk/core/http" - + coreHttp "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/zcncore" diff --git a/zcncore/get_data.go b/zcncore/get_data.go index 56be171fb..97f47d61a 100644 --- a/zcncore/get_data.go +++ b/zcncore/get_data.go @@ -7,7 +7,6 @@ import ( "fmt" "github.com/0chain/gosdk/core/block" "github.com/0chain/gosdk/core/client" - coreHttp "github.com/0chain/gosdk/core/http" "github.com/0chain/gosdk/core/tokenrate" "github.com/0chain/gosdk/core/util" "github.com/0chain/gosdk/core/zcncrypto" @@ -209,7 +208,7 @@ func GetMinerSCNodeInfo(id string) ([]byte, error) { return nil, err } - return coreHttp.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_NODE, Params{ + return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_NODE, Params{ "id": id, }, nil) } @@ -222,7 +221,7 @@ func GetMintNonce() ([]byte, error) { return nil, err } - return coreHttp.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINT_NONCE, Params{ + return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINT_NONCE, Params{ "client_id": client.ClientID(), }, nil) } @@ -232,7 +231,7 @@ func GetMiners(active, stakable bool, limit, offset int) ([]byte, error) { return nil, err } - return coreHttp.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_MINERS, Params{ + return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_MINERS, Params{ "active": strconv.FormatBool(active), "stakable": strconv.FormatBool(stakable), "offset": strconv.FormatInt(int64(offset), 10), @@ -245,7 +244,7 @@ func GetSharders(active, stakable bool, limit, offset int) ([]byte, error) { return nil, err } - return coreHttp.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_SHARDERS, Params{ + return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_SHARDERS, Params{ "active": strconv.FormatBool(active), "stakable": strconv.FormatBool(stakable), "offset": strconv.FormatInt(int64(offset), 10), @@ -257,7 +256,7 @@ func GetSharders(active, stakable bool, limit, offset int) ([]byte, error) { // - numSharders: number of sharders // - timeout: request timeout func GetLatestFinalizedMagicBlock(ctx context.Context, numSharders int) (m *block.MagicBlock, err error) { - res, err := coreHttp.MakeSCRestAPICall(MinerSmartContractAddress, GET_LATEST_FINALIZED_MAGIC_BLOCK, nil, nil) + res, err := client.MakeSCRestAPICall(MinerSmartContractAddress, GET_LATEST_FINALIZED_MAGIC_BLOCK, nil, nil) if err != nil { return nil, err } @@ -280,7 +279,7 @@ func GetMinerSCUserInfo(clientID string) ([]byte, error) { clientID = client.ClientID() } - return coreHttp.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_USER, Params{ + return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_USER, Params{ "client_id": clientID, }, nil) } @@ -292,7 +291,7 @@ func GetMinerSCNodePool(id string) ([]byte, error) { return nil, err } - return coreHttp.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_POOL, Params{ + return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_POOL, Params{ "id": id, "pool_id": client.ClientID(), }, nil) @@ -307,7 +306,7 @@ func GetNotProcessedZCNBurnTickets(ethereumAddress, startNonce string) ([]byte, if err != nil { return nil, err } - return coreHttp.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_POOL, Params{ + return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_POOL, Params{ "ethereum_address": ethereumAddress, "nonce": startNonce, }, nil) From 08b4d99cc1a78f0ac4e5b011d64daa3426308a26 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 11 Sep 2024 01:28:15 +0530 Subject: [PATCH 057/319] Fix --- core/client/http.go | 23 +++++++++++++++++++++-- core/transaction/entity.go | 20 ++++++++++++-------- core/transaction/utils.go | 10 +++++++++- zcnbridge/http/client.go | 2 +- zcncore/execute_transactions.go | 10 ++++++++++ zcncore/wallet_base.go | 26 ++++++++++++++++++++++++++ 6 files changed, 79 insertions(+), 12 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index ed11fc1b3..091bf62d8 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -6,7 +6,9 @@ import ( "github.com/0chain/errors" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/logger" + "github.com/shopspring/decimal" "io/ioutil" + "math" "net" "net/http" "net/url" @@ -217,7 +219,7 @@ func getEnvAny(names ...string) string { return "" } -func GetBalance(clientId string) (*GetBalanceResponse, error) { +func GetBalance(clientIDs ...string) (*GetBalanceResponse, error) { const GET_BALANCE = "/client/get/balance" var ( balance GetBalanceResponse @@ -225,8 +227,15 @@ func GetBalance(clientId string) (*GetBalanceResponse, error) { res []byte ) + var clientID string + if len(clientIDs) > 0 { + clientID = clientIDs[0] + } else { + clientID = ClientID() + } + if res, err = MakeSCRestAPICall("", GET_BALANCE, map[string]string{ - "client_id": clientId, + "client_id": clientID, }, nil); err != nil { return nil, err } @@ -244,3 +253,13 @@ type GetBalanceResponse struct { Balance int64 `json:"balance"` Nonce int64 `json:"nonce"` } + +// ToToken converts Balance to ZCN tokens. +func (b GetBalanceResponse) ToToken() (float64, error) { + if b.Balance > math.MaxInt64 { + return 0.0, errors.New("to_token failed", "value is too large") + } + + f, _ := decimal.New(b.Balance, -10).Float64() + return f, nil +} diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 294d3e833..cc401fe33 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -486,20 +486,20 @@ 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) ( +func SmartContractTxn(scAddress string, sn SmartContractTxnData, toClient ...string) ( hash, out string, nonce int64, txn *Transaction, err error) { - return SmartContractTxnValue(scAddress, sn, 0) + return SmartContractTxnValue(scAddress, sn, 0, toClient...) } -func SmartContractTxnValue(scAddress string, sn SmartContractTxnData, value uint64) ( +func SmartContractTxnValue(scAddress string, sn SmartContractTxnData, value uint64, toClient ...string) ( hash, out string, nonce int64, txn *Transaction, err error) { - return SmartContractTxnValueFeeWithRetry(scAddress, sn, value, client.TxnFee()) + return SmartContractTxnValueFeeWithRetry(scAddress, sn, value, client.TxnFee(), toClient...) } func SmartContractTxnValueFeeWithRetry(scAddress string, sn SmartContractTxnData, - value, fee uint64) (hash, out string, nonce int64, t *Transaction, err error) { - hash, out, nonce, t, err = SmartContractTxnValueFee(scAddress, sn, value, fee) + 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...) if err != nil && strings.Contains(err.Error(), "invalid transaction nonce") { return SmartContractTxnValueFee(scAddress, sn, value, fee) @@ -508,7 +508,7 @@ func SmartContractTxnValueFeeWithRetry(scAddress string, sn SmartContractTxnData } func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, - value, fee uint64) (hash, out string, nonce int64, t *Transaction, err error) { + value, fee uint64, toClient ...string) (hash, out string, nonce int64, t *Transaction, err error) { var requestBytes []byte if requestBytes, err = json.Marshal(sn); err != nil { @@ -534,6 +534,10 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, txn.TransactionFee = fee txn.TransactionType = TxnTypeSmartContract + if len(toClient) > 0 { + txn.ToClientID = toClient[0] + } + // adjust fees if not set if fee == 0 { fee, err = EstimateFee(txn, nodeClient.Network().Miners, 0.2) @@ -574,7 +578,7 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, sys.Sleep(querySleepTime) for retries < cfg.MaxTxnQuery { - t, err = VerifyTransaction(txn.Hash, nodeClient.Sharders().Healthy()) + t, err = VerifyTransaction(txn.Hash) if err == nil { break } diff --git a/core/transaction/utils.go b/core/transaction/utils.go index e306b73c8..c716f22df 100644 --- a/core/transaction/utils.go +++ b/core/transaction/utils.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/0chain/gosdk/core/client" "math" "net/http" "strconv" @@ -281,7 +282,14 @@ func validateBlockHash(b *RoundBlockHeader) error { } // VerifyTransaction query transaction status from sharders, and verify it by mininal confirmation -func VerifyTransaction(txnHash string, sharders []string) (*Transaction, error) { +func VerifyTransaction(txnHash string) (*Transaction, error) { + nodeClient, err := client.GetNode() + if err != nil { + return nil, err + } + + sharders := nodeClient.Sharders().Healthy() + cfg, err := conf.GetClientConfig() if err != nil { return nil, err diff --git a/zcnbridge/http/client.go b/zcnbridge/http/client.go index 0687a2c45..1d2716e56 100644 --- a/zcnbridge/http/client.go +++ b/zcnbridge/http/client.go @@ -32,7 +32,7 @@ func CleanClient() *http.Client { func NewRetryableClient(verbose bool) *retryablehttp.Client { client := retryablehttp.NewClient() client.HTTPClient = &http.Client{ - Transport: client.DefaultTransport, + Transport: http2.DefaultTransport, } if !verbose { diff --git a/zcncore/execute_transactions.go b/zcncore/execute_transactions.go index 5dcbe7c8a..85d78805c 100644 --- a/zcncore/execute_transactions.go +++ b/zcncore/execute_transactions.go @@ -196,5 +196,15 @@ func ZCNSCCollectReward(providerId string, providerType Provider) (hash, out str Name: transaction.ZCNSC_COLLECT_REWARD, InputArgs: pr, }) +} + +type SendTxnData struct { + Note string `json:"note"` +} +func Send(toClientID string, tokens uint64, desc string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { + return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.ZCNSC_COLLECT_REWARD, + InputArgs: SendTxnData{Note: desc}, + }, toClientID) } diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 7cb92d670..59a94941b 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -2,7 +2,10 @@ package zcncore import ( "encoding/hex" + "fmt" "github.com/0chain/gosdk/core/common" + "github.com/0chain/gosdk/core/util" + "strings" "time" "errors" @@ -384,3 +387,26 @@ var AddSignature = func(privateKey, signature string, hash string) (string, erro func ConvertToToken(token int64) float64 { return float64(token) / float64(common.TokenUnit) } + +// GetIdForUrl retrieve the ID of the network node (miner/sharder) given its url. +// - url: url of the node. +func GetIdForUrl(url string) string { + url = strings.TrimRight(url, "/") + url = fmt.Sprintf("%v/_nh/whoami", url) + req, err := util.NewHTTPGetRequest(url) + if err != nil { + logging.Error(url, "new get request failed. ", err.Error()) + return "" + } + res, err := req.Get() + if err != nil { + logging.Error(url, "get error. ", err.Error()) + return "" + } + + s := strings.Split(res.Body, ",") + if len(s) >= 3 { + return s[3] + } + return "" +} From 101039cf0deb8b475e291aef9a2c8d4a0e651422 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Wed, 11 Sep 2024 11:50:21 +0530 Subject: [PATCH 058/319] add commit for v2 --- core/version/version.go | 3 +- go.mod | 48 ++- go.sum | 128 ++++++-- wasmsdk/demo/index.html | 14 +- wasmsdk/demo/zcn.js | 402 ++++++++++++++----------- wasmsdk/jsbridge/template_data.go | 2 +- zboxcore/allocationchange/change.go | 7 + zboxcore/blockchain/entity.go | 4 +- zboxcore/fileref/fileref.go | 16 + zboxcore/sdk/commitworker.go | 375 +++++++++++++++++++++-- zboxcore/sdk/copyworker.go | 13 + zboxcore/sdk/deleteworker.go | 13 + zboxcore/sdk/dirworker.go | 13 + zboxcore/sdk/moveworker.go | 13 + zboxcore/sdk/multi_operation_worker.go | 96 +++++- zboxcore/sdk/renameworker.go | 13 + zboxcore/sdk/rollback.go | 1 + zboxcore/sdk/upload_worker.go | 35 +++ zboxcore/zboxutil/http.go | 29 ++ 19 files changed, 976 insertions(+), 249 deletions(-) diff --git a/core/version/version.go b/core/version/version.go index 9213a1e29..fc76fab51 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -1,5 +1,6 @@ + //====== THIS IS AUTOGENERATED FILE. DO NOT MODIFY ======== package version +const VERSIONSTR = "v1.17.6-6-gdd52546d" -const VERSIONSTR = "v1.16.3-10-g66360b13" diff --git a/go.mod b/go.mod index 9f85673bd..10007582c 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/0chain/gosdk go 1.21 require ( - github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565 + github.com/0chain/common v1.13.1-0.20240910144331-3a5093424892 github.com/0chain/errors v1.0.3 github.com/Luzifer/go-openssl/v3 v3.1.0 github.com/btcsuite/btcd v0.23.4 @@ -28,15 +28,15 @@ require ( github.com/pkg/errors v0.9.1 github.com/shopspring/decimal v1.3.1 github.com/spf13/viper v1.15.0 - github.com/stretchr/testify v1.8.4 + github.com/stretchr/testify v1.9.0 github.com/tyler-smith/go-bip39 v1.1.0 github.com/uptrace/bunrouter v1.0.20 go.dedis.ch/kyber/v3 v3.1.0 go.uber.org/zap v1.24.0 - golang.org/x/crypto v0.17.0 + golang.org/x/crypto v0.21.0 golang.org/x/image v0.14.0 - golang.org/x/sync v0.5.0 - google.golang.org/grpc v1.53.0 + golang.org/x/sync v0.7.0 + google.golang.org/grpc v1.56.3 gopkg.in/cheggaaa/pb.v1 v1.0.28 gopkg.in/natefinch/lumberjack.v2 v2.2.1 gopkg.in/yaml.v2 v2.4.0 @@ -47,16 +47,25 @@ require ( github.com/hack-pad/go-webworkers v0.1.0 github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d github.com/minio/sha256-simd v1.0.1 + github.com/valyala/bytebufferpool v1.0.0 github.com/ybbus/jsonrpc/v3 v3.1.5 ) require ( + github.com/DataDog/zstd v1.4.5 // indirect github.com/VictoriaMetrics/fastcache v1.6.0 // indirect github.com/andybalholm/brotli v1.0.5 // indirect + github.com/beorn7/perks v1.0.1 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect github.com/cespare/xxhash v1.1.0 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/cockroachdb/errors v1.11.3 // indirect + github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce // indirect + github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect + github.com/cockroachdb/pebble v1.1.2 // indirect + github.com/cockroachdb/redact v1.1.5 // indirect + github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/deckarep/golang-set v1.8.0 // indirect @@ -67,15 +76,17 @@ require ( github.com/felixge/httpsnoop v1.0.3 // indirect github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect + github.com/fxamacker/cbor/v2 v2.7.0 // indirect github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect + github.com/getsentry/sentry-go v0.27.0 // indirect github.com/go-logfmt/logfmt v0.5.1 // indirect github.com/go-ole/go-ole v1.2.6 // indirect github.com/go-stack/stack v1.8.1 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang-jwt/jwt/v4 v4.3.0 // indirect - github.com/golang/glog v1.0.0 // indirect + github.com/golang/glog v1.1.0 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect - github.com/golang/protobuf v1.5.2 // indirect + github.com/golang/protobuf v1.5.3 // indirect github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect github.com/google/flatbuffers v22.9.29+incompatible // indirect github.com/gorilla/websocket v1.5.0 // indirect @@ -87,20 +98,27 @@ require ( github.com/huin/goupnp v1.0.3 // indirect github.com/jackpal/go-nat-pmp v1.0.2 // indirect github.com/kr/pretty v0.3.1 // indirect + github.com/kr/text v0.2.0 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/matryer/is v1.4.1 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect github.com/mitchellh/pointerstructure v1.2.0 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect github.com/patrickmn/go-cache v2.1.0+incompatible // indirect github.com/pelletier/go-toml/v2 v2.0.6 // indirect github.com/philhofer/fwd v1.1.2-0.20210722190033-5c56ac6d0bb9 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/prometheus/client_golang v1.12.0 // indirect + github.com/prometheus/client_model v0.2.1-0.20210607210712-147c58e9608a // indirect + github.com/prometheus/common v0.32.1 // indirect + github.com/prometheus/procfs v0.7.3 // indirect github.com/prometheus/tsdb v0.7.1 // indirect github.com/rivo/uniseg v0.4.3 // indirect github.com/rjeczalik/notify v0.9.1 // indirect + github.com/rogpeppe/go-internal v1.9.0 // indirect github.com/rs/cors v1.7.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/shirou/gopsutil v3.21.11+incompatible // indirect @@ -109,22 +127,22 @@ require ( github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/status-im/keycard-go v0.2.0 // indirect - github.com/stretchr/objx v0.5.0 // indirect + github.com/stretchr/objx v0.5.2 // indirect github.com/subosito/gotenv v1.4.2 // indirect github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect github.com/tinylib/msgp v1.1.6 // indirect github.com/tklauser/go-sysconf v0.3.11 // indirect github.com/tklauser/numcpus v0.6.0 // indirect github.com/urfave/cli/v2 v2.17.2-0.20221006022127-8f469abc00aa // indirect - github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.51.0 // indirect + github.com/x448/float16 v0.8.4 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect go.dedis.ch/fixbuf v1.0.3 // indirect go.opencensus.io v0.24.0 // indirect - golang.org/x/sys v0.15.0 // indirect + golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect + golang.org/x/sys v0.18.0 // indirect golang.org/x/text v0.14.0 // indirect - google.golang.org/protobuf v1.28.1 // indirect - gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect + google.golang.org/protobuf v1.33.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect ) @@ -138,9 +156,9 @@ require ( github.com/yusufpapurcu/wmi v1.2.2 // indirect go.uber.org/atomic v1.10.0 // indirect go.uber.org/multierr v1.9.0 // indirect - golang.org/x/net v0.19.0 // indirect + golang.org/x/net v0.23.0 // indirect golang.org/x/time v0.3.0 // indirect - google.golang.org/genproto v0.0.0-20230216225411-c8e22ba71e44 // indirect + google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect ) -//replace github.com/ethereum/go-ethereum => github.com/certifaction/go-ethereum v1.10.3-wasm \ No newline at end of file +//replace github.com/ethereum/go-ethereum => github.com/certifaction/go-ethereum v1.10.3-wasm diff --git a/go.sum b/go.sum index d64c6f548..e49db747d 100644 --- a/go.sum +++ b/go.sum @@ -40,13 +40,15 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565 h1:z+DtCR8mBsjPnEsT2XtRu4X7GfBiMnz9dYvWYs9V0B4= -github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565/go.mod h1:UyDC8Qyl5z9lGkCnf9RHJPMektnFX8XtCJZHXCCVj8E= +github.com/0chain/common v1.13.1-0.20240910144331-3a5093424892 h1:UA3fxT6u9fZPXtcAMY5asEWATVos/Tk0kyMa6dGYAAM= +github.com/0chain/common v1.13.1-0.20240910144331-3a5093424892/go.mod h1:lcwOe6fIgP7w/0sFSFB13rqDI43BGv5jk4PddArBaBM= github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM= github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= +github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ= +github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo= github.com/Luzifer/go-openssl/v3 v3.1.0 h1:QqKqo6kYXGGUsvtUoCpRZm8lHw+jDfhbzr36gVj+/gw= github.com/Luzifer/go-openssl/v3 v3.1.0/go.mod h1:liy3FXuuS8hfDlYh1T+l78AwQ/NjZflJz0NDvjKhwDs= github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= @@ -56,7 +58,10 @@ github.com/VictoriaMetrics/fastcache v1.6.0/go.mod h1:0qHz5QP0GMX4pfmMA/zt5RgfNu github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah4HI848JfFxHt+iPb26b4zyfspmqY0/8= github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= @@ -68,6 +73,8 @@ github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLj github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c= github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ= @@ -103,6 +110,7 @@ github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= @@ -112,6 +120,20 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= +github.com/cockroachdb/datadriven v1.0.3-0.20230413201302-be42291fc80f h1:otljaYPt5hWxV3MUfO5dFPFiOXg9CyG5/kCfayTqsJ4= +github.com/cockroachdb/datadriven v1.0.3-0.20230413201302-be42291fc80f/go.mod h1:a9RdTaap04u637JoCzcUoIcDmvwSUtcUFtT/C3kJlTU= +github.com/cockroachdb/errors v1.11.3 h1:5bA+k2Y6r+oz/6Z/RFlNeVCesGARKuC6YymtcDrbC/I= +github.com/cockroachdb/errors v1.11.3/go.mod h1:m4UIW4CDjx+R5cybPsNrRbreomiFqt8o1h1wUVazSd8= +github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce h1:giXvy4KSc/6g/esnpM7Geqxka4WSqI1SZc7sMJFd3y4= +github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce/go.mod h1:9/y3cnZ5GKakj/H4y9r9GTjCvAFta7KLgSHPJJYc52M= +github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b h1:r6VH0faHjZeQy818SGhaone5OnYfxFR/+AzdY3sf5aE= +github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b/go.mod h1:Vz9DsVWQQhf3vs21MhPMZpMGSht7O/2vFW2xusFUVOs= +github.com/cockroachdb/pebble v1.1.2 h1:CUh2IPtR4swHlEj48Rhfzw6l/d0qA31fItcIszQVIsA= +github.com/cockroachdb/pebble v1.1.2/go.mod h1:4exszw1r40423ZsmkG/09AFEG83I0uDgfujJdbL6kYU= +github.com/cockroachdb/redact v1.1.5 h1:u1PMllDkdFfPWaNGMyLD1+so+aq3uUItthCFqzwPJ30= +github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg= +github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAKVxetITBuuhv3BI9cMrmStnpT18zmgmTxunpo= +github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06/go.mod h1:7nc4anLGjupUW/PeY5qiNYsdNXj7zopG+eqsS7To5IQ= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= @@ -171,17 +193,26 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= +github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E= +github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ= github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff h1:tY80oXqGNY4FhTFhk+o9oFHGINQ/+vhlm8HFzi6znCI= github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww= +github.com/getsentry/sentry-go v0.27.0 h1:Pv98CIbtB3LkMWmXi4Joa5OOcwbmnX88sF5qbK3r3Ps= +github.com/getsentry/sentry-go v0.27.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY= github.com/glycerine/go-unsnap-stream v0.0.0-20180323001048-9f0cb55181dd/go.mod h1:/20jfyN9Y5QPEAprSgKAUr+glWDY39ZiUEAYOEv5dsE= github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24= +github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA= +github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= -github.com/go-kit/kit v0.8.0 h1:Wz+5lgoB0kkuqLEc6NVmwRknTKP6dTGbSqvhZtBI/j0= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-kit/kit v0.9.0 h1:wDJmvq38kDhkVxi50ni9ykkdUr1PKgqKOoi01fa0Mdk= +github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA= github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= @@ -200,8 +231,8 @@ github.com/golang-jwt/jwt/v4 v4.3.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzw github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/golang/geo v0.0.0-20190916061304-5b978397cfec/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= -github.com/golang/glog v1.0.0 h1:nfP3RFugxnNRyKgeWd4oI1nYvXpxrx8ck8ZrcizshdQ= -github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4= +github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE= +github.com/golang/glog v1.1.0/go.mod h1:pfYeQZ3JWZoXTV5sFc986z3HTpwQs9At6P4ImfuP3NQ= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= @@ -229,8 +260,9 @@ github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QD github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= @@ -255,6 +287,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -334,13 +367,18 @@ github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7Bd github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/jsternberg/zap-logfmt v1.0.0/go.mod h1:uvPs/4X51zdkcm5jXl5SYoN+4RK21K8mysFmDaM/h+o= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= github.com/jwilder/encoding v0.0.0-20170811194829-b4e1701a28ef/go.mod h1:Ct9fl0F6iIOGgxJ5npU/IUOhOhqlVrGjyIZc8/MagT0= github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= @@ -359,10 +397,10 @@ github.com/klauspost/pgzip v1.0.2-0.20170402124221-0bf5dcad4ada/go.mod h1:Ch1tH6 github.com/klauspost/reedsolomon v1.11.8 h1:s8RpUW5TK4hjr+djiOpbZJB4ksx+TdYbRH7vHQpwPOY= github.com/klauspost/reedsolomon v1.11.8/go.mod h1:4bXRN+cVzMdml6ti7qLouuYi32KHJ5MGv0Qd8a47h6A= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -395,6 +433,8 @@ github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-tty v0.0.0-20180907095812-13ff1204f104/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI= +github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM= github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= @@ -405,10 +445,14 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/pointerstructure v1.2.0 h1:O+i9nHnXS3l/9Wu7r4NrEdwA2VFTicjUEN1uBnDo34A= github.com/mitchellh/pointerstructure v1.2.0/go.mod h1:BRAsLI5zgXmw97Lf6s25bs8ohIXc3tViBH44KcwB2g4= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE+eKJXWVjKXM4ck2QobLqTDytGJbLLhJg= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= @@ -438,6 +482,8 @@ github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG github.com/philhofer/fwd v1.1.2-0.20210722190033-5c56ac6d0bb9 h1:6ob53CVz+ja2i7easAStApZJlh7sxyq3Cm7g1Di6iqA= github.com/philhofer/fwd v1.1.2-0.20210722190033-5c56ac6d0bb9/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU= github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= +github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= +github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -449,14 +495,29 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= +github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M= +github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0= +github.com/prometheus/client_golang v1.12.0 h1:C+UIj/QWtmqY13Arb8kwMt5j34/0Z2iKamrJ+ryC0Gg= +github.com/prometheus/client_golang v1.12.0/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/client_model v0.2.1-0.20210607210712-147c58e9608a h1:CmF68hwI0XsOQ5UwlBopMi2Ow4Pbg32akc4KIVCOm+Y= +github.com/prometheus/client_model v0.2.1-0.20210607210712-147c58e9608a/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w= github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= github.com/prometheus/common v0.6.0/go.mod h1:eBmuwkDJBwy6iBfxCBob6t6dR6ENT/y+J+Zk0j9GMYc= +github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo= +github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc= +github.com/prometheus/common v0.32.1 h1:hWIdL3N2HoUx3B8j3YN9mWor0qhY/NlEKZEaXxuIRh4= +github.com/prometheus/common v0.32.1/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= +github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= +github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU= +github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/tsdb v0.7.1 h1:YZcsG11NqnK4czYLrWd9mpEuAJIHVQLwdrleYfszMAA= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/remeh/sizedwaitgroup v1.0.0 h1:VNGGFwNo/R5+MJBf6yrsr110p0m4/OX4S3DCy7Kyl5E= @@ -483,6 +544,8 @@ github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMT github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8= github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= @@ -510,8 +573,9 @@ github.com/status-im/keycard-go v0.2.0/go.mod h1:wlp8ZLbsmrF6g6WjugPAx+IzoLrkdf9 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.2.0/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= @@ -521,8 +585,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8= github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY= @@ -546,6 +610,8 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= github.com/willf/bitset v1.1.3/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4= +github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= +github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= github.com/xlab/treeprint v0.0.0-20180616005107-d6fb6747feb6/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= @@ -601,8 +667,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= -golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= +golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -616,6 +682,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= +golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME= +golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= @@ -680,8 +748,9 @@ golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= -golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= +golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -691,6 +760,7 @@ golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43/go.mod h1:KelEdhl1UZF7XfJ golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= +golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -702,8 +772,8 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= -golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -714,6 +784,7 @@ golang.org/x/sys v0.0.0-20190124100055-b90733256f2e/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -726,6 +797,7 @@ golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200107162124-548cf772de50/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -740,6 +812,8 @@ golang.org/x/sys v0.0.0-20200511232937-7e40ca221e25/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -748,17 +822,21 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210104204734-6f8348627aad/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -766,6 +844,7 @@ golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3 golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -905,8 +984,8 @@ google.golang.org/genproto v0.0.0-20201210142538-e3217bee35cc/go.mod h1:FWY/as6D google.golang.org/genproto v0.0.0-20201214200347-8c77b98c765d/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20210108203827-ffc7fda8c3d7/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20210226172003-ab064af71705/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20230216225411-c8e22ba71e44 h1:EfLuoKW5WfkgVdDy7dTK8qSbH37AX5mj/MFh+bGPz14= -google.golang.org/genproto v0.0.0-20230216225411-c8e22ba71e44/go.mod h1:8B0gmkoRebU8ukX6HP+4wrVQUY1+6PkQ44BSyIlflHA= +google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 h1:KpwkzHKEF7B9Zxg18WzOa7djJ+Ha5DzthMyZYQfEn2A= +google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1/go.mod h1:nKE/iIaLqn2bQwXBg8f1g2Ylh6r5MN5CmZvuzZCgsCU= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= @@ -923,8 +1002,8 @@ google.golang.org/grpc v1.31.1/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/grpc v1.34.0/go.mod h1:WotjhfgOW/POjDeRt8vscBtXq+2VjORFy659qA51WJ8= google.golang.org/grpc v1.35.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= -google.golang.org/grpc v1.53.0 h1:LAv2ds7cmFV/XTS3XG1NneeENYrXGmorPxsBbptIjNc= -google.golang.org/grpc v1.53.0/go.mod h1:OnIrk0ipVdj4N5d9IUoFUx72/VlD7+jUsHwZgwSMQpw= +google.golang.org/grpc v1.56.3 h1:8I4C0Yq1EjstUzUJzpcRVbuYA2mODtEmpWiQoN/b2nc= +google.golang.org/grpc v1.56.3/go.mod h1:I9bI3vqKfayGqPUAwGdOSu7kt6oIJLixfffKrpXqQ9s= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= @@ -937,8 +1016,8 @@ google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGj google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= -google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= +google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -960,6 +1039,7 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= diff --git a/wasmsdk/demo/index.html b/wasmsdk/demo/index.html index bc4f1fb98..c6c73a3a8 100644 --- a/wasmsdk/demo/index.html +++ b/wasmsdk/demo/index.html @@ -171,10 +171,10 @@

please download zcn.wasm from https://github.com/0chain/gosdk/releases/lates } const getWallet = () => { - const clientID = "ab5b8ab19abe574d92238e0a5dc0c2abd53614cd12cab6b09576fab2a0f64a83" - const publicKey = "d430b1b33eab43bd09886e6125e246600e36e3f88d658d00bf836aa564251e2364bccbfb18f1cb1b5fa4d96ba602b59bc009e6e0223b6a8ebdafc14822b78d23" - const privateKey = "0c2cb85c8c33b3cc35dc12b4754e61ae9488bba857982c382caa461ecac19d19" - const mnemonic = "grace fiscal menu squeeze certain drum ostrich lunar ugly remember cousin observe oxygen brisk toward notable shoot cushion develop marble open aspect couch noise" + const clientID = "7b830dd6695f1cbb4f9ea9e5d3f7b5c527ec7dfa4e9ac91133ea540f2b5ce818" + const publicKey = "363d1d38acccb7772aa1ed66676333fcc3e3fad63522993722aff01153d2100dbd9f0cae95baf18b8dab37c5e51693e2c84866fecf83a9669b5191e92771d1a3" + const privateKey = "d3e5868e762f57d330d306b346c68a0cf0b9bef1f0eb9a50f43825e668f84a16" + const mnemonic = "when wait pyramid until current tornado proud solar body atom push throw detail loan figure crystal youth relax vanish west side achieve social powder" return { clientID, publicKey, privateKey, mnemonic } @@ -225,6 +225,7 @@

please download zcn.wasm from https://github.com/0chain/gosdk/releases/lates 'https://0box.' + network, //zboxHost 'vult', //zboxAppType 3, + false ] const bls = window.bls @@ -233,9 +234,12 @@

please download zcn.wasm from https://github.com/0chain/gosdk/releases/lates await wasm.sdk.init(...config) await bls.init(bls.BN254) const { clientID, privateKey, publicKey, mnemonic } = getWallet() - await wasm.setWallet(bls, clientID, privateKey, publicKey, mnemonic) + console.log("setWalet") + await wasm.setWallet(bls, clientID,publicKey,"", privateKey, publicKey, mnemonic,false) goWasm = wasm + }).catch(e => { + console.log(e) }) onClick('btnSetWallet', async () => { diff --git a/wasmsdk/demo/zcn.js b/wasmsdk/demo/zcn.js index 468890b05..ee4f44e96 100644 --- a/wasmsdk/demo/zcn.js +++ b/wasmsdk/demo/zcn.js @@ -15,67 +15,26 @@ * along with this program. If not, see . */ -'use strict'; +'use strict' -const g = window; +const g = window function hexStringToByte(str) { - if (!str) return new Uint8Array(); + if (!str) return new Uint8Array() - const a = []; + const a = [] for (let i = 0, len = str.length; i < len; i += 2) { - a.push(parseInt(str.substr(i, 2), 16)); + a.push(parseInt(str.substr(i, 2), 16)) } - return new Uint8Array(a); -} - -function blsSign(hash, secretKey) { - const { jsProxy } = g.__zcn_wasm__; - - if (!jsProxy || !secretKey) { - const errMsg = 'err: bls.secretKey is not initialized'; - console.warn(errMsg); - throw new Error(errMsg); - } - - const bytes = hexStringToByte(hash); - const sk = bls.deserializeHexStrToSecretKey(secretKey); - const sig = sk.sign(bytes); - - if (!sig) { - const errMsg = 'err: wasm blsSign function failed to sign transaction'; - console.warn(errMsg); - throw new Error(errMsg); - } - - return sig.serializeToHexStr(); + return new Uint8Array(a) } async function createObjectURL(buf, mimeType) { - var blob = new Blob([buf], { type: mimeType }); - return URL.createObjectURL(blob); + var blob = new Blob([buf], { type: mimeType }) + return URL.createObjectURL(blob) } -const readChunk = (offset, chunkSize, file) => - new Promise((res, rej) => { - const fileReader = new FileReader(); - const blob = file.slice(offset, chunkSize + offset); - fileReader.onload = (e) => { - const t = e.target; - if (t.error == null) { - res({ - size: t.result.byteLength, - buffer: new Uint8Array(t.result), - }); - } else { - rej(t.error); - } - }; - - fileReader.readAsArrayBuffer(blob); - }); - /** * Sleep is used when awaiting for Go Wasm to initialize. * It uses the lowest possible sane delay time (via requestAnimationFrame). @@ -87,10 +46,10 @@ const readChunk = (offset, chunkSize, file) => * completed. */ const sleep = (ms = 1000) => - new Promise((res) => { - requestAnimationFrame(res); - setTimeout(res, ms); - }); + new Promise(res => { + requestAnimationFrame(res) + setTimeout(res, ms) + }) /** * The maximum amount of time that we would expect Wasm to take to initialize. @@ -98,13 +57,11 @@ const sleep = (ms = 1000) => * Most likely something has gone wrong if it takes more than 3 seconds to * initialize. */ -const maxTime = 10 * 1000; +const maxTime = 10 * 1000 // Initialize __zcn_wasm__ -g.__zcn_wasm__ = g.__zcn_wasm_ || { - glob: { - index: 0, - }, +g.__zcn_wasm__ = { + glob: { index: 0 }, jsProxy: { secretKey: null, publicKey: null, @@ -115,12 +72,45 @@ g.__zcn_wasm__ = g.__zcn_wasm_ || { sleep, }, sdk: {}, //proxy object for go to expose its methods -}; +} /** * bridge is an easier way to refer to the Go WASM object. */ -const bridge = g.__zcn_wasm__; +const bridge = g.__zcn_wasm__ + +const readChunk = (offset, chunkSize, file) => + new Promise((res, rej) => { + const fileReader = new FileReader() + const blob = file.slice(offset, chunkSize + offset) + fileReader.onload = e => { + const t = e.target + if (t.error == null) { + res({ + size: t.result.byteLength, + buffer: new Uint8Array(t.result), + }) + } else { + rej(t.error) + } + } + + fileReader.readAsArrayBuffer(blob) + }) + +async function md5Hash(file) { + const result = new Promise((resolve, reject) => { + const worker = new Worker('md5worker.js') + worker.postMessage(file) + worker.onmessage = e => { + resolve(e.data) + worker.terminate() + } + worker.onerror = reject + }) + + return result +} // bulk upload files with FileReader // objects: the list of upload object @@ -134,29 +124,40 @@ const bridge = g.__zcn_wasm__; // - numBlocks: int // - callback: function(totalBytes,completedBytes,error) async function bulkUpload(options) { - const start = bridge.glob.index; - const opts = options.map((obj) => { - const i = bridge.glob.index; - bridge.glob.index++; - const readChunkFuncName = '__zcn_upload_reader_' + i.toString(); - const callbackFuncName = '__zcn_upload_callback_' + i.toString(); - var md5HashFuncName = ''; + console.log('bulkUpload') + const start = bridge.glob.index + const opts = options.map(obj => { + const i = bridge.glob.index + bridge.glob.index++ + const readChunkFuncName = '__zcn_upload_reader_' + i.toString() + const callbackFuncName = '__zcn_upload_callback_' + i.toString() + let md5HashFuncName = '' + g[readChunkFuncName] = async (offset, chunkSize) => { - const chunk = await readChunk(offset, chunkSize, obj.file); - return chunk.buffer; - }; + console.log( + 'bulk_upload: read chunk remotePath:' + + obj.remotePath + + ' offset:' + + offset + + ' chunkSize:' + + chunkSize + ) + const chunk = await readChunk(offset, chunkSize, obj.file) + return chunk.buffer + } + if (obj.file.size > 25 * 1024 * 1024) { - md5HashFuncName = '__zcn_md5_hash_' + i.toString(); - const md5Res = md5Hash(obj.file); + md5HashFuncName = '__zcn_md5_hash_' + i.toString() + const md5Res = md5Hash(obj.file) g[md5HashFuncName] = async () => { - const hash = await md5Res; - return hash; - }; + const hash = await md5Res + return hash + } } if (obj.callback) { g[callbackFuncName] = async (totalBytes, completedBytes, error) => - obj.callback(totalBytes, completedBytes, error); + obj.callback(totalBytes, completedBytes, error) } return { @@ -164,7 +165,7 @@ async function bulkUpload(options) { remotePath: obj.remotePath, readChunkFuncName: readChunkFuncName, fileSize: obj.file.size, - thumbnailBytes: obj.thumbnailBytes ? obj.thumbnailBytes.toString() : '', + thumbnailBytes: Array.from(obj?.thumbnailBytes || []).toString(), encrypt: obj.encrypt, webstreaming: obj.webstreaming, isUpdate: obj.isUpdate, @@ -172,197 +173,258 @@ async function bulkUpload(options) { numBlocks: obj.numBlocks, callbackFuncName: callbackFuncName, md5HashFuncName: md5HashFuncName, - }; - }); + } + }) - // md5Hash(options[0].file).then(hash=>{ - // console.log("md5 hash: ",hash) - // }).catch(err=>{ - // console.log("md5 hash error: ",err) - // }) + const end = bridge.glob.index - const end = bridge.glob.index; - const result = await bridge.__proxy__.sdk.multiUpload(JSON.stringify(opts)); + const result = await bridge.__proxy__.sdk.multiUpload(JSON.stringify(opts)) for (let i = start; i < end; i++) { - g['__zcn_upload_reader_' + i.toString()] = null; - g['__zcn_upload_callback_' + i.toString()] = null; - g['__zcn_md5_hash_' + i.toString()] = null; + g['__zcn_upload_reader_' + i.toString()] = null + g['__zcn_upload_callback_' + i.toString()] = null } - return result; -} - -async function md5Hash(file) { - const result = new Promise((resolve, reject) => { - const worker = new Worker('md5worker.js'); - worker.postMessage(file); - worker.onmessage = (e) => { - resolve(e.data); - worker.terminate(); - }; - worker.onerror = reject; - }); - return result; + return result } -async function blsSign(hash, secretKey) { - if (!bridge.jsProxy && !secretKey) { - const errMsg = 'err: bls.secretKey is not initialized'; - console.warn(errMsg); - throw new Error(errMsg); +async function blsSign(hash) { + if (!bridge.jsProxy && !bridge.jsProxy.secretKey) { + const errMsg = 'err: bls.secretKey is not initialized' + console.warn(errMsg) + throw new Error(errMsg) } - const bytes = hexStringToByte(hash); - const sk = bls.deserializeHexStrToSecretKey(secretKey); - const sig = sk.sign(bytes); + const bytes = hexStringToByte(hash) + + const sig = bridge.jsProxy.secretKey.sign(bytes) if (!sig) { - const errMsg = 'err: wasm blsSign function failed to sign transaction'; - console.warn(errMsg); - throw new Error(errMsg); + const errMsg = 'err: wasm blsSign function failed to sign transaction' + console.warn(errMsg) + throw new Error(errMsg) } - return sig.serializeToHexStr(); + return sig.serializeToHexStr() } async function blsVerifyWith(pk, signature, hash) { - const publicKey = bls.deserializeHexStrToPublicKey(pk); - const bytes = hexStringToByte(hash); - const sig = bls.deserializeHexStrToSignature(signature); - return publicKey.verify(sig, bytes); + const publicKey = bridge.jsProxy.bls.deserializeHexStrToPublicKey(pk) + const bytes = hexStringToByte(hash) + const sig = bridge.jsProxy.bls.deserializeHexStrToSignature(signature) + return publicKey.verify(sig, bytes) } async function blsVerify(signature, hash) { if (!bridge.jsProxy && !bridge.jsProxy.publicKey) { - const errMsg = 'err: bls.publicKey is not initialized'; - console.warn(errMsg); - throw new Error(errMsg); + const errMsg = 'err: bls.publicKey is not initialized' + console.warn(errMsg) + throw new Error(errMsg) } - const bytes = hexStringToByte(hash); - const sig = bridge.jsProxy.bls.deserializeHexStrToSignature(signature); - return bridge.jsProxy.publicKey.verify(sig, bytes); + const bytes = hexStringToByte(hash) + const sig = bridge.jsProxy.bls.deserializeHexStrToSignature(signature) + return bridge.jsProxy.publicKey.verify(sig, bytes) } -async function setWallet(bls, clientID, sk, pk, mnemonic) { - if (!bls) throw new Error('bls is undefined, on wasm setWallet fn'); - if (!sk) throw new Error('secret key is undefined, on wasm setWallet fn'); - if (!pk) throw new Error('public key is undefined, on wasm setWallet fn'); - - if (bridge.walletId != clientID) { - console.log('setWallet: ', clientID, sk, pk); - bridge.jsProxy.bls = bls; - bridge.jsProxy.publicKey = bls.deserializeHexStrToPublicKey(pk); +async function setWallet( + bls, + clientID, + clientKey, + peerPublicKey, + sk, + pk, + mnemonic, + isSplit +) { + if (!bls) throw new Error('bls is undefined, on wasm setWallet fn') + if (!sk) throw new Error('secret key is undefined, on wasm setWallet fn') + if (!pk) throw new Error('public key is undefined, on wasm setWallet fn') + if (isSplit && !clientKey) + throw new Error('clientKey is undefined, on wasm setWallet fn') + + if ( + bridge.walletId != clientID || + bridge.jsProxy.pubkeyStr != pk || + bridge.jsProxy.isSplit != isSplit + ) { + bridge.jsProxy.bls = bls + bridge.jsProxy.secretKey = bls.deserializeHexStrToSecretKey(sk) + bridge.jsProxy.publicKey = bls.deserializeHexStrToPublicKey(pk) + bridge.jsProxy.pubkeyStr = pk + bridge.jsProxy.isSplit = isSplit // use proxy.sdk to detect if sdk is ready - await bridge.__proxy__.sdk.setWallet(clientID, pk, sk, mnemonic); - bridge.walletId = clientID; + await bridge.__proxy__.sdk.setWallet( + clientID, + clientKey, + peerPublicKey, + pk, + sk, + mnemonic, + isSplit + ) + bridge.walletId = clientID + bridge.secretKey = sk + bridge.peerPublicKey = peerPublicKey } } +function getWalletId() { + return bridge.walletId +} + +function getPrivateKey() { + return bridge.secretKey +} + +function getPeerPublicKey() { + return bridge.peerPublicKey +} + async function loadWasm(go) { // If instantiateStreaming doesn't exists, polyfill/create it on top of instantiate if (!WebAssembly?.instantiateStreaming) { WebAssembly.instantiateStreaming = async (resp, importObject) => { - const source = await (await resp).arrayBuffer(); - return await WebAssembly.instantiate(source, importObject); - }; + const source = await (await resp).arrayBuffer() + return await WebAssembly.instantiate(source, importObject) + } + } + if (!g.__zcn_wasm__) { + console.log('g.__zcn_wasm__ is not initialized') + return } - const result = await WebAssembly.instantiateStreaming( - await fetch('zcn.wasm'), - go.importObject - ); + let suffix = 'mainnet' + const currentLocation = window?.location?.hostname + if ( + currentLocation?.includes('localhost') || + currentLocation?.includes('mob') || + currentLocation?.includes('desktop') + ) { + suffix = 'mob' + } else if (currentLocation?.includes('dev')) { + suffix = 'dev' + } else if (currentLocation?.includes('demo')) { + suffix = 'demo' + } else if (currentLocation?.includes('staging')) { + suffix = 'staging' + } else if (currentLocation?.includes('test')) { + suffix = 'test' + } + + const wasmPath = '/zcn.wasm' + let wasmUrl = `https://d2os1u2xwjukgsdradds.cloudfront.net/${suffix}/zcn.wasm` + + let source = await fetch(wasmUrl) + .then(res => res) + .catch(err => err) + // fallback to webapps server + if (!source?.ok) { + wasmUrl = '/wasmsdk/demo/zcn.wasm' + source = await fetch(wasmUrl).then(res => res) + } + + // cache wasm response + const cacheWasm = await caches.open('wasm-cache') + await cacheWasm.put(wasmPath, source.clone()) + + // set SUFFIX env variable in gosdk + go.env = { SUFFIX: suffix } + + // initiate wasm + const result = await WebAssembly.instantiateStreaming(source, go.importObject) setTimeout(() => { if (g.__zcn_wasm__?.__wasm_initialized__ !== true) { console.warn( 'wasm window.__zcn_wasm__ (zcn.__wasm_initialized__) still not true after max time' - ); + ) } - }, maxTime); + }, maxTime) - go.run(result.instance); + go.run(result.instance) } async function createWasm() { if (bridge.__proxy__) { - return bridge.__proxy__; + return bridge.__proxy__ } - const go = new g.Go(); - go.env = { SUFFIX: 'dev' }; + const go = new g.Go() - loadWasm(go); + loadWasm(go) const sdkGet = (_, key) => (...args) => // eslint-disable-next-line - new Promise(async (resolve, reject) => { + new Promise(async (resolve, reject) => { if (!go || go.exited) { - return reject(new Error('The Go instance is not active.')); + return reject(new Error('The Go instance is not active.')) } while (bridge.__wasm_initialized__ !== true) { - await sleep(1000); + await sleep(1000) } if (typeof bridge.sdk[key] !== 'function') { - resolve(bridge.sdk[key]); + resolve(bridge.sdk[key]) if (args.length !== 0) { reject( new Error( 'Retrieved value from WASM returned function type, however called with arguments.' ) - ); + ) } - return; + return } try { - let resp = bridge.sdk[key].apply(undefined, args); + let resp = bridge.sdk[key].apply(undefined, args) // support wasm.BindAsyncFunc if (resp && typeof resp.then === 'function') { - resp = await Promise.race([resp]); + resp = await Promise.race([resp]) } if (resp && resp.error) { - reject(resp.error); + reject(resp.error) } else { - resolve(resp); + resolve(resp) } } catch (e) { - reject(e); + reject(e) } - }); + }) const sdkProxy = new Proxy( {}, { get: sdkGet, } - ); + ) const jsProxy = new Proxy( {}, { get: (_, key) => bridge.jsProxy[key], set: (_, key, value) => { - bridge.jsProxy[key] = value; + bridge.jsProxy[key] = value }, } - ); + ) const proxy = { - bulkUpload: bulkUpload, - setWallet: setWallet, + bulkUpload, + setWallet, + getWalletId, + getPrivateKey, + getPeerPublicKey, sdk: sdkProxy, //expose sdk methods for js jsProxy, //expose js methods for go - }; + } - bridge.__proxy__ = proxy; + bridge.__proxy__ = proxy - return proxy; + return proxy } diff --git a/wasmsdk/jsbridge/template_data.go b/wasmsdk/jsbridge/template_data.go index cc3c2c7d7..d9512d9d7 100644 --- a/wasmsdk/jsbridge/template_data.go +++ b/wasmsdk/jsbridge/template_data.go @@ -51,7 +51,7 @@ func buildJS(args, env []string, wasmPath string, tpl []byte) (string, error) { if suffix == "" { suffix = "dev" } - cdnPath := fmt.Sprintf("https://d2os1u2xwjukgr.cloudfront.net/%s/zcn.wasm", suffix) + cdnPath := fmt.Sprintf("https://d2os1u2xwjukgrs.cloudfront.net/%s/zcn.wasm", suffix) data := templateData{ Path: cdnPath, Args: args, diff --git a/zboxcore/allocationchange/change.go b/zboxcore/allocationchange/change.go index 56fcd9b04..f5ab83508 100644 --- a/zboxcore/allocationchange/change.go +++ b/zboxcore/allocationchange/change.go @@ -1,6 +1,7 @@ package allocationchange import ( + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/gosdk/zboxcore/fileref" ) @@ -16,6 +17,12 @@ type AllocationChange interface { GetSize() int64 } +type AllocationChangeV2 interface { + ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error + GetLookupHash(changeIndex uint64) string + GetHash(changeIndex uint64, id string) string +} + type EmptyFileChange struct { change } diff --git a/zboxcore/blockchain/entity.go b/zboxcore/blockchain/entity.go index 8ea74175a..2e3fde348 100644 --- a/zboxcore/blockchain/entity.go +++ b/zboxcore/blockchain/entity.go @@ -8,6 +8,7 @@ import ( "sync/atomic" "github.com/0chain/gosdk/core/util" + "github.com/0chain/gosdk/zboxcore/marker" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/node" @@ -86,7 +87,8 @@ type UpdateValidationNode struct { type StorageNode struct { ID string `json:"id"` Baseurl string `json:"url"` - AllocationRoot string `json:"allocation_root"` + AllocationRoot string `json:"-"` + LatestWM *marker.WriteMarker skip uint64 `json:"-"` // skip on error } diff --git a/zboxcore/fileref/fileref.go b/zboxcore/fileref/fileref.go index 42995a765..63967ad33 100644 --- a/zboxcore/fileref/fileref.go +++ b/zboxcore/fileref/fileref.go @@ -250,6 +250,14 @@ func (fr *FileRef) GetFileMetaHashData() string { fr.ActualFileSize, fr.ActualFileHash) } +func (fr *FileRef) GetFileMetaHashDataV2() string { + return fmt.Sprintf( + "%s:%s:%d:%d:%s", + fr.AllocationID, + fr.Path, fr.Size, + fr.ActualFileSize, fr.ActualFileHash) +} + func (fr *FileRef) GetHashData() string { return fmt.Sprintf( "%s:%s:%s:%s:%d:%s:%s:%d:%s:%d:%s", @@ -267,6 +275,10 @@ func (fr *FileRef) GetHashData() string { ) } +func (fr *FileRef) GetFileHashDataV2(blobberID string) string { + return fmt.Sprintf("%s:%s:%s:%d:%s:%d:%s", blobberID, fr.AllocationID, fr.Path, fr.Size, fr.FixedMerkleRoot, fr.ActualFileSize, fr.ActualFileHash) +} + func (fr *FileRef) GetHash() string { return fr.Hash } @@ -278,6 +290,10 @@ func (fr *FileRef) CalculateHash() string { return fr.Hash } +func (fr *FileRef) GetFileMetaHashV2() []byte { + return encryption.RawHash(fr.GetFileMetaHashDataV2()) +} + func (fr *FileRef) GetType() string { return fr.Type } diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 567f4b499..ee2525369 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "io/ioutil" + "math" "mime/multipart" "net/http" "strconv" @@ -15,6 +16,7 @@ import ( "sync" "time" + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/errors" thrown "github.com/0chain/errors" "github.com/0chain/gosdk/zboxcore/allocationchange" @@ -34,6 +36,12 @@ type ReferencePathResult struct { Version string `json:"version"` } +type ReferencePathResultV2 struct { + Path []byte `json:"path"` + LatestWM *marker.WriteMarker `json:"latest_write_marker"` + Version string `json:"version"` +} + type CommitResult struct { Success bool `json:"success"` ErrorMessage string `json:"error_msg,omitempty"` @@ -64,19 +72,39 @@ type CommitRequest struct { blobberInd uint64 } -var commitChan map[string]chan *CommitRequest -var initCommitMutex sync.Mutex +type CommitRequestInterface interface { + processCommit() + blobberID() string +} + +type CommitRequestV2 struct { + changes []allocationchange.AllocationChangeV2 + allocationObj *Allocation + connectionID string + sig string + wg *sync.WaitGroup + result *CommitResult + timestamp int64 + consensusThresh int + commitMask zboxutil.Uint128 + changeIndex uint64 +} + +var ( + commitChan map[string]chan CommitRequestInterface + initCommitMutex sync.Mutex +) func InitCommitWorker(blobbers []*blockchain.StorageNode) { initCommitMutex.Lock() defer initCommitMutex.Unlock() if commitChan == nil { - commitChan = make(map[string]chan *CommitRequest) + commitChan = make(map[string]chan CommitRequestInterface) } for _, blobber := range blobbers { if _, ok := commitChan[blobber.ID]; !ok { - commitChan[blobber.ID] = make(chan *CommitRequest, 1) + commitChan[blobber.ID] = make(chan CommitRequestInterface, 1) blobberChan := commitChan[blobber.ID] go startCommitWorker(blobberChan, blobber.ID) } @@ -84,7 +112,7 @@ func InitCommitWorker(blobbers []*blockchain.StorageNode) { } -func startCommitWorker(blobberChan chan *CommitRequest, blobberID string) { +func startCommitWorker(blobberChan chan CommitRequestInterface, blobberID string) { for { commitreq, open := <-blobberChan if !open { @@ -97,6 +125,10 @@ func startCommitWorker(blobberChan chan *CommitRequest, blobberID string) { delete(commitChan, blobberID) } +func (commitreq *CommitRequest) blobberID() string { + return commitreq.blobber.ID +} + func (commitreq *CommitRequest) processCommit() { defer commitreq.wg.Done() start := time.Now() @@ -346,38 +378,177 @@ func (req *CommitRequest) commitBlobber( return thrown.New("commit_error", fmt.Sprintf("Commit failed with response status %d", resp.StatusCode)) } -func AddCommitRequest(req *CommitRequest) { - commitChan[req.blobber.ID] <- req +func AddCommitRequest(req CommitRequestInterface) { + commitChan[req.blobberID()] <- req } -func (commitreq *CommitRequest) calculateHashRequest(ctx context.Context, paths []string) error { //nolint - var req *http.Request - req, err := zboxutil.NewCalculateHashRequest(commitreq.blobber.Baseurl, commitreq.allocationID, commitreq.allocationTx, paths) - if err != nil || len(paths) == 0 { - l.Logger.Error("Creating calculate hash req", err) - return err +func (commitReq *CommitRequestV2) blobberID() string { + var pos uint64 + for i := commitReq.commitMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + commitReq.changeIndex = pos + return commitReq.allocationObj.Blobbers[pos].ID } - ctx, cncl := context.WithTimeout(ctx, (time.Second * 30)) - err = zboxutil.HttpDo(ctx, cncl, req, func(resp *http.Response, err error) error { + // we should never reach here + return "" +} + +func (commitReq *CommitRequestV2) processCommit() { + defer commitReq.wg.Done() + l.Logger.Debug("received a commit request") + paths := make([]string, 0) + changeIndex := commitReq.changeIndex + for i := 0; i < len(commitReq.changes); i++ { + lookupHash := commitReq.changes[i].GetLookupHash(changeIndex) + if lookupHash != "" { + paths = append(paths, lookupHash) + } else { + commitReq.changes[i] = nil + } + } + if len(paths) == 0 { + l.Logger.Debug("Nothing to commit") + commitReq.result = SuccessCommitResult() + return + } + var ( + trie *wmpt.WeightedMerkleTrie + err error + pos uint64 + ) + + for i := commitReq.commitMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + blobber := commitReq.allocationObj.Blobbers[pos] + trie, err = getReferencePathV2(blobber, commitReq.allocationObj.ID, commitReq.allocationObj.Tx, commitReq.sig, paths) if err != nil { - l.Logger.Error("Calculate hash error:", err) - return err + commitReq.commitMask = commitReq.commitMask.And(zboxutil.NewUint128(1).Lsh(pos).Not()) + l.Logger.Error("Error getting reference path: ", err) + continue } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - l.Logger.Error("Calculate hash response : ", resp.StatusCode) + changeIndex = pos + break + } + if trie == nil { + commitReq.result = ErrorCommitResult("Failed to get reference path") + return + } + if commitReq.commitMask.CountOnes() < commitReq.consensusThresh { + commitReq.result = ErrorCommitResult("Failed to get reference path") + return + } + + for _, change := range commitReq.changes { + if change == nil { + continue } - resp_body, err := ioutil.ReadAll(resp.Body) + err = change.ProcessChangeV2(trie, changeIndex) + if err != nil { + commitReq.result = ErrorCommitResult("Failed to process change " + err.Error()) + return + } + } + rootHash := trie.GetRoot().CalcHash() + rootWeight := trie.Weight() + + pos = 0 + wg := sync.WaitGroup{} + errSlice := make([]error, commitReq.commitMask.CountOnes()) + counter := 0 + mu := sync.Mutex{} + for i := commitReq.commitMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + blobber := commitReq.allocationObj.Blobbers[pos] + blobberPos := pos + wg.Add(1) + go func(ind int) { + defer wg.Done() + err = commitReq.commitBlobber(rootHash, rootWeight, blobberPos, blobber) + if err != nil { + errSlice[ind] = err + mu.Lock() + commitReq.commitMask = commitReq.commitMask.And(zboxutil.NewUint128(1).Lsh(pos).Not()) + mu.Unlock() + return + } + }(counter) + counter++ + } + wg.Wait() + if commitReq.commitMask.CountOnes() < commitReq.consensusThresh { + err = zboxutil.MajorError(errSlice) + if err == nil { + err = errors.New("consensus_not_met", fmt.Sprintf("Successfully committed to %d blobbers, but required %d", commitReq.commitMask.CountOnes(), commitReq.consensusThresh)) + } + commitReq.result = ErrorCommitResult(err.Error()) + return + } + commitReq.allocationObj.allocationRoot = hex.EncodeToString(rootHash) + commitReq.result = SuccessCommitResult() +} + +func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, changeIndex uint64, blobber *blockchain.StorageNode) (err error) { + hashSignatureMap := make(map[string]string) + for _, change := range req.changes { + if change == nil { + continue + } + hash := change.GetHash(changeIndex, blobber.ID) + if hash == "" { + return errors.New("hash_signature_failed", "Failed to add hash signature") + } + sig, err := client.Sign(hash) if err != nil { - l.Logger.Error("Calculate hash: Resp", err) return err } - if resp.StatusCode != http.StatusOK { - return errors.New(strconv.Itoa(resp.StatusCode), fmt.Sprintf("Calculate hash error response: Body: %s ", string(resp_body))) + hashSignatureMap[change.GetLookupHash(changeIndex)] = sig + } + hasher := sha256.New() + var prevChainSize int64 + if blobber.LatestWM != nil { + prevChainHash, err := hex.DecodeString(blobber.LatestWM.ChainHash) + if err != nil { + return err } - return nil - }) - return err + hasher.Write(prevChainHash) //nolint:errcheck + prevChainSize = numBlocks(blobber.LatestWM.ChainSize) + } + hasher.Write(rootHash) //nolint:errcheck + chainHash := hex.EncodeToString(hasher.Sum(nil)) + allocationRoot := hex.EncodeToString(rootHash) + wm := &marker.WriteMarker{} + wm.AllocationRoot = allocationRoot + wm.Size = (int64(rootWeight) - prevChainSize) * CHUNK_SIZE + wm.ChainHash = chainHash + wm.ChainSize = int64(rootWeight) * CHUNK_SIZE + if blobber.LatestWM != nil { + wm.PreviousAllocationRoot = blobber.LatestWM.AllocationRoot + } + wm.BlobberID = blobber.ID + wm.Timestamp = req.timestamp + wm.AllocationID = req.allocationObj.ID + wm.FileMetaRoot = allocationRoot + wm.ClientID = client.GetClientID() + err = wm.Sign() + if err != nil { + return err + } + wmData, err := json.Marshal(wm) + if err != nil { + return err + } + hashSignatureData, err := json.Marshal(hashSignatureMap) + if err != nil { + return err + } + + err = submitWriteMarker(wmData, hashSignatureData, blobber, req.connectionID, req.allocationObj.ID, req.allocationObj.Tx) + if err != nil { + return err + } + blobber.LatestWM = wm + blobber.AllocationRoot = allocationRoot + return } func getFormWritter(connectionID string, wmData, fileIDMetaData []byte, body *bytes.Buffer) (*multipart.Writer, error) { @@ -399,3 +570,153 @@ func getFormWritter(connectionID string, wmData, fileIDMetaData []byte, body *by formWriter.Close() return formWriter, nil } + +func getReferencePathV2(blobber *blockchain.StorageNode, allocationID, allocationTx, sig string, paths []string) (*wmpt.WeightedMerkleTrie, error) { + req, err := zboxutil.NewReferencePathRequestV2(blobber.Baseurl, allocationID, allocationTx, sig, paths) + if err != nil { + l.Logger.Error("Creating ref path req", err) + return nil, err + } + var lR ReferencePathResultV2 + ctx, cncl := context.WithTimeout(context.Background(), (time.Second * 30)) + err = zboxutil.HttpDo(ctx, cncl, req, func(resp *http.Response, err error) error { + if err != nil { + l.Logger.Error("Ref path error:", err) + return err + } + defer resp.Body.Close() + respBody, err := io.ReadAll(resp.Body) + if err != nil { + l.Logger.Error("Ref path: Resp", err) + return err + } + if resp.StatusCode != http.StatusOK { + return errors.New( + strconv.Itoa(resp.StatusCode), + fmt.Sprintf("Reference path error response: Status: %d - %s ", + resp.StatusCode, string(respBody))) + } + err = json.Unmarshal(respBody, &lR) + if err != nil { + l.Logger.Error("Reference path json decode error: ", err) + return err + } + return nil + }) + + if err != nil { + return nil, err + } + + trie := wmpt.New(nil, nil) + if lR.LatestWM != nil { + err = lR.LatestWM.VerifySignature(client.GetClientPublicKey()) + if err != nil { + return nil, errors.New("signature_verification_failed", err.Error()) + } + err = trie.Deserialize(lR.Path) + if err != nil { + return nil, err + } + chainBlocks := numBlocks(lR.LatestWM.ChainSize) + if trie.Weight() != uint64(chainBlocks) { + return nil, errors.New("chain_length_mismatch", fmt.Sprintf("Expected chain length %d, got %d", chainBlocks, trie.Weight())) + } + if hex.EncodeToString(trie.Root()) != lR.LatestWM.AllocationRoot { + return nil, errors.New("allocation_root_mismatch", fmt.Sprintf("Expected allocation root %s, got %s", lR.LatestWM.AllocationRoot, hex.EncodeToString(trie.Root()))) + } + } + + return trie, nil +} + +func submitWriteMarker(wmData, metaData []byte, blobber *blockchain.StorageNode, connectionID, allocationID, allocationTx string) (err error) { + var ( + resp *http.Response + shouldContinue bool + ) + for retries := 0; retries < 6; retries++ { + err, shouldContinue = func() (err error, shouldContinue bool) { + body := new(bytes.Buffer) + formWriter, err := getFormWritter(connectionID, wmData, metaData, body) + if err != nil { + l.Logger.Error("Creating form writer failed: ", err) + return + } + httpreq, err := zboxutil.NewCommitRequest(blobber.Baseurl, allocationID, allocationTx, body) + if err != nil { + l.Logger.Error("Error creating commit req: ", err) + return + } + httpreq.Header.Add("Content-Type", formWriter.FormDataContentType()) + reqCtx, ctxCncl := context.WithTimeout(context.Background(), time.Second*60) + resp, err = zboxutil.Client.Do(httpreq.WithContext(reqCtx)) + defer ctxCncl() + + if err != nil { + logger.Logger.Error("Commit: ", err) + return + } + + if resp.Body != nil { + defer resp.Body.Close() + } + + var respBody []byte + respBody, err = io.ReadAll(resp.Body) + if err != nil { + logger.Logger.Error("Response read: ", err) + return + } + if resp.StatusCode == http.StatusOK { + logger.Logger.Debug(blobber.Baseurl, " committed") + return + } + + if resp.StatusCode == http.StatusTooManyRequests { + logger.Logger.Debug(blobber.Baseurl, + " got too many request error. Retrying") + + var r int + r, err = zboxutil.GetRateLimitValue(resp) + if err != nil { + logger.Logger.Error(err) + return + } + + time.Sleep(time.Duration(r) * time.Second) + shouldContinue = true + return + } + + if strings.Contains(string(respBody), "pending_markers:") { + logger.Logger.Debug("Commit pending for blobber ", + blobber.Baseurl, " Retrying") + time.Sleep(5 * time.Second) + shouldContinue = true + return + } + + if strings.Contains(string(respBody), "chain_length_exceeded") { + l.Logger.Error("Chain length exceeded for blobber ", + blobber.Baseurl, " Retrying") + time.Sleep(5 * time.Second) + shouldContinue = true + return + } + + err = thrown.New("commit_error", + fmt.Sprintf("Got error response %s with status %d", respBody, resp.StatusCode)) + return + }() + if shouldContinue { + continue + } + return + } + return thrown.New("commit_error", fmt.Sprintf("Commit failed with response status %d", resp.StatusCode)) +} + +func numBlocks(size int64) int64 { + return int64(math.Ceil(float64(size*1.0) / CHUNK_SIZE)) +} diff --git a/zboxcore/sdk/copyworker.go b/zboxcore/sdk/copyworker.go index 25d9dd06d..4310e7e30 100644 --- a/zboxcore/sdk/copyworker.go +++ b/zboxcore/sdk/copyworker.go @@ -11,6 +11,7 @@ import ( "sync" "time" + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/errors" "github.com/google/uuid" @@ -408,3 +409,15 @@ func NewCopyOperation(remotePath string, destPath string, copyMask zboxutil.Uint return co } + +func (co *CopyOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { + return nil +} + +func (co *CopyOperation) GetLookupHash(changeIndex uint64) string { + return "" +} + +func (co *CopyOperation) GetHash(changeIndex uint64, id string) string { + return "" +} diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index c04f3a693..77992f10e 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -11,6 +11,7 @@ import ( "sync/atomic" "time" + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/errors" thrown "github.com/0chain/errors" "github.com/google/uuid" @@ -454,3 +455,15 @@ func NewDeleteOperation(remotePath string, deleteMask zboxutil.Uint128, maskMu * dop.ctx, dop.ctxCncl = context.WithCancel(ctx) return dop } + +func (dop *DeleteOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { + return nil +} + +func (dop *DeleteOperation) GetLookupHash(changeIndex uint64) string { + return "" +} + +func (dop *DeleteOperation) GetHash(changeIndex uint64, id string) string { + return "" +} diff --git a/zboxcore/sdk/dirworker.go b/zboxcore/sdk/dirworker.go index 27d2c5f93..faca09211 100644 --- a/zboxcore/sdk/dirworker.go +++ b/zboxcore/sdk/dirworker.go @@ -12,6 +12,7 @@ import ( "sync" "time" + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/errors" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/util" @@ -371,3 +372,15 @@ func NewDirOperation(remotePath, customMeta string, dirMask zboxutil.Uint128, ma dirOp.alreadyExists = make(map[uint64]bool) return dirOp } + +func (dirOp *DirOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { + return nil +} + +func (dirOp *DirOperation) GetLookupHash(changeIndex uint64) string { + return "" +} + +func (dirOp *DirOperation) GetHash(changeIndex uint64, id string) string { + return "" +} diff --git a/zboxcore/sdk/moveworker.go b/zboxcore/sdk/moveworker.go index 69455c769..4d32d3201 100644 --- a/zboxcore/sdk/moveworker.go +++ b/zboxcore/sdk/moveworker.go @@ -11,6 +11,7 @@ import ( "sync" "time" + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/errors" thrown "github.com/0chain/errors" "github.com/google/uuid" @@ -398,3 +399,15 @@ func NewMoveOperation(remotePath string, destPath string, moveMask zboxutil.Uint mo.ctx, mo.ctxCncl = context.WithCancel(ctx) return mo } + +func (mo *MoveOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { + return nil +} + +func (mo *MoveOperation) GetLookupHash(changeIndex uint64) string { + return "" +} + +func (mo *MoveOperation) GetHash(changeIndex uint64, id string) string { + return "" +} diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 4b50baefd..4ba6df132 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -10,6 +10,7 @@ import ( "sync" "time" + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/errors" "github.com/remeh/sizedwaitgroup" @@ -47,6 +48,9 @@ type Operationer interface { Verify(allocObj *Allocation) error Completed(allocObj *Allocation) Error(allocObj *Allocation, consensus int, err error) + ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error + GetLookupHash(changeIndex uint64) string + GetHash(changeIndex uint64, id string) string } type MultiOperation struct { @@ -190,10 +194,19 @@ func (mo *MultiOperation) Process() error { return } mo.maskMU.Lock() - mo.operationMask = mo.operationMask.Or(mask) - mo.maskMU.Unlock() - changes := op.buildChange(refs, uid) - mo.changes[idx] = changes + if mo.allocationObj.StorageVersion == StorageV2 { + if mo.isRepair { + mo.operationMask = mo.operationMask.Or(mask) + } else { + mo.operationMask = mo.operationMask.And(mask) + } + mo.maskMU.Unlock() + } else { + mo.operationMask = mo.operationMask.Or(mask) + mo.maskMU.Unlock() + changes := op.buildChange(refs, uid) + mo.changes[idx] = changes + } }(op, idx) } swg.Wait() @@ -219,7 +232,9 @@ func (mo *MultiOperation) Process() error { // But we want mo.changes[0] to have allocationChange for blobber 1 and mo.changes[1] to have allocationChange for // blobber 2 and so on. start := time.Now() - mo.changes = zboxutil.Transpose(mo.changes) + if mo.allocationObj.StorageVersion != StorageV2 { + mo.changes = zboxutil.Transpose(mo.changes) + } writeMarkerMutex, err := CreateWriteMarkerMutex(client.GetClient(), mo.allocationObj) if err != nil { @@ -354,3 +369,74 @@ func (mo *MultiOperation) Process() error { return nil } + +func (mo *MultiOperation) commitV2() error { + + rootMap := make(map[string]zboxutil.Uint128) + var pos uint64 + for i := mo.operationMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + rootMap[mo.allocationObj.Blobbers[pos].AllocationRoot] = rootMap[mo.allocationObj.Blobbers[pos].AllocationRoot].Or(zboxutil.NewUint128(1).Lsh(pos)) + } + commitReqs := make([]*CommitRequestV2, len(rootMap)) + counter := 0 + timestamp := int64(common.Now()) + wg := &sync.WaitGroup{} + for _, mask := range rootMap { + wg.Add(1) + changes := make([]allocationchange.AllocationChangeV2, 0, len(mo.operations)) + for _, op := range mo.operations { + changes = append(changes, op) + } + commitReq := &CommitRequestV2{ + allocationObj: mo.allocationObj, + connectionID: mo.connectionID, + sig: mo.allocationObj.sig, + wg: wg, + timestamp: timestamp, + commitMask: mask, + consensusThresh: mo.consensusThresh, + changes: changes, + } + commitReqs[counter] = commitReq + counter++ + go AddCommitRequest(commitReq) + } + wg.Wait() + rollbackMask := zboxutil.NewUint128(0) + errSlice := make([]error, len(commitReqs)) + for idx, commitReq := range commitReqs { + if commitReq.result != nil { + if commitReq.result.Success { + mo.consensus += commitReq.commitMask.CountOnes() + } else { + errSlice[idx] = errors.New("commit_failed", commitReq.result.ErrorMessage) + l.Logger.Error("Commit failed", commitReq.result.ErrorMessage) + } + if !mo.isRepair { + rollbackMask = rollbackMask.Or(commitReq.commitMask) + } + } else { + l.Logger.Debug("Commit result not set") + } + } + if !mo.isConsensusOk() { + err := zboxutil.MajorError(errSlice) + if err != nil { + err = errors.New("consensus_not_met", fmt.Sprintf("Successfully committed to %d blobbers, but required %d", mo.consensus, len(mo.allocationObj.Blobbers))) + } + if mo.getConsensus() != 0 { + l.Logger.Info("Rolling back changes on minority blobbers") + mo.allocationObj.RollbackWithMask(rollbackMask) + } + for _, op := range mo.operations { + op.Error(mo.allocationObj, mo.getConsensus(), err) + } + return err + } else { + for _, op := range mo.operations { + op.Completed(mo.allocationObj) + } + } + return nil +} diff --git a/zboxcore/sdk/renameworker.go b/zboxcore/sdk/renameworker.go index c42746751..4070e8579 100644 --- a/zboxcore/sdk/renameworker.go +++ b/zboxcore/sdk/renameworker.go @@ -11,6 +11,7 @@ import ( "sync" "time" + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/errors" "github.com/google/uuid" @@ -411,3 +412,15 @@ func NewRenameOperation(remotePath string, destName string, renameMask zboxutil. return ro } + +func (ro *RenameOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { + return nil +} + +func (ro *RenameOperation) GetLookupHash(changeIndex uint64) string { + return "" +} + +func (ro *RenameOperation) GetHash(changeIndex uint64, id string) string { + return "" +} diff --git a/zboxcore/sdk/rollback.go b/zboxcore/sdk/rollback.go index 98f56a693..6040427ad 100644 --- a/zboxcore/sdk/rollback.go +++ b/zboxcore/sdk/rollback.go @@ -288,6 +288,7 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { } if wr.LatestWM != nil { blobber.AllocationRoot = wr.LatestWM.AllocationRoot + blobber.LatestWM = wr.LatestWM } } blobberRes[ind] = blobStatus diff --git a/zboxcore/sdk/upload_worker.go b/zboxcore/sdk/upload_worker.go index 76021f904..c9454010c 100644 --- a/zboxcore/sdk/upload_worker.go +++ b/zboxcore/sdk/upload_worker.go @@ -3,10 +3,12 @@ package sdk import ( "bytes" "context" + "encoding/hex" "errors" "io" "strings" + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/zboxcore/allocationchange" @@ -23,6 +25,7 @@ type UploadOperation struct { chunkedUpload *ChunkedUpload isUpdate bool isDownload bool + lookupHash string } var ErrPauseUpload = errors.New("upload paused by user") @@ -59,6 +62,7 @@ func (uo *UploadOperation) Process(allocObj *Allocation, connectionID string) ([ fileref.DeleteFileRef(cacheKey) } } + uo.lookupHash = fileref.GetReferenceLookup(uo.chunkedUpload.allocationObj.ID, uo.chunkedUpload.fileMeta.RemotePath) l.Logger.Info("UploadOperation Success", zap.String("name", uo.chunkedUpload.fileMeta.RemoteName)) return nil, uo.chunkedUpload.uploadMask, nil } @@ -141,3 +145,34 @@ func NewUploadOperation(ctx context.Context, workdir string, allocObj *Allocatio uo.isDownload = isMemoryDownload return uo, cu.progress.ConnectionID, nil } + +func (uo *UploadOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { + if uo.refs[changeIndex] == nil { + return nil + } + ref := uo.refs[changeIndex] + ref.NumBlocks = int64(numBlocks(ref.Size)) + decodedKey, _ := hex.DecodeString(uo.lookupHash) + fileMetaRawHash := ref.GetFileMetaHashV2() + err := trie.Update(decodedKey, fileMetaRawHash, uint64(ref.NumBlocks)) + if err != nil { + l.Logger.Error("Error updating trie", zap.Error(err)) + return err + } + return nil +} + +func (uo *UploadOperation) GetLookupHash(changeIndex uint64) string { + if uo.refs[changeIndex] == nil { + return "" + } + return uo.lookupHash +} + +func (uo *UploadOperation) GetHash(changeIndex uint64, id string) string { + if uo.refs[changeIndex] == nil { + return "" + } + ref := uo.refs[changeIndex] + return ref.GetFileHashDataV2(id) +} diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index 8299a5bef..3f9b455e1 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -73,6 +73,7 @@ const ( MOVE_ENDPOINT = "/v1/file/move/" LIST_ENDPOINT = "/v1/file/list/" REFERENCE_ENDPOINT = "/v1/file/referencepath/" + REFERENCE_ENDPOINT_V2 = "/v2/file/referencepath/" CONNECTION_ENDPOINT = "/v1/connection/details/" COMMIT_ENDPOINT = "/v1/connection/commit/" DOWNLOAD_ENDPOINT = "/v1/file/download/" @@ -282,6 +283,34 @@ func NewReferencePathRequest(baseUrl, allocationID string, allocationTx string, return req, nil } +func NewReferencePathRequestV2(baseUrl, allocationID string, allocationTx string, sig string, paths []string) (*http.Request, error) { + nurl, err := joinUrl(baseUrl, REFERENCE_ENDPOINT_V2, allocationTx) + if err != nil { + return nil, err + } + + pathBytes, err := json.Marshal(paths) + if err != nil { + return nil, err + } + params := url.Values{} + params.Add("paths", string(pathBytes)) + nurl.RawQuery = params.Encode() // Escape Query Parameters + + req, err := http.NewRequest(http.MethodGet, nurl.String(), nil) + if err != nil { + return nil, err + } + + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + return nil, err + } + + req.Header.Set(ALLOCATION_ID_HEADER, allocationID) + + return req, nil +} + func NewCalculateHashRequest(baseUrl, allocationID string, allocationTx string, paths []string) (*http.Request, error) { nurl, err := joinUrl(baseUrl, CALCULATE_HASH_ENDPOINT, allocationTx) if err != nil { From d83cf1845a1ef65a34bd707e9f999b95adebffe7 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 11 Sep 2024 18:41:54 +0530 Subject: [PATCH 059/319] Cleanup --- zcncore/get_data.go | 33 +-------------------------------- 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/zcncore/get_data.go b/zcncore/get_data.go index 97f47d61a..23e06501a 100644 --- a/zcncore/get_data.go +++ b/zcncore/get_data.go @@ -85,6 +85,7 @@ func SetWalletInfo(jsonWallet, sigScheme string, splitKeyWallet bool) error { client.SetWallet(wallet) client.SetSignatureScheme(sigScheme) + return client.SetSplitKeyWallet(splitKeyWallet) } @@ -95,38 +96,6 @@ func SetAuthUrl(url string) error { return client.SetAuthUrl(url) } -//// GetMintNonce retrieve the client's latest mint nonce from sharders -//// - cb: info callback instance, carries the response of the GET request to the sharders -//func GetMintNonce(cb GetInfoCallback) error { -// err := CheckConfig() -// if err != nil { -// return err -// } -// -// go GetInfoFromSharders(withParams(GET_MINT_NONCE, Params{ -// "client_id": client.Wallet().ClientID, -// }), OpGetMintNonce, cb) -// return nil -//} -// -//// GetNotProcessedZCNBurnTickets retrieve burn tickets that are not compensated by minting -//// - ethereumAddress: ethereum address for the issuer of the burn tickets -//// - startNonce: start nonce for the burn tickets -//// - cb: info callback instance, carries the response of the GET request to the sharders -//func GetNotProcessedZCNBurnTickets(ethereumAddress, startNonce string, cb GetInfoCallback) error { -// err := CheckConfig() -// if err != nil { -// return err -// } -// -// go GetInfoFromSharders(withParams(GET_NOT_PROCESSED_BURN_TICKETS, Params{ -// "ethereum_address": ethereumAddress, -// "nonce": startNonce, -// }), OpGetNotProcessedBurnTickets, cb) -// -// return nil -//} - // ConvertTokenToUSD converts the ZCN tokens to USD amount // - token: ZCN tokens amount func ConvertTokenToUSD(token float64) (float64, error) { From 2469b260a49a32a8587c8479f337c7c0dfb8e502 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Wed, 11 Sep 2024 20:42:40 +0530 Subject: [PATCH 060/319] add delete change --- zboxcore/sdk/allocation.go | 2 ++ zboxcore/sdk/chunked_upload_blobber.go | 2 +- zboxcore/sdk/commitworker.go | 26 +++++++++++++++++++++----- zboxcore/sdk/deleteworker.go | 23 +++++++++++++++++++++-- zboxcore/sdk/dirworker.go | 2 +- zboxcore/sdk/multi_operation_worker.go | 10 ++++++++-- zboxcore/sdk/upload_worker.go | 3 ++- zboxcore/zboxutil/http.go | 13 +++++++++++-- 8 files changed, 67 insertions(+), 14 deletions(-) diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 6ddf96543..9715c76bb 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -434,6 +434,8 @@ func (a *Allocation) InitAllocation() { a.startWorker(a.ctx) InitCommitWorker(a.Blobbers) InitBlockDownloader(a.Blobbers, downloadWorkerCount) + a.StorageVersion = 1 + a.CheckAllocStatus() //nolint:errcheck a.initialized = true } diff --git a/zboxcore/sdk/chunked_upload_blobber.go b/zboxcore/sdk/chunked_upload_blobber.go index 1074b659b..0ca8081e8 100644 --- a/zboxcore/sdk/chunked_upload_blobber.go +++ b/zboxcore/sdk/chunked_upload_blobber.go @@ -237,7 +237,7 @@ func (sb *ChunkedUploadBlobber) processCommit(ctx context.Context, su *ChunkedUp formWriter.Close() - req, err := zboxutil.NewCommitRequest(sb.blobber.Baseurl, su.allocationObj.ID, su.allocationObj.Tx, body) + req, err := zboxutil.NewCommitRequest(sb.blobber.Baseurl, su.allocationObj.ID, su.allocationObj.Tx, body, 0) if err != nil { logger.Logger.Error("Error creating commit req: ", err) return err diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index ee2525369..b8254c63d 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -304,7 +304,7 @@ func (req *CommitRequest) commitBlobber( l.Logger.Error("Creating form writer failed: ", err) return } - httpreq, err := zboxutil.NewCommitRequest(req.blobber.Baseurl, req.allocationID, req.allocationTx, body) + httpreq, err := zboxutil.NewCommitRequest(req.blobber.Baseurl, req.allocationID, req.allocationTx, body, 0) if err != nil { l.Logger.Error("Error creating commit req: ", err) return @@ -416,6 +416,7 @@ func (commitReq *CommitRequestV2) processCommit() { err error pos uint64 ) + now := time.Now() for i := commitReq.commitMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { pos = uint64(i.TrailingZeros()) @@ -437,6 +438,7 @@ func (commitReq *CommitRequestV2) processCommit() { commitReq.result = ErrorCommitResult("Failed to get reference path") return } + elapsedGetRefPath := time.Since(now) for _, change := range commitReq.changes { if change == nil { @@ -444,14 +446,15 @@ func (commitReq *CommitRequestV2) processCommit() { } err = change.ProcessChangeV2(trie, changeIndex) if err != nil { + l.Logger.Error("Error processing change", err) commitReq.result = ErrorCommitResult("Failed to process change " + err.Error()) return } } rootHash := trie.GetRoot().CalcHash() rootWeight := trie.Weight() - pos = 0 + elapsedProcessChanges := time.Since(now) - elapsedGetRefPath wg := sync.WaitGroup{} errSlice := make([]error, commitReq.commitMask.CountOnes()) counter := 0 @@ -465,6 +468,7 @@ func (commitReq *CommitRequestV2) processCommit() { defer wg.Done() err = commitReq.commitBlobber(rootHash, rootWeight, blobberPos, blobber) if err != nil { + l.Logger.Error("Error committing to blobber", err) errSlice[ind] = err mu.Lock() commitReq.commitMask = commitReq.commitMask.And(zboxutil.NewUint128(1).Lsh(pos).Not()) @@ -475,6 +479,7 @@ func (commitReq *CommitRequestV2) processCommit() { counter++ } wg.Wait() + elapsedCommit := time.Since(now) - elapsedProcessChanges - elapsedGetRefPath if commitReq.commitMask.CountOnes() < commitReq.consensusThresh { err = zboxutil.MajorError(errSlice) if err == nil { @@ -484,6 +489,7 @@ func (commitReq *CommitRequestV2) processCommit() { return } commitReq.allocationObj.allocationRoot = hex.EncodeToString(rootHash) + l.Logger.Info("[commit] ", "elapsedGetRefPath", elapsedGetRefPath.Milliseconds(), " elapsedProcessChanges", elapsedProcessChanges.Milliseconds(), " elapsedCommit", elapsedCommit.Milliseconds(), " total", time.Since(now).Milliseconds()) commitReq.result = SuccessCommitResult() } @@ -497,8 +503,12 @@ func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, changeInd if hash == "" { return errors.New("hash_signature_failed", "Failed to add hash signature") } + if hash == emptyHash { + continue + } sig, err := client.Sign(hash) if err != nil { + l.Logger.Error("Error signing hash", err) return err } hashSignatureMap[change.GetLookupHash(changeIndex)] = sig @@ -508,6 +518,7 @@ func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, changeInd if blobber.LatestWM != nil { prevChainHash, err := hex.DecodeString(blobber.LatestWM.ChainHash) if err != nil { + l.Logger.Error("Error decoding prev chain hash", err) return err } hasher.Write(prevChainHash) //nolint:errcheck @@ -531,19 +542,23 @@ func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, changeInd wm.ClientID = client.GetClientID() err = wm.Sign() if err != nil { + l.Logger.Error("Error signing writemarker", err) return err } wmData, err := json.Marshal(wm) if err != nil { + l.Logger.Error("Error marshalling writemarker data", err) return err } hashSignatureData, err := json.Marshal(hashSignatureMap) if err != nil { + l.Logger.Error("Error marshalling hash signature data", err) return err } - err = submitWriteMarker(wmData, hashSignatureData, blobber, req.connectionID, req.allocationObj.ID, req.allocationObj.Tx) + err = submitWriteMarker(wmData, hashSignatureData, blobber, req.connectionID, req.allocationObj.ID, req.allocationObj.Tx, req.allocationObj.StorageVersion) if err != nil { + l.Logger.Error("Error submitting writemarker", err) return err } blobber.LatestWM = wm @@ -616,6 +631,7 @@ func getReferencePathV2(blobber *blockchain.StorageNode, allocationID, allocatio } err = trie.Deserialize(lR.Path) if err != nil { + l.Logger.Error("Error deserializing trie", err) return nil, err } chainBlocks := numBlocks(lR.LatestWM.ChainSize) @@ -630,7 +646,7 @@ func getReferencePathV2(blobber *blockchain.StorageNode, allocationID, allocatio return trie, nil } -func submitWriteMarker(wmData, metaData []byte, blobber *blockchain.StorageNode, connectionID, allocationID, allocationTx string) (err error) { +func submitWriteMarker(wmData, metaData []byte, blobber *blockchain.StorageNode, connectionID, allocationID, allocationTx string, apiVersion int) (err error) { var ( resp *http.Response shouldContinue bool @@ -643,7 +659,7 @@ func submitWriteMarker(wmData, metaData []byte, blobber *blockchain.StorageNode, l.Logger.Error("Creating form writer failed: ", err) return } - httpreq, err := zboxutil.NewCommitRequest(blobber.Baseurl, allocationID, allocationTx, body) + httpreq, err := zboxutil.NewCommitRequest(blobber.Baseurl, allocationID, allocationTx, body, apiVersion) if err != nil { l.Logger.Error("Error creating commit req: ", err) return diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index 77992f10e..59a42a3d4 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -2,6 +2,7 @@ package sdk import ( "context" + "encoding/hex" "fmt" "io" "io/ioutil" @@ -44,6 +45,8 @@ type DeleteRequest struct { timestamp int64 } +const emptyHash = "e" + func (req *DeleteRequest) deleteBlobberFile( blobber *blockchain.StorageNode, blobberIdx int) error { @@ -332,6 +335,8 @@ type DeleteOperation struct { deleteMask zboxutil.Uint128 maskMu *sync.Mutex consensus Consensus + lookupHash string + refs []fileref.RefEntity } func (dop *DeleteOperation) Process(allocObj *Allocation, connectionID string) ([]fileref.RefEntity, zboxutil.Uint128, error) { @@ -399,6 +404,8 @@ func (dop *DeleteOperation) Process(allocObj *Allocation, connectionID string) ( deleteReq.consensus.consensusThresh, deleteReq.consensus.consensus)) } l.Logger.Debug("Delete Process Ended ") + dop.refs = objectTreeRefs + dop.lookupHash = fileref.GetReferenceLookup(allocObj.ID, dop.remotefilepath) return objectTreeRefs, deleteReq.deleteMask, nil } @@ -457,13 +464,25 @@ func NewDeleteOperation(remotePath string, deleteMask zboxutil.Uint128, maskMu * } func (dop *DeleteOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { + if dop.refs[changeIndex] == nil { + return nil + } + decodedKey, _ := hex.DecodeString(dop.lookupHash) + err := trie.Update(decodedKey, nil, 0) + if err != nil { + logger.Logger.Error("Error updating trie", err) + return err + } return nil } func (dop *DeleteOperation) GetLookupHash(changeIndex uint64) string { - return "" + if dop.refs[changeIndex] == nil { + return "" + } + return dop.lookupHash } func (dop *DeleteOperation) GetHash(changeIndex uint64, id string) string { - return "" + return emptyHash } diff --git a/zboxcore/sdk/dirworker.go b/zboxcore/sdk/dirworker.go index faca09211..a2e296494 100644 --- a/zboxcore/sdk/dirworker.go +++ b/zboxcore/sdk/dirworker.go @@ -382,5 +382,5 @@ func (dirOp *DirOperation) GetLookupHash(changeIndex uint64) string { } func (dirOp *DirOperation) GetHash(changeIndex uint64, id string) string { - return "" + return emptyHash } diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 4ba6df132..0bef25949 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -172,7 +172,9 @@ func (mo *MultiOperation) Process() error { defer ctxCncl(nil) swg := sizedwaitgroup.New(BatchSize) errsSlice := make([]error, len(mo.operations)) - mo.operationMask = zboxutil.NewUint128(0) + if mo.allocationObj.StorageVersion != StorageV2 { + mo.operationMask = zboxutil.NewUint128(0) + } for idx, op := range mo.operations { uid := util.GetNewUUID() swg.Add() @@ -296,6 +298,7 @@ func (mo *MultiOperation) Process() error { for i := mo.operationMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { pos = uint64(i.TrailingZeros()) if mo.allocationObj.Blobbers[pos].AllocationRoot != mo.allocationObj.allocationRoot { + l.Logger.Debug("Blobber allocation root mismatch", mo.allocationObj.Blobbers[pos].Baseurl, mo.allocationObj.Blobbers[pos].AllocationRoot, mo.allocationObj.allocationRoot) mo.operationMask = mo.operationMask.And(zboxutil.NewUint128(1).Lsh(pos).Not()) } } @@ -305,6 +308,9 @@ func (mo *MultiOperation) Process() error { if activeBlobbers < mo.consensusThresh { return errors.New("consensus_not_met", fmt.Sprintf("Active blobbers %d is less than consensus threshold %d", activeBlobbers, mo.consensusThresh)) } + if mo.allocationObj.StorageVersion == StorageV2 { + return mo.commitV2() + } commitReqs := make([]*CommitRequest, activeBlobbers) start = time.Now() wg.Add(activeBlobbers) @@ -411,7 +417,7 @@ func (mo *MultiOperation) commitV2() error { mo.consensus += commitReq.commitMask.CountOnes() } else { errSlice[idx] = errors.New("commit_failed", commitReq.result.ErrorMessage) - l.Logger.Error("Commit failed", commitReq.result.ErrorMessage) + l.Logger.Error("Commit failed ", commitReq.result.ErrorMessage) } if !mo.isRepair { rollbackMask = rollbackMask.Or(commitReq.commitMask) diff --git a/zboxcore/sdk/upload_worker.go b/zboxcore/sdk/upload_worker.go index c9454010c..fda7d0b3c 100644 --- a/zboxcore/sdk/upload_worker.go +++ b/zboxcore/sdk/upload_worker.go @@ -10,6 +10,7 @@ import ( "github.com/0chain/common/core/util/wmpt" "github.com/0chain/gosdk/constants" + "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk/zboxcore/fileref" @@ -174,5 +175,5 @@ func (uo *UploadOperation) GetHash(changeIndex uint64, id string) string { return "" } ref := uo.refs[changeIndex] - return ref.GetFileHashDataV2(id) + return encryption.Hash(ref.GetFileHashDataV2(id)) } diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index 3f9b455e1..4473d837f 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -76,6 +76,7 @@ const ( REFERENCE_ENDPOINT_V2 = "/v2/file/referencepath/" CONNECTION_ENDPOINT = "/v1/connection/details/" COMMIT_ENDPOINT = "/v1/connection/commit/" + COMMIT_ENDPOINT_V2 = "/v2/connection/commit/" DOWNLOAD_ENDPOINT = "/v1/file/download/" LATEST_READ_MARKER = "/v1/readmarker/latest" FILE_META_ENDPOINT = "/v1/file/meta/" @@ -237,8 +238,16 @@ func setClientInfoWithSign(req *http.Request, sig, allocation, baseURL string) e return nil } -func NewCommitRequest(baseUrl, allocationID string, allocationTx string, body io.Reader) (*http.Request, error) { - u, err := joinUrl(baseUrl, COMMIT_ENDPOINT, allocationTx) +func NewCommitRequest(baseUrl, allocationID string, allocationTx string, body io.Reader, apiVersion int) (*http.Request, error) { + var ( + u *url.URL + err error + ) + if apiVersion == 1 { + u, err = joinUrl(baseUrl, COMMIT_ENDPOINT_V2, allocationTx) + } else { + u, err = joinUrl(baseUrl, COMMIT_ENDPOINT, allocationTx) + } if err != nil { return nil, err } From c9dfb8637fff4b141e42a1de8a8425dd45aa6590 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 11 Sep 2024 21:46:36 +0530 Subject: [PATCH 061/319] Fix make sc rest api call --- core/client/http.go | 216 +++++++++++++++++------------------ core/transaction/get_data.go | 3 +- zboxcore/sdk/sdk.go | 34 +++--- zcnbridge/rest.go | 6 +- zcncore/get_data.go | 16 +-- 5 files changed, 137 insertions(+), 138 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index 091bf62d8..fcf6ede4d 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -1,19 +1,21 @@ package client import ( + "crypto/sha1" + "encoding/hex" "encoding/json" "fmt" "github.com/0chain/errors" - "github.com/0chain/gosdk/core/conf" - "github.com/0chain/gosdk/core/logger" + "github.com/0chain/gosdk/core/util" + "github.com/hashicorp/go-retryablehttp" "github.com/shopspring/decimal" + "io" "io/ioutil" "math" "net" "net/http" "net/url" "os" - "sync" "time" ) @@ -38,128 +40,126 @@ var DefaultTransport = &http.Transport{ // `err` - the error if any type SCRestAPIHandler func(response map[string][]byte, numSharders int, err error) -const SC_REST_API_URL = "v1/screst/" +const ( + // clientTimeout represents default http.Client timeout. + clientTimeout = 10 * time.Second -const MAX_RETRIES = 5 -const SLEEP_BETWEEN_RETRIES = 5 + // tlsHandshakeTimeout represents default http.Transport TLS handshake timeout. + tlsHandshakeTimeout = 5 * time.Second -// In percentage -const consensusThresh = float32(25.0) + // dialTimeout represents default net.Dialer timeout. + dialTimeout = 5 * time.Second +) -// MakeSCRestAPICall makes a rest api call to the sharders. -// - scAddress is the address of the smart contract -// - relativePath is the relative path of the api -// - params is the query parameters -// - handler is the handler function to handle the response -func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string, handler SCRestAPIHandler) ([]byte, error) { - nodeClient, err := GetNode() - if err != nil { - return nil, err - } - numSharders := len(nodeClient.Sharders().Healthy()) - sharders := nodeClient.Sharders().Healthy() - responses := make(map[int]int) - mu := &sync.Mutex{} - entityResult := make(map[string][]byte) - var retObj []byte - maxCount := 0 - dominant := 200 - wg := sync.WaitGroup{} - - cfg, err := conf.GetClientConfig() - if err != nil { - return nil, err +// NewClient creates default http.Client with timeouts. +func NewClient() *http.Client { + return &http.Client{ + Timeout: clientTimeout, + Transport: &http.Transport{ + TLSHandshakeTimeout: tlsHandshakeTimeout, + DialContext: (&net.Dialer{ + Timeout: dialTimeout, + }).DialContext, + }, } +} - for _, sharder := range sharders { - wg.Add(1) - go func(sharder string) { - defer wg.Done() - urlString := fmt.Sprintf("%v/%v%v%v", sharder, SC_REST_API_URL, scAddress, relativePath) - urlObj, err := url.Parse(urlString) - if err != nil { - logger.Log.Error(err) - return - } - q := urlObj.Query() - for k, v := range params { - q.Add(k, v) - } - urlObj.RawQuery = q.Encode() - clientObj := &http.Client{Transport: DefaultTransport} - response, err := clientObj.Get(urlObj.String()) - if err != nil { - nodeClient.Sharders().Fail(sharder) - return - } - - defer response.Body.Close() - entityBytes, _ := ioutil.ReadAll(response.Body) - mu.Lock() - if response.StatusCode > http.StatusBadRequest { - nodeClient.Sharders().Fail(sharder) - } else { - nodeClient.Sharders().Success(sharder) - } - responses[response.StatusCode]++ - if responses[response.StatusCode] > maxCount { - maxCount = responses[response.StatusCode] - } - - if IsCurrentDominantStatus(response.StatusCode, responses, maxCount) { - dominant = response.StatusCode - retObj = entityBytes - } - - entityResult[sharder] = entityBytes - nodeClient.Sharders().Success(sharder) - mu.Unlock() - }(sharder) - } - wg.Wait() +// NewRetryableClient creates default retryablehttp.Client with timeouts and embedded NewClient result. +func NewRetryableClient(retryMax int) *retryablehttp.Client { + client := retryablehttp.NewClient() + client.HTTPClient = NewClient() + client.RetryWaitMax = clientTimeout + client.RetryMax = retryMax + client.Logger = nil - rate := float32(maxCount*100) / float32(cfg.SharderConsensous) - if rate < consensusThresh { - err = errors.New("consensus_failed", "consensus failed on sharders") - } + return client +} + +// MakeSCRestAPICall calls smart contract with provided address +// and makes retryable request to smart contract resource with provided relative path using params. +func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string) ([]byte, error) { + var ( + resMaxCounterBody []byte + + hashMaxCounter int + hashCounters = make(map[string]int) - if dominant != 200 { - var objmap map[string]json.RawMessage - err := json.Unmarshal(retObj, &objmap) + sharders = extractSharders() + + lastErrMsg string + ) + + for _, sharder := range sharders { + var ( + retryableClient = NewRetryableClient(5) + u = makeScURL(params, sharder, scAddress, relativePath) + ) + + resp, err := retryableClient.Get(u.String()) if err != nil { - return nil, errors.New("", string(retObj)) + lastErrMsg = fmt.Sprintf("error while requesting sharders: %v", err) + continue } - - var parsed string - err = json.Unmarshal(objmap["error"], &parsed) - if err != nil || parsed == "" { - return nil, errors.New("", string(retObj)) + hash, resBody, err := hashAndBytesOfReader(resp.Body) + _ = resp.Body.Close() + if err != nil { + lastErrMsg = fmt.Sprintf("error while reading response body: %v", err) + continue + } + if resp.StatusCode != http.StatusOK { + lastErrMsg = fmt.Sprintf("response status is not OK; response body: %s", string(resBody)) + continue } - return nil, errors.New("", parsed) + hashCounters[hash]++ + if hashCounters[hash] > hashMaxCounter { + hashMaxCounter = hashCounters[hash] + resMaxCounterBody = resBody + } } - if handler != nil { - handler(entityResult, numSharders, err) + if hashMaxCounter == 0 { + return nil, errors.New("request_sharders", "no valid responses, last err: "+lastErrMsg) } - if rate > consensusThresh { - return retObj, nil + return resMaxCounterBody, nil +} + +// hashAndBytesOfReader computes hash of readers data and returns hash encoded to hex and bytes of reader data. +// If error occurs while reading data from reader, it returns non nil error. +func hashAndBytesOfReader(r io.Reader) (hash string, reader []byte, err error) { + h := sha1.New() + teeReader := io.TeeReader(r, h) + readerBytes, err := ioutil.ReadAll(teeReader) + if err != nil { + return "", nil, err } - return nil, err + + return hex.EncodeToString(h.Sum(nil)), readerBytes, nil } -// IsCurrentDominantStatus determines whether the current response status is the dominant status among responses. -// -// The dominant status is where the response status is counted the most. -// On tie-breakers, 200 will be selected if included. -// -// Function assumes runningTotalPerStatus can be accessed safely concurrently. -func IsCurrentDominantStatus(respStatus int, currentTotalPerStatus map[int]int, currentMax int) bool { - // mark status as dominant if - // - running total for status is the max and response is 200 or - // - running total for status is the max and count for 200 is lower - return currentTotalPerStatus[respStatus] == currentMax && (respStatus == 200 || currentTotalPerStatus[200] < currentMax) +// extractSharders returns string slice of randomly ordered sharders existing in the current network. +func extractSharders() []string { + sharders := nodeClient.Network().Sharders + return util.GetRandom(sharders, len(sharders)) +} + +const ( + // ScRestApiUrl represents base URL path to execute smart contract rest points. + ScRestApiUrl = "v1/screst/" +) + +// makeScURL creates url.URL to make smart contract request to sharder. +func makeScURL(params map[string]string, sharder, scAddress, relativePath string) *url.URL { + uString := fmt.Sprintf("%v/%v%v%v", sharder, ScRestApiUrl, scAddress, relativePath) + u, _ := url.Parse(uString) + q := u.Query() + for k, v := range params { + q.Add(k, v) + } + u.RawQuery = q.Encode() + + return u } func (pfe *proxyFromEnv) Proxy(req *http.Request) (proxy *url.URL, err error) { @@ -236,7 +236,7 @@ func GetBalance(clientIDs ...string) (*GetBalanceResponse, error) { if res, err = MakeSCRestAPICall("", GET_BALANCE, map[string]string{ "client_id": clientID, - }, nil); err != nil { + }); err != nil { return nil, err } diff --git a/core/transaction/get_data.go b/core/transaction/get_data.go index 66061e631..0d5315a68 100644 --- a/core/transaction/get_data.go +++ b/core/transaction/get_data.go @@ -90,8 +90,7 @@ func GetConfig(configType string) (conf *InputMap, err error) { relativePath = GET_MINERSC_GLOBALS } - b, err = coreHttp.MakeSCRestAPICall(scAddress, relativePath, nil, - nil) + b, err = coreHttp.MakeSCRestAPICall(scAddress, relativePath, nil) if err != nil { return nil, errors.Wrap(err, "error requesting storage SC configs:") } diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index 9e872a0ab..3699e67dd 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -12,6 +12,7 @@ import ( "github.com/0chain/gosdk/core/zcncrypto" "gopkg.in/natefinch/lumberjack.v2" "io/ioutil" + "log" "math" "net/http" "strconv" @@ -184,7 +185,7 @@ func GetReadPoolInfo(clientID string) (info *ReadPool, err error) { var b []byte b, err = client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getReadPoolStat", - map[string]string{"client_id": clientID}, nil) + map[string]string{"client_id": clientID}) if err != nil { return nil, errors.Wrap(err, "error requesting read pool info") } @@ -290,7 +291,7 @@ func GetStakePoolInfo(providerType ProviderType, providerID string) (info *Stake var b []byte b, err = client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getStakePoolStat", - map[string]string{"provider_type": strconv.Itoa(int(providerType)), "provider_id": providerID}, nil) + map[string]string{"provider_type": strconv.Itoa(int(providerType)), "provider_id": providerID}) if err != nil { return nil, errors.Wrap(err, "error requesting stake pool info:") } @@ -331,7 +332,7 @@ func GetStakePoolUserInfo(clientID string, offset, limit int) (info *StakePoolUs "limit": strconv.FormatInt(int64(limit), 10), } b, err = client.MakeSCRestAPICall(STORAGE_SCADDRESS, - "/getUserStakePoolStat", params, nil) + "/getUserStakePoolStat", params) if err != nil { return nil, errors.Wrap(err, "error requesting stake pool user info:") } @@ -543,8 +544,7 @@ func GetChallengePoolInfo(allocID string) (info *ChallengePoolInfo, err error) { var b []byte b, err = client.MakeSCRestAPICall(STORAGE_SCADDRESS, - "/getChallengePoolStat", map[string]string{"allocation_id": allocID}, - nil) + "/getChallengePoolStat", map[string]string{"allocation_id": allocID}) if err != nil { return nil, errors.Wrap(err, "error requesting challenge pool info:") } @@ -569,7 +569,6 @@ func GetMptData(key string) ([]byte, error) { var b []byte b, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/get_mpt_key", map[string]string{"key": key}, - nil, ) if err != nil { return nil, errors.Wrap(err, "error requesting mpt key data:") @@ -738,7 +737,7 @@ func getBlobbersInternal(active, stakable bool, limit, offset int) (bs []*Blobbe offset, strconv.FormatBool(stakable), ) - b, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, url, nil, nil) + b, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, url, nil) var wrap nodes if err != nil { return nil, errors.Wrap(err, "error requesting blobbers:") @@ -758,6 +757,7 @@ func getBlobbersInternal(active, stakable bool, limit, offset int) (bs []*Blobbe // - active: if true then only active blobbers are returned // - stakable: if true then only stakable blobbers are returned func GetBlobbers(active, stakable bool) (bs []*Blobber, err error) { + log.Println("GetBlobbers : active ", active, stakable) if !sdkInitialized { return nil, sdkNotInitialized } @@ -800,7 +800,7 @@ func GetBlobber(blobberID string) (blob *Blobber, err error) { STORAGE_SCADDRESS, "/getBlobber", map[string]string{"blobber_id": blobberID}, - nil) + ) if err != nil { return nil, errors.Wrap(err, "requesting blobber:") } @@ -825,7 +825,7 @@ func GetValidator(validatorID string) (validator *Validator, err error) { STORAGE_SCADDRESS, "/get_validator", map[string]string{"validator_id": validatorID}, - nil) + ) if err != nil { return nil, errors.Wrap(err, "requesting validator:") } @@ -852,7 +852,7 @@ func GetValidators(stakable bool) (validators []*Validator, err error) { map[string]string{ "stakable": strconv.FormatBool(stakable), }, - nil) + ) if err != nil { return nil, errors.Wrap(err, "requesting validator list") } @@ -910,7 +910,7 @@ func GetAllocation(allocationID string) (*Allocation, error) { } params := make(map[string]string) params["allocation"] = allocationID - allocationBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params, nil) + allocationBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params) if err != nil { return nil, errors.New("allocation_fetch_error", "Error fetching the allocation."+err.Error()) } @@ -931,7 +931,7 @@ func GetAllocationUpdates(allocation *Allocation) error { params := make(map[string]string) params["allocation"] = allocation.ID - allocationBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params, nil) + allocationBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params) if err != nil { return errors.New("allocation_fetch_error", "Error fetching the allocation."+err.Error()) } @@ -984,7 +984,7 @@ func getAllocationsInternal(clientID string, limit, offset int) ([]*Allocation, params["client"] = clientID params["limit"] = fmt.Sprint(limit) params["offset"] = fmt.Sprint(offset) - allocationsBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocations", params, nil) + allocationsBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocations", params) if err != nil { return nil, errors.New("allocations_fetch_error", "Error fetching the allocations."+err.Error()) } @@ -1165,7 +1165,7 @@ func GetAllocationBlobbers( params["force"] = strconv.FormatBool(force[0]) } - allocBlobber, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/alloc_blobbers", params, nil) + allocBlobber, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/alloc_blobbers", params) if err != nil { return nil, err } @@ -1250,7 +1250,7 @@ func GetBlobberIds(blobberUrls []string) ([]string, error) { params := make(map[string]string) params["blobber_urls"] = string(urlsStr) - idsStr, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/blobber_ids", params, nil) + idsStr, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/blobber_ids", params) if err != nil { return nil, err } @@ -1275,7 +1275,7 @@ func GetFreeAllocationBlobbers(request map[string]interface{}) ([]string, error) params := make(map[string]string) params["free_allocation_data"] = string(data) - allocBlobber, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/free_alloc_blobbers", params, nil) + allocBlobber, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/free_alloc_blobbers", params) if err != nil { return nil, err } @@ -1778,7 +1778,7 @@ func GetUpdateAllocationMinLock( params := make(map[string]string) params["data"] = string(data) - responseBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation-update-min-lock", params, nil) + responseBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation-update-min-lock", params) if err != nil { return 0, errors.Wrap(err, "failed to request allocation update min lock") } diff --git a/zcnbridge/rest.go b/zcnbridge/rest.go index 22075a59c..ab860b16d 100644 --- a/zcnbridge/rest.go +++ b/zcnbridge/rest.go @@ -90,7 +90,7 @@ func GetAuthorizer(id string) (res []byte, err error) { return coreHttp.MakeSCRestAPICall(zcncore.ZCNSCSmartContractAddress, PathGetAuthorizer, zcncore.Params{ "id": id, - }, nil) + }) } // GetAuthorizers Returns all or only active authorizers @@ -101,7 +101,7 @@ func GetAuthorizers(active bool) (res []byte, err error) { if err != nil { return nil, err } - return coreHttp.MakeSCRestAPICall(zcncore.ZCNSCSmartContractAddress, fmt.Sprintf(PathGetAuthorizerNodes, active), nil, nil) + return coreHttp.MakeSCRestAPICall(zcncore.ZCNSCSmartContractAddress, fmt.Sprintf(PathGetAuthorizerNodes, active), nil) } // GetGlobalConfig Returns global config @@ -111,5 +111,5 @@ func GetGlobalConfig() (res []byte, err error) { if err != nil { return nil, err } - return coreHttp.MakeSCRestAPICall(zcncore.ZCNSCSmartContractAddress, PathGetGlobalConfig, nil, nil) + return coreHttp.MakeSCRestAPICall(zcncore.ZCNSCSmartContractAddress, PathGetGlobalConfig, nil) } diff --git a/zcncore/get_data.go b/zcncore/get_data.go index 23e06501a..b994bc8e4 100644 --- a/zcncore/get_data.go +++ b/zcncore/get_data.go @@ -179,7 +179,7 @@ func GetMinerSCNodeInfo(id string) ([]byte, error) { return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_NODE, Params{ "id": id, - }, nil) + }) } // GetMintNonce retrieve the client's latest mint nonce from sharders @@ -192,7 +192,7 @@ func GetMintNonce() ([]byte, error) { return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINT_NONCE, Params{ "client_id": client.ClientID(), - }, nil) + }) } func GetMiners(active, stakable bool, limit, offset int) ([]byte, error) { @@ -205,7 +205,7 @@ func GetMiners(active, stakable bool, limit, offset int) ([]byte, error) { "stakable": strconv.FormatBool(stakable), "offset": strconv.FormatInt(int64(offset), 10), "limit": strconv.FormatInt(int64(limit), 10), - }, nil) + }) } func GetSharders(active, stakable bool, limit, offset int) ([]byte, error) { @@ -218,14 +218,14 @@ func GetSharders(active, stakable bool, limit, offset int) ([]byte, error) { "stakable": strconv.FormatBool(stakable), "offset": strconv.FormatInt(int64(offset), 10), "limit": strconv.FormatInt(int64(limit), 10), - }, nil) + }) } // GetLatestFinalizedMagicBlock gets latest finalized magic block // - numSharders: number of sharders // - timeout: request timeout func GetLatestFinalizedMagicBlock(ctx context.Context, numSharders int) (m *block.MagicBlock, err error) { - res, err := client.MakeSCRestAPICall(MinerSmartContractAddress, GET_LATEST_FINALIZED_MAGIC_BLOCK, nil, nil) + res, err := client.MakeSCRestAPICall(MinerSmartContractAddress, GET_LATEST_FINALIZED_MAGIC_BLOCK, nil) if err != nil { return nil, err } @@ -250,7 +250,7 @@ func GetMinerSCUserInfo(clientID string) ([]byte, error) { return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_USER, Params{ "client_id": clientID, - }, nil) + }) } // GetMinerSCNodePool get miner smart contract node pool @@ -263,7 +263,7 @@ func GetMinerSCNodePool(id string) ([]byte, error) { return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_POOL, Params{ "id": id, "pool_id": client.ClientID(), - }, nil) + }) } // GetNotProcessedZCNBurnTickets retrieve burn tickets that are not compensated by minting @@ -278,5 +278,5 @@ func GetNotProcessedZCNBurnTickets(ethereumAddress, startNonce string) ([]byte, return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_POOL, Params{ "ethereum_address": ethereumAddress, "nonce": startNonce, - }, nil) + }) } From f9ac97756654327a337c76dfe0758e508531d128 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 11 Sep 2024 23:48:28 +0530 Subject: [PATCH 062/319] Fix --- core/client/node.go | 2 - zcnbridge/bridge_test.go | 17 +- zcnbridge/transaction/callback.go | 89 ------ zcncore/ethwallet_base_test.go | 35 +-- zmagmacore/build/info.go | 7 - zmagmacore/chain/entity.go | 45 --- zmagmacore/config/chain.go | 12 - zmagmacore/config/consumer.go | 29 -- zmagmacore/config/handler.go | 14 - zmagmacore/config/provider.go | 30 -- zmagmacore/config/workers.go | 9 - zmagmacore/crypto/hash.go | 46 --- zmagmacore/crypto/keys.go | 64 ---- zmagmacore/doc.go | 2 - zmagmacore/errors/errors.go | 91 ------ zmagmacore/errors/errors_test.go | 243 --------------- zmagmacore/http/client.go | 45 --- zmagmacore/http/sc-api.go | 106 ------- zmagmacore/http/server.go | 81 ----- zmagmacore/limiter/limiter.go | 49 --- zmagmacore/log/handler.go | 58 ---- zmagmacore/log/logging.go | 93 ------ zmagmacore/magmasc/acknowledgment.go | 123 -------- zmagmacore/magmasc/acknowledgment_test.go | 173 ----------- zmagmacore/magmasc/actions.go | 284 ------------------ zmagmacore/magmasc/api.go | 180 ----------- zmagmacore/magmasc/billing.go | 79 ----- zmagmacore/magmasc/billing_test.go | 221 -------------- zmagmacore/magmasc/const.go | 111 ------- zmagmacore/magmasc/consumer.go | 75 ----- zmagmacore/magmasc/consumer_test.go | 146 --------- zmagmacore/magmasc/datausage.go | 60 ---- zmagmacore/magmasc/datausage_test.go | 134 --------- zmagmacore/magmasc/errors.go | 36 --- zmagmacore/magmasc/interfaces.go | 51 ---- zmagmacore/magmasc/mocks_test.go | 115 ------- zmagmacore/magmasc/provider.go | 81 ----- zmagmacore/magmasc/provider_terms.go | 200 ------------ zmagmacore/magmasc/provider_test.go | 154 ---------- zmagmacore/magmasc/tokenpool.go | 47 --- zmagmacore/magmasc/tokenpool_configurator.go | 21 -- zmagmacore/magmasc/tokenpool_test.go | 86 ------ zmagmacore/magmasc/tokenpool_transfer.go | 35 --- zmagmacore/magmasc/tokenpool_transfer_test.go | 86 ------ zmagmacore/node/self.go | 80 ----- zmagmacore/registration/node.go | 119 -------- zmagmacore/registration/types.go | 38 --- zmagmacore/shutdown/shutdown.go | 55 ---- zmagmacore/storage/interface.go | 126 -------- zmagmacore/time/time.go | 35 --- zmagmacore/transaction/callback.go | 96 ------ zmagmacore/transaction/const.go | 62 ---- zmagmacore/transaction/http.go | 22 -- zmagmacore/transaction/txn.go | 140 --------- zmagmacore/wallet/balance.go | 52 ---- zmagmacore/wallet/callback.go | 18 -- zmagmacore/wallet/setup.go | 44 --- zmagmacore/wallet/types.go | 18 -- zmagmacore/wallet/wallet.go | 63 ---- 59 files changed, 24 insertions(+), 4609 deletions(-) delete mode 100644 zcnbridge/transaction/callback.go delete mode 100644 zmagmacore/build/info.go delete mode 100644 zmagmacore/chain/entity.go delete mode 100644 zmagmacore/config/chain.go delete mode 100644 zmagmacore/config/consumer.go delete mode 100644 zmagmacore/config/handler.go delete mode 100644 zmagmacore/config/provider.go delete mode 100644 zmagmacore/config/workers.go delete mode 100644 zmagmacore/crypto/hash.go delete mode 100644 zmagmacore/crypto/keys.go delete mode 100644 zmagmacore/doc.go delete mode 100644 zmagmacore/errors/errors.go delete mode 100644 zmagmacore/errors/errors_test.go delete mode 100644 zmagmacore/http/client.go delete mode 100644 zmagmacore/http/sc-api.go delete mode 100644 zmagmacore/http/server.go delete mode 100644 zmagmacore/limiter/limiter.go delete mode 100644 zmagmacore/log/handler.go delete mode 100644 zmagmacore/log/logging.go delete mode 100644 zmagmacore/magmasc/acknowledgment.go delete mode 100644 zmagmacore/magmasc/acknowledgment_test.go delete mode 100644 zmagmacore/magmasc/actions.go delete mode 100644 zmagmacore/magmasc/api.go delete mode 100644 zmagmacore/magmasc/billing.go delete mode 100644 zmagmacore/magmasc/billing_test.go delete mode 100644 zmagmacore/magmasc/const.go delete mode 100644 zmagmacore/magmasc/consumer.go delete mode 100644 zmagmacore/magmasc/consumer_test.go delete mode 100644 zmagmacore/magmasc/datausage.go delete mode 100644 zmagmacore/magmasc/datausage_test.go delete mode 100644 zmagmacore/magmasc/errors.go delete mode 100644 zmagmacore/magmasc/interfaces.go delete mode 100644 zmagmacore/magmasc/mocks_test.go delete mode 100644 zmagmacore/magmasc/provider.go delete mode 100644 zmagmacore/magmasc/provider_terms.go delete mode 100644 zmagmacore/magmasc/provider_test.go delete mode 100644 zmagmacore/magmasc/tokenpool.go delete mode 100644 zmagmacore/magmasc/tokenpool_configurator.go delete mode 100644 zmagmacore/magmasc/tokenpool_test.go delete mode 100644 zmagmacore/magmasc/tokenpool_transfer.go delete mode 100644 zmagmacore/magmasc/tokenpool_transfer_test.go delete mode 100644 zmagmacore/node/self.go delete mode 100644 zmagmacore/registration/node.go delete mode 100644 zmagmacore/registration/types.go delete mode 100644 zmagmacore/shutdown/shutdown.go delete mode 100644 zmagmacore/storage/interface.go delete mode 100644 zmagmacore/time/time.go delete mode 100644 zmagmacore/transaction/callback.go delete mode 100644 zmagmacore/transaction/const.go delete mode 100644 zmagmacore/transaction/http.go delete mode 100644 zmagmacore/transaction/txn.go delete mode 100644 zmagmacore/wallet/balance.go delete mode 100644 zmagmacore/wallet/callback.go delete mode 100644 zmagmacore/wallet/setup.go delete mode 100644 zmagmacore/wallet/types.go delete mode 100644 zmagmacore/wallet/wallet.go diff --git a/core/client/node.go b/core/client/node.go index c56ac9e63..6b994cede 100644 --- a/core/client/node.go +++ b/core/client/node.go @@ -4,11 +4,9 @@ package client import ( "sort" "sync" - "time" ) const statSize = 20 -const defaultTimeout = 5 * time.Second type NodeHolder struct { consensus int diff --git a/zcnbridge/bridge_test.go b/zcnbridge/bridge_test.go index 1fbc002b7..70d02bfac 100644 --- a/zcnbridge/bridge_test.go +++ b/zcnbridge/bridge_test.go @@ -22,7 +22,6 @@ import ( "github.com/0chain/gosdk/zcnbridge/ethereum/authorizers" binding "github.com/0chain/gosdk/zcnbridge/ethereum/bridge" bridgemocks "github.com/0chain/gosdk/zcnbridge/mocks" - "github.com/0chain/gosdk/zcnbridge/transaction" transactionmocks "github.com/0chain/gosdk/zcnbridge/transaction/mocks" "github.com/0chain/gosdk/zcnbridge/wallet" "github.com/0chain/gosdk/zcnbridge/zcnsc" @@ -222,7 +221,7 @@ func getEthereumClient(t mock.TestingT) *bridgemocks.EthereumClient { return bridgemocks.NewEthereumClient(ðereumClientMock{t}) } -func getBridgeClient(ethereumNodeURL string, ethereumClient EthereumClient, transactionProvider transaction.TransactionProvider, keyStore KeyStore) *BridgeClient { +func getBridgeClient(ethereumNodeURL string, ethereumClient EthereumClient, keyStore KeyStore) *BridgeClient { cfg := viper.New() tempConfigFile, err := os.CreateTemp(".", "config.yaml") @@ -260,7 +259,6 @@ func getBridgeClient(ethereumNodeURL string, ethereumClient EthereumClient, tran cfg.GetFloat64("bridge.consensus_threshold"), ethereumClient, - transactionProvider, keyStore, ) } @@ -318,13 +316,10 @@ func Test_ZCNBridge(t *testing.T) { tx := getTransaction(t) prepareTransactionGeneralMockCalls(&tx.Mock) - transactionProvider := getTransactionProvider(t) - prepareTransactionProviderGeneralMockCalls(&transactionProvider.Mock, tx) - keyStore := getKeyStore(t) prepareKeyStoreGeneralMockCalls(keyStore) - bridgeClient := getBridgeClient(alchemyEthereumNodeURL, ethereumClient, transactionProvider, keyStore) + bridgeClient := getBridgeClient(alchemyEthereumNodeURL, ethereumClient, keyStore) t.Run("should update authorizer config.", func(t *testing.T) { source := &authorizerNodeSource{ @@ -436,7 +431,7 @@ func Test_ZCNBridge(t *testing.T) { }) t.Run("should check configuration used by BurnZCN", func(t *testing.T) { - _, err := bridgeClient.BurnZCN(amount) + _, _, err := bridgeClient.BurnZCN(amount) require.NoError(t, err) require.True(t, tx.AssertCalled( @@ -634,21 +629,21 @@ func Test_ZCNBridge(t *testing.T) { }) t.Run("should check if gas price estimation works with correct alchemy ethereum node url", func(t *testing.T) { - bridgeClient = getBridgeClient(alchemyEthereumNodeURL, ethereumClient, transactionProvider, keyStore) + bridgeClient = getBridgeClient(alchemyEthereumNodeURL, ethereumClient, keyStore) _, err := bridgeClient.EstimateGasPrice(context.Background()) require.Contains(t, err.Error(), "Must be authenticated!") }) t.Run("should check if gas price estimation works with correct tenderly ethereum node url", func(t *testing.T) { - bridgeClient = getBridgeClient(tenderlyEthereumNodeURL, ethereumClient, transactionProvider, keyStore) + bridgeClient = getBridgeClient(tenderlyEthereumNodeURL, ethereumClient, keyStore) _, err := bridgeClient.EstimateGasPrice(context.Background()) require.NoError(t, err) }) t.Run("should check if gas price estimation works with incorrect ethereum node url", func(t *testing.T) { - bridgeClient = getBridgeClient(infuraEthereumNodeURL, ethereumClient, transactionProvider, keyStore) + bridgeClient = getBridgeClient(infuraEthereumNodeURL, ethereumClient, keyStore) _, err := bridgeClient.EstimateGasPrice(context.Background()) require.Error(t, err) diff --git a/zcnbridge/transaction/callback.go b/zcnbridge/transaction/callback.go deleted file mode 100644 index 0ce4a1e1a..000000000 --- a/zcnbridge/transaction/callback.go +++ /dev/null @@ -1,89 +0,0 @@ -package transaction - -import ( - "context" - - "github.com/0chain/gosdk/zcnbridge/errors" - "github.com/0chain/gosdk/zcncore" -) - -var ( - // Ensure callback implements interface. - _ zcncore.TransactionCallback = (*callback)(nil) -) - -type ( - // TransactionCallbackAwaitable extends zcncore.TransactionCallback with synchronization methods - TransactionCallbackAwaitable interface { - zcncore.TransactionCallback - - WaitCompleteCall(ctx context.Context) error - WaitVerifyCall(ctx context.Context) error - } - - // callback implements zcncore.TransactionCallback interface. - callback struct { - // waitCh represents channel for making callback.OnTransactionComplete, - // callback.OnVerifyComplete and callBack.OnAuthComplete operations async. - waitCh chan interface{} - err error - } -) - -func NewStatus() TransactionCallbackAwaitable { - return &callback{ - waitCh: make(chan interface{}), - } -} - -// OnTransactionComplete implements zcncore.TransactionCallback interface. -func (cb *callback) OnTransactionComplete(zcnTxn *zcncore.Transaction, status int) { - if status != zcncore.StatusSuccess { - msg := "status is not success: " + TxnStatus(status).String() + "; err: " + zcnTxn.GetTransactionError() - cb.err = errors.New("on_transaction_complete", msg) - } - - cb.sendCall() -} - -// OnVerifyComplete implements zcncore.TransactionCallback interface. -func (cb *callback) OnVerifyComplete(zcnTxn *zcncore.Transaction, status int) { - if status != zcncore.StatusSuccess { - msg := "status is not success: " + TxnStatus(status).String() + "; err: " + zcnTxn.GetVerifyError() - cb.err = errors.New("on_transaction_verify", msg) - } - - cb.sendCall() -} - -// OnAuthComplete implements zcncore.TransactionCallback interface. -func (cb *callback) OnAuthComplete(_ *zcncore.Transaction, status int) { - if status != zcncore.StatusSuccess { - msg := "status is not success: " + TxnStatus(status).String() - cb.err = errors.New("on_transaction_verify", msg) - } - - cb.sendCall() -} - -func (cb *callback) WaitCompleteCall(ctx context.Context) error { - select { - case <-ctx.Done(): - return errors.New("completing_transaction", "completing transaction context deadline exceeded") - case <-cb.waitCh: - return cb.err - } -} - -func (cb *callback) WaitVerifyCall(ctx context.Context) error { - select { - case <-ctx.Done(): - return errors.New("verifying_transaction", "verifying transaction context deadline exceeded") - case <-cb.waitCh: - return cb.err - } -} - -func (cb *callback) sendCall() { - cb.waitCh <- true -} diff --git a/zcncore/ethwallet_base_test.go b/zcncore/ethwallet_base_test.go index 018bf4513..b60588832 100644 --- a/zcncore/ethwallet_base_test.go +++ b/zcncore/ethwallet_base_test.go @@ -183,23 +183,24 @@ func TestSuggestEthGasPrice(t *testing.T) { }) } -func TestTransferEthTokens(t *testing.T) { - t.Run("success transfer", func(t *testing.T) { - backend, _ := newTestBackend(t) - client, _ := backend.Attach() - defer backend.Close() - defer client.Close() - - realClient := ethclient.NewClient(client) - getEthClient = func() (*ethclient.Client, error) { - return realClient, nil - } - - hash, err := TransferEthTokens("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291", 1000000000000, 10000) - require.Nil(t, err) - require.EqualValues(t, hash, "0x43eba8525933e34908e766de93176d810e8582e886052708d44d9db157803aec") - }) -} +//TODO:JAYASHTODO +//func TestTransferEthTokens(t *testing.T) { +// t.Run("success transfer", func(t *testing.T) { +// backend, _ := newTestBackend(t) +// client, _ := backend.Attach() +// defer backend.Close() +// defer client.Close() +// +// realClient := ethclient.NewClient(client) +// getEthClient = func() (*ethclient.Client, error) { +// return realClient, nil +// } +// +// hash, err := TransferEthTokens("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291", 1000000000000, 10000) +// require.Nil(t, err) +// require.EqualValues(t, hash, "0x43eba8525933e34908e766de93176d810e8582e886052708d44d9db157803aec") +// }) +//} type MockBalanceCallback struct { wg *sync.WaitGroup diff --git a/zmagmacore/build/info.go b/zmagmacore/build/info.go deleted file mode 100644 index f75decb53..000000000 --- a/zmagmacore/build/info.go +++ /dev/null @@ -1,7 +0,0 @@ -// DEPRECATED: This package is deprecated and will be removed in a future release. -package build - -var ( - // Tag represents the git commit for the build. - Tag = "is not set" -) diff --git a/zmagmacore/chain/entity.go b/zmagmacore/chain/entity.go deleted file mode 100644 index c57bdb243..000000000 --- a/zmagmacore/chain/entity.go +++ /dev/null @@ -1,45 +0,0 @@ -// DEPRECATED: This package is deprecated and will be removed in a future release. -package chain - -import ( - "github.com/0chain/gosdk/zmagmacore/time" -) - -// Chain represents data structure that holds the chain data. -type Chain struct { - ID string - Version string - CreationDate time.Timestamp - OwnerID string - BlockWorker string -} - -// serverChain is the chain object of the chain the server is responsible for. -var serverChain = new(Chain) - -// SetServerChain sets the server chain object to package variable serverChain. -func SetServerChain(c *Chain) { - serverChain = c -} - -// GetServerChain returns the chain object for the server chain. -func GetServerChain() *Chain { - return serverChain -} - -// NewChain creates a new Chain. -func NewChain(id, OwnerID, blockWorker string) *Chain { - chain := Provider() - chain.ID = id - chain.OwnerID = OwnerID - chain.BlockWorker = blockWorker - return chain -} - -// Provider returns entity for chain object. -func Provider() *Chain { - c := &Chain{} - c.Version = "1.0" - c.CreationDate = time.Now() - return c -} diff --git a/zmagmacore/config/chain.go b/zmagmacore/config/chain.go deleted file mode 100644 index 872deecd3..000000000 --- a/zmagmacore/config/chain.go +++ /dev/null @@ -1,12 +0,0 @@ -// DEPRECATED: This package is deprecated and will be removed in a future release. -package config - -type ( - // ServerChain represents config describes blockchain options and signature scheme options. - ServerChain struct { - ID string `yaml:"id"` - OwnerID string `yaml:"owner_id"` - BlockWorker string `yaml:"block_worker"` - SignatureScheme string `yaml:"signature_scheme"` - } -) diff --git a/zmagmacore/config/consumer.go b/zmagmacore/config/consumer.go deleted file mode 100644 index 282024701..000000000 --- a/zmagmacore/config/consumer.go +++ /dev/null @@ -1,29 +0,0 @@ -package config - -import ( - "os" - - "gopkg.in/yaml.v3" -) - -type ( - // Consumer represents config used for registration of node. - Consumer struct { - ID string `yaml:"id"` - ExtID string `yaml:"ext_id"` - Host string `yaml:"host"` - } -) - -// Read reads config yaml file from path. -func (c *Consumer) Read(path string) error { - f, err := os.Open(path) - if err != nil { - return err - } - defer func(f *os.File) { _ = f.Close() }(f) - - decoder := yaml.NewDecoder(f) - - return decoder.Decode(c) -} diff --git a/zmagmacore/config/handler.go b/zmagmacore/config/handler.go deleted file mode 100644 index b0b10aba1..000000000 --- a/zmagmacore/config/handler.go +++ /dev/null @@ -1,14 +0,0 @@ -package config - -type ( - // Handler represents config options for handlers. - Handler struct { - RateLimit float64 `yaml:"rate_limit"` // per second - Log LogHandler `yaml:"log"` - } - - // LogHandler represents config options described in "handler.log" section of the config yaml file. - LogHandler struct { - BufLength int64 `yaml:"buf_length"` // in kilobytes - } -) diff --git a/zmagmacore/config/provider.go b/zmagmacore/config/provider.go deleted file mode 100644 index 54ff6c4cd..000000000 --- a/zmagmacore/config/provider.go +++ /dev/null @@ -1,30 +0,0 @@ -package config - -import ( - "os" - - "gopkg.in/yaml.v3" -) - -type ( - // Provider represents configs of the providers' node. - Provider struct { - ID string `yaml:"id"` - ExtID string `yaml:"ext_id"` - Host string `yaml:"host"` - MinStake int64 `yaml:"min_stake"` - } -) - -// Read reads config yaml file from path. -func (p *Provider) Read(path string) error { - f, err := os.Open(path) - if err != nil { - return err - } - defer func(f *os.File) { _ = f.Close() }(f) - - decoder := yaml.NewDecoder(f) - - return decoder.Decode(p) -} diff --git a/zmagmacore/config/workers.go b/zmagmacore/config/workers.go deleted file mode 100644 index 4c440de4c..000000000 --- a/zmagmacore/config/workers.go +++ /dev/null @@ -1,9 +0,0 @@ -package config - -type ( - // BalanceWorker represents worker options described in "workers.balance" section of the config yaml file. - BalanceWorker struct { - WaitResponseTimeout int64 `yaml:"wait_response_timeout"` // in seconds - ScrapingTime int64 `yaml:"scraping_time"` // in seconds - } -) diff --git a/zmagmacore/crypto/hash.go b/zmagmacore/crypto/hash.go deleted file mode 100644 index cffb0d197..000000000 --- a/zmagmacore/crypto/hash.go +++ /dev/null @@ -1,46 +0,0 @@ -// DEPRECATED: This package is deprecated and will be removed in a future release. -package crypto - -import ( - "encoding/hex" - - "golang.org/x/crypto/sha3" -) - -const HashLength = 32 - -type HashBytes [HashLength]byte - -// Hash computes hash of the given data using RawHash and returns result as hex decoded string. -func Hash(data interface{}) string { - return hex.EncodeToString(RawHash(data)) -} - -// RawHash computes SHA3-256 hash depending on data type and returns the hash bytes. -// -// RawHash panics if data type is unknown. -// -// Known types: -// -// - []byte -// -// - HashBytes -// -// - string -func RawHash(data interface{}) []byte { - var databuf []byte - switch dataImpl := data.(type) { - case []byte: - databuf = dataImpl - case HashBytes: - databuf = dataImpl[:] - case string: - databuf = []byte(dataImpl) - default: - panic("unknown type") - } - hash := sha3.New256() - hash.Write(databuf) - var buf []byte - return hash.Sum(buf) -} diff --git a/zmagmacore/crypto/keys.go b/zmagmacore/crypto/keys.go deleted file mode 100644 index ec8422937..000000000 --- a/zmagmacore/crypto/keys.go +++ /dev/null @@ -1,64 +0,0 @@ -package crypto - -import ( - "bufio" - "encoding/hex" - "io" - "os" - - "github.com/0chain/gosdk/core/zcncrypto" - "github.com/0chain/gosdk/zmagmacore/errors" -) - -// ReadKeysFile reads file existing in keysFile dir and parses public and private keys from file. -func ReadKeysFile(keysFile string) (publicKey, privateKey []byte, err error) { - const errCode = "read_keys" - - reader, err := os.Open(keysFile) - if err != nil { - return nil, nil, errors.Wrap(errCode, "error while open keys file", err) - } - - publicKeyHex, privateKeyHex := readKeys(reader) - err = reader.Close() - if err != nil { - return nil, nil, errors.Wrap(errCode, "error while close keys file", err) - } - publicKey, err = hex.DecodeString(publicKeyHex) - if err != nil { - return nil, nil, errors.Wrap(errCode, "error while decoding public key", err) - } - privateKey, err = hex.DecodeString(privateKeyHex) - if err != nil { - return nil, nil, errors.Wrap(errCode, "error while decoding private key", err) - } - - return publicKey, privateKey, nil -} - -// readKeys reads a publicKey and a privateKey from a io.Reader passed in args. -// They are assumed to be in two separate lines one followed by the other. -func readKeys(reader io.Reader) (publicKey string, privateKey string) { - scanner := bufio.NewScanner(reader) - scanner.Scan() - publicKey = scanner.Text() - scanner.Scan() - privateKey = scanner.Text() - scanner.Scan() - - return publicKey, privateKey -} - -// Verify verifies passed signature of the passed hash with passed public key using the signature scheme. -func Verify(publicKey, signature, hash, scheme string) (bool, error) { - signScheme := zcncrypto.NewSignatureScheme(scheme) - if signScheme != nil { - err := signScheme.SetPublicKey(publicKey) - if err != nil { - return false, err - } - return signScheme.Verify(signature, hash) - } - - return false, errors.New("invalid_signature_scheme", "invalid signature scheme") -} diff --git a/zmagmacore/doc.go b/zmagmacore/doc.go deleted file mode 100644 index 99d371946..000000000 --- a/zmagmacore/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// DEPRECATED: This package is deprecated and will be removed in a future release. -package zmagmacore diff --git a/zmagmacore/errors/errors.go b/zmagmacore/errors/errors.go deleted file mode 100644 index 28e44b3e7..000000000 --- a/zmagmacore/errors/errors.go +++ /dev/null @@ -1,91 +0,0 @@ -// DEPRECATED: This package is deprecated and will be removed in a future release. -package errors - -import ( - "errors" - "os" -) - -const ( - delim = ": " -) - -type ( - // Error type for a new application error. - Error struct { - Code string `json:"code,omitempty"` - Msg string `json:"msg"` - } -) - -type ( - // wrapper implements error wrapper interface. - errWrapper struct { - code string - text string - wrap error - } -) - -// Error implements error interface. -func (e *errWrapper) Error() string { - return e.code + delim + e.text -} - -// Unwrap implements error unwrap interface. -func (e *errWrapper) Unwrap() error { - return e.wrap -} - -// Wrap implements error wrapper interface. -func (e *errWrapper) Wrap(err error) *errWrapper { - return Wrap(e.code, e.text, err) -} - -// Any reports whether an error in error's chain -// matches to any error provided in list. -func Any(err error, targets ...error) bool { - for _, target := range targets { - if errors.Is(err, target) { - return true - } - } - - return false -} - -// ExitErr prints error to os.Stderr and call os.Exit with given code. -func ExitErr(text string, err error, code int) { - text = Wrap("exit", text, err).Error() - _, _ = os.Stderr.Write([]byte(text)) - os.Exit(code) -} - -// ExitMsg prints message to os.Stderr and call os.Exit with given code. -func ExitMsg(text string, code int) { - text = New("exit", text).Error() - _, _ = os.Stderr.Write([]byte(text)) - os.Exit(code) -} - -// Is wraps function errors.Is from stdlib to avoid import it -// in other places of the magma smart contract (magmasc) package. -func Is(err, target error) bool { - return errors.Is(err, target) -} - -// New returns constructed error wrapper interface. -func New(code, text string) *errWrapper { - return &errWrapper{code: code, text: text} -} - -// Wrap wraps given error into a new error with format. -func Wrap(code, text string, err error) *errWrapper { - wrapper := &errWrapper{code: code, text: text} - if err != nil && !errors.Is(wrapper, err) { - wrapper.wrap = err - wrapper.text += delim + err.Error() - } - - return wrapper -} diff --git a/zmagmacore/errors/errors_test.go b/zmagmacore/errors/errors_test.go deleted file mode 100644 index 0858773ae..000000000 --- a/zmagmacore/errors/errors_test.go +++ /dev/null @@ -1,243 +0,0 @@ -package errors - -import ( - "reflect" - "testing" -) - -const ( - testCode = "test_code" - testText = "test text" - wrapCode = "wrap_code" - wrapText = "wrap text" -) - -func Test_errWrapper_Error(t *testing.T) { - t.Parallel() - - tests := [1]struct { - name string - err error - want string - }{ - { - name: "OK", - err: Wrap(wrapCode, wrapText, New(testCode, testText)), - want: wrapCode + delim + wrapText + delim + testCode + delim + testText, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if got := test.err.Error(); got != test.want { - t.Errorf("Error() got: %v | want: %v", got, test.want) - } - }) - } -} - -func Test_errWrapper_Unwrap(t *testing.T) { - t.Parallel() - - err := New(testCode, testText) - - tests := [1]struct { - name string - wrapper *errWrapper - want error - }{ - { - name: "OK", - wrapper: Wrap(wrapCode, wrapText, err), - want: err, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if got := test.wrapper.Unwrap(); !reflect.DeepEqual(got, test.want) { - t.Errorf("Unwrap() got: %#v | want: %#v", got, test.want) - } - }) - } -} - -func Test_errWrapper_Wrap(t *testing.T) { - t.Parallel() - - err := New(testCode, testText) - - tests := [1]struct { - name string - error error - wrapper *errWrapper - want *errWrapper - }{ - { - name: "OK", - error: New(testCode, testText), - wrapper: New(wrapCode, wrapText), - want: &errWrapper{code: wrapCode, text: wrapText + delim + err.Error(), wrap: err}, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if got := test.wrapper.Wrap(test.error); !reflect.DeepEqual(got, test.want) { - t.Errorf("Wrap() got: %#v | want: %#v", got, test.want) - } - }) - } -} - -func Test_errAny(t *testing.T) { - t.Parallel() - - testErr := New(testCode, testText) - wrapErr := Wrap(wrapCode, wrapText, testErr) - - tests := [2]struct { - name string - list []error - wrapErr error - want bool - }{ - { - name: "TRUE", - list: []error{testErr}, - wrapErr: wrapErr, - want: true, - }, - { - name: "FALSE", - list: []error{testErr}, - wrapErr: Wrap(wrapCode, wrapText, New(testCode, testText)), - want: false, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if got := Any(test.wrapErr, test.list...); got != test.want { - t.Errorf("errIs() got: %v | want: %v", got, test.want) - } - }) - } -} - -func Test_errIs(t *testing.T) { - t.Parallel() - - testErr := New(testCode, testText) - wrapErr := Wrap(wrapCode, wrapText, testErr) - - tests := [2]struct { - name string - testErr error - wrapErr error - want bool - }{ - { - name: "TRUE", - testErr: testErr, - wrapErr: wrapErr, - want: true, - }, - { - name: "FALSE", - testErr: testErr, - wrapErr: Wrap(wrapCode, wrapText, New(testCode, testText)), - want: false, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if got := Is(test.wrapErr, test.testErr); got != test.want { - t.Errorf("errIs() got: %v | want: %v", got, test.want) - } - }) - } -} - -func Test_errNew(t *testing.T) { - t.Parallel() - - tests := [1]struct { - name string - code string - text string - want *errWrapper - }{ - { - name: "Equal", - code: testCode, - text: testText, - want: &errWrapper{code: testCode, text: testText}, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if got := New(test.code, test.text); !reflect.DeepEqual(got, test.want) { - t.Errorf("errNew() got: %#v | want: %#v", got, test.want) - } - }) - } -} - -func Test_errWrap(t *testing.T) { - t.Parallel() - - tests := [2]struct { - name string - code string - text string - wrap error - want string - }{ - { - name: "OK", - code: wrapCode, - text: wrapText, - wrap: New(testCode, testText), - want: wrapCode + delim + wrapText + delim + testCode + delim + testText, - }, - { - name: "nil_Wrap_OK", - code: wrapCode, - text: wrapText, - wrap: nil, - want: wrapCode + delim + wrapText, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if got := Wrap(test.code, test.text, test.wrap).Error(); got != test.want { - t.Errorf("errWrap() got: %#v | want: %#v", got, test.want) - } - }) - } -} diff --git a/zmagmacore/http/client.go b/zmagmacore/http/client.go deleted file mode 100644 index 318d6c14c..000000000 --- a/zmagmacore/http/client.go +++ /dev/null @@ -1,45 +0,0 @@ -// DEPRECATED: This package is deprecated and will be removed in a future release. -package http - -import ( - "net" - "net/http" - "time" - - "github.com/hashicorp/go-retryablehttp" -) - -const ( - // clientTimeout represents default http.Client timeout. - clientTimeout = 10 * time.Second - - // tlsHandshakeTimeout represents default http.Transport TLS handshake timeout. - tlsHandshakeTimeout = 5 * time.Second - - // dialTimeout represents default net.Dialer timeout. - dialTimeout = 5 * time.Second -) - -// NewClient creates default http.Client with timeouts. -func NewClient() *http.Client { - return &http.Client{ - Timeout: clientTimeout, - Transport: &http.Transport{ - TLSHandshakeTimeout: tlsHandshakeTimeout, - DialContext: (&net.Dialer{ - Timeout: dialTimeout, - }).DialContext, - }, - } -} - -// NewRetryableClient creates default retryablehttp.Client with timeouts and embedded NewClient result. -func NewRetryableClient(retryMax int) *retryablehttp.Client { - client := retryablehttp.NewClient() - client.HTTPClient = NewClient() - client.RetryWaitMax = clientTimeout - client.RetryMax = retryMax - client.Logger = nil - - return client -} diff --git a/zmagmacore/http/sc-api.go b/zmagmacore/http/sc-api.go deleted file mode 100644 index 777b69122..000000000 --- a/zmagmacore/http/sc-api.go +++ /dev/null @@ -1,106 +0,0 @@ -package http - -import ( - "crypto/sha1" - "encoding/hex" - "fmt" - "io" - "io/ioutil" - "net/http" - "net/url" - - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/util" - "github.com/0chain/gosdk/zmagmacore/errors" -) - -// MakeSCRestAPICall calls smart contract with provided address -// and makes retryable request to smart contract resource with provided relative path using params. -func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string) ([]byte, error) { - var ( - resMaxCounterBody []byte - - hashMaxCounter int - hashCounters = make(map[string]int) - - sharders = extractSharders() - - lastErrMsg string - ) - - for _, sharder := range sharders { - var ( - client = NewRetryableClient(5) - u = makeScURL(params, sharder, scAddress, relativePath) - ) - - resp, err := client.Get(u.String()) - if err != nil { - lastErrMsg = fmt.Sprintf("error while requesting sharders: %v", err) - continue - } - hash, resBody, err := hashAndBytesOfReader(resp.Body) - _ = resp.Body.Close() - if err != nil { - lastErrMsg = fmt.Sprintf("error while reading response body: %v", err) - continue - } - if resp.StatusCode != http.StatusOK { - lastErrMsg = fmt.Sprintf("response status is not OK; response body: %s", string(resBody)) - continue - } - - hashCounters[hash]++ - if hashCounters[hash] > hashMaxCounter { - hashMaxCounter = hashCounters[hash] - resMaxCounterBody = resBody - } - } - - if hashMaxCounter == 0 { - return nil, errors.New("request_sharders", "no valid responses, last err: "+lastErrMsg) - } - - return resMaxCounterBody, nil -} - -// hashAndBytesOfReader computes hash of readers data and returns hash encoded to hex and bytes of reader data. -// If error occurs while reading data from reader, it returns non nil error. -func hashAndBytesOfReader(r io.Reader) (hash string, reader []byte, err error) { - h := sha1.New() - teeReader := io.TeeReader(r, h) - readerBytes, err := ioutil.ReadAll(teeReader) - if err != nil { - return "", nil, err - } - - return hex.EncodeToString(h.Sum(nil)), readerBytes, nil -} - -// extractSharders returns string slice of randomly ordered sharders existing in the current network. -func extractSharders() []string { - nodeClient, err := client.GetNode() - if err != nil { - panic(err) - } - sharders := nodeClient.Network().Sharders - return util.GetRandom(sharders, len(sharders)) -} - -const ( - // ScRestApiUrl represents base URL path to execute smart contract rest points. - ScRestApiUrl = "v1/screst/" -) - -// makeScURL creates url.URL to make smart contract request to sharder. -func makeScURL(params map[string]string, sharder, scAddress, relativePath string) *url.URL { - uString := fmt.Sprintf("%v/%v%v%v", sharder, ScRestApiUrl, scAddress, relativePath) - u, _ := url.Parse(uString) - q := u.Query() - for k, v := range params { - q.Add(k, v) - } - u.RawQuery = q.Encode() - - return u -} diff --git a/zmagmacore/http/server.go b/zmagmacore/http/server.go deleted file mode 100644 index 2ae5de73c..000000000 --- a/zmagmacore/http/server.go +++ /dev/null @@ -1,81 +0,0 @@ -package http - -import ( - "net/http" - "net/url" - "strconv" - "strings" - "time" - - "github.com/gorilla/handlers" - "github.com/gorilla/mux" - - "github.com/0chain/gosdk/zmagmacore/config" - "github.com/0chain/gosdk/zmagmacore/log" -) - -type setupHandlers func(r *mux.Router, cfg config.Handler) - -// CreateServer creates http.Server and setups handlers. -func CreateServer(setupHandlers setupHandlers, cfg config.Handler, port int, development bool) *http.Server { - // setup CORS - router := mux.NewRouter() - setupHandlers(router, cfg) - - address := ":" + strconv.Itoa(port) - originsOk := handlers.AllowedOriginValidator(isValidOrigin) - headersOk := handlers.AllowedHeaders([]string{ - "X-Requested-With", "X-App-cmd-ID", - "X-App-cmd-Key", "Content-Type", - }) - methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"}) - - server := &http.Server{ - Addr: address, - ReadHeaderTimeout: 30 * time.Second, - WriteTimeout: 30 * time.Second, - IdleTimeout: 30 * time.Second, - MaxHeaderBytes: 1 << 20, - Handler: handlers.CORS(originsOk, headersOk, methodsOk)(router), - } - if development { // non idle & write timeouts setup to enable pprof - server.IdleTimeout = 0 - server.WriteTimeout = 0 - } - - log.Logger.Info("Ready to listen to the requests") - - return server -} - -// StartServer calls http.Server.ListenAndServe and calls app context cancel if error occurs. -func StartServer(server *http.Server, appCtxCancel func()) { - err := server.ListenAndServe() - if err != nil { - log.Logger.Warn(err.Error()) - appCtxCancel() - } -} - -func isValidOrigin(origin string) bool { - uri, err := url.Parse(origin) - if err != nil { - return false - } - - host := uri.Hostname() - switch { // allowed origins - case host == "localhost": - case host == "0chain.net": - case strings.HasSuffix(host, ".0chain.net"): - case strings.HasSuffix(host, ".alphanet-0chain.net"): - case strings.HasSuffix(host, ".devnet-0chain.net"): - case strings.HasSuffix(host, ".testnet-0chain.net"): - case strings.HasSuffix(host, ".mainnet-0chain.net"): - - default: // not allowed - return false - } - - return true -} diff --git a/zmagmacore/limiter/limiter.go b/zmagmacore/limiter/limiter.go deleted file mode 100644 index c324facc4..000000000 --- a/zmagmacore/limiter/limiter.go +++ /dev/null @@ -1,49 +0,0 @@ -// DEPRECATED: This package is deprecated and will be removed in a future release. -package limiter - -import ( - "net/http" - "time" - - "github.com/didip/tollbooth" - "github.com/didip/tollbooth/limiter" -) - -// rateLimiter represents custom wrapper above limiter.Limiter. -type rateLimit struct { - Limiter *limiter.Limiter - RateLimit bool - RequestsPerSecond float64 -} - -// userRateLimit represents application level limiter. -var userRateLimit *rateLimit - -func (rl *rateLimit) init() { - if rl.RequestsPerSecond == 0 { - rl.RateLimit = false - return - } - rl.RateLimit = true - rl.Limiter = tollbooth.NewLimiter(rl.RequestsPerSecond, &limiter.ExpirableOptions{DefaultExpirationTTL: time.Hour}). - SetIPLookups([]string{"RemoteAddr", "X-Forwarded-For", "X-Real-IP"}). - SetMethods([]string{"GET", "POST", "PUT", "DELETE"}) -} - -// ConfigRateLimits configures rate limits used in app. -// -// Should be called only once while application starting process. -func ConfigRateLimits(limit float64) { - userRateLimit = &rateLimit{RequestsPerSecond: limit} - userRateLimit.init() -} - -// UserRateLimit is a middleware that performs rate-limiting given request handler function. -func UserRateLimit(handler http.HandlerFunc) http.HandlerFunc { - if !userRateLimit.RateLimit { - return handler - } - return func(writer http.ResponseWriter, request *http.Request) { - tollbooth.LimitFuncHandler(userRateLimit.Limiter, handler).ServeHTTP(writer, request) - } -} diff --git a/zmagmacore/log/handler.go b/zmagmacore/log/handler.go deleted file mode 100644 index acafcf141..000000000 --- a/zmagmacore/log/handler.go +++ /dev/null @@ -1,58 +0,0 @@ -// DEPRECATED: This package is deprecated and will be removed in a future release. -package log - -import ( - "net/http" - "os" - "strings" - - "github.com/0chain/gosdk/core/sys" -) - -// HandleFunc returns handle function that writes logs to http.ResponseWriter with provided buffer size. -// Buffered length represented in kilobytes. -func HandleFunc(buffLen int64) func(http.ResponseWriter, *http.Request) { - return func(w http.ResponseWriter, _ *http.Request) { - file, err := os.Open(logName) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } - defer func() { - _ = file.Close() - }() - - stat, err := sys.Files.Stat(logName) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } - - var ( - writeLen = buffLen * 1024 - brokenLines = true // flag that describes existence of broken lines - ) - if writeLen > stat.Size() { - writeLen = stat.Size() - brokenLines = false - } - - buf := make([]byte, writeLen) - _, err = file.ReadAt(buf, stat.Size()-writeLen) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } - - // cut broken lines if it exist - if brokenLines { - lbInd := strings.Index(string(buf), "\n") - buf = buf[lbInd+1:] - } - - if _, err := w.Write(buf); err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } - } -} diff --git a/zmagmacore/log/logging.go b/zmagmacore/log/logging.go deleted file mode 100644 index 7348bcb08..000000000 --- a/zmagmacore/log/logging.go +++ /dev/null @@ -1,93 +0,0 @@ -package log - -import ( - "os" - - "go.uber.org/zap" - "go.uber.org/zap/zapcore" - "gopkg.in/natefinch/lumberjack.v2" - - "github.com/0chain/gosdk/zmagmacore/errors" -) - -var ( - // Logger represents main logger implementation used in app. - Logger = zap.NewNop() - - // logName - logName string -) - -// InitLogging initializes the main Logger consistent with passed log directory and level. -// -// If an error occurs during execution, the program terminates with code 2 and the error will be written in os.Stderr. -// -// InitLogging should be used only once while application is starting. -func InitLogging(development bool, logDir, level string) { - logName = logDir + "/" + "logs.log" - var ( - logWriter = getWriteSyncer(logName) - logCfg zap.Config - ) - - if development { - logCfg = zap.NewProductionConfig() - logCfg.DisableCaller = true - } else { - logCfg = zap.NewDevelopmentConfig() - logCfg.EncoderConfig.LevelKey = "level" - logCfg.EncoderConfig.NameKey = "name" - logCfg.EncoderConfig.MessageKey = "msg" - logCfg.EncoderConfig.CallerKey = "caller" - logCfg.EncoderConfig.StacktraceKey = "stacktrace" - - logWriter = zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), logWriter) - } - _ = logCfg.Level.UnmarshalText([]byte(level)) - logCfg.Encoding = consoleEncoderType - logCfg.EncoderConfig.TimeKey = "timestamp" - logCfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder - - l, err := logCfg.Build(setOutput(logWriter, logCfg)) - if err != nil { - errors.ExitErr("error while build logger config", err, 2) - } - - Logger = l -} - -const ( - jsonEncoderType = "json" - consoleEncoderType = "console" -) - -// setOutput replaces existing Core with new, that writes to passed zapcore.WriteSyncer. -func setOutput(ws zapcore.WriteSyncer, conf zap.Config) zap.Option { - var enc zapcore.Encoder - switch conf.Encoding { - case jsonEncoderType: - enc = zapcore.NewJSONEncoder(conf.EncoderConfig) - case consoleEncoderType: - enc = zapcore.NewConsoleEncoder(conf.EncoderConfig) - default: - errors.ExitMsg("error while build logger config", 2) - } - - return zap.WrapCore(func(core zapcore.Core) zapcore.Core { - return zapcore.NewCore(enc, ws, conf.Level) - }) -} - -// getWriteSyncer creates zapcore.WriteSyncer using provided log file. -func getWriteSyncer(logName string) zapcore.WriteSyncer { - var ioWriter = &lumberjack.Logger{ - Filename: logName, - MaxSize: 10, // MB - MaxBackups: 3, // number of backups - MaxAge: 28, // days - LocalTime: true, - Compress: false, // disabled by default - } - _ = ioWriter.Rotate() - return zapcore.AddSync(ioWriter) -} diff --git a/zmagmacore/magmasc/acknowledgment.go b/zmagmacore/magmasc/acknowledgment.go deleted file mode 100644 index bef901ccf..000000000 --- a/zmagmacore/magmasc/acknowledgment.go +++ /dev/null @@ -1,123 +0,0 @@ -// DEPRECATED: This package is deprecated and will be removed in a future release. -package magmasc - -import ( - "encoding/json" - - "github.com/0chain/gosdk/core/util" - "github.com/0chain/gosdk/zmagmacore/errors" - "github.com/0chain/gosdk/zmagmacore/storage" -) - -type ( - // Acknowledgment contains the necessary data obtained when the consumer - // accepts the provider terms and stores in the state of the blockchain - // as a result of performing the consumerAcceptTerms MagmaSmartContract function. - Acknowledgment struct { - SessionID string `json:"session_id"` - AccessPointID string `json:"access_point_id"` - Billing Billing `json:"billing"` - Consumer *Consumer `json:"consumer,omitempty"` - Provider *Provider `json:"provider,omitempty"` - Terms ProviderTerms `json:"terms"` - TokenPool *TokenPool `json:"token_pool,omitempty"` - } -) - -var ( - // Make sure Acknowledgment implements PoolConfigurator interface. - _ PoolConfigurator = (*Acknowledgment)(nil) - - // Make sure Acknowledgment implements Value interface. - _ storage.Value = (*Acknowledgment)(nil) - - // Make sure Acknowledgment implements Serializable interface. - _ util.Serializable = (*Acknowledgment)(nil) -) - -// ActiveKey returns key used for operations with storage.Storage -// AcknowledgmentPrefix + AcknowledgmentActivePrefixPart + Acknowledgment.SessionID. -func (m *Acknowledgment) ActiveKey() []byte { - return []byte(AcknowledgmentPrefix + AcknowledgmentActivePrefixPart + m.SessionID) -} - -// Decode implements util.Serializable interface. -func (m *Acknowledgment) Decode(blob []byte) error { - var ackn Acknowledgment - if err := json.Unmarshal(blob, &ackn); err != nil { - return errDecodeData.Wrap(err) - } - if err := ackn.Validate(); err != nil { - return err - } - - m.SessionID = ackn.SessionID - m.AccessPointID = ackn.AccessPointID - m.Billing = ackn.Billing - m.Consumer = ackn.Consumer - m.Provider = ackn.Provider - m.Terms = ackn.Terms - m.TokenPool = ackn.TokenPool - - return nil -} - -// Encode implements util.Serializable interface. -func (m *Acknowledgment) Encode() []byte { - blob, _ := json.Marshal(m) - return blob -} - -// Key returns key with AcknowledgmentPrefix. -// Used for operations with storage.Storage. -func (m *Acknowledgment) Key() []byte { - return []byte(AcknowledgmentPrefix + m.SessionID) -} - -// PoolBalance implements PoolConfigurator interface. -func (m *Acknowledgment) PoolBalance() uint64 { - return m.Terms.GetAmount() -} - -// PoolID implements PoolConfigurator interface. -func (m *Acknowledgment) PoolID() string { - return m.SessionID -} - -// PoolHolderID implements PoolConfigurator interface. -func (m *Acknowledgment) PoolHolderID() string { - return Address -} - -// PoolPayerID implements PoolConfigurator interface. -func (m *Acknowledgment) PoolPayerID() string { - return m.Consumer.ID -} - -// PoolPayeeID implements PoolConfigurator interface. -func (m *Acknowledgment) PoolPayeeID() string { - return m.Provider.ID -} - -// Validate checks Acknowledgment for correctness. -// If it is not return errInvalidAcknowledgment. -func (m *Acknowledgment) Validate() (err error) { - switch { // is invalid - case m.SessionID == "": - err = errors.New(errCodeBadRequest, "session id is required") - - case m.AccessPointID == "": - err = errors.New(errCodeBadRequest, "access point id is required") - - case m.Consumer == nil || m.Consumer.ExtID == "": - err = errors.New(errCodeBadRequest, "consumer external id is required") - - case m.Provider == nil || m.Provider.ExtID == "": - err = errors.New(errCodeBadRequest, "provider external id is required") - - default: - return nil // is valid - } - - return errInvalidAcknowledgment.Wrap(err) -} diff --git a/zmagmacore/magmasc/acknowledgment_test.go b/zmagmacore/magmasc/acknowledgment_test.go deleted file mode 100644 index 1e0a821f1..000000000 --- a/zmagmacore/magmasc/acknowledgment_test.go +++ /dev/null @@ -1,173 +0,0 @@ -package magmasc - -import ( - "encoding/json" - "reflect" - "testing" -) - -func Test_Acknowledgment_Decode(t *testing.T) { - t.Parallel() - - ackn := mockAcknowledgment() - blob, err := json.Marshal(ackn) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - acknInvalid := mockAcknowledgment() - acknInvalid.SessionID = "" - blobInvalid, err := json.Marshal(acknInvalid) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - tests := [3]struct { - name string - blob []byte - want *Acknowledgment - error bool - }{ - { - name: "OK", - blob: blob, - want: ackn, - error: false, - }, - { - name: "Decode_ERR", - blob: []byte(":"), // invalid json - want: &Acknowledgment{}, - error: true, - }, - { - name: "Invalid_ERR", - blob: blobInvalid, - want: &Acknowledgment{}, - error: true, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - got := &Acknowledgment{} - if err := got.Decode(test.blob); (err != nil) != test.error { - t.Errorf("Decode() error: %v | want: %v", err, test.error) - return - } - if !reflect.DeepEqual(got, test.want) { - t.Errorf("Decode() got: %#v | want: %#v", got, test.want) - } - }) - } -} - -func Test_Acknowledgment_Encode(t *testing.T) { - t.Parallel() - - ackn := mockAcknowledgment() - blob, err := json.Marshal(ackn) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - tests := [1]struct { - name string - ackn *Acknowledgment - want []byte - }{ - { - name: "OK", - ackn: ackn, - want: blob, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if got := test.ackn.Encode(); !reflect.DeepEqual(got, test.want) { - t.Errorf("Encode() got: %#v | want: %#v", got, test.want) - } - }) - } -} - -func Test_Acknowledgment_Key(t *testing.T) { - t.Parallel() - - t.Run("OK", func(t *testing.T) { - t.Parallel() - - ackn := mockAcknowledgment() - want := []byte(AcknowledgmentPrefix + ackn.SessionID) - - if got := ackn.Key(); !reflect.DeepEqual(got, want) { - t.Errorf("Key() got: %v | want: %v", string(got), string(want)) - } - }) -} - -func Test_Acknowledgment_Validate(t *testing.T) { - t.Parallel() - - acknEmptySessionID := mockAcknowledgment() - acknEmptySessionID.SessionID = "" - - acknEmptyAccessPointID := mockAcknowledgment() - acknEmptyAccessPointID.AccessPointID = "" - - acknEmptyConsumerExtID := mockAcknowledgment() - acknEmptyConsumerExtID.Consumer.ExtID = "" - - acknEmptyProviderExtID := mockAcknowledgment() - acknEmptyProviderExtID.Provider.ExtID = "" - - tests := [5]struct { - name string - ackn *Acknowledgment - error bool - }{ - { - name: "OK", - ackn: mockAcknowledgment(), - error: false, - }, - { - name: "Empty_Session_ID", - ackn: acknEmptySessionID, - error: true, - }, - { - name: "Empty_Access_Point_ID", - ackn: acknEmptyAccessPointID, - error: true, - }, - { - name: "Empty_Consumer_Ext_ID", - ackn: acknEmptyConsumerExtID, - error: true, - }, - { - name: "Empty_Provider_Txt_ID", - ackn: acknEmptyProviderExtID, - error: true, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if err := test.ackn.Validate(); (err != nil) != test.error { - t.Errorf("validate() error: %v | want: %v", err, test.error) - } - }) - } -} diff --git a/zmagmacore/magmasc/actions.go b/zmagmacore/magmasc/actions.go deleted file mode 100644 index d164866e9..000000000 --- a/zmagmacore/magmasc/actions.go +++ /dev/null @@ -1,284 +0,0 @@ -package magmasc - -import ( - "context" - "encoding/json" - - "github.com/0chain/gosdk/zmagmacore/transaction" -) - -// ExecuteSessionStart starts session for provided IDs by executing ConsumerSessionStartFuncName. -func ExecuteSessionStart(ctx context.Context, sessID string) (*Acknowledgment, error) { - txn, err := transaction.NewTransactionEntity() - if err != nil { - return nil, err - } - - ackn, err := RequestAcknowledgment(sessID) - if err != nil { - return nil, err - } - input, err := json.Marshal(&ackn) - if err != nil { - return nil, err - } - txnHash, err := txn.ExecuteSmartContract( - ctx, - Address, - ConsumerSessionStartFuncName, - string(input), - ackn.Terms.GetAmount(), - ) - if err != nil { - return nil, err - } - - txn, err = transaction.VerifyTransaction(ctx, txnHash) - if err != nil { - return nil, err - } - - ackn = new(Acknowledgment) - if err := json.Unmarshal([]byte(txn.TransactionOutput), &ackn); err != nil { - return nil, err - } - - return ackn, err -} - -// ExecuteDataUsage executes ProviderDataUsageFuncName and returns current Acknowledgment. -func ExecuteDataUsage( - ctx context.Context, downloadBytes, uploadBytes uint64, sessID string, sessTime uint32) (*Acknowledgment, error) { - - txn, err := transaction.NewTransactionEntity() - if err != nil { - return nil, err - } - - dataUsage := DataUsage{ - DownloadBytes: downloadBytes, - UploadBytes: uploadBytes, - SessionID: sessID, - SessionTime: sessTime, - } - input, err := json.Marshal(&dataUsage) - if err != nil { - return nil, err - } - txnHash, err := txn.ExecuteSmartContract(ctx, Address, ProviderDataUsageFuncName, string(input), 0) - if err != nil { - return nil, err - } - - txn, err = transaction.VerifyTransaction(ctx, txnHash) - if err != nil { - return nil, err - } - - ackn := Acknowledgment{} - if err = json.Unmarshal([]byte(txn.TransactionOutput), &ackn); err != nil { - return nil, err - } - - return &ackn, err -} - -// ExecuteSessionStop requests Acknowledgment from the blockchain and executes ConsumerSessionStopFuncName -// and verifies including the transaction in the blockchain. -// -// Returns Acknowledgment for session with provided ID. -func ExecuteSessionStop(ctx context.Context, sessionID string) (*Acknowledgment, error) { - txn, err := transaction.NewTransactionEntity() - if err != nil { - return nil, err - } - - // need to respond billing to compute value of txn - ackn, err := RequestAcknowledgment(sessionID) - if err != nil { - return nil, err - } - - input, err := json.Marshal(&ackn) - if err != nil { - return nil, err - } - txnHash, err := txn.ExecuteSmartContract( - ctx, - Address, - ConsumerSessionStopFuncName, - string(input), - ackn.Billing.Amount, - ) - if err != nil { - return nil, err - } - - txn, err = transaction.VerifyTransaction(ctx, txnHash) - if err != nil { - return nil, err - } - - ackn = new(Acknowledgment) - if err := json.Unmarshal([]byte(txn.TransactionOutput), ackn); err != nil { - return nil, err - } - - return ackn, err -} - -// ExecuteProviderRegister executes provider registration magma sc function and returns current Provider. -func ExecuteProviderRegister(ctx context.Context, provider *Provider) (*Provider, error) { - txn, err := transaction.NewTransactionEntity() - if err != nil { - return nil, err - } - - input, err := json.Marshal(provider) - if err != nil { - return nil, err - } - txnHash, err := txn.ExecuteSmartContract(ctx, Address, ProviderRegisterFuncName, string(input), 0) - if err != nil { - return nil, err - } - - txn, err = transaction.VerifyTransaction(ctx, txnHash) - if err != nil { - return nil, err - } - - provider = &Provider{} - if err = json.Unmarshal([]byte(txn.TransactionOutput), provider); err != nil { - return nil, err - } - - return provider, nil -} - -// ExecuteProviderUpdate executes update provider magma sc function and returns updated Provider. -func ExecuteProviderUpdate(ctx context.Context, provider *Provider) (*Provider, error) { - txn, err := transaction.NewTransactionEntity() - if err != nil { - return nil, err - } - - input := provider.Encode() - hash, err := txn.ExecuteSmartContract(ctx, Address, ProviderUpdateFuncName, string(input), 0) - if err != nil { - return nil, err - } - - txn, err = transaction.VerifyTransaction(ctx, hash) - if err != nil { - return nil, err - } - - provider = new(Provider) - if err := json.Unmarshal([]byte(txn.TransactionOutput), provider); err != nil { - return nil, err - } - - return provider, nil -} - -// ExecuteConsumerRegister executes consumer registration magma sc function and returns current Consumer. -func ExecuteConsumerRegister(ctx context.Context, consumer *Consumer) (*Consumer, error) { - txn, err := transaction.NewTransactionEntity() - if err != nil { - return nil, err - } - - input, err := json.Marshal(consumer) - if err != nil { - return nil, err - } - txnHash, err := txn.ExecuteSmartContract(ctx, Address, ConsumerRegisterFuncName, string(input), 0) - if err != nil { - return nil, err - } - - txn, err = transaction.VerifyTransaction(ctx, txnHash) - if err != nil { - return nil, err - } - - consumer = &Consumer{} - if err = json.Unmarshal([]byte(txn.TransactionOutput), consumer); err != nil { - return nil, err - } - - return consumer, nil -} - -// ExecuteConsumerUpdate executes update consumer magma sc function and returns updated Consumer. -func ExecuteConsumerUpdate(ctx context.Context, consumer *Consumer) (*Consumer, error) { - txn, err := transaction.NewTransactionEntity() - if err != nil { - return nil, err - } - - input := consumer.Encode() - hash, err := txn.ExecuteSmartContract(ctx, Address, ConsumerUpdateFuncName, string(input), 0) - if err != nil { - return nil, err - } - - txn, err = transaction.VerifyTransaction(ctx, hash) - if err != nil { - return nil, err - } - - consumer = new(Consumer) - if err := json.Unmarshal([]byte(txn.TransactionOutput), consumer); err != nil { - return nil, err - } - - return consumer, nil -} - -// ExecuteSessionInit executes session init magma sc function and returns Acknowledgment. -func ExecuteSessionInit(ctx context.Context, consExtID, provExtID, apID, sessID string, terms ProviderTerms) (*Acknowledgment, error) { - txn, err := transaction.NewTransactionEntity() - if err != nil { - return nil, err - } - - ackn := Acknowledgment{ - Consumer: &Consumer{ - ExtID: consExtID, - }, - Provider: &Provider{ - ExtID: provExtID, - }, - AccessPointID: apID, - SessionID: sessID, - Terms: terms, - } - input, err := json.Marshal(&ackn) - if err != nil { - return nil, err - } - txnHash, err := txn.ExecuteSmartContract( - ctx, - Address, - ProviderSessionInitFuncName, - string(input), - 0, - ) - if err != nil { - return nil, err - } - - txn, err = transaction.VerifyTransaction(ctx, txnHash) - if err != nil { - return nil, err - } - - ackn = Acknowledgment{} - if err := json.Unmarshal([]byte(txn.TransactionOutput), &ackn); err != nil { - return nil, err - } - - return &ackn, err -} diff --git a/zmagmacore/magmasc/api.go b/zmagmacore/magmasc/api.go deleted file mode 100644 index eda97d70b..000000000 --- a/zmagmacore/magmasc/api.go +++ /dev/null @@ -1,180 +0,0 @@ -package magmasc - -import ( - "encoding/json" - - "github.com/0chain/gosdk/zmagmacore/http" -) - -// GetAllConsumers makes smart contract rest api call to magma smart contract -// GetAllConsumersRP rest point to retrieve all registered consumer.Consumer. -func GetAllConsumers() ([]Consumer, error) { - resp, err := http.MakeSCRestAPICall(Address, GetAllConsumersRP, nil) - if err != nil { - return nil, err - } - - consumers := make([]Consumer, 0) - if err = json.Unmarshal(resp, &consumers); err != nil { - return nil, err - } - - return consumers, err -} - -// GetAllProviders makes smart contract rest api call to magma smart contract -// GetAllProvidersRP rest point to retrieve all registered provider.Provider. -func GetAllProviders() ([]Provider, error) { - resp, err := http.MakeSCRestAPICall(Address, GetAllProvidersRP, nil) - if err != nil { - return nil, err - } - - providers := make([]Provider, 0) - if err = json.Unmarshal(resp, &providers); err != nil { - return nil, err - } - - return providers, err -} - -// RequestAcknowledgment makes smart contract rest api call to magma smart contract -// AcknowledgmentRP rest point to retrieve Acknowledgment. -func RequestAcknowledgment(sessionID string) (*Acknowledgment, error) { - params := map[string]string{ - "id": sessionID, - } - - blob, err := http.MakeSCRestAPICall(Address, AcknowledgmentRP, params) - if err != nil { - return nil, err - } - - ackn := Acknowledgment{} - if err = json.Unmarshal(blob, &ackn); err != nil { - return nil, err - } - - return &ackn, nil -} - -// IsAcknowledgmentExist makes smart contract rest api call to magma smart contract -// IsAcknowledgmentExistRP rest point to ensure that Acknowledgment with provided session ID exist in the blockchain. -func IsAcknowledgmentExist(sessionID string) (bool, error) { - params := map[string]string{ - "id": sessionID, - } - - blob, err := http.MakeSCRestAPICall(Address, IsAcknowledgmentExistRP, params) - if err != nil { - return false, err - } - - var exist bool - if err = json.Unmarshal(blob, &exist); err != nil { - return false, err - } - - return exist, nil -} - -// VerifyAcknowledgmentAccepted makes smart contract rest api call to magma smart contract -// VerifyAcknowledgmentAcceptedRP rest point to ensure that Acknowledgment with provided IDs was accepted. -func VerifyAcknowledgmentAccepted(sessionID, accessPointID, consumerExtID, providerExtID string) (*Acknowledgment, error) { - params := map[string]string{ - "session_id": sessionID, - "access_point_id": accessPointID, - "provider_ext_id": providerExtID, - "consumer_ext_id": consumerExtID, - } - - blob, err := http.MakeSCRestAPICall(Address, VerifyAcknowledgmentAcceptedRP, params) - if err != nil { - return nil, err - } - - ackn := Acknowledgment{} - if err = json.Unmarshal(blob, &ackn); err != nil { - return nil, err - } - - return &ackn, nil -} - -// ConsumerFetch makes smart contract rest api call to magma smart contract -// ConsumerFetchRP rest point to fetch Consumer info. -func ConsumerFetch(id string) (*Consumer, error) { - params := map[string]string{ - "ext_id": id, - } - - blob, err := http.MakeSCRestAPICall(Address, ConsumerFetchRP, params) - if err != nil { - return nil, err - } - - cons := Consumer{} - if err = json.Unmarshal(blob, &cons); err != nil { - return nil, err - } - - return &cons, nil -} - -// ProviderFetch makes smart contract rest api call to magma smart contract -// ProviderFetchRP rest point to fetch Provider info. -func ProviderFetch(id string) (*Provider, error) { - params := map[string]string{ - "ext_id": id, - } - - blob, err := http.MakeSCRestAPICall(Address, ProviderFetchRP, params) - if err != nil { - return nil, err - } - - prov := Provider{} - if err = json.Unmarshal(blob, &prov); err != nil { - return nil, err - } - - return &prov, nil -} - -// IsConsumerRegisteredRP makes smart contract rest api call to magma smart contract -// ConsumerRegisteredRP rest point to check registration of the consumer with provided external ID. -func IsConsumerRegisteredRP(extID string) (bool, error) { - params := map[string]string{ - "ext_id": extID, - } - registeredByt, err := http.MakeSCRestAPICall(Address, ConsumerRegisteredRP, params) - if err != nil { - return false, err - } - - var registered bool - if err := json.Unmarshal(registeredByt, ®istered); err != nil { - return false, err - } - - return registered, nil -} - -// IsProviderRegisteredRP makes smart contract rest api call to magma smart contract -// ProviderRegisteredRP rest point to check registration of the provider with provided external ID. -func IsProviderRegisteredRP(extID string) (bool, error) { - params := map[string]string{ - "ext_id": extID, - } - registeredByt, err := http.MakeSCRestAPICall(Address, ProviderRegisteredRP, params) - if err != nil { - return false, err - } - - var registered bool - if err := json.Unmarshal(registeredByt, ®istered); err != nil { - return false, err - } - - return registered, nil -} diff --git a/zmagmacore/magmasc/billing.go b/zmagmacore/magmasc/billing.go deleted file mode 100644 index f28daf85f..000000000 --- a/zmagmacore/magmasc/billing.go +++ /dev/null @@ -1,79 +0,0 @@ -package magmasc - -import ( - "encoding/json" - - "github.com/0chain/gosdk/core/util" - "github.com/0chain/gosdk/zmagmacore/errors" - "github.com/0chain/gosdk/zmagmacore/time" -) - -type ( - // Billing represents all info about data usage. - Billing struct { - Amount uint64 `json:"amount"` - DataUsage DataUsage `json:"data_usage"` - CompletedAt time.Timestamp `json:"completed_at,omitempty"` - } -) - -var ( - // Make sure Billing implements Serializable interface. - _ util.Serializable = (*Billing)(nil) -) - -// CalcAmount calculates and sets the billing Amount value by given price. -// NOTE: the cost value must be represented in token units per megabyte. -func (m *Billing) CalcAmount(terms ProviderTerms) { - price := float64(terms.GetPrice()) - if price > 0 { - // data usage summary in megabytes - mbps := float64(m.DataUsage.UploadBytes+m.DataUsage.DownloadBytes) / million - m.Amount = uint64(mbps * price) // rounded amount of megabytes multiplied by price - } - if minCost := terms.GetMinCost(); m.Amount < minCost { - m.Amount = minCost - } -} - -// Decode implements util.Serializable interface. -func (m *Billing) Decode(blob []byte) error { - var bill Billing - if err := json.Unmarshal(blob, &bill); err != nil { - return errDecodeData.Wrap(err) - } - - m.Amount = bill.Amount - m.DataUsage = bill.DataUsage - m.CompletedAt = bill.CompletedAt - - return nil -} - -// Encode implements util.Serializable interface. -func (m *Billing) Encode() []byte { - blob, _ := json.Marshal(m) - return blob -} - -// Validate checks given data usage is correctness for the billing. -func (m *Billing) Validate(dataUsage *DataUsage) (err error) { - switch { - case dataUsage == nil: - err = errors.New(errCodeBadRequest, "data usage required") - - case m.DataUsage.SessionTime > dataUsage.SessionTime: - err = errors.New(errCodeBadRequest, "invalid session time") - - case m.DataUsage.UploadBytes > dataUsage.UploadBytes: - err = errors.New(errCodeBadRequest, "invalid upload bytes") - - case m.DataUsage.DownloadBytes > dataUsage.DownloadBytes: - err = errors.New(errCodeBadRequest, "invalid download bytes") - - default: - return nil // is valid - everything is ok - } - - return errInvalidDataUsage.Wrap(err) -} diff --git a/zmagmacore/magmasc/billing_test.go b/zmagmacore/magmasc/billing_test.go deleted file mode 100644 index fdce86e09..000000000 --- a/zmagmacore/magmasc/billing_test.go +++ /dev/null @@ -1,221 +0,0 @@ -package magmasc - -import ( - "encoding/json" - "reflect" - "testing" - - "github.com/0chain/gosdk/zmagmacore/time" -) - -func Test_Billing_CalcAmount(t *testing.T) { - t.Parallel() - - bill, terms := mockBilling(), mockProviderTerms() - - termsMinCost := mockProviderTerms() - termsMinCost.MinCost = 1000 - - // data usage summary in megabytes - mbps := float64(bill.DataUsage.UploadBytes+bill.DataUsage.DownloadBytes) / million - want := uint64(mbps * float64(terms.GetPrice())) - - tests := [3]struct { - name string - bill Billing - terms ProviderTerms - want uint64 - }{ - { - name: "OK", - bill: bill, - terms: terms, - want: want, - }, - { - name: "Zero_Amount_OK", - bill: mockBilling(), - terms: ProviderTerms{}, - want: 0, - }, - { - name: "Min_Cost_Amount_OK", - bill: mockBilling(), - terms: termsMinCost, - want: termsMinCost.GetMinCost(), - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if test.bill.Amount != 0 { // must be zero before first call CalcAmount() - t.Errorf("Billing.Amount is: %v | want: %v", test.bill.Amount, 0) - } - - test.bill.CalcAmount(test.terms) - if test.bill.Amount != test.want { // must be the same value with test.want after called CalcAmount() - t.Errorf("GetVolume() got: %v | want: %v", test.bill.Amount, test.want) - } - }) - } -} - -func Test_Billing_Decode(t *testing.T) { - t.Parallel() - - bill := mockBilling() - blob, err := json.Marshal(bill) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - billCompleted := mockBilling() - billCompleted.CalcAmount(mockProviderTerms()) - billCompleted.CompletedAt = time.Now() - blobCompleted, err := json.Marshal(billCompleted) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - tests := [3]struct { - name string - blob []byte - want Billing - error bool - }{ - { - name: "OK", - blob: blob, - want: bill, - error: false, - }, - { - name: "Completed_OK", - blob: blobCompleted, - want: billCompleted, - error: false, - }, - { - name: "Decode_ERR", - blob: []byte(":"), // invalid json - want: Billing{}, - error: true, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - got := Billing{} - if err := got.Decode(test.blob); (err != nil) != test.error { - t.Errorf("Decode() error: %v | want: %v", err, test.error) - return - } - if !reflect.DeepEqual(got, test.want) { - t.Errorf("Decode() got: %#v | want: %#v", got, test.want) - } - }) - } -} - -func Test_Billing_Encode(t *testing.T) { - t.Parallel() - - bill := mockBilling() - blob, err := json.Marshal(bill) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - tests := [1]struct { - name string - bill Billing - want []byte - }{ - { - name: "OK", - bill: bill, - want: blob, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if got := test.bill.Encode(); !reflect.DeepEqual(got, test.want) { - t.Errorf("Encode() got: %#v | want: %#v", got, test.want) - } - }) - } -} - -func Test_Billing_Validate(t *testing.T) { - t.Parallel() - - bill, dataUsage := mockBilling(), mockDataUsage() - - duInvalidSessionTime := mockDataUsage() - duInvalidSessionTime.SessionTime = bill.DataUsage.SessionTime - 1 - - duInvalidUploadBytes := mockDataUsage() - duInvalidUploadBytes.UploadBytes = bill.DataUsage.UploadBytes - 1 - - duInvalidDownloadBytes := mockDataUsage() - duInvalidDownloadBytes.DownloadBytes = bill.DataUsage.DownloadBytes - 1 - - tests := [5]struct { - name string - du *DataUsage - bill Billing - error bool - }{ - { - name: "OK", - du: &dataUsage, - bill: bill, - error: false, - }, - { - name: "nil_Data_Usage_ERR", - du: nil, - bill: bill, - error: true, - }, - { - name: "Invalid_Session_Time_ERR", - du: &duInvalidSessionTime, - bill: bill, - error: true, - }, - { - name: "Invalid_Upload_Bytes_ERR", - du: &duInvalidUploadBytes, - bill: bill, - error: true, - }, - { - name: "Invalid_Download_Bytes_ERR", - du: &duInvalidDownloadBytes, - bill: bill, - error: true, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if err := test.bill.Validate(test.du); (err != nil) != test.error { - t.Errorf("validate() error: %v | want: %v", err, test.error) - } - }) - } -} diff --git a/zmagmacore/magmasc/const.go b/zmagmacore/magmasc/const.go deleted file mode 100644 index 3c2d46afc..000000000 --- a/zmagmacore/magmasc/const.go +++ /dev/null @@ -1,111 +0,0 @@ -package magmasc - -const ( - // Address represents the address of the Magma smart contract. - // Used while making requests to smart contract's rest points and executing smart contracts functions. - Address = "11f8411db41e34cea7c100f19faff32da8f3cd5a80635731cec06f32d08089be" - - // GetAllConsumersRP represents MagmaSC relative path. - // Used to list all registered in the blockchain consumers. - GetAllConsumersRP = "/allConsumers" - - // GetAllProvidersRP represents MagmaSC relative path. - // Used to list all registered in the blockchain providers. - GetAllProvidersRP = "/allProviders" -) - -const ( - // AcknowledgmentPrefix represents prefix to save into storage. - AcknowledgmentPrefix = "ackn" - - // AcknowledgmentActivePrefixPart represents prefix part to save into storage. - AcknowledgmentActivePrefixPart = "act" - - // AcknowledgmentRP represents MagmaSC relative path. - // Used to retrieve accepted acknowledgment. - AcknowledgmentRP = "/acknowledgmentAccepted" - - // IsAcknowledgmentExistRP represents MagmaSC relative path. - // Used to check existing of acknowledgment. - IsAcknowledgmentExistRP = "/acknowledgmentExist" - - // VerifyAcknowledgmentAcceptedRP represents MagmaSC relative path. - // Used to verify accepting Provider's terms by Consumer. - VerifyAcknowledgmentAcceptedRP = "/acknowledgmentAcceptedVerify" -) - -const ( - // ConsumerRegisterFuncName represents MagmaSC function. - // Used to register bandwidth-marketplace's node. - ConsumerRegisterFuncName = "consumer_register" - - // ConsumerSessionStartFuncName represents MagmaSC function. - // Used to start session. - ConsumerSessionStartFuncName = "consumer_session_start" - - // ConsumerSessionStopFuncName represents MagmaSC function. - // Used to stop session. - ConsumerSessionStopFuncName = "consumer_session_stop" - - // ConsumerUpdateFuncName represents MagmaSC function. - // Used to update consumer node info. - ConsumerUpdateFuncName = "consumer_update" - - // ConsumerFetchRP represents MagmaSC relative path. - // Used to fetch consumer info. - ConsumerFetchRP = "/consumerFetch" - - // ConsumerRegisteredRP represents MagmaSC relative path. - // Used to fetch consumer registered info. - ConsumerRegisteredRP = "/consumerExist" - - // consumerType contents a value of consumer node type. - consumerType = "consumer" -) - -const ( - // TermsExpiredDuration represents value for - // minimal duration of provider terms that will pass check it's expired. - TermsExpiredDuration = 1 * 60 // 1 minute - - // ProviderDataUsageFuncName represents MagmaSC function. - // Used to update session info about data usages and collecting payments data - // from consumer to provider. - ProviderDataUsageFuncName = "provider_data_usage" - - // ProviderRegisterFuncName represents MagmaSC function. - // Used to register bandwidth-marketplace's node. - ProviderRegisterFuncName = "provider_register" - - // ProviderUpdateFuncName represents MagmaSC function. - // Used for updating provider terms. - ProviderUpdateFuncName = "provider_update" - - // ProviderSessionInitFuncName represents MagmaSC function. - // Used for initializing session by a provider. - ProviderSessionInitFuncName = "provider_session_init" - - // ProviderFetchRP represents MagmaSC relative path. - // Used to fetch provider info. - ProviderFetchRP = "/providerFetch" - - // ProviderRegisteredRP represents MagmaSC relative path. - // Used to fetch provider registered info. - ProviderRegisteredRP = "/providerExist" - - // providerType contents a value of provider node type. - providerType = "provider" -) - -const ( - // one billion (Giga) is a unit prefix in metric systems - // of units denoting a factor of one billion (1e9 or 1_000_000_000). - billion = 1e9 - - // one million (Mega) is a unit prefix in metric systems - // of units denoting a factor of one million (1e6 or 1_000_000). - million = 1e6 - - // octet represents number of bits in an octet. - octet = 8 -) diff --git a/zmagmacore/magmasc/consumer.go b/zmagmacore/magmasc/consumer.go deleted file mode 100644 index 988ac715d..000000000 --- a/zmagmacore/magmasc/consumer.go +++ /dev/null @@ -1,75 +0,0 @@ -package magmasc - -import ( - "encoding/json" - - "github.com/0chain/gosdk/core/util" - "github.com/0chain/gosdk/zmagmacore/config" - "github.com/0chain/gosdk/zmagmacore/errors" - "github.com/0chain/gosdk/zmagmacore/node" -) - -type ( - // Consumer represents consumers node stored in blockchain. - Consumer struct { - ID string `json:"id"` - ExtID string `json:"ext_id"` - Host string `json:"host,omitempty"` - } -) - -var ( - // Make sure Consumer implements Serializable interface. - _ util.Serializable = (*Consumer)(nil) -) - -// NewConsumerFromCfg creates Consumer from config.Consumer. -func NewConsumerFromCfg(cfg *config.Consumer) *Consumer { - return &Consumer{ - ID: node.ID(), - ExtID: cfg.ExtID, - Host: cfg.Host, - } -} - -// Decode implements util.Serializable interface. -func (m *Consumer) Decode(blob []byte) error { - var consumer Consumer - if err := json.Unmarshal(blob, &consumer); err != nil { - return errDecodeData.Wrap(err) - } - if err := consumer.Validate(); err != nil { - return err - } - - m.ID = consumer.ID - m.ExtID = consumer.ExtID - m.Host = consumer.Host - - return nil -} - -// Encode implements util.Serializable interface. -func (m *Consumer) Encode() []byte { - blob, _ := json.Marshal(m) - return blob -} - -// GetType returns node type. -func (m *Consumer) GetType() string { - return consumerType -} - -// Validate checks the Consumer for correctness. -// If it is not return errInvalidConsumer. -func (m *Consumer) Validate() (err error) { - switch { // is invalid - case m.ExtID == "": - err = errors.New(errCodeBadRequest, "consumer external id is required") - - default: - return nil // is valid - } - - return errInvalidConsumer.Wrap(err) -} diff --git a/zmagmacore/magmasc/consumer_test.go b/zmagmacore/magmasc/consumer_test.go deleted file mode 100644 index 424e7cc0e..000000000 --- a/zmagmacore/magmasc/consumer_test.go +++ /dev/null @@ -1,146 +0,0 @@ -package magmasc - -import ( - "encoding/json" - "reflect" - "testing" -) - -func Test_Consumer_Decode(t *testing.T) { - t.Parallel() - - cons := mockConsumer() - blob, err := json.Marshal(cons) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - consInvalid := mockConsumer() - consInvalid.ExtID = "" - extIDBlobInvalid, err := json.Marshal(consInvalid) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - tests := [3]struct { - name string - blob []byte - want *Consumer - error bool - }{ - { - name: "OK", - blob: blob, - want: cons, - error: false, - }, - { - name: "Decode_ERR", - blob: []byte(":"), // invalid json - want: &Consumer{}, - error: true, - }, - { - name: "Ext_ID_Invalid_ERR", - blob: extIDBlobInvalid, - want: &Consumer{}, - error: true, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - got := &Consumer{} - if err := got.Decode(test.blob); (err != nil) != test.error { - t.Errorf("Decode() error: %v | want: %v", err, nil) - } - if !reflect.DeepEqual(got, test.want) { - t.Errorf("Decode() got: %#v | want: %#v", got, test.want) - } - }) - } -} - -func Test_Consumer_Encode(t *testing.T) { - t.Parallel() - - cons := mockConsumer() - blob, err := json.Marshal(cons) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - tests := [1]struct { - name string - cons *Consumer - want []byte - }{ - { - name: "OK", - cons: cons, - want: blob, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if got := test.cons.Encode(); !reflect.DeepEqual(got, test.want) { - t.Errorf("Encode() got: %#v | want: %#v", got, test.want) - } - }) - } -} - -func Test_Consumer_GetType(t *testing.T) { - t.Parallel() - - t.Run("OK", func(t *testing.T) { - t.Parallel() - - cons := Consumer{} - if got := cons.GetType(); got != consumerType { - t.Errorf("GetType() got: %v | want: %v", got, consumerType) - } - }) -} - -func Test_Consumer_Validate(t *testing.T) { - t.Parallel() - - consEmptyExtID := mockConsumer() - consEmptyExtID.ExtID = "" - - tests := [2]struct { - name string - cons *Consumer - error bool - }{ - { - name: "OK", - cons: mockConsumer(), - error: false, - }, - { - name: "Empty_Ext_ID_ERR", - cons: consEmptyExtID, - error: true, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if err := test.cons.Validate(); (err != nil) != test.error { - t.Errorf("validate() error: %v | want: %v", err, test.error) - } - }) - } -} diff --git a/zmagmacore/magmasc/datausage.go b/zmagmacore/magmasc/datausage.go deleted file mode 100644 index 6e33daf0a..000000000 --- a/zmagmacore/magmasc/datausage.go +++ /dev/null @@ -1,60 +0,0 @@ -package magmasc - -import ( - "encoding/json" - - "github.com/0chain/gosdk/core/util" - "github.com/0chain/gosdk/zmagmacore/errors" -) - -type ( - // DataUsage represents session data sage implementation. - DataUsage struct { - DownloadBytes uint64 `json:"download_bytes"` - UploadBytes uint64 `json:"upload_bytes"` - SessionID string `json:"session_id"` - SessionTime uint32 `json:"session_time"` - } -) - -var ( - // Make sure DataUsage implements Serializable interface. - _ util.Serializable = (*DataUsage)(nil) -) - -// Decode implements util.Serializable interface. -func (m *DataUsage) Decode(blob []byte) error { - var dataUsage DataUsage - if err := json.Unmarshal(blob, &dataUsage); err != nil { - return errDecodeData.Wrap(err) - } - if err := dataUsage.Validate(); err != nil { - return err - } - - m.DownloadBytes = dataUsage.DownloadBytes - m.UploadBytes = dataUsage.UploadBytes - m.SessionID = dataUsage.SessionID - m.SessionTime = dataUsage.SessionTime - - return nil -} - -// Encode implements util.Serializable interface. -func (m *DataUsage) Encode() []byte { - blob, _ := json.Marshal(m) - return blob -} - -// Validate checks DataUsage for correctness. -func (m *DataUsage) Validate() (err error) { - switch { // is invalid - case m.SessionID == "": - err = errors.New(errCodeBadRequest, "session id is required") - - default: // is valid - return nil - } - - return errInvalidDataUsage.Wrap(err) -} diff --git a/zmagmacore/magmasc/datausage_test.go b/zmagmacore/magmasc/datausage_test.go deleted file mode 100644 index 01012868e..000000000 --- a/zmagmacore/magmasc/datausage_test.go +++ /dev/null @@ -1,134 +0,0 @@ -package magmasc - -import ( - "encoding/json" - "reflect" - "testing" -) - -func Test_DataUsage_Decode(t *testing.T) { - t.Parallel() - - dataUsage := mockDataUsage() - blob, err := json.Marshal(dataUsage) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - dataUsageInvalid := mockDataUsage() - dataUsageInvalid.SessionID = "" - blobInvalid, err := json.Marshal(dataUsageInvalid) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - tests := [3]struct { - name string - blob []byte - want *DataUsage - error bool - }{ - { - name: "OK", - blob: blob, - want: &dataUsage, - error: false, - }, - { - name: "Decode_ERR", - blob: []byte(":"), // invalid json - want: &DataUsage{}, - error: true, - }, - { - name: "Invalid_ERR", - blob: blobInvalid, - want: &DataUsage{}, - error: true, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - got := &DataUsage{} - if err := got.Decode(test.blob); (err != nil) != test.error { - t.Errorf("Decode() error: %v | want: %v", err, test.error) - return - } - if !reflect.DeepEqual(got, test.want) { - t.Errorf("Decode() got: %#v | want: %#v", got, test.want) - } - }) - } -} - -func Test_DataUsage_Encode(t *testing.T) { - t.Parallel() - - dataUsage := mockDataUsage() - blob, err := json.Marshal(dataUsage) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - tests := [1]struct { - name string - dataUsage *DataUsage - want []byte - }{ - { - name: "OK", - dataUsage: &dataUsage, - want: blob, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if got := test.dataUsage.Encode(); !reflect.DeepEqual(got, test.want) { - t.Errorf("Encode() got: %#v | want: %#v", got, test.want) - } - }) - } -} - -func Test_DataUsage_Validate(t *testing.T) { - t.Parallel() - - duEmptySessionID := mockDataUsage() - duEmptySessionID.SessionID = "" - - tests := [2]struct { - name string - usage DataUsage - error bool - }{ - { - name: "OK", - usage: mockDataUsage(), - error: false, - }, - { - name: "EmptySessionID", - usage: duEmptySessionID, - error: true, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if err := test.usage.Validate(); (err != nil) != test.error { - t.Errorf("validate() error: %v | want: %v", err, test.error) - } - }) - } -} diff --git a/zmagmacore/magmasc/errors.go b/zmagmacore/magmasc/errors.go deleted file mode 100644 index f67599bf2..000000000 --- a/zmagmacore/magmasc/errors.go +++ /dev/null @@ -1,36 +0,0 @@ -package magmasc - -import ( - "github.com/0chain/gosdk/zmagmacore/errors" -) - -const ( - errCodeBadRequest = "bad_request" - errCodeInvalid = "invalid_error" -) - -var ( - // errDecodeData represents an error - // that decode data was failed. - errDecodeData = errors.New("decode_error", "decode error") - - // errInvalidAcknowledgment represents an error - // that an acknowledgment was invalidated. - errInvalidAcknowledgment = errors.New(errCodeInvalid, "invalid acknowledgment") - - // errInvalidConsumer represents an error - // that consumer was invalidated. - errInvalidConsumer = errors.New(errCodeInvalid, "invalid consumer") - - // errInvalidDataUsage represents an error - // that a data usage was invalidated. - errInvalidDataUsage = errors.New(errCodeInvalid, "invalid data usage") - - // errInvalidProvider represents an error - // that provider was invalidated. - errInvalidProvider = errors.New(errCodeInvalid, "invalid provider") - - // errInvalidProviderTerms represents an error - // that provider terms was invalidated. - errInvalidProviderTerms = errors.New(errCodeInvalid, "invalid provider terms") -) diff --git a/zmagmacore/magmasc/interfaces.go b/zmagmacore/magmasc/interfaces.go deleted file mode 100644 index 092130274..000000000 --- a/zmagmacore/magmasc/interfaces.go +++ /dev/null @@ -1,51 +0,0 @@ -package magmasc - -// ExternalID represents simple getter for ExtID. -func (m *Consumer) ExternalID() string { - return m.ExtID -} - -// FetchNodeRP returns name of magma sc rest point used for fetching consumer's node info. -func (m *Consumer) FetchNodeRP() string { - return ConsumerFetchRP -} - -// IsNodeRegisteredRP returns name of magma sc rest point used for checking consumer's node registration. -func (m *Consumer) IsNodeRegisteredRP() string { - return ConsumerRegisteredRP -} - -// RegistrationFuncName returns name of magma sc function used for consumer's node registration. -func (m *Consumer) RegistrationFuncName() string { - return ConsumerRegisterFuncName -} - -// UpdateNodeFuncName returns name of magma sc function used for consumer's node updating. -func (m *Consumer) UpdateNodeFuncName() string { - return ConsumerUpdateFuncName -} - -// ExternalID represents simple getter for ExtID. -func (m *Provider) ExternalID() string { - return m.ExtID -} - -// FetchNodeRP returns name of magma sc rest point used for fetching provider's node info. -func (m *Provider) FetchNodeRP() string { - return ProviderFetchRP -} - -// IsNodeRegisteredRP returns name of magma sc rest point used for checking provider's node registration. -func (m *Provider) IsNodeRegisteredRP() string { - return ProviderRegisteredRP -} - -// RegistrationFuncName returns name of magma sc function used for provider's node registration. -func (m *Provider) RegistrationFuncName() string { - return ProviderRegisterFuncName -} - -// UpdateNodeFuncName returns name of magma sc function used for provider's node updating. -func (m *Provider) UpdateNodeFuncName() string { - return ProviderUpdateFuncName -} diff --git a/zmagmacore/magmasc/mocks_test.go b/zmagmacore/magmasc/mocks_test.go deleted file mode 100644 index 5c0245fa2..000000000 --- a/zmagmacore/magmasc/mocks_test.go +++ /dev/null @@ -1,115 +0,0 @@ -package magmasc - -import ( - "encoding/hex" - "time" - - magma "github.com/magma/augmented-networks/accounting/protos" - "golang.org/x/crypto/sha3" - - ts "github.com/0chain/gosdk/zmagmacore/time" -) - -func mockAcknowledgment() *Acknowledgment { - now := time.Now().Format(time.RFC3339Nano) - billing := mockBilling() - - return &Acknowledgment{ - SessionID: billing.DataUsage.SessionID, - AccessPointID: "id:access:point:" + now, - Billing: billing, - Consumer: mockConsumer(), - Provider: mockProvider(), - } -} - -func mockBilling() Billing { - return Billing{ - DataUsage: mockDataUsage(), - } -} - -func mockConsumer() *Consumer { - now := time.Now().Format(time.RFC3339Nano) - return &Consumer{ - ID: "id:consumer:" + now, - ExtID: "id:consumer:external:" + now, - Host: "localhost:8010", - } -} - -func mockDataUsage() DataUsage { - now := time.Now().Format(time.RFC3339Nano) - return DataUsage{ - DownloadBytes: 3 * million, - UploadBytes: 2 * million, - SessionID: "id:session:" + now, - SessionTime: 1 * 60, // 1 minute - } -} - -func mockProvider() *Provider { - now := time.Now().Format(time.RFC3339Nano) - return &Provider{ - ID: "id:provider:" + now, - ExtID: "id:provider:external:" + now, - Host: "localhost:8020", - MinStake: billion, - } -} - -func mockProviderTerms() ProviderTerms { - return ProviderTerms{ - AccessPointID: "id:access:point" + time.Now().Format(time.RFC3339Nano), - Price: 0.1, - PriceAutoUpdate: 0.001, - MinCost: 0.5, - Volume: 0, - QoS: mockQoS(), - QoSAutoUpdate: &QoSAutoUpdate{ - DownloadMbps: 0.001, - UploadMbps: 0.001, - }, - ProlongDuration: 1 * 60 * 60, // 1 hour - ExpiredAt: ts.Now() + (1 * 60 * 60), // 1 hour from now - } -} - -func mockTokenPool() *TokenPool { - now := time.Now().Format(time.RFC3339Nano) - return &TokenPool{ - ID: "id:session:" + now, - Balance: 1000, - HolderID: "id:holder:" + now, - PayerID: "id:payer:" + now, - PayeeID: "id:payee:" + now, - Transfers: []TokenPoolTransfer{ - mockTokenPoolTransfer(), - mockTokenPoolTransfer(), - mockTokenPoolTransfer(), - }, - } -} - -func mockTokenPoolTransfer() TokenPoolTransfer { - now := time.Now() - bin, _ := time.Now().MarshalBinary() - hash := sha3.Sum256(bin) - fix := now.Format(time.RFC3339Nano) - - return TokenPoolTransfer{ - TxnHash: hex.EncodeToString(hash[:]), - FromPool: "id:from:pool:" + fix, - ToPool: "id:to:pool:" + fix, - Value: 1111, - FromClient: "id:from:client:" + fix, - ToClient: "id:to:client:" + fix, - } -} - -func mockQoS() *magma.QoS { - return &magma.QoS{ - DownloadMbps: 5.4321, - UploadMbps: 1.2345, - } -} diff --git a/zmagmacore/magmasc/provider.go b/zmagmacore/magmasc/provider.go deleted file mode 100644 index 20581af6d..000000000 --- a/zmagmacore/magmasc/provider.go +++ /dev/null @@ -1,81 +0,0 @@ -package magmasc - -import ( - "encoding/json" - - "github.com/0chain/gosdk/core/util" - "github.com/0chain/gosdk/zmagmacore/config" - "github.com/0chain/gosdk/zmagmacore/errors" - "github.com/0chain/gosdk/zmagmacore/node" -) - -type ( - // Provider represents providers node stored in blockchain. - Provider struct { - ID string `json:"id"` - ExtID string `json:"ext_id"` - Host string `json:"host,omitempty"` - MinStake int64 `json:"min_stake,omitempty"` - } -) - -var ( - // Make sure Provider implements Serializable interface. - _ util.Serializable = (*Provider)(nil) -) - -// NewProviderFromCfg creates Provider from config.Provider. -func NewProviderFromCfg(cfg *config.Provider) *Provider { - return &Provider{ - ID: node.ID(), - ExtID: cfg.ExtID, - Host: cfg.Host, - MinStake: cfg.MinStake, - } -} - -// Decode implements util.Serializable interface. -func (m *Provider) Decode(blob []byte) error { - var provider Provider - if err := json.Unmarshal(blob, &provider); err != nil { - return errDecodeData.Wrap(err) - } - if err := provider.Validate(); err != nil { - return err - } - - m.ID = provider.ID - m.ExtID = provider.ExtID - m.Host = provider.Host - m.MinStake = provider.MinStake - - return nil -} - -// Encode implements util.Serializable interface. -func (m *Provider) Encode() []byte { - blob, _ := json.Marshal(m) - return blob -} - -// GetType returns Provider's type. -func (m *Provider) GetType() string { - return providerType -} - -// Validate checks Provider for correctness. -// If it is not return errInvalidProvider. -func (m *Provider) Validate() (err error) { - switch { // is invalid - case m.ExtID == "": - err = errors.New(errCodeBadRequest, "provider external id is required") - - case m.Host == "": - err = errors.New(errCodeBadRequest, "provider host is required") - - default: - return nil // is valid - } - - return errInvalidProvider.Wrap(err) -} diff --git a/zmagmacore/magmasc/provider_terms.go b/zmagmacore/magmasc/provider_terms.go deleted file mode 100644 index 7896a46dc..000000000 --- a/zmagmacore/magmasc/provider_terms.go +++ /dev/null @@ -1,200 +0,0 @@ -package magmasc - -import ( - "encoding/json" - - magma "github.com/magma/augmented-networks/accounting/protos" - - "github.com/0chain/gosdk/core/util" - "github.com/0chain/gosdk/zmagmacore/errors" - "github.com/0chain/gosdk/zmagmacore/time" -) - -type ( - // ProviderTerms represents a provider and service terms. - ProviderTerms struct { - AccessPointID string `json:"apid"` // access point id - Price float32 `json:"price"` // tokens per Megabyte - PriceAutoUpdate float32 `json:"price_auto_update,omitempty"` // price change on auto update - MinCost float32 `json:"min_cost"` // minimal cost for a session - Volume int64 `json:"volume"` // bytes per a session - QoS *magma.QoS `json:"qos"` // quality of service guarantee - QoSAutoUpdate *QoSAutoUpdate `json:"qos_auto_update,omitempty"` // qos change on auto update - ProlongDuration time.Duration `json:"prolong_duration,omitempty"` // duration in seconds to prolong the terms - ExpiredAt time.Timestamp `json:"expired_at,omitempty"` // timestamp till a session valid - } - - // QoSAutoUpdate represents data of qos terms on auto update. - QoSAutoUpdate struct { - DownloadMbps float32 `json:"download_mbps"` - UploadMbps float32 `json:"upload_mbps"` - } -) - -var ( - // Make sure ProviderTerms implements Serializable interface. - _ util.Serializable = (*ProviderTerms)(nil) -) - -// NewProviderTerms returns a new constructed provider terms. -func NewProviderTerms() *ProviderTerms { - return &ProviderTerms{QoS: &magma.QoS{}} -} - -// Decode implements util.Serializable interface. -func (m *ProviderTerms) Decode(blob []byte) error { - var terms ProviderTerms - if err := json.Unmarshal(blob, &terms); err != nil { - return errDecodeData.Wrap(err) - } - if err := terms.Validate(); err != nil { - return err - } - - m.AccessPointID = terms.AccessPointID - m.Price = terms.Price - m.PriceAutoUpdate = terms.PriceAutoUpdate - m.MinCost = terms.MinCost - m.Volume = terms.Volume - m.QoS.UploadMbps = terms.QoS.UploadMbps - m.QoS.DownloadMbps = terms.QoS.DownloadMbps - m.QoSAutoUpdate = terms.QoSAutoUpdate - m.ProlongDuration = terms.ProlongDuration - m.ExpiredAt = terms.ExpiredAt - - return nil -} - -// Decrease makes automatically Decrease provider terms by config. -func (m *ProviderTerms) Decrease() *ProviderTerms { - m.Volume = 0 // the volume of terms must be zeroed - - if m.ProlongDuration != 0 { - m.ExpiredAt += time.Timestamp(m.ProlongDuration) // prolong expire of terms - } - - if m.PriceAutoUpdate != 0 && m.Price > m.PriceAutoUpdate { - m.Price -= m.PriceAutoUpdate // down the price - } - - if m.QoSAutoUpdate != nil { - if m.QoSAutoUpdate.UploadMbps != 0 { - m.QoS.UploadMbps += m.QoSAutoUpdate.UploadMbps // up the qos of upload mbps - } - - if m.QoSAutoUpdate.DownloadMbps != 0 { - m.QoS.DownloadMbps += m.QoSAutoUpdate.DownloadMbps // up the qos of download mbps - } - } - - return m -} - -// Encode implements util.Serializable interface. -func (m *ProviderTerms) Encode() []byte { - blob, _ := json.Marshal(m) - return blob -} - -// Expired returns if terms already expired. -func (m *ProviderTerms) Expired() bool { - return m.ExpiredAt < time.Now()+TermsExpiredDuration -} - -// GetAmount returns calculated amount value of provider terms. -func (m *ProviderTerms) GetAmount() (amount uint64) { - price := m.GetPrice() - vol := m.GetVolume() - if vol > 0 { - amount = price * uint64(vol) - if minCost := m.GetMinCost(); amount < minCost { - amount = minCost - } - } - - return amount -} - -// GetMinCost returns calculated min cost value of provider terms. -func (m *ProviderTerms) GetMinCost() (cost uint64) { - if m.MinCost > 0 { - cost = uint64(m.MinCost * billion) - } - - return cost -} - -// GetPrice returns calculated price value of provider terms. -// NOTE: the price value will be represented in token units per megabyte. -func (m *ProviderTerms) GetPrice() (price uint64) { - if m.Price > 0 { - price = uint64(m.Price * billion) - } - - return price -} - -// GetVolume returns value of the provider terms volume. -// If the Volume is empty it will be calculated by the provider terms. -func (m *ProviderTerms) GetVolume() int64 { - if m.Volume == 0 { - mbps := (m.QoS.UploadMbps + m.QoS.DownloadMbps) / octet // megabytes per second - duration := float32(m.ExpiredAt - time.Now()) // duration in seconds - // rounded of bytes per second multiplied by duration in seconds - m.Volume = int64(mbps * duration) - } - - return m.Volume -} - -// Increase makes automatically Increase provider terms by config. -func (m *ProviderTerms) Increase() *ProviderTerms { - m.Volume = 0 // the volume of terms must be zeroed - - if m.ProlongDuration != 0 { - m.ExpiredAt += time.Timestamp(m.ProlongDuration) // prolong expire of terms - } - - if m.PriceAutoUpdate != 0 { - m.Price += m.PriceAutoUpdate // up the price - } - - if m.QoSAutoUpdate != nil { - if m.QoSAutoUpdate.UploadMbps != 0 && m.QoS.UploadMbps > m.QoSAutoUpdate.UploadMbps { - m.QoS.UploadMbps -= m.QoSAutoUpdate.UploadMbps // down the qos of upload mbps - } - - if m.QoSAutoUpdate.DownloadMbps != 0 && m.QoS.DownloadMbps > m.QoSAutoUpdate.DownloadMbps { - m.QoS.DownloadMbps -= m.QoSAutoUpdate.DownloadMbps // down the qos of download mbps - } - } - - return m -} - -// Validate checks ProviderTerms for correctness. -// If it is not return errInvalidProviderTerms. -func (m *ProviderTerms) Validate() (err error) { - switch { // is invalid - case m.AccessPointID == "": - err = errors.New(errCodeBadRequest, "invalid access point id") - - case m.QoS == nil: - err = errors.New(errCodeBadRequest, "invalid terms qos") - - case m.QoS.UploadMbps <= 0: - err = errors.New(errCodeBadRequest, "invalid terms qos upload mbps") - - case m.QoS.DownloadMbps <= 0: - err = errors.New(errCodeBadRequest, "invalid terms qos download mbps") - - case m.Expired(): - now := time.NowTime().Add(TermsExpiredDuration).Format(time.RFC3339) - err = errors.New(errCodeBadRequest, "expired at must be after "+now) - - default: - return nil // is valid - } - - return errInvalidProviderTerms.Wrap(err) -} diff --git a/zmagmacore/magmasc/provider_test.go b/zmagmacore/magmasc/provider_test.go deleted file mode 100644 index c5c6ba7e3..000000000 --- a/zmagmacore/magmasc/provider_test.go +++ /dev/null @@ -1,154 +0,0 @@ -package magmasc - -import ( - "encoding/json" - "reflect" - "testing" -) - -func Test_Provider_Decode(t *testing.T) { - t.Parallel() - - prov := mockProvider() - blob, err := json.Marshal(prov) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - provInvalid := mockProvider() - provInvalid.ExtID = "" - extIDBlobInvalid, err := json.Marshal(provInvalid) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - tests := [3]struct { - name string - blob []byte - want *Provider - error bool - }{ - { - name: "OK", - blob: blob, - want: prov, - error: false, - }, - { - name: "Decode_ERR", - blob: []byte(":"), // invalid json - want: &Provider{}, - error: true, - }, - { - name: "Ext_ID_Invalid_ERR", - blob: extIDBlobInvalid, - want: &Provider{}, - error: true, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - got := &Provider{} - if err := got.Decode(test.blob); (err != nil) != test.error { - t.Errorf("Decode() error: %v | want: %v", err, test.error) - } - if !reflect.DeepEqual(got, test.want) { - t.Errorf("Decode() got: %#v | want: %#v", got, test.want) - } - }) - } -} - -func Test_Provider_Encode(t *testing.T) { - t.Parallel() - - prov := mockProvider() - blob, err := json.Marshal(prov) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - tests := [1]struct { - name string - prov *Provider - want []byte - }{ - { - name: "OK", - prov: prov, - want: blob, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if got := test.prov.Encode(); !reflect.DeepEqual(got, test.want) { - t.Errorf("Encode() got: %#v | want: %#v", got, test.want) - } - }) - } -} - -func Test_Provider_GetType(t *testing.T) { - t.Parallel() - - t.Run("OK", func(t *testing.T) { - t.Parallel() - - prov := Provider{} - if got := prov.GetType(); got != providerType { - t.Errorf("GetType() got: %v | want: %v", got, providerType) - } - }) -} - -func Test_Provider_Validate(t *testing.T) { - t.Parallel() - - provEmptyExtID := mockProvider() - provEmptyExtID.ExtID = "" - - provEmptyHost := mockProvider() - provEmptyHost.Host = "" - - tests := [3]struct { - name string - prov *Provider - error bool - }{ - { - name: "OK", - prov: mockProvider(), - error: false, - }, - { - name: "Empty_Ext_ID_ERR", - prov: provEmptyExtID, - error: true, - }, - { - name: "Empty_Host_ERR", - prov: provEmptyHost, - error: true, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if err := test.prov.Validate(); (err != nil) != test.error { - t.Errorf("validate() error: %v | want: %v", err, test.error) - } - }) - } -} diff --git a/zmagmacore/magmasc/tokenpool.go b/zmagmacore/magmasc/tokenpool.go deleted file mode 100644 index a0c695186..000000000 --- a/zmagmacore/magmasc/tokenpool.go +++ /dev/null @@ -1,47 +0,0 @@ -package magmasc - -import ( - "encoding/json" - - "github.com/0chain/gosdk/core/util" -) - -type ( - // TokenPool represents token pool implementation. - TokenPool struct { - ID string `json:"id"` - Balance int64 `json:"balance"` - HolderID string `json:"holder_id"` - PayerID string `json:"payer_id"` - PayeeID string `json:"payee_id"` - Transfers []TokenPoolTransfer `json:"transfers,omitempty"` - } -) - -var ( - // Make sure tokenPool implements Serializable interface. - _ util.Serializable = (*TokenPool)(nil) -) - -// Decode implements util.Serializable interface. -func (m *TokenPool) Decode(blob []byte) error { - var pool TokenPool - if err := json.Unmarshal(blob, &pool); err != nil { - return errDecodeData.Wrap(err) - } - - m.ID = pool.ID - m.Balance = pool.Balance - m.HolderID = pool.HolderID - m.PayerID = pool.PayerID - m.PayeeID = pool.PayeeID - m.Transfers = pool.Transfers - - return nil -} - -// Encode implements util.Serializable interface. -func (m *TokenPool) Encode() []byte { - blob, _ := json.Marshal(m) - return blob -} diff --git a/zmagmacore/magmasc/tokenpool_configurator.go b/zmagmacore/magmasc/tokenpool_configurator.go deleted file mode 100644 index e793327ac..000000000 --- a/zmagmacore/magmasc/tokenpool_configurator.go +++ /dev/null @@ -1,21 +0,0 @@ -package magmasc - -type ( - // PoolConfigurator represents a pool config interface. - PoolConfigurator interface { - // PoolBalance returns the amount value of token pool. - PoolBalance() uint64 - - // PoolID returns the token pool ID. - PoolID() string - - // PoolHolderID returns the token pool holder ID. - PoolHolderID() string - - // PoolPayerID returns the token pool payer ID. - PoolPayerID() string - - // PoolPayeeID returns the token pool payee ID. - PoolPayeeID() string - } -) diff --git a/zmagmacore/magmasc/tokenpool_test.go b/zmagmacore/magmasc/tokenpool_test.go deleted file mode 100644 index e525de6ea..000000000 --- a/zmagmacore/magmasc/tokenpool_test.go +++ /dev/null @@ -1,86 +0,0 @@ -package magmasc - -import ( - "encoding/json" - "reflect" - "testing" -) - -func Test_tokenPool_Decode(t *testing.T) { - t.Parallel() - - pool := mockTokenPool() - blob, err := json.Marshal(pool) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - tests := [2]struct { - name string - blob []byte - want *TokenPool - error bool - }{ - { - name: "OK", - blob: blob, - want: pool, - error: false, - }, - { - name: "Decode_ERR", - blob: []byte(":"), // invalid json - want: &TokenPool{}, - error: true, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - got := &TokenPool{} - if err := got.Decode(test.blob); (err != nil) != test.error { - t.Errorf("Decode() error: %v | want: %v", err, test.error) - return - } - if !reflect.DeepEqual(got, test.want) { - t.Errorf("Decode() got: %#v | want: %#v", got, test.want) - } - }) - } -} - -func Test_tokenPool_Encode(t *testing.T) { - t.Parallel() - - pool := mockTokenPool() - blob, err := json.Marshal(pool) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - tests := [1]struct { - name string - pool *TokenPool - want []byte - }{ - { - name: "OK", - pool: pool, - want: blob, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if got := test.pool.Encode(); !reflect.DeepEqual(got, test.want) { - t.Errorf("Encode() got: %#v | want: %#v", got, test.want) - } - }) - } -} diff --git a/zmagmacore/magmasc/tokenpool_transfer.go b/zmagmacore/magmasc/tokenpool_transfer.go deleted file mode 100644 index f2c419a8e..000000000 --- a/zmagmacore/magmasc/tokenpool_transfer.go +++ /dev/null @@ -1,35 +0,0 @@ -package magmasc - -import ( - "encoding/json" - - "github.com/0chain/gosdk/core/util" -) - -type ( - // TokenPoolTransfer stores info about token transfers from pool to pool. - TokenPoolTransfer struct { - TxnHash string `json:"txn_hash,omitempty"` - FromPool string `json:"from_pool,omitempty"` - ToPool string `json:"to_pool,omitempty"` - Value int64 `json:"value,omitempty"` - FromClient string `json:"from_client,omitempty"` - ToClient string `json:"to_client,omitempty"` - } -) - -var ( - // Make sure TokenPoolTransfer implements Serializable interface. - _ util.Serializable = (*TokenPoolTransfer)(nil) -) - -// Decode implements util.Serializable interface. -func (m *TokenPoolTransfer) Decode(blob []byte) error { - return json.Unmarshal(blob, m) -} - -// Encode implements util.Serializable interface. -func (m *TokenPoolTransfer) Encode() []byte { - blob, _ := json.Marshal(m) - return blob -} diff --git a/zmagmacore/magmasc/tokenpool_transfer_test.go b/zmagmacore/magmasc/tokenpool_transfer_test.go deleted file mode 100644 index 2b4d4bcbe..000000000 --- a/zmagmacore/magmasc/tokenpool_transfer_test.go +++ /dev/null @@ -1,86 +0,0 @@ -package magmasc - -import ( - "encoding/json" - "reflect" - "testing" -) - -func Test_TokenPoolTransfer_Decode(t *testing.T) { - t.Parallel() - - resp := mockTokenPoolTransfer() - blob, err := json.Marshal(resp) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - tests := [2]struct { - name string - blob []byte - want TokenPoolTransfer - error bool - }{ - { - name: "OK", - blob: blob, - want: resp, - error: false, - }, - { - name: "Decode_ERR", - blob: []byte(":"), // invalid json - want: TokenPoolTransfer{}, - error: true, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - got := TokenPoolTransfer{} - if err := got.Decode(test.blob); (err != nil) != test.error { - t.Errorf("Decode() error: %v | want: %v", err, test.error) - return - } - if !reflect.DeepEqual(got, test.want) { - t.Errorf("Decode() got: %#v | want: %#v", got, test.want) - } - }) - } -} - -func Test_TokenPoolTransfer_Encode(t *testing.T) { - t.Parallel() - - resp := mockTokenPoolTransfer() - blob, err := json.Marshal(resp) - if err != nil { - t.Fatalf("json.Marshal() error: %v | want: %v", err, nil) - } - - tests := [1]struct { - name string - resp TokenPoolTransfer - want []byte - }{ - { - name: "OK", - resp: resp, - want: blob, - }, - } - - for idx := range tests { - test := tests[idx] - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - if got := test.resp.Encode(); !reflect.DeepEqual(got, test.want) { - t.Errorf("Encode() got: %#v | want: %#v", got, test.want) - } - }) - } -} diff --git a/zmagmacore/node/self.go b/zmagmacore/node/self.go deleted file mode 100644 index 563523484..000000000 --- a/zmagmacore/node/self.go +++ /dev/null @@ -1,80 +0,0 @@ -// DEPRECATED: This package is deprecated and will be removed in a future release. -package node - -import ( - "net/url" - "strconv" - "time" - - "github.com/0chain/gosdk/zmagmacore/wallet" -) - -// Node represent self node type. -type Node struct { - url string - wallet *wallet.Wallet - extID string - startTime time.Time -} - -var ( - self = &Node{} -) - -// Start writes to self node current time, sets wallet, external id and url -// -// Start should be used only once while application is starting. -func Start(host string, port int, extID string, wallet *wallet.Wallet) { - self = &Node{ - url: makeHostURL(host, port), - wallet: wallet, - extID: extID, - startTime: time.Now(), - } -} - -func makeHostURL(host string, port int) string { - if host == "" { - host = "localhost" - } - - uri := url.URL{ - Scheme: "http", - Host: host + ":" + strconv.Itoa(port), - } - - return uri.String() -} - -// GetWalletString returns marshaled to JSON string nodes wallet. -func GetWalletString() (string, error) { - return self.wallet.StringJSON() -} - -func SetWallet(wall *wallet.Wallet) { - self.wallet = wall -} - -// ID returns id of Node. -func ID() string { - return self.wallet.ID() -} - -func ExtID() string { - return self.extID -} - -// PublicKey returns id of Node. -func PublicKey() string { - return self.wallet.PublicKey() -} - -// PrivateKey returns id of Node. -func PrivateKey() string { - return self.wallet.PrivateKey() -} - -// StartTime returns time when Node is started. -func StartTime() time.Time { - return self.startTime -} diff --git a/zmagmacore/registration/node.go b/zmagmacore/registration/node.go deleted file mode 100644 index b6f3a2184..000000000 --- a/zmagmacore/registration/node.go +++ /dev/null @@ -1,119 +0,0 @@ -// DEPRECATED: This package is deprecated and will be removed in a future release. -package registration - -import ( - "context" - "encoding/json" - "time" - - "go.uber.org/zap" - - "github.com/0chain/gosdk/core/sys" - "github.com/0chain/gosdk/zmagmacore/errors" - "github.com/0chain/gosdk/zmagmacore/http" - "github.com/0chain/gosdk/zmagmacore/log" - "github.com/0chain/gosdk/zmagmacore/magmasc" - "github.com/0chain/gosdk/zmagmacore/transaction" -) - -// RegisterOrUpdateWithRetries registers bandwidth-marketplace Node in blockchain -// if node is not registered or updates if is with retries. -// -// If an error occurs during execution, the program terminates with code 2 and the last error will be written in os.Stderr. -// -// RegisterOrUpdateWithRetries should be used only once while application is starting. -func RegisterOrUpdateWithRetries(ctx context.Context, bmNode Node, numTries int) { - var ( - executeSC executeSmartContract - ) - - registered, err := isNodeRegistered(bmNode) - if err != nil { - errors.ExitErr("err while checking nodes registration", err, 2) - } - if registered { - executeSC = update - log.Logger.Debug("Node is already registered in the blockchain, trying to update ...") - } else { - executeSC = register - log.Logger.Debug("Node is not registered in the Blockchain, trying to start registration ...") - } - - var ( - txnOutput string - ) - for ind := 0; ind < numTries; ind++ { - txnOutput, err = executeSC(ctx, bmNode) - if err != nil { - log.Logger.Debug("Executing smart contract failed. Sleep for 1 seconds ...", - zap.String("err", err.Error()), - ) - sys.Sleep(time.Second) - continue - } - - log.Logger.Info("Node is registered in the blockchain", zap.Any("txn_output", txnOutput)) - break - } - - if err != nil { - errors.ExitErr("error while registering", err, 2) - } -} - -func isNodeRegistered(bmNode Node) (bool, error) { - params := map[string]string{ - "ext_id": bmNode.ExternalID(), - } - registeredByt, err := http.MakeSCRestAPICall(magmasc.Address, bmNode.IsNodeRegisteredRP(), params) - if err != nil { - return false, err - } - - var registered bool - if err := json.Unmarshal(registeredByt, ®istered); err != nil { - return false, err - } - - return registered, nil -} - -func register(ctx context.Context, bmNode Node) (txnOutput string, err error) { - txn, err := transaction.NewTransactionEntity() - if err != nil { - return "", err - } - - input := bmNode.Encode() - hash, err := txn.ExecuteSmartContract(ctx, magmasc.Address, bmNode.RegistrationFuncName(), string(input), 0) - if err != nil { - return "", err - } - - txn, err = transaction.VerifyTransaction(ctx, hash) - if err != nil { - return "", err - } - - return txn.TransactionOutput, nil -} - -func update(ctx context.Context, bmNode Node) (txnOutput string, err error) { - txn, err := transaction.NewTransactionEntity() - if err != nil { - return "", err - } - - input := bmNode.Encode() - hash, err := txn.ExecuteSmartContract(ctx, magmasc.Address, bmNode.UpdateNodeFuncName(), string(input), 0) - if err != nil { - return "", err - } - - txn, err = transaction.VerifyTransaction(ctx, hash) - if err != nil { - return "", err - } - - return txn.TransactionOutput, nil -} diff --git a/zmagmacore/registration/types.go b/zmagmacore/registration/types.go deleted file mode 100644 index af36ea592..000000000 --- a/zmagmacore/registration/types.go +++ /dev/null @@ -1,38 +0,0 @@ -package registration - -import ( - "context" - - "github.com/0chain/gosdk/zmagmacore/magmasc" -) - -type ( - // Node represent bandwidth-marketplace node that can be registered. - Node interface { - // RegistrationFuncName returns name of magma sc function used for registration Node. - RegistrationFuncName() string - - // UpdateNodeFuncName returns name of magma sc function used for updating Node. - UpdateNodeFuncName() string - - // IsNodeRegisteredRP returns name of magma sc relative path for checking registration of Node. - IsNodeRegisteredRP() string - - // ExternalID returns external ID of Node - ExternalID() string - - // Encode encodes Node in json bytes. - Encode() []byte - } - - // executeSmartContract represent types for functions that executes sc functions. - executeSmartContract func(ctx context.Context, node Node) (string, error) -) - -var ( - // Ensure Consumer implements interface. - _ Node = (*magmasc.Consumer)(nil) - - // Ensure Provider implements interface. - _ Node = (*magmasc.Provider)(nil) -) diff --git a/zmagmacore/shutdown/shutdown.go b/zmagmacore/shutdown/shutdown.go deleted file mode 100644 index 34837d8ad..000000000 --- a/zmagmacore/shutdown/shutdown.go +++ /dev/null @@ -1,55 +0,0 @@ -// DEPRECATED: This package is deprecated and will be removed in a future release. -package shutdown - -import ( - "context" - "net/http" - "os" - "os/signal" - "syscall" - "time" - - "go.uber.org/zap" - "google.golang.org/grpc" - - "github.com/0chain/gosdk/zmagmacore/log" -) - -type ( - // Closable represents interface for types that might be closed. - Closable interface { - Close() error - } -) - -// Handle handles various shutdown signals. -func Handle(ctx context.Context, server *http.Server, grpcServer *grpc.Server, closable ...Closable) { - c := make(chan os.Signal, 1) - signal.Notify(c, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM) - - // wait for signals or app context done - select { - case <-ctx.Done(): - case <-c: - } - shutdown(server, grpcServer, closable...) -} - -func shutdown(server *http.Server, grpcServer *grpc.Server, closable ...Closable) { - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - if err := server.Shutdown(ctx); err != nil { - log.Logger.Warn("Server failed to gracefully shuts down", zap.Error(err)) - } - log.Logger.Debug("Server is shut down.") - - grpcServer.GracefulStop() - log.Logger.Debug("GRPC server is shut down.") - - log.Logger.Debug("Closing rest ...") - for _, cl := range closable { - if err := cl.Close(); err != nil { - log.Logger.Warn("Can not close.", zap.String("err", err.Error())) - } - } -} diff --git a/zmagmacore/storage/interface.go b/zmagmacore/storage/interface.go deleted file mode 100644 index 0a1b86aca..000000000 --- a/zmagmacore/storage/interface.go +++ /dev/null @@ -1,126 +0,0 @@ -// DEPRECATED: This package is deprecated and will be removed in a future release. -package storage - -import ( - "sync" - - "github.com/dgraph-io/badger/v3" - - "github.com/0chain/gosdk/zmagmacore/errors" -) - -type ( - // Storage represents the main storage based on badger.DB. - Storage struct { - db *badger.DB - - singleton sync.Once // used for opening connection only once - } - - // Value represent value that can be stored as encoded bytes. - Value interface { - Encode() []byte - } -) - -var ( - // storageInst represents singleton storage. - storageInst = &Storage{} -) - -// Open opens singleton connection to storage. -// -// If an error occurs during execution, the program terminates with code 2 and the error will be written in os.Stderr. -// -// Should be used only once while application is starting. -func Open(path string) { - storageInst.singleton.Do(func() { - opts := badger.DefaultOptions(path) - opts.Logger = nil - - db, err := badger.Open(opts) - if err != nil { - errors.ExitErr("error while opening storage: %v", err, 2) - } - - storageInst.db = db - }) -} - -// GetStorage returns current storage implementation. -func GetStorage() *Storage { - return storageInst -} - -// Del deletes entry by the key. -func (s *Storage) Del(key []byte) error { - return s.db.Update(func(txn *badger.Txn) error { - return txn.Delete(key) - }) -} - -// Get retrieves entry by the key. -func (s *Storage) Get(key []byte) (value []byte, err error) { - err = s.db.View(func(txn *badger.Txn) error { - var item *badger.Item - item, err = txn.Get(key) - if err != nil { - return err - } - - return item.Value(func(val []byte) error { - value = val - return nil - }) - }) - - return -} - -// Set sets encoded Value with provided key. -func (s *Storage) Set(key []byte, value Value) error { - return s.db.Update(func(txn *badger.Txn) error { - blob := value.Encode() - return txn.Set(key, blob) - }) -} - -// SetWithRetries sets encoded Value with provided key with retries. -func (s *Storage) SetWithRetries(key []byte, value Value, numRetries int) error { - var err error - for i := 0; i < numRetries; i++ { - if err = s.Set(key, value); err != nil { - continue - } - break - } - - return err -} - -// Iterate iterates all elements with provided prefix and processes it with the handler. -func (s *Storage) Iterate(handler func(item *badger.Item) error, prefix []byte) error { - return s.db.View(func(txn *badger.Txn) error { - it := txn.NewIterator(badger.DefaultIteratorOptions) - defer it.Close() - - for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() { - if err := handler(it.Item()); err != nil { - return err - } - } - return nil - }) -} - -// NewTransaction creates new badger.Txn. -// -// For read-only transactions set update flag to false. -func (s *Storage) NewTransaction(update bool) *badger.Txn { - return s.db.NewTransaction(update) -} - -// Close closes a DB. -func (s *Storage) Close() error { - return s.db.Close() -} diff --git a/zmagmacore/time/time.go b/zmagmacore/time/time.go deleted file mode 100644 index 3478d5314..000000000 --- a/zmagmacore/time/time.go +++ /dev/null @@ -1,35 +0,0 @@ -// DEPRECATED: This package is deprecated and will be removed in a future release. -package time - -import ( - "time" -) - -const ( - // RFC3339 is useful for formatting time. - RFC3339 = "2006-01-02T15:04:05Z07:00" -) - -type ( - // A Duration represents the elapsed time between two instants - // as an int64 nanosecond count. The representation limits the - // largest representable duration to approximately 290 years. - Duration = time.Duration - - // Time is a copy of time from golang std lib - // to avoid import it from other packages. - Time = time.Time - - // Timestamp represents a wrapper to control the json encoding. - Timestamp int64 -) - -// Now returns current Unix time. -func Now() Timestamp { - return Timestamp(time.Now().Unix()) -} - -// NowTime returns the current local time. -func NowTime() Time { - return time.Now() -} diff --git a/zmagmacore/transaction/callback.go b/zmagmacore/transaction/callback.go deleted file mode 100644 index c8b7356a8..000000000 --- a/zmagmacore/transaction/callback.go +++ /dev/null @@ -1,96 +0,0 @@ -// DEPRECATED: This package is deprecated and will be removed in a future release. -package transaction - -import ( - "context" - - "github.com/0chain/gosdk/zcncore" - "github.com/0chain/gosdk/zmagmacore/errors" -) - -type ( - // callback implements zcncore.TransactionCallback interface. - callback struct { - // waitCh represents channel for making callback.OnTransactionComplete, - // callback.OnVerifyComplete and callBack.OnAuthComplete operations async. - waitCh chan interface{} - - err error - } -) - -var ( - // Ensure callback implements interface. - _ zcncore.TransactionCallback = (*callback)(nil) -) - -func newCallBack() *callback { - return &callback{ - waitCh: make(chan interface{}), - } -} - -// OnTransactionComplete implements zcncore.TransactionCallback interface. -func (cb *callback) OnTransactionComplete(zcnTxn *zcncore.Transaction, status int) { - if status != zcncore.StatusSuccess { - msg := "status is not success: " + TxnStatus(status).String() + "; err: " + zcnTxn.GetTransactionError() - cb.err = errors.New("on_transaction_complete", msg) - } - - cb.sendCall() -} - -func (cb *callback) waitCompleteCall(ctx context.Context) error { - select { - case <-ctx.Done(): - return errors.New("completing_transaction", "completing transaction context deadline exceeded") - - case <-cb.waitCh: - return cb.err - } -} - -// OnVerifyComplete implements zcncore.TransactionCallback interface. -func (cb *callback) OnVerifyComplete(zcnTxn *zcncore.Transaction, status int) { - if status != zcncore.StatusSuccess { - msg := "status is not success: " + TxnStatus(status).String() + "; err: " + zcnTxn.GetVerifyError() - cb.err = errors.New("on_transaction_verify", msg) - } - - cb.sendCall() -} - -func (cb *callback) waitVerifyCall(ctx context.Context) error { - select { - case <-ctx.Done(): - return errors.New("verifying_transaction", "verifying transaction context deadline exceeded") - - case <-cb.waitCh: - return cb.err - } -} - -// OnAuthComplete implements zcncore.TransactionCallback interface. -func (cb *callback) OnAuthComplete(zcnTxn *zcncore.Transaction, status int) { - if status != zcncore.StatusSuccess { - msg := "status is not success: " + TxnStatus(status).String() - cb.err = errors.New("on_transaction_verify", msg) - } - - cb.sendCall() -} - -//nolint:unused -func (cb *callback) waitAuthCall(ctx context.Context) error { - select { - case <-ctx.Done(): - return errors.New("auth_transaction", "authenticating transaction context deadline exceeded") - - case <-cb.waitCh: - return cb.err - } -} - -func (cb *callback) sendCall() { - cb.waitCh <- true -} diff --git a/zmagmacore/transaction/const.go b/zmagmacore/transaction/const.go deleted file mode 100644 index 3ad2971c2..000000000 --- a/zmagmacore/transaction/const.go +++ /dev/null @@ -1,62 +0,0 @@ -package transaction - -type ( - // TxnStatus represented zcncore.TransactionCallback operations statuses. - TxnStatus int -) - -const ( - // StatusSuccess represent zcncore.StatusSuccess. - StatusSuccess TxnStatus = iota - // StatusNetworkError represent zcncore.StatusNetworkError. - StatusNetworkError - // StatusError represent zcncore.StatusError. - StatusError - // StatusRejectedByUser represent zcncore.StatusRejectedByUser. - StatusRejectedByUser - // StatusInvalidSignature represent zcncore.StatusInvalidSignature. - StatusInvalidSignature - // StatusAuthError represent zcncore.StatusAuthError. - StatusAuthError - // StatusAuthVerifyFailed represent zcncore.StatusAuthVerifyFailed. - StatusAuthVerifyFailed - // StatusAuthTimeout represent zcncore.StatusAuthTimeout. - StatusAuthTimeout - // StatusUnknown represent zcncore.StatusUnknown. - StatusUnknown = -1 -) - -// String returns represented in string format TxnStatus. -func (ts TxnStatus) String() string { - switch ts { - case StatusSuccess: - return "success" - - case StatusNetworkError: - return "network error" - - case StatusError: - return "error" - - case StatusRejectedByUser: - return "rejected byt user" - - case StatusInvalidSignature: - return "invalid signature" - - case StatusAuthError: - return "auth error" - - case StatusAuthVerifyFailed: - return "auth verify error" - - case StatusAuthTimeout: - return "auth timeout error" - - case StatusUnknown: - return "unknown" - - default: - return "" - } -} diff --git a/zmagmacore/transaction/http.go b/zmagmacore/transaction/http.go deleted file mode 100644 index afe75d2ba..000000000 --- a/zmagmacore/transaction/http.go +++ /dev/null @@ -1,22 +0,0 @@ -package transaction - -import ( - "context" -) - -// VerifyTransaction verifies including in blockchain transaction with provided hash. -// -// If execution completed with no error, returns Transaction with provided hash. -func VerifyTransaction(ctx context.Context, txnHash string) (*Transaction, error) { - txn, err := NewTransactionEntity() - if err != nil { - return nil, err - } - - txn.Hash = txnHash - err = txn.Verify(ctx) - if err != nil { - return nil, err - } - return txn, nil -} diff --git a/zmagmacore/transaction/txn.go b/zmagmacore/transaction/txn.go deleted file mode 100644 index 528d34320..000000000 --- a/zmagmacore/transaction/txn.go +++ /dev/null @@ -1,140 +0,0 @@ -package transaction - -import ( - "context" - "encoding/json" - "fmt" - - "github.com/0chain/gosdk/core/util" - "github.com/0chain/gosdk/zcncore" - "github.com/0chain/gosdk/zmagmacore/chain" - "github.com/0chain/gosdk/zmagmacore/errors" - "github.com/0chain/gosdk/zmagmacore/node" - ctime "github.com/0chain/gosdk/zmagmacore/time" -) - -type ( - // Transaction entity that encapsulates the transaction related data and metadata. - Transaction struct { - Hash string `json:"hash,omitempty"` - Version string `json:"version,omitempty"` - ClientID string `json:"client_id,omitempty"` - PublicKey string `json:"public_key,omitempty"` - ToClientID string `json:"to_client_id,omitempty"` - ChainID string `json:"chain_id,omitempty"` - TransactionData string `json:"transaction_data,omitempty"` - Value int64 `json:"transaction_value,omitempty"` - Signature string `json:"signature,omitempty"` - CreationDate ctime.Timestamp `json:"creation_date,omitempty"` - TransactionType int `json:"transaction_type,omitempty"` - TransactionOutput string `json:"transaction_output,omitempty"` - OutputHash string `json:"txn_output_hash"` - - scheme zcncore.TransactionScheme - - callBack *callback - } -) - -// NewTransactionEntity creates Transaction with initialized fields. -// Sets version, client ID, creation date, public key and creates internal zcncore.TransactionScheme. -func NewTransactionEntity() (*Transaction, error) { - txn := &Transaction{ - Version: "1.0", - ClientID: node.ID(), - CreationDate: ctime.Now(), - ChainID: chain.GetServerChain().ID, - PublicKey: node.PublicKey(), - callBack: newCallBack(), - } - zcntxn, err := zcncore.NewTransaction(txn.callBack, 0, 0) - if err != nil { - return nil, err - } - txn.scheme = zcntxn - - return txn, nil -} - -// ExecuteSmartContract executes function of smart contract with provided address. -// -// Returns hash of executed transaction. -func (t *Transaction) ExecuteSmartContract(ctx context.Context, address, funcName, input string, - val uint64) (string, error) { - const errCode = "transaction_send" - - _, err := t.scheme.ExecuteSmartContract(address, funcName, input, val) - if err != nil { - msg := fmt.Sprintf("error while sending txn: %v", err) - return "", errors.New(errCode, msg) - } - - if err := t.callBack.waitCompleteCall(ctx); err != nil { - msg := fmt.Sprintf("error while sending txn: %v", err) - return "", errors.New(errCode, msg) - } - - t.Hash = t.scheme.GetTransactionHash() - if len(t.scheme.GetTransactionError()) > 0 { - return "", errors.New(errCode, t.scheme.GetTransactionError()) - } - - return t.Hash, nil -} - -type ( - verifyOutput struct { - Confirmation confirmation `json:"confirmation"` - } - - // confirmation represents the acceptance that a transaction is included into the blockchain. - confirmation struct { - Version string `json:"version"` - Hash string `json:"hash"` - BlockHash string `json:"block_hash"` - PreviousBlockHash string `json:"previous_block_hash"` - Transaction *Transaction `json:"txn,omitempty"` - CreationDate ctime.Timestamp `json:"creation_date"` - MinerID string `json:"miner_id"` - Round int64 `json:"round"` - Status int `json:"transaction_status"` - RoundRandomSeed int64 `json:"round_random_seed"` - MerkleTreeRoot string `json:"merkle_tree_root"` - MerkleTreePath *util.MTPath `json:"merkle_tree_path"` - ReceiptMerkleTreeRoot string `json:"receipt_merkle_tree_root"` - ReceiptMerkleTreePath *util.MTPath `json:"receipt_merkle_tree_path"` - } -) - -// Verify checks including of transaction in the blockchain. -func (t *Transaction) Verify(ctx context.Context) error { - const errCode = "transaction_verify" - - if err := t.scheme.SetTransactionHash(t.Hash); err != nil { - return err - } - - err := t.scheme.Verify() - if err != nil { - msg := fmt.Sprintf("error while verifying txn: %v; txn hash: %s", err, t.scheme.GetTransactionHash()) - return errors.New(errCode, msg) - } - - if err := t.callBack.waitVerifyCall(ctx); err != nil { - msg := fmt.Sprintf("error while verifying txn: %v; txn hash: %s", err, t.scheme.GetTransactionHash()) - return errors.New(errCode, msg) - } - - vo := new(verifyOutput) - if err := json.Unmarshal([]byte(t.scheme.GetVerifyOutput()), vo); err != nil { - return errors.New(errCode, "error while unmarshalling confirmation: "+err.Error()+", json: "+t.scheme.GetVerifyOutput()) - } - - if vo.Confirmation.Transaction != nil { - t.TransactionOutput = vo.Confirmation.Transaction.TransactionOutput - } else { - return errors.New(errCode, "got invalid confirmation (missing transaction)") - } - - return nil -} diff --git a/zmagmacore/wallet/balance.go b/zmagmacore/wallet/balance.go deleted file mode 100644 index 1bd1c7165..000000000 --- a/zmagmacore/wallet/balance.go +++ /dev/null @@ -1,52 +0,0 @@ -// DEPRECATED: This package is deprecated and will be removed in a future release. -package wallet - -import ( - "context" - - "github.com/0chain/gosdk/zcncore" - "github.com/0chain/gosdk/zmagmacore/errors" -) - -type ( - // getBalanceCallBack implements zcncore.GetBalanceCallback interface. - getBalanceCallBack struct { - balanceCh chan int64 - err error - } -) - -// Balance responds balance of the wallet that used. -// -// NOTE: for using Balance you must set wallet info by running zcncore.SetWalletInfo. -func Balance(ctx context.Context) (int64, error) { - cb := newGetBalanceCallBack() - err := zcncore.GetBalance(cb) - if err != nil { - return 0, err - } - - var b int64 - select { - case <-ctx.Done(): - return 0, errors.New("get_balance", "context done is called") - case b = <-cb.balanceCh: - return b, cb.err - } -} - -// newGetBalanceCallBack creates initialized getBalanceCallBack. -func newGetBalanceCallBack() *getBalanceCallBack { - return &getBalanceCallBack{ - balanceCh: make(chan int64), - } -} - -// OnBalanceAvailable implements zcncore.GetBalanceCallback interface. -func (b *getBalanceCallBack) OnBalanceAvailable(status int, value int64, _ string) { - if status != zcncore.StatusSuccess { - b.err = errors.New("get_balance", "failed respond balance") - } - - b.balanceCh <- value -} diff --git a/zmagmacore/wallet/callback.go b/zmagmacore/wallet/callback.go deleted file mode 100644 index 3f93bda21..000000000 --- a/zmagmacore/wallet/callback.go +++ /dev/null @@ -1,18 +0,0 @@ -package wallet - -import ( - "github.com/0chain/gosdk/zcncore" -) - -type ( - // walletCallback provides callback struct for operations with wallet. - walletCallback struct{} -) - -var ( - // Ensure walletCallback implements interface. - _ zcncore.WalletCallback = (*walletCallback)(nil) -) - -// OnWalletCreateComplete implements zcncore.WalletCallback interface. -func (wb *walletCallback) OnWalletCreateComplete(_ int, _ string, _ string) {} diff --git a/zmagmacore/wallet/setup.go b/zmagmacore/wallet/setup.go deleted file mode 100644 index de4847d21..000000000 --- a/zmagmacore/wallet/setup.go +++ /dev/null @@ -1,44 +0,0 @@ -package wallet - -import ( - "context" - - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/conf" - "github.com/0chain/gosdk/core/logger" - "github.com/0chain/gosdk/zcncore" -) - -// SetupZCNSDK runs zcncore.SetLogFile, zcncore.SetLogLevel and zcncore.InitZCNSDK using provided Config. -// -// If an error occurs during execution, the program terminates with code 2 and the error will be written in os.Stderr. -// -// SetupZCNSDK should be used only once while application is starting. -func SetupZCNSDK(cfg Config) error { - var logName = cfg.LogDir() + "/zsdk.log" - zcncore.SetLogFile(logName, false) - zcncore.SetLogLevel(logLevelFromStr(cfg.LogLvl())) - return client.Init(context.Background(), conf.Config{ - BlockWorker: cfg.BlockWorker(), - SignatureScheme: cfg.SignatureScheme(), - }) -} - -// logLevelFromStr converts string log level to gosdk logger level int value. -func logLevelFromStr(level string) int { - switch level { - case "none": - return logger.NONE - case "fatal": - return logger.FATAL - case "error": - return logger.ERROR - case "info": - return logger.INFO - case "debug": - return logger.DEBUG - - default: - return -1 - } -} diff --git a/zmagmacore/wallet/types.go b/zmagmacore/wallet/types.go deleted file mode 100644 index 3f893ba70..000000000 --- a/zmagmacore/wallet/types.go +++ /dev/null @@ -1,18 +0,0 @@ -package wallet - -type ( - // Config represents config interface used for setup wallet. - Config interface { - // LogDir returns directory to store logs. - LogDir() string - - // LogLvl returns level of logs. - LogLvl() string - - // BlockWorker returns address of dns server. - BlockWorker() string - - // SignatureScheme returns signature scheme. - SignatureScheme() string - } -) diff --git a/zmagmacore/wallet/wallet.go b/zmagmacore/wallet/wallet.go deleted file mode 100644 index 348902f71..000000000 --- a/zmagmacore/wallet/wallet.go +++ /dev/null @@ -1,63 +0,0 @@ -package wallet - -import ( - "encoding/hex" - "encoding/json" - - "github.com/0chain/gosdk/core/zcncrypto" - "github.com/0chain/gosdk/zmagmacore/crypto" -) - -type ( - // Wallet represents a wallet that stores keys and additional info. - Wallet struct { - ZCNWallet *zcncrypto.Wallet - } -) - -// New creates initialized Wallet. -func New(publicKey, privateKey []byte) *Wallet { - var ( - publicKeyHex, privateKeyHex = hex.EncodeToString(publicKey), hex.EncodeToString(privateKey) - ) - return &Wallet{ - ZCNWallet: &zcncrypto.Wallet{ - ClientID: crypto.Hash(publicKey), - ClientKey: publicKeyHex, - Keys: []zcncrypto.KeyPair{ - { - PublicKey: publicKeyHex, - PrivateKey: privateKeyHex, - }, - }, - Version: zcncrypto.CryptoVersion, - }, - } -} - -// PublicKey returns the public key. -func (w *Wallet) PublicKey() string { - return w.ZCNWallet.Keys[0].PublicKey -} - -// PrivateKey returns the public key. -func (w *Wallet) PrivateKey() string { - return w.ZCNWallet.Keys[0].PrivateKey -} - -// ID returns the client id. -// -// NOTE: client id represents hex encoded SHA3-256 hash of the raw public key. -func (w *Wallet) ID() string { - return w.ZCNWallet.ClientID -} - -// StringJSON returns marshalled to JSON string Wallet.ZCNWallet. -func (w *Wallet) StringJSON() (string, error) { - byt, err := json.Marshal(w.ZCNWallet) - if err != nil { - return "", err - } - - return string(byt), err -} From 0b717cee58c8383ea129f815ca3d12a12512333a Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 12 Sep 2024 00:02:08 +0530 Subject: [PATCH 063/319] Fix unit tests --- zcncore/mocks/AuthCallback.go | 30 -- zcncore/mocks/FeeOption.go | 33 -- zcncore/mocks/GetBalanceCallback.go | 30 -- zcncore/mocks/GetInfoCallback.go | 30 -- zcncore/mocks/GetNonceCallback.go | 30 -- zcncore/mocks/MSVoteCallback.go | 30 -- zcncore/mocks/QueryResultHandle.go | 42 -- zcncore/mocks/TransactionCallback.go | 43 -- zcncore/mocks/TransactionCommon.go | 553 -------------------- zcncore/mocks/TransactionScheme.go | 737 --------------------------- zcncore/mocks/WalletCallback.go | 30 -- zcncore/mocks/doc.go | 2 - zcncore/sample/snapshot_test.go | 122 ----- 13 files changed, 1712 deletions(-) delete mode 100644 zcncore/mocks/AuthCallback.go delete mode 100644 zcncore/mocks/FeeOption.go delete mode 100644 zcncore/mocks/GetBalanceCallback.go delete mode 100644 zcncore/mocks/GetInfoCallback.go delete mode 100644 zcncore/mocks/GetNonceCallback.go delete mode 100644 zcncore/mocks/MSVoteCallback.go delete mode 100644 zcncore/mocks/QueryResultHandle.go delete mode 100644 zcncore/mocks/TransactionCallback.go delete mode 100644 zcncore/mocks/TransactionCommon.go delete mode 100644 zcncore/mocks/TransactionScheme.go delete mode 100644 zcncore/mocks/WalletCallback.go delete mode 100644 zcncore/mocks/doc.go delete mode 100644 zcncore/sample/snapshot_test.go diff --git a/zcncore/mocks/AuthCallback.go b/zcncore/mocks/AuthCallback.go deleted file mode 100644 index 555aead13..000000000 --- a/zcncore/mocks/AuthCallback.go +++ /dev/null @@ -1,30 +0,0 @@ -// Code generated by mockery v2.28.1. DO NOT EDIT. - -package mocks - -import mock "github.com/stretchr/testify/mock" - -// AuthCallback is an autogenerated mock type for the AuthCallback type -type AuthCallback struct { - mock.Mock -} - -// OnSetupComplete provides a mock function with given fields: status, err -func (_m *AuthCallback) OnSetupComplete(status int, err string) { - _m.Called(status, err) -} - -type mockConstructorTestingTNewAuthCallback interface { - mock.TestingT - Cleanup(func()) -} - -// NewAuthCallback creates a new instance of AuthCallback. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewAuthCallback(t mockConstructorTestingTNewAuthCallback) *AuthCallback { - mock := &AuthCallback{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/zcncore/mocks/FeeOption.go b/zcncore/mocks/FeeOption.go deleted file mode 100644 index 34678d44b..000000000 --- a/zcncore/mocks/FeeOption.go +++ /dev/null @@ -1,33 +0,0 @@ -// Code generated by mockery v2.28.1. DO NOT EDIT. - -package mocks - -import ( - zcncore "github.com/0chain/gosdk/zcncore" - mock "github.com/stretchr/testify/mock" -) - -// FeeOption is an autogenerated mock type for the FeeOption type -type FeeOption struct { - mock.Mock -} - -// Execute provides a mock function with given fields: _a0 -func (_m *FeeOption) Execute(_a0 *zcncore.TxnFeeOption) { - _m.Called(_a0) -} - -type mockConstructorTestingTNewFeeOption interface { - mock.TestingT - Cleanup(func()) -} - -// NewFeeOption creates a new instance of FeeOption. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewFeeOption(t mockConstructorTestingTNewFeeOption) *FeeOption { - mock := &FeeOption{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/zcncore/mocks/GetBalanceCallback.go b/zcncore/mocks/GetBalanceCallback.go deleted file mode 100644 index 36284afcc..000000000 --- a/zcncore/mocks/GetBalanceCallback.go +++ /dev/null @@ -1,30 +0,0 @@ -// Code generated by mockery v2.28.1. DO NOT EDIT. - -package mocks - -import mock "github.com/stretchr/testify/mock" - -// GetBalanceCallback is an autogenerated mock type for the GetBalanceCallback type -type GetBalanceCallback struct { - mock.Mock -} - -// OnBalanceAvailable provides a mock function with given fields: status, value, info -func (_m *GetBalanceCallback) OnBalanceAvailable(status int, value int64, info string) { - _m.Called(status, value, info) -} - -type mockConstructorTestingTNewGetBalanceCallback interface { - mock.TestingT - Cleanup(func()) -} - -// NewGetBalanceCallback creates a new instance of GetBalanceCallback. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewGetBalanceCallback(t mockConstructorTestingTNewGetBalanceCallback) *GetBalanceCallback { - mock := &GetBalanceCallback{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/zcncore/mocks/GetInfoCallback.go b/zcncore/mocks/GetInfoCallback.go deleted file mode 100644 index 5d62dee17..000000000 --- a/zcncore/mocks/GetInfoCallback.go +++ /dev/null @@ -1,30 +0,0 @@ -// Code generated by mockery v2.28.1. DO NOT EDIT. - -package mocks - -import mock "github.com/stretchr/testify/mock" - -// GetInfoCallback is an autogenerated mock type for the GetInfoCallback type -type GetInfoCallback struct { - mock.Mock -} - -// OnInfoAvailable provides a mock function with given fields: op, status, info, err -func (_m *GetInfoCallback) OnInfoAvailable(op int, status int, info string, err string) { - _m.Called(op, status, info, err) -} - -type mockConstructorTestingTNewGetInfoCallback interface { - mock.TestingT - Cleanup(func()) -} - -// NewGetInfoCallback creates a new instance of GetInfoCallback. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewGetInfoCallback(t mockConstructorTestingTNewGetInfoCallback) *GetInfoCallback { - mock := &GetInfoCallback{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/zcncore/mocks/GetNonceCallback.go b/zcncore/mocks/GetNonceCallback.go deleted file mode 100644 index d18111569..000000000 --- a/zcncore/mocks/GetNonceCallback.go +++ /dev/null @@ -1,30 +0,0 @@ -// Code generated by mockery v2.28.1. DO NOT EDIT. - -package mocks - -import mock "github.com/stretchr/testify/mock" - -// GetNonceCallback is an autogenerated mock type for the GetNonceCallback type -type GetNonceCallback struct { - mock.Mock -} - -// OnNonceAvailable provides a mock function with given fields: status, nonce, info -func (_m *GetNonceCallback) OnNonceAvailable(status int, nonce int64, info string) { - _m.Called(status, nonce, info) -} - -type mockConstructorTestingTNewGetNonceCallback interface { - mock.TestingT - Cleanup(func()) -} - -// NewGetNonceCallback creates a new instance of GetNonceCallback. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewGetNonceCallback(t mockConstructorTestingTNewGetNonceCallback) *GetNonceCallback { - mock := &GetNonceCallback{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/zcncore/mocks/MSVoteCallback.go b/zcncore/mocks/MSVoteCallback.go deleted file mode 100644 index 0100e83ee..000000000 --- a/zcncore/mocks/MSVoteCallback.go +++ /dev/null @@ -1,30 +0,0 @@ -// Code generated by mockery v2.28.1. DO NOT EDIT. - -package mocks - -import mock "github.com/stretchr/testify/mock" - -// MSVoteCallback is an autogenerated mock type for the MSVoteCallback type -type MSVoteCallback struct { - mock.Mock -} - -// OnVoteComplete provides a mock function with given fields: status, proposal, err -func (_m *MSVoteCallback) OnVoteComplete(status int, proposal string, err string) { - _m.Called(status, proposal, err) -} - -type mockConstructorTestingTNewMSVoteCallback interface { - mock.TestingT - Cleanup(func()) -} - -// NewMSVoteCallback creates a new instance of MSVoteCallback. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewMSVoteCallback(t mockConstructorTestingTNewMSVoteCallback) *MSVoteCallback { - mock := &MSVoteCallback{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/zcncore/mocks/QueryResultHandle.go b/zcncore/mocks/QueryResultHandle.go deleted file mode 100644 index de84a6e22..000000000 --- a/zcncore/mocks/QueryResultHandle.go +++ /dev/null @@ -1,42 +0,0 @@ -// Code generated by mockery v2.28.1. DO NOT EDIT. - -package mocks - -import ( - zcncore "github.com/0chain/gosdk/zcncore" - mock "github.com/stretchr/testify/mock" -) - -// QueryResultHandle is an autogenerated mock type for the QueryResultHandle type -type QueryResultHandle struct { - mock.Mock -} - -// Execute provides a mock function with given fields: result -func (_m *QueryResultHandle) Execute(result zcncore.QueryResult) bool { - ret := _m.Called(result) - - var r0 bool - if rf, ok := ret.Get(0).(func(zcncore.QueryResult) bool); ok { - r0 = rf(result) - } else { - r0 = ret.Get(0).(bool) - } - - return r0 -} - -type mockConstructorTestingTNewQueryResultHandle interface { - mock.TestingT - Cleanup(func()) -} - -// NewQueryResultHandle creates a new instance of QueryResultHandle. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewQueryResultHandle(t mockConstructorTestingTNewQueryResultHandle) *QueryResultHandle { - mock := &QueryResultHandle{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/zcncore/mocks/TransactionCallback.go b/zcncore/mocks/TransactionCallback.go deleted file mode 100644 index c51628063..000000000 --- a/zcncore/mocks/TransactionCallback.go +++ /dev/null @@ -1,43 +0,0 @@ -// Code generated by mockery v2.28.1. DO NOT EDIT. - -package mocks - -import ( - zcncore "github.com/0chain/gosdk/zcncore" - mock "github.com/stretchr/testify/mock" -) - -// TransactionCallback is an autogenerated mock type for the TransactionCallback type -type TransactionCallback struct { - mock.Mock -} - -// OnAuthComplete provides a mock function with given fields: t, status -func (_m *TransactionCallback) OnAuthComplete(t *zcncore.Transaction, status int) { - _m.Called(t, status) -} - -// OnTransactionComplete provides a mock function with given fields: t, status -func (_m *TransactionCallback) OnTransactionComplete(t *zcncore.Transaction, status int) { - _m.Called(t, status) -} - -// OnVerifyComplete provides a mock function with given fields: t, status -func (_m *TransactionCallback) OnVerifyComplete(t *zcncore.Transaction, status int) { - _m.Called(t, status) -} - -type mockConstructorTestingTNewTransactionCallback interface { - mock.TestingT - Cleanup(func()) -} - -// NewTransactionCallback creates a new instance of TransactionCallback. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewTransactionCallback(t mockConstructorTestingTNewTransactionCallback) *TransactionCallback { - mock := &TransactionCallback{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/zcncore/mocks/TransactionCommon.go b/zcncore/mocks/TransactionCommon.go deleted file mode 100644 index c7d19ba81..000000000 --- a/zcncore/mocks/TransactionCommon.go +++ /dev/null @@ -1,553 +0,0 @@ -// Code generated by mockery v2.28.1. DO NOT EDIT. - -package mocks - -import ( - transaction "github.com/0chain/gosdk/core/transaction" - mock "github.com/stretchr/testify/mock" - - zcncore "github.com/0chain/gosdk/zcncore" -) - -// TransactionCommon is an autogenerated mock type for the TransactionCommon type -type TransactionCommon struct { - mock.Mock -} - -// AddHardfork provides a mock function with given fields: ip -func (_m *TransactionCommon) AddHardfork(ip *zcncore.InputMap) error { - ret := _m.Called(ip) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.InputMap) error); ok { - r0 = rf(ip) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// CancelAllocation provides a mock function with given fields: allocID -func (_m *TransactionCommon) CancelAllocation(allocID string) error { - ret := _m.Called(allocID) - - var r0 error - if rf, ok := ret.Get(0).(func(string) error); ok { - r0 = rf(allocID) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// CreateAllocation provides a mock function with given fields: car, lock -func (_m *TransactionCommon) CreateAllocation(car *zcncore.CreateAllocationRequest, lock uint64) error { - ret := _m.Called(car, lock) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.CreateAllocationRequest, uint64) error); ok { - r0 = rf(car, lock) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// CreateReadPool provides a mock function with given fields: -func (_m *TransactionCommon) CreateReadPool() error { - ret := _m.Called() - - var r0 error - if rf, ok := ret.Get(0).(func() error); ok { - r0 = rf() - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ExecuteSmartContract provides a mock function with given fields: address, methodName, input, val, feeOpts -func (_m *TransactionCommon) ExecuteSmartContract(address string, methodName string, input interface{}, val uint64, feeOpts ...zcncore.FeeOption) (*transaction.Transaction, error) { - _va := make([]interface{}, len(feeOpts)) - for _i := range feeOpts { - _va[_i] = feeOpts[_i] - } - var _ca []interface{} - _ca = append(_ca, address, methodName, input, val) - _ca = append(_ca, _va...) - ret := _m.Called(_ca...) - - var r0 *transaction.Transaction - var r1 error - if rf, ok := ret.Get(0).(func(string, string, interface{}, uint64, ...zcncore.FeeOption) (*transaction.Transaction, error)); ok { - return rf(address, methodName, input, val, feeOpts...) - } - if rf, ok := ret.Get(0).(func(string, string, interface{}, uint64, ...zcncore.FeeOption) *transaction.Transaction); ok { - r0 = rf(address, methodName, input, val, feeOpts...) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*transaction.Transaction) - } - } - - if rf, ok := ret.Get(1).(func(string, string, interface{}, uint64, ...zcncore.FeeOption) error); ok { - r1 = rf(address, methodName, input, val, feeOpts...) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// FaucetUpdateConfig provides a mock function with given fields: _a0 -func (_m *TransactionCommon) FaucetUpdateConfig(_a0 *zcncore.InputMap) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.InputMap) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// FinalizeAllocation provides a mock function with given fields: allocID -func (_m *TransactionCommon) FinalizeAllocation(allocID string) error { - ret := _m.Called(allocID) - - var r0 error - if rf, ok := ret.Get(0).(func(string) error); ok { - r0 = rf(allocID) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// GetVerifyConfirmationStatus provides a mock function with given fields: -func (_m *TransactionCommon) GetVerifyConfirmationStatus() zcncore.ConfirmationStatus { - ret := _m.Called() - - var r0 zcncore.ConfirmationStatus - if rf, ok := ret.Get(0).(func() zcncore.ConfirmationStatus); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(zcncore.ConfirmationStatus) - } - - return r0 -} - -// MinerSCCollectReward provides a mock function with given fields: providerID, providerType -func (_m *TransactionCommon) MinerSCCollectReward(providerID string, providerType zcncore.Provider) error { - ret := _m.Called(providerID, providerType) - - var r0 error - if rf, ok := ret.Get(0).(func(string, zcncore.Provider) error); ok { - r0 = rf(providerID, providerType) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerSCDeleteMiner provides a mock function with given fields: _a0 -func (_m *TransactionCommon) MinerSCDeleteMiner(_a0 *zcncore.MinerSCMinerInfo) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.MinerSCMinerInfo) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerSCDeleteSharder provides a mock function with given fields: _a0 -func (_m *TransactionCommon) MinerSCDeleteSharder(_a0 *zcncore.MinerSCMinerInfo) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.MinerSCMinerInfo) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerSCKill provides a mock function with given fields: providerID, providerType -func (_m *TransactionCommon) MinerSCKill(providerID string, providerType zcncore.Provider) error { - ret := _m.Called(providerID, providerType) - - var r0 error - if rf, ok := ret.Get(0).(func(string, zcncore.Provider) error); ok { - r0 = rf(providerID, providerType) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerSCLock provides a mock function with given fields: providerId, providerType, lock -func (_m *TransactionCommon) MinerSCLock(providerId string, providerType zcncore.Provider, lock uint64) error { - ret := _m.Called(providerId, providerType, lock) - - var r0 error - if rf, ok := ret.Get(0).(func(string, zcncore.Provider, uint64) error); ok { - r0 = rf(providerId, providerType, lock) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerSCMinerSettings provides a mock function with given fields: _a0 -func (_m *TransactionCommon) MinerSCMinerSettings(_a0 *zcncore.MinerSCMinerInfo) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.MinerSCMinerInfo) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerSCSharderSettings provides a mock function with given fields: _a0 -func (_m *TransactionCommon) MinerSCSharderSettings(_a0 *zcncore.MinerSCMinerInfo) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.MinerSCMinerInfo) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerSCUnlock provides a mock function with given fields: providerId, providerType -func (_m *TransactionCommon) MinerSCUnlock(providerId string, providerType zcncore.Provider) error { - ret := _m.Called(providerId, providerType) - - var r0 error - if rf, ok := ret.Get(0).(func(string, zcncore.Provider) error); ok { - r0 = rf(providerId, providerType) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerScUpdateConfig provides a mock function with given fields: _a0 -func (_m *TransactionCommon) MinerScUpdateConfig(_a0 *zcncore.InputMap) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.InputMap) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerScUpdateGlobals provides a mock function with given fields: _a0 -func (_m *TransactionCommon) MinerScUpdateGlobals(_a0 *zcncore.InputMap) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.InputMap) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ReadPoolLock provides a mock function with given fields: allocID, blobberID, duration, lock -func (_m *TransactionCommon) ReadPoolLock(allocID string, blobberID string, duration int64, lock uint64) error { - ret := _m.Called(allocID, blobberID, duration, lock) - - var r0 error - if rf, ok := ret.Get(0).(func(string, string, int64, uint64) error); ok { - r0 = rf(allocID, blobberID, duration, lock) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ReadPoolUnlock provides a mock function with given fields: -func (_m *TransactionCommon) ReadPoolUnlock() error { - ret := _m.Called() - - var r0 error - if rf, ok := ret.Get(0).(func() error); ok { - r0 = rf() - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// Send provides a mock function with given fields: toClientID, val, desc -func (_m *TransactionCommon) Send(toClientID string, val uint64, desc string) error { - ret := _m.Called(toClientID, val, desc) - - var r0 error - if rf, ok := ret.Get(0).(func(string, uint64, string) error); ok { - r0 = rf(toClientID, val, desc) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// StakePoolLock provides a mock function with given fields: providerId, providerType, lock -func (_m *TransactionCommon) StakePoolLock(providerId string, providerType zcncore.Provider, lock uint64) error { - ret := _m.Called(providerId, providerType, lock) - - var r0 error - if rf, ok := ret.Get(0).(func(string, zcncore.Provider, uint64) error); ok { - r0 = rf(providerId, providerType, lock) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// StakePoolUnlock provides a mock function with given fields: providerId, providerType -func (_m *TransactionCommon) StakePoolUnlock(providerId string, providerType zcncore.Provider) error { - ret := _m.Called(providerId, providerType) - - var r0 error - if rf, ok := ret.Get(0).(func(string, zcncore.Provider) error); ok { - r0 = rf(providerId, providerType) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// StorageSCCollectReward provides a mock function with given fields: providerID, providerType -func (_m *TransactionCommon) StorageSCCollectReward(providerID string, providerType zcncore.Provider) error { - ret := _m.Called(providerID, providerType) - - var r0 error - if rf, ok := ret.Get(0).(func(string, zcncore.Provider) error); ok { - r0 = rf(providerID, providerType) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// StorageScUpdateConfig provides a mock function with given fields: _a0 -func (_m *TransactionCommon) StorageScUpdateConfig(_a0 *zcncore.InputMap) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.InputMap) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// UpdateAllocation provides a mock function with given fields: allocID, sizeDiff, expirationDiff, lock -func (_m *TransactionCommon) UpdateAllocation(allocID string, sizeDiff int64, expirationDiff int64, lock uint64) error { - ret := _m.Called(allocID, sizeDiff, expirationDiff, lock) - - var r0 error - if rf, ok := ret.Get(0).(func(string, int64, int64, uint64) error); ok { - r0 = rf(allocID, sizeDiff, expirationDiff, lock) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// UpdateBlobberSettings provides a mock function with given fields: blobber -func (_m *TransactionCommon) UpdateBlobberSettings(blobber *zcncore.Blobber) error { - ret := _m.Called(blobber) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.Blobber) error); ok { - r0 = rf(blobber) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// UpdateValidatorSettings provides a mock function with given fields: validator -func (_m *TransactionCommon) UpdateValidatorSettings(validator *zcncore.Validator) error { - ret := _m.Called(validator) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.Validator) error); ok { - r0 = rf(validator) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// WritePoolLock provides a mock function with given fields: allocID, blobberID, duration, lock -func (_m *TransactionCommon) WritePoolLock(allocID string, blobberID string, duration int64, lock uint64) error { - ret := _m.Called(allocID, blobberID, duration, lock) - - var r0 error - if rf, ok := ret.Get(0).(func(string, string, int64, uint64) error); ok { - r0 = rf(allocID, blobberID, duration, lock) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// WritePoolUnlock provides a mock function with given fields: allocID -func (_m *TransactionCommon) WritePoolUnlock(allocID string) error { - ret := _m.Called(allocID) - - var r0 error - if rf, ok := ret.Get(0).(func(string) error); ok { - r0 = rf(allocID) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ZCNSCAddAuthorizer provides a mock function with given fields: _a0 -func (_m *TransactionCommon) ZCNSCAddAuthorizer(_a0 *zcncore.AddAuthorizerPayload) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.AddAuthorizerPayload) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ZCNSCAuthorizerHealthCheck provides a mock function with given fields: _a0 -func (_m *TransactionCommon) ZCNSCAuthorizerHealthCheck(_a0 *zcncore.AuthorizerHealthCheckPayload) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.AuthorizerHealthCheckPayload) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ZCNSCCollectReward provides a mock function with given fields: providerID, providerType -func (_m *TransactionCommon) ZCNSCCollectReward(providerID string, providerType zcncore.Provider) error { - ret := _m.Called(providerID, providerType) - - var r0 error - if rf, ok := ret.Get(0).(func(string, zcncore.Provider) error); ok { - r0 = rf(providerID, providerType) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ZCNSCDeleteAuthorizer provides a mock function with given fields: _a0 -func (_m *TransactionCommon) ZCNSCDeleteAuthorizer(_a0 *zcncore.DeleteAuthorizerPayload) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.DeleteAuthorizerPayload) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ZCNSCUpdateAuthorizerConfig provides a mock function with given fields: _a0 -func (_m *TransactionCommon) ZCNSCUpdateAuthorizerConfig(_a0 *zcncore.AuthorizerNode) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.AuthorizerNode) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ZCNSCUpdateGlobalConfig provides a mock function with given fields: _a0 -func (_m *TransactionCommon) ZCNSCUpdateGlobalConfig(_a0 *zcncore.InputMap) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.InputMap) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -type mockConstructorTestingTNewTransactionCommon interface { - mock.TestingT - Cleanup(func()) -} - -// NewTransactionCommon creates a new instance of TransactionCommon. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewTransactionCommon(t mockConstructorTestingTNewTransactionCommon) *TransactionCommon { - mock := &TransactionCommon{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/zcncore/mocks/TransactionScheme.go b/zcncore/mocks/TransactionScheme.go deleted file mode 100644 index bdfdfde40..000000000 --- a/zcncore/mocks/TransactionScheme.go +++ /dev/null @@ -1,737 +0,0 @@ -// Code generated by mockery v2.28.1. DO NOT EDIT. - -package mocks - -import ( - transaction "github.com/0chain/gosdk/core/transaction" - mock "github.com/stretchr/testify/mock" - - zcncore "github.com/0chain/gosdk/zcncore" -) - -// TransactionScheme is an autogenerated mock type for the TransactionScheme type -type TransactionScheme struct { - mock.Mock -} - -// AddHardfork provides a mock function with given fields: ip -func (_m *TransactionScheme) AddHardfork(ip *zcncore.InputMap) error { - ret := _m.Called(ip) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.InputMap) error); ok { - r0 = rf(ip) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// CancelAllocation provides a mock function with given fields: allocID -func (_m *TransactionScheme) CancelAllocation(allocID string) error { - ret := _m.Called(allocID) - - var r0 error - if rf, ok := ret.Get(0).(func(string) error); ok { - r0 = rf(allocID) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// CreateAllocation provides a mock function with given fields: car, lock -func (_m *TransactionScheme) CreateAllocation(car *zcncore.CreateAllocationRequest, lock uint64) error { - ret := _m.Called(car, lock) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.CreateAllocationRequest, uint64) error); ok { - r0 = rf(car, lock) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// CreateReadPool provides a mock function with given fields: -func (_m *TransactionScheme) CreateReadPool() error { - ret := _m.Called() - - var r0 error - if rf, ok := ret.Get(0).(func() error); ok { - r0 = rf() - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ExecuteFaucetSCWallet provides a mock function with given fields: walletStr, methodName, input -func (_m *TransactionScheme) ExecuteFaucetSCWallet(walletStr string, methodName string, input []byte) error { - ret := _m.Called(walletStr, methodName, input) - - var r0 error - if rf, ok := ret.Get(0).(func(string, string, []byte) error); ok { - r0 = rf(walletStr, methodName, input) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ExecuteSmartContract provides a mock function with given fields: address, methodName, input, val, feeOpts -func (_m *TransactionScheme) ExecuteSmartContract(address string, methodName string, input interface{}, val uint64, feeOpts ...zcncore.FeeOption) (*transaction.Transaction, error) { - _va := make([]interface{}, len(feeOpts)) - for _i := range feeOpts { - _va[_i] = feeOpts[_i] - } - var _ca []interface{} - _ca = append(_ca, address, methodName, input, val) - _ca = append(_ca, _va...) - ret := _m.Called(_ca...) - - var r0 *transaction.Transaction - var r1 error - if rf, ok := ret.Get(0).(func(string, string, interface{}, uint64, ...zcncore.FeeOption) (*transaction.Transaction, error)); ok { - return rf(address, methodName, input, val, feeOpts...) - } - if rf, ok := ret.Get(0).(func(string, string, interface{}, uint64, ...zcncore.FeeOption) *transaction.Transaction); ok { - r0 = rf(address, methodName, input, val, feeOpts...) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*transaction.Transaction) - } - } - - if rf, ok := ret.Get(1).(func(string, string, interface{}, uint64, ...zcncore.FeeOption) error); ok { - r1 = rf(address, methodName, input, val, feeOpts...) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// FaucetUpdateConfig provides a mock function with given fields: _a0 -func (_m *TransactionScheme) FaucetUpdateConfig(_a0 *zcncore.InputMap) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.InputMap) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// FinalizeAllocation provides a mock function with given fields: allocID -func (_m *TransactionScheme) FinalizeAllocation(allocID string) error { - ret := _m.Called(allocID) - - var r0 error - if rf, ok := ret.Get(0).(func(string) error); ok { - r0 = rf(allocID) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// GetTransactionError provides a mock function with given fields: -func (_m *TransactionScheme) GetTransactionError() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// GetTransactionHash provides a mock function with given fields: -func (_m *TransactionScheme) GetTransactionHash() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// GetTransactionNonce provides a mock function with given fields: -func (_m *TransactionScheme) GetTransactionNonce() int64 { - ret := _m.Called() - - var r0 int64 - if rf, ok := ret.Get(0).(func() int64); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(int64) - } - - return r0 -} - -// GetVerifyConfirmationStatus provides a mock function with given fields: -func (_m *TransactionScheme) GetVerifyConfirmationStatus() zcncore.ConfirmationStatus { - ret := _m.Called() - - var r0 zcncore.ConfirmationStatus - if rf, ok := ret.Get(0).(func() zcncore.ConfirmationStatus); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(zcncore.ConfirmationStatus) - } - - return r0 -} - -// GetVerifyError provides a mock function with given fields: -func (_m *TransactionScheme) GetVerifyError() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// GetVerifyOutput provides a mock function with given fields: -func (_m *TransactionScheme) GetVerifyOutput() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// Hash provides a mock function with given fields: -func (_m *TransactionScheme) Hash() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// MinerSCCollectReward provides a mock function with given fields: providerID, providerType -func (_m *TransactionScheme) MinerSCCollectReward(providerID string, providerType zcncore.Provider) error { - ret := _m.Called(providerID, providerType) - - var r0 error - if rf, ok := ret.Get(0).(func(string, zcncore.Provider) error); ok { - r0 = rf(providerID, providerType) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerSCDeleteMiner provides a mock function with given fields: _a0 -func (_m *TransactionScheme) MinerSCDeleteMiner(_a0 *zcncore.MinerSCMinerInfo) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.MinerSCMinerInfo) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerSCDeleteSharder provides a mock function with given fields: _a0 -func (_m *TransactionScheme) MinerSCDeleteSharder(_a0 *zcncore.MinerSCMinerInfo) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.MinerSCMinerInfo) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerSCKill provides a mock function with given fields: providerID, providerType -func (_m *TransactionScheme) MinerSCKill(providerID string, providerType zcncore.Provider) error { - ret := _m.Called(providerID, providerType) - - var r0 error - if rf, ok := ret.Get(0).(func(string, zcncore.Provider) error); ok { - r0 = rf(providerID, providerType) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerSCLock provides a mock function with given fields: providerId, providerType, lock -func (_m *TransactionScheme) MinerSCLock(providerId string, providerType zcncore.Provider, lock uint64) error { - ret := _m.Called(providerId, providerType, lock) - - var r0 error - if rf, ok := ret.Get(0).(func(string, zcncore.Provider, uint64) error); ok { - r0 = rf(providerId, providerType, lock) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerSCMinerSettings provides a mock function with given fields: _a0 -func (_m *TransactionScheme) MinerSCMinerSettings(_a0 *zcncore.MinerSCMinerInfo) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.MinerSCMinerInfo) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerSCSharderSettings provides a mock function with given fields: _a0 -func (_m *TransactionScheme) MinerSCSharderSettings(_a0 *zcncore.MinerSCMinerInfo) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.MinerSCMinerInfo) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerSCUnlock provides a mock function with given fields: providerId, providerType -func (_m *TransactionScheme) MinerSCUnlock(providerId string, providerType zcncore.Provider) error { - ret := _m.Called(providerId, providerType) - - var r0 error - if rf, ok := ret.Get(0).(func(string, zcncore.Provider) error); ok { - r0 = rf(providerId, providerType) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerScUpdateConfig provides a mock function with given fields: _a0 -func (_m *TransactionScheme) MinerScUpdateConfig(_a0 *zcncore.InputMap) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.InputMap) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// MinerScUpdateGlobals provides a mock function with given fields: _a0 -func (_m *TransactionScheme) MinerScUpdateGlobals(_a0 *zcncore.InputMap) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.InputMap) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// Output provides a mock function with given fields: -func (_m *TransactionScheme) Output() []byte { - ret := _m.Called() - - var r0 []byte - if rf, ok := ret.Get(0).(func() []byte); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]byte) - } - } - - return r0 -} - -// ReadPoolLock provides a mock function with given fields: allocID, blobberID, duration, lock -func (_m *TransactionScheme) ReadPoolLock(allocID string, blobberID string, duration int64, lock uint64) error { - ret := _m.Called(allocID, blobberID, duration, lock) - - var r0 error - if rf, ok := ret.Get(0).(func(string, string, int64, uint64) error); ok { - r0 = rf(allocID, blobberID, duration, lock) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ReadPoolUnlock provides a mock function with given fields: -func (_m *TransactionScheme) ReadPoolUnlock() error { - ret := _m.Called() - - var r0 error - if rf, ok := ret.Get(0).(func() error); ok { - r0 = rf() - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// Send provides a mock function with given fields: toClientID, val, desc -func (_m *TransactionScheme) Send(toClientID string, val uint64, desc string) error { - ret := _m.Called(toClientID, val, desc) - - var r0 error - if rf, ok := ret.Get(0).(func(string, uint64, string) error); ok { - r0 = rf(toClientID, val, desc) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// SetTransactionCallback provides a mock function with given fields: cb -func (_m *TransactionScheme) SetTransactionCallback(cb zcncore.TransactionCallback) error { - ret := _m.Called(cb) - - var r0 error - if rf, ok := ret.Get(0).(func(zcncore.TransactionCallback) error); ok { - r0 = rf(cb) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// SetTransactionHash provides a mock function with given fields: hash -func (_m *TransactionScheme) SetTransactionHash(hash string) error { - ret := _m.Called(hash) - - var r0 error - if rf, ok := ret.Get(0).(func(string) error); ok { - r0 = rf(hash) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// SetTransactionNonce provides a mock function with given fields: txnNonce -func (_m *TransactionScheme) SetTransactionNonce(txnNonce int64) error { - ret := _m.Called(txnNonce) - - var r0 error - if rf, ok := ret.Get(0).(func(int64) error); ok { - r0 = rf(txnNonce) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// StakePoolLock provides a mock function with given fields: providerId, providerType, lock -func (_m *TransactionScheme) StakePoolLock(providerId string, providerType zcncore.Provider, lock uint64) error { - ret := _m.Called(providerId, providerType, lock) - - var r0 error - if rf, ok := ret.Get(0).(func(string, zcncore.Provider, uint64) error); ok { - r0 = rf(providerId, providerType, lock) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// StakePoolUnlock provides a mock function with given fields: providerId, providerType -func (_m *TransactionScheme) StakePoolUnlock(providerId string, providerType zcncore.Provider) error { - ret := _m.Called(providerId, providerType) - - var r0 error - if rf, ok := ret.Get(0).(func(string, zcncore.Provider) error); ok { - r0 = rf(providerId, providerType) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// StorageSCCollectReward provides a mock function with given fields: providerID, providerType -func (_m *TransactionScheme) StorageSCCollectReward(providerID string, providerType zcncore.Provider) error { - ret := _m.Called(providerID, providerType) - - var r0 error - if rf, ok := ret.Get(0).(func(string, zcncore.Provider) error); ok { - r0 = rf(providerID, providerType) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// StorageScUpdateConfig provides a mock function with given fields: _a0 -func (_m *TransactionScheme) StorageScUpdateConfig(_a0 *zcncore.InputMap) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.InputMap) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// StoreData provides a mock function with given fields: data -func (_m *TransactionScheme) StoreData(data string) error { - ret := _m.Called(data) - - var r0 error - if rf, ok := ret.Get(0).(func(string) error); ok { - r0 = rf(data) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// UpdateAllocation provides a mock function with given fields: allocID, sizeDiff, expirationDiff, lock -func (_m *TransactionScheme) UpdateAllocation(allocID string, sizeDiff int64, expirationDiff int64, lock uint64) error { - ret := _m.Called(allocID, sizeDiff, expirationDiff, lock) - - var r0 error - if rf, ok := ret.Get(0).(func(string, int64, int64, uint64) error); ok { - r0 = rf(allocID, sizeDiff, expirationDiff, lock) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// UpdateBlobberSettings provides a mock function with given fields: blobber -func (_m *TransactionScheme) UpdateBlobberSettings(blobber *zcncore.Blobber) error { - ret := _m.Called(blobber) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.Blobber) error); ok { - r0 = rf(blobber) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// UpdateValidatorSettings provides a mock function with given fields: validator -func (_m *TransactionScheme) UpdateValidatorSettings(validator *zcncore.Validator) error { - ret := _m.Called(validator) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.Validator) error); ok { - r0 = rf(validator) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// Verify provides a mock function with given fields: -func (_m *TransactionScheme) Verify() error { - ret := _m.Called() - - var r0 error - if rf, ok := ret.Get(0).(func() error); ok { - r0 = rf() - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// WritePoolLock provides a mock function with given fields: allocID, blobberID, duration, lock -func (_m *TransactionScheme) WritePoolLock(allocID string, blobberID string, duration int64, lock uint64) error { - ret := _m.Called(allocID, blobberID, duration, lock) - - var r0 error - if rf, ok := ret.Get(0).(func(string, string, int64, uint64) error); ok { - r0 = rf(allocID, blobberID, duration, lock) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// WritePoolUnlock provides a mock function with given fields: allocID -func (_m *TransactionScheme) WritePoolUnlock(allocID string) error { - ret := _m.Called(allocID) - - var r0 error - if rf, ok := ret.Get(0).(func(string) error); ok { - r0 = rf(allocID) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ZCNSCAddAuthorizer provides a mock function with given fields: _a0 -func (_m *TransactionScheme) ZCNSCAddAuthorizer(_a0 *zcncore.AddAuthorizerPayload) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.AddAuthorizerPayload) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ZCNSCAuthorizerHealthCheck provides a mock function with given fields: _a0 -func (_m *TransactionScheme) ZCNSCAuthorizerHealthCheck(_a0 *zcncore.AuthorizerHealthCheckPayload) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.AuthorizerHealthCheckPayload) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ZCNSCCollectReward provides a mock function with given fields: providerID, providerType -func (_m *TransactionScheme) ZCNSCCollectReward(providerID string, providerType zcncore.Provider) error { - ret := _m.Called(providerID, providerType) - - var r0 error - if rf, ok := ret.Get(0).(func(string, zcncore.Provider) error); ok { - r0 = rf(providerID, providerType) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ZCNSCDeleteAuthorizer provides a mock function with given fields: _a0 -func (_m *TransactionScheme) ZCNSCDeleteAuthorizer(_a0 *zcncore.DeleteAuthorizerPayload) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.DeleteAuthorizerPayload) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ZCNSCUpdateAuthorizerConfig provides a mock function with given fields: _a0 -func (_m *TransactionScheme) ZCNSCUpdateAuthorizerConfig(_a0 *zcncore.AuthorizerNode) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.AuthorizerNode) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// ZCNSCUpdateGlobalConfig provides a mock function with given fields: _a0 -func (_m *TransactionScheme) ZCNSCUpdateGlobalConfig(_a0 *zcncore.InputMap) error { - ret := _m.Called(_a0) - - var r0 error - if rf, ok := ret.Get(0).(func(*zcncore.InputMap) error); ok { - r0 = rf(_a0) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -type mockConstructorTestingTNewTransactionScheme interface { - mock.TestingT - Cleanup(func()) -} - -// NewTransactionScheme creates a new instance of TransactionScheme. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewTransactionScheme(t mockConstructorTestingTNewTransactionScheme) *TransactionScheme { - mock := &TransactionScheme{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/zcncore/mocks/WalletCallback.go b/zcncore/mocks/WalletCallback.go deleted file mode 100644 index 06b0ea6db..000000000 --- a/zcncore/mocks/WalletCallback.go +++ /dev/null @@ -1,30 +0,0 @@ -// Code generated by mockery v2.28.1. DO NOT EDIT. - -package mocks - -import mock "github.com/stretchr/testify/mock" - -// WalletCallback is an autogenerated mock type for the WalletCallback type -type WalletCallback struct { - mock.Mock -} - -// OnWalletCreateComplete provides a mock function with given fields: status, wallet, err -func (_m *WalletCallback) OnWalletCreateComplete(status int, wallet string, err string) { - _m.Called(status, wallet, err) -} - -type mockConstructorTestingTNewWalletCallback interface { - mock.TestingT - Cleanup(func()) -} - -// NewWalletCallback creates a new instance of WalletCallback. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewWalletCallback(t mockConstructorTestingTNewWalletCallback) *WalletCallback { - mock := &WalletCallback{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/zcncore/mocks/doc.go b/zcncore/mocks/doc.go deleted file mode 100644 index d5f5048fa..000000000 --- a/zcncore/mocks/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// AUTOGENERATED! Do not use. -package mocks \ No newline at end of file diff --git a/zcncore/sample/snapshot_test.go b/zcncore/sample/snapshot_test.go deleted file mode 100644 index 53cf727d1..000000000 --- a/zcncore/sample/snapshot_test.go +++ /dev/null @@ -1,122 +0,0 @@ -package sample - -import ( - "context" - "fmt" - "sync" - "testing" - - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/conf" - "github.com/0chain/gosdk/zcnbridge/wallet" - "github.com/0chain/gosdk/zcncore" -) - -type BlobberAggregate struct { - BlobberID string `json:"blobber_id" gorm:"index:idx_blobber_aggregate,unique"` - Round int64 `json:"round" gorm:"index:idx_blobber_aggregate,unique"` - - WritePrice uint64 `json:"write_price"` - Capacity int64 `json:"capacity"` // total blobber capacity - Allocated int64 `json:"allocated"` // allocated capacity - SavedData int64 `json:"saved_data"` - ReadData int64 `json:"read_data"` - OffersTotal uint64 `json:"offers_total"` - TotalStake uint64 `json:"total_stake"` - - TotalServiceCharge uint64 `json:"total_service_charge"` - ChallengesPassed uint64 `json:"challenges_passed"` - ChallengesCompleted uint64 `json:"challenges_completed"` - OpenChallenges uint64 `json:"open_challenges"` - InactiveRounds int64 `json:"InactiveRounds"` - RankMetric float64 `json:"rank_metric" gorm:"index:idx_ba_rankmetric"` -} - -const ChainConfig = ` - { - "chain_id":"0afc093ffb509f059c55478bc1a60351cef7b4e9c008a53a6cc8241ca8617dfe", - "signature_scheme" : "bls0chain", - "block_worker" : "http://dev.zus.network/dns", - "min_submit" : 50, - "min_confirmation" : 50, - "confirmation_chain_length" : 3, - "num_keys" : 1, - "eth_node" : "https://ropsten.infura.io/v3/xxxxxxxxxxxxxxx" - } -` - -func TestGetAggregates(t *testing.T) { - t.Skip("learning test") - cfg := conf.Config{} - err := client.Init(context.Background(), cfg) - if err != nil { - fmt.Println("Init failed") - return - } - - var w = ` - { - "client_id":"0bc96a0980170045863d826f9eb579d8144013210602e88426408e9f83c236f6", - "client_key":"a4e58c66b072d27288b650db9a476fe66a1a4f69e0f8fb11499f9ec3a579e21e5dc0298b8c5ae5baa205730d06bc04b07a31943ab3bd620e8427c15d5c413b9e", - "keys":[ - { - "public_key":"a4e58c66b072d27288b650db9a476fe66a1a4f69e0f8fb11499f9ec3a579e21e5dc0298b8c5ae5baa205730d06bc04b07a31943ab3bd620e8427c15d5c413b9e", - "private_key":"c0f3a3100241888ea9c2cc5c7300e3e510a8e7190c2c20b03f80e3937a91530d" - }], - "mnemonics":"snake mixed bird cream cotton trouble small fee finger catalog measure spoon private second canal pact unable close predict dream mask delay path inflict", - "version":"1.0", - "date_created":"2019-06-19 13:37:50.466889 -0700 PDT m=+0.023873276" - }` - - err = zcncore.SetWalletInfo(w, false) - if err != nil { - fmt.Println("set wallet info failed: ", err) - return - } - - wg := &sync.WaitGroup{} - var snaps []BlobberAggregate - statusBar := wallet.NewZCNStatus(&snaps) - statusBar.Wg = wg - wg.Add(5) - err = zcncore.GetBlobberSnapshots(int64(587), 20, 0, statusBar) - if err != nil { - t.Error(err) - return - } - - err = zcncore.GetSharderSnapshots(int64(587), 20, 0, statusBar) - if err != nil { - t.Error(err) - return - } - - err = zcncore.GetMinerSnapshots(int64(587), 20, 0, statusBar) - if err != nil { - t.Error(err) - return - } - - err = zcncore.GetAuthorizerSnapshots(int64(587), 20, 0, statusBar) - if err != nil { - t.Error(err) - return - } - - err = zcncore.GetValidatorSnapshots(int64(587), 20, 0, statusBar) - if err != nil { - t.Error(err) - return - } - - err = zcncore.GetUserSnapshots(int64(587), 20, 0, statusBar) - if err != nil { - t.Error(err) - return - } - - wg.Wait() - if !statusBar.Success { - t.Error(statusBar.Err) - } -} From 2247bb39c45462a987a056aee5ee47af644f8f3c Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 12 Sep 2024 00:10:24 +0530 Subject: [PATCH 064/319] Fix --- zboxapi/sdk.go | 4 +- zboxcore/zboxutil/http_test.go | 68 --------- zcnbridge/authorizer/proofBurnTicket.go | 3 +- zcnbridge/mocks/Transaction.go | 135 ------------------ .../mocks/TransactionCallbackAwaitable.go | 74 ---------- zcnbridge/mocks/TransactionProvider.go | 54 ------- 6 files changed, 4 insertions(+), 334 deletions(-) delete mode 100644 zboxcore/zboxutil/http_test.go delete mode 100644 zcnbridge/mocks/Transaction.go delete mode 100644 zcnbridge/mocks/TransactionCallbackAwaitable.go delete mode 100644 zcnbridge/mocks/TransactionProvider.go diff --git a/zboxapi/sdk.go b/zboxapi/sdk.go index 6c2d61094..c98d25876 100644 --- a/zboxapi/sdk.go +++ b/zboxapi/sdk.go @@ -6,7 +6,7 @@ import ( "encoding/json" "errors" "fmt" - "github.com/0chain/gosdk/zcncore" + "github.com/0chain/gosdk/core/client" "net/http" "strconv" "time" @@ -101,7 +101,7 @@ func (c *Client) createResty(ctx context.Context, csrfToken, userID string, head data := fmt.Sprintf("%v:%v:%v", c.clientID, userID, c.clientPublicKey) hash := encryption.Hash(data) - sign, err := zcncore.SignFn(hash) + sign, err := client.Sign(hash) if err != nil { return nil, err } diff --git a/zboxcore/zboxutil/http_test.go b/zboxcore/zboxutil/http_test.go deleted file mode 100644 index 750cd2afd..000000000 --- a/zboxcore/zboxutil/http_test.go +++ /dev/null @@ -1,68 +0,0 @@ -package zboxutil - -import ( - coreHttp "github.com/0chain/gosdk/core/client" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestIsCurrentDominantStatus(t *testing.T) { - for _, tc := range []struct { - name string - status int - runningTotalPerStatus map[int]int - runningMax int - wantIsDominant bool - }{ - { - name: "first response - 200", - status: 200, - runningTotalPerStatus: map[int]int{200: 1}, - runningMax: 1, - wantIsDominant: true, - }, - { - name: "first response - 500", - status: 500, - runningTotalPerStatus: map[int]int{500: 1}, - runningMax: 1, - wantIsDominant: true, - }, - { - name: "current response - 200 (previous was 500) - tiebreakers", - status: 200, - runningTotalPerStatus: map[int]int{200: 1, 500: 1}, - runningMax: 1, - wantIsDominant: true, - }, - { - name: "current response - 500 (previous was 200) - tiebreakers - should not be dominant", - status: 500, - runningTotalPerStatus: map[int]int{200: 1, 500: 1}, - runningMax: 1, - wantIsDominant: false, - }, - { - name: "current response - 500 (previous were 200, 500)", - status: 500, - runningTotalPerStatus: map[int]int{200: 1, 500: 2}, - runningMax: 2, - wantIsDominant: true, - }, - { - name: "current response - 200 (previous were 400, 404, 500) - tiebreakers", - status: 200, - runningTotalPerStatus: map[int]int{200: 1, 400: 1, 404: 1, 500: 1}, - runningMax: 1, - wantIsDominant: true, - }, - } { - tt := tc - t.Run(tt.name, func(t *testing.T) { - got := coreHttp.IsCurrentDominantStatus(tt.status, tt.runningTotalPerStatus, tt.runningMax) - - assert.Equal(t, tt.wantIsDominant, got) - }) - } -} diff --git a/zcnbridge/authorizer/proofBurnTicket.go b/zcnbridge/authorizer/proofBurnTicket.go index 84ecdec18..ca2c3ec9e 100644 --- a/zcnbridge/authorizer/proofBurnTicket.go +++ b/zcnbridge/authorizer/proofBurnTicket.go @@ -3,6 +3,7 @@ package authorizer import ( "encoding/json" "fmt" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zcncore" @@ -61,7 +62,7 @@ func (pb *ProofOfBurn) SignWithEthereum(b *zcnbridge.BridgeClient) (err error) { // Sign can sign if chain config is initialized func (pb *ProofOfBurn) Sign() (err error) { hash := zcncrypto.Sha3Sum256(pb.UnsignedMessage()) - sig, err := zcncore.Sign(hash) + sig, err := client.Sign(hash) if err != nil { return errors.Wrap("signature_0chain", "failed to sign proof-of-burn ticket using walletString ID ", err) } diff --git a/zcnbridge/mocks/Transaction.go b/zcnbridge/mocks/Transaction.go deleted file mode 100644 index ae323f12b..000000000 --- a/zcnbridge/mocks/Transaction.go +++ /dev/null @@ -1,135 +0,0 @@ -// Code generated by mockery v2.28.1. DO NOT EDIT. - -package mocks - -import ( - context "context" - - transaction "github.com/0chain/gosdk/zcnbridge/transaction" - mock "github.com/stretchr/testify/mock" - - zcncore "github.com/0chain/gosdk/zcncore" -) - -// Transaction is an autogenerated mock type for the Transaction type -type Transaction struct { - mock.Mock -} - -// ExecuteSmartContract provides a mock function with given fields: ctx, address, funcName, input, val -func (_m *Transaction) ExecuteSmartContract(ctx context.Context, address string, funcName string, input interface{}, val uint64) (string, error) { - ret := _m.Called(ctx, address, funcName, input, val) - - var r0 string - var r1 error - if rf, ok := ret.Get(0).(func(context.Context, string, string, interface{}, uint64) (string, error)); ok { - return rf(ctx, address, funcName, input, val) - } - if rf, ok := ret.Get(0).(func(context.Context, string, string, interface{}, uint64) string); ok { - r0 = rf(ctx, address, funcName, input, val) - } else { - r0 = ret.Get(0).(string) - } - - if rf, ok := ret.Get(1).(func(context.Context, string, string, interface{}, uint64) error); ok { - r1 = rf(ctx, address, funcName, input, val) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// GetCallback provides a mock function with given fields: -func (_m *Transaction) GetCallback() transaction.TransactionCallbackAwaitable { - ret := _m.Called() - - var r0 transaction.TransactionCallbackAwaitable - if rf, ok := ret.Get(0).(func() transaction.TransactionCallbackAwaitable); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(transaction.TransactionCallbackAwaitable) - } - } - - return r0 -} - -// GetHash provides a mock function with given fields: -func (_m *Transaction) GetHash() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// GetScheme provides a mock function with given fields: -func (_m *Transaction) GetScheme() zcncore.TransactionScheme { - ret := _m.Called() - - var r0 zcncore.TransactionScheme - if rf, ok := ret.Get(0).(func() zcncore.TransactionScheme); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(zcncore.TransactionScheme) - } - } - - return r0 -} - -// GetTransactionOutput provides a mock function with given fields: -func (_m *Transaction) GetTransactionOutput() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// SetHash provides a mock function with given fields: _a0 -func (_m *Transaction) SetHash(_a0 string) { - _m.Called(_a0) -} - -// Verify provides a mock function with given fields: ctx -func (_m *Transaction) Verify(ctx context.Context) error { - ret := _m.Called(ctx) - - var r0 error - if rf, ok := ret.Get(0).(func(context.Context) error); ok { - r0 = rf(ctx) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -type mockConstructorTestingTNewTransaction interface { - mock.TestingT - Cleanup(func()) -} - -// NewTransaction creates a new instance of Transaction. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewTransaction(t mockConstructorTestingTNewTransaction) *Transaction { - mock := &Transaction{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/zcnbridge/mocks/TransactionCallbackAwaitable.go b/zcnbridge/mocks/TransactionCallbackAwaitable.go deleted file mode 100644 index 9966f8fba..000000000 --- a/zcnbridge/mocks/TransactionCallbackAwaitable.go +++ /dev/null @@ -1,74 +0,0 @@ -// Code generated by mockery v2.28.1. DO NOT EDIT. - -package mocks - -import ( - context "context" - - mock "github.com/stretchr/testify/mock" - - zcncore "github.com/0chain/gosdk/zcncore" -) - -// TransactionCallbackAwaitable is an autogenerated mock type for the TransactionCallbackAwaitable type -type TransactionCallbackAwaitable struct { - mock.Mock -} - -// OnAuthComplete provides a mock function with given fields: t, status -func (_m *TransactionCallbackAwaitable) OnAuthComplete(t *zcncore.Transaction, status int) { - _m.Called(t, status) -} - -// OnTransactionComplete provides a mock function with given fields: t, status -func (_m *TransactionCallbackAwaitable) OnTransactionComplete(t *zcncore.Transaction, status int) { - _m.Called(t, status) -} - -// OnVerifyComplete provides a mock function with given fields: t, status -func (_m *TransactionCallbackAwaitable) OnVerifyComplete(t *zcncore.Transaction, status int) { - _m.Called(t, status) -} - -// WaitCompleteCall provides a mock function with given fields: ctx -func (_m *TransactionCallbackAwaitable) WaitCompleteCall(ctx context.Context) error { - ret := _m.Called(ctx) - - var r0 error - if rf, ok := ret.Get(0).(func(context.Context) error); ok { - r0 = rf(ctx) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// WaitVerifyCall provides a mock function with given fields: ctx -func (_m *TransactionCallbackAwaitable) WaitVerifyCall(ctx context.Context) error { - ret := _m.Called(ctx) - - var r0 error - if rf, ok := ret.Get(0).(func(context.Context) error); ok { - r0 = rf(ctx) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -type mockConstructorTestingTNewTransactionCallbackAwaitable interface { - mock.TestingT - Cleanup(func()) -} - -// NewTransactionCallbackAwaitable creates a new instance of TransactionCallbackAwaitable. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewTransactionCallbackAwaitable(t mockConstructorTestingTNewTransactionCallbackAwaitable) *TransactionCallbackAwaitable { - mock := &TransactionCallbackAwaitable{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/zcnbridge/mocks/TransactionProvider.go b/zcnbridge/mocks/TransactionProvider.go deleted file mode 100644 index 2ca8c875b..000000000 --- a/zcnbridge/mocks/TransactionProvider.go +++ /dev/null @@ -1,54 +0,0 @@ -// Code generated by mockery v2.28.1. DO NOT EDIT. - -package mocks - -import ( - transaction "github.com/0chain/gosdk/zcnbridge/transaction" - mock "github.com/stretchr/testify/mock" -) - -// TransactionProvider is an autogenerated mock type for the TransactionProvider type -type TransactionProvider struct { - mock.Mock -} - -// NewTransactionEntity provides a mock function with given fields: txnFee -func (_m *TransactionProvider) NewTransactionEntity(txnFee uint64) (transaction.Transaction, error) { - ret := _m.Called(txnFee) - - var r0 transaction.Transaction - var r1 error - if rf, ok := ret.Get(0).(func(uint64) (transaction.Transaction, error)); ok { - return rf(txnFee) - } - if rf, ok := ret.Get(0).(func(uint64) transaction.Transaction); ok { - r0 = rf(txnFee) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(transaction.Transaction) - } - } - - if rf, ok := ret.Get(1).(func(uint64) error); ok { - r1 = rf(txnFee) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -type mockConstructorTestingTNewTransactionProvider interface { - mock.TestingT - Cleanup(func()) -} - -// NewTransactionProvider creates a new instance of TransactionProvider. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewTransactionProvider(t mockConstructorTestingTNewTransactionProvider) *TransactionProvider { - mock := &TransactionProvider{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} From 4a33b6b6accfba8cd5873c5827762ce4b8ee8f80 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 12 Sep 2024 00:13:22 +0530 Subject: [PATCH 065/319] Fix --- zcnbridge/http/rest_test.go | 26 ---- zcnbridge/transaction/mocks/Transaction.go | 135 ------------------ .../mocks/TransactionCallbackAwaitable.go | 74 ---------- .../transaction/mocks/TransactionProvider.go | 54 ------- zcnbridge/transaction/mocks/doc.go | 2 - 5 files changed, 291 deletions(-) delete mode 100644 zcnbridge/http/rest_test.go delete mode 100644 zcnbridge/transaction/mocks/Transaction.go delete mode 100644 zcnbridge/transaction/mocks/TransactionCallbackAwaitable.go delete mode 100644 zcnbridge/transaction/mocks/TransactionProvider.go delete mode 100644 zcnbridge/transaction/mocks/doc.go diff --git a/zcnbridge/http/rest_test.go b/zcnbridge/http/rest_test.go deleted file mode 100644 index feff0d7cf..000000000 --- a/zcnbridge/http/rest_test.go +++ /dev/null @@ -1,26 +0,0 @@ -package http - -import ( - "testing" - - "github.com/0chain/gosdk/zcncore" - - "github.com/stretchr/testify/require" -) - -func Test_MakeURL(t *testing.T) { - p := Params{ - "blobber_id": "1", - } - - url := makeURL(p, "https://baseuri", "/relativePath").String() - require.Equal(t, "https://baseuri/v1/screst/"+zcncore.ZCNSCSmartContractAddress+"/relativePath?blobber_id=1", url) - - p = Params{ - "blobber_id": "1", - "path": "2", - } - - url = makeURL(p, "https://baseuri", "/relativePath").String() - require.Equal(t, "https://baseuri/v1/screst/"+zcncore.ZCNSCSmartContractAddress+"/relativePath?blobber_id=1&path=2", url) -} diff --git a/zcnbridge/transaction/mocks/Transaction.go b/zcnbridge/transaction/mocks/Transaction.go deleted file mode 100644 index ae323f12b..000000000 --- a/zcnbridge/transaction/mocks/Transaction.go +++ /dev/null @@ -1,135 +0,0 @@ -// Code generated by mockery v2.28.1. DO NOT EDIT. - -package mocks - -import ( - context "context" - - transaction "github.com/0chain/gosdk/zcnbridge/transaction" - mock "github.com/stretchr/testify/mock" - - zcncore "github.com/0chain/gosdk/zcncore" -) - -// Transaction is an autogenerated mock type for the Transaction type -type Transaction struct { - mock.Mock -} - -// ExecuteSmartContract provides a mock function with given fields: ctx, address, funcName, input, val -func (_m *Transaction) ExecuteSmartContract(ctx context.Context, address string, funcName string, input interface{}, val uint64) (string, error) { - ret := _m.Called(ctx, address, funcName, input, val) - - var r0 string - var r1 error - if rf, ok := ret.Get(0).(func(context.Context, string, string, interface{}, uint64) (string, error)); ok { - return rf(ctx, address, funcName, input, val) - } - if rf, ok := ret.Get(0).(func(context.Context, string, string, interface{}, uint64) string); ok { - r0 = rf(ctx, address, funcName, input, val) - } else { - r0 = ret.Get(0).(string) - } - - if rf, ok := ret.Get(1).(func(context.Context, string, string, interface{}, uint64) error); ok { - r1 = rf(ctx, address, funcName, input, val) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// GetCallback provides a mock function with given fields: -func (_m *Transaction) GetCallback() transaction.TransactionCallbackAwaitable { - ret := _m.Called() - - var r0 transaction.TransactionCallbackAwaitable - if rf, ok := ret.Get(0).(func() transaction.TransactionCallbackAwaitable); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(transaction.TransactionCallbackAwaitable) - } - } - - return r0 -} - -// GetHash provides a mock function with given fields: -func (_m *Transaction) GetHash() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// GetScheme provides a mock function with given fields: -func (_m *Transaction) GetScheme() zcncore.TransactionScheme { - ret := _m.Called() - - var r0 zcncore.TransactionScheme - if rf, ok := ret.Get(0).(func() zcncore.TransactionScheme); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(zcncore.TransactionScheme) - } - } - - return r0 -} - -// GetTransactionOutput provides a mock function with given fields: -func (_m *Transaction) GetTransactionOutput() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// SetHash provides a mock function with given fields: _a0 -func (_m *Transaction) SetHash(_a0 string) { - _m.Called(_a0) -} - -// Verify provides a mock function with given fields: ctx -func (_m *Transaction) Verify(ctx context.Context) error { - ret := _m.Called(ctx) - - var r0 error - if rf, ok := ret.Get(0).(func(context.Context) error); ok { - r0 = rf(ctx) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -type mockConstructorTestingTNewTransaction interface { - mock.TestingT - Cleanup(func()) -} - -// NewTransaction creates a new instance of Transaction. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewTransaction(t mockConstructorTestingTNewTransaction) *Transaction { - mock := &Transaction{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/zcnbridge/transaction/mocks/TransactionCallbackAwaitable.go b/zcnbridge/transaction/mocks/TransactionCallbackAwaitable.go deleted file mode 100644 index 9966f8fba..000000000 --- a/zcnbridge/transaction/mocks/TransactionCallbackAwaitable.go +++ /dev/null @@ -1,74 +0,0 @@ -// Code generated by mockery v2.28.1. DO NOT EDIT. - -package mocks - -import ( - context "context" - - mock "github.com/stretchr/testify/mock" - - zcncore "github.com/0chain/gosdk/zcncore" -) - -// TransactionCallbackAwaitable is an autogenerated mock type for the TransactionCallbackAwaitable type -type TransactionCallbackAwaitable struct { - mock.Mock -} - -// OnAuthComplete provides a mock function with given fields: t, status -func (_m *TransactionCallbackAwaitable) OnAuthComplete(t *zcncore.Transaction, status int) { - _m.Called(t, status) -} - -// OnTransactionComplete provides a mock function with given fields: t, status -func (_m *TransactionCallbackAwaitable) OnTransactionComplete(t *zcncore.Transaction, status int) { - _m.Called(t, status) -} - -// OnVerifyComplete provides a mock function with given fields: t, status -func (_m *TransactionCallbackAwaitable) OnVerifyComplete(t *zcncore.Transaction, status int) { - _m.Called(t, status) -} - -// WaitCompleteCall provides a mock function with given fields: ctx -func (_m *TransactionCallbackAwaitable) WaitCompleteCall(ctx context.Context) error { - ret := _m.Called(ctx) - - var r0 error - if rf, ok := ret.Get(0).(func(context.Context) error); ok { - r0 = rf(ctx) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// WaitVerifyCall provides a mock function with given fields: ctx -func (_m *TransactionCallbackAwaitable) WaitVerifyCall(ctx context.Context) error { - ret := _m.Called(ctx) - - var r0 error - if rf, ok := ret.Get(0).(func(context.Context) error); ok { - r0 = rf(ctx) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -type mockConstructorTestingTNewTransactionCallbackAwaitable interface { - mock.TestingT - Cleanup(func()) -} - -// NewTransactionCallbackAwaitable creates a new instance of TransactionCallbackAwaitable. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewTransactionCallbackAwaitable(t mockConstructorTestingTNewTransactionCallbackAwaitable) *TransactionCallbackAwaitable { - mock := &TransactionCallbackAwaitable{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/zcnbridge/transaction/mocks/TransactionProvider.go b/zcnbridge/transaction/mocks/TransactionProvider.go deleted file mode 100644 index 2ca8c875b..000000000 --- a/zcnbridge/transaction/mocks/TransactionProvider.go +++ /dev/null @@ -1,54 +0,0 @@ -// Code generated by mockery v2.28.1. DO NOT EDIT. - -package mocks - -import ( - transaction "github.com/0chain/gosdk/zcnbridge/transaction" - mock "github.com/stretchr/testify/mock" -) - -// TransactionProvider is an autogenerated mock type for the TransactionProvider type -type TransactionProvider struct { - mock.Mock -} - -// NewTransactionEntity provides a mock function with given fields: txnFee -func (_m *TransactionProvider) NewTransactionEntity(txnFee uint64) (transaction.Transaction, error) { - ret := _m.Called(txnFee) - - var r0 transaction.Transaction - var r1 error - if rf, ok := ret.Get(0).(func(uint64) (transaction.Transaction, error)); ok { - return rf(txnFee) - } - if rf, ok := ret.Get(0).(func(uint64) transaction.Transaction); ok { - r0 = rf(txnFee) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(transaction.Transaction) - } - } - - if rf, ok := ret.Get(1).(func(uint64) error); ok { - r1 = rf(txnFee) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -type mockConstructorTestingTNewTransactionProvider interface { - mock.TestingT - Cleanup(func()) -} - -// NewTransactionProvider creates a new instance of TransactionProvider. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewTransactionProvider(t mockConstructorTestingTNewTransactionProvider) *TransactionProvider { - mock := &TransactionProvider{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/zcnbridge/transaction/mocks/doc.go b/zcnbridge/transaction/mocks/doc.go deleted file mode 100644 index d5f5048fa..000000000 --- a/zcnbridge/transaction/mocks/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// AUTOGENERATED! Do not use. -package mocks \ No newline at end of file From c1c10ab1f80385d0a073e09b17aaad1725983eea Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 12 Sep 2024 00:16:58 +0530 Subject: [PATCH 066/319] Fix --- zcnbridge/bridge_test.go | 96 +++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 56 deletions(-) diff --git a/zcnbridge/bridge_test.go b/zcnbridge/bridge_test.go index 70d02bfac..0d09846d6 100644 --- a/zcnbridge/bridge_test.go +++ b/zcnbridge/bridge_test.go @@ -22,8 +22,6 @@ import ( "github.com/0chain/gosdk/zcnbridge/ethereum/authorizers" binding "github.com/0chain/gosdk/zcnbridge/ethereum/bridge" bridgemocks "github.com/0chain/gosdk/zcnbridge/mocks" - transactionmocks "github.com/0chain/gosdk/zcnbridge/transaction/mocks" - "github.com/0chain/gosdk/zcnbridge/wallet" "github.com/0chain/gosdk/zcnbridge/zcnsc" eth "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/accounts" @@ -271,23 +269,11 @@ func prepareEthereumClientGeneralMockCalls(ethereumClient *mock.Mock) { ethereumClient.On("SendTransaction", mock.Anything, mock.Anything).Return(nil) } -func getTransaction(t mock.TestingT) *transactionmocks.Transaction { - return transactionmocks.NewTransaction(&transactionMock{t}) -} - func prepareTransactionGeneralMockCalls(transaction *mock.Mock) { transaction.On("ExecuteSmartContract", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(zcnTxnID, nil) transaction.On("Verify", mock.Anything).Return(nil) } -func getTransactionProvider(t mock.TestingT) *transactionmocks.TransactionProvider { - return transactionmocks.NewTransactionProvider(&transactionProviderMock{t}) -} - -func prepareTransactionProviderGeneralMockCalls(transactionProvider *mock.Mock, transaction *transactionmocks.Transaction) { - transactionProvider.On("NewTransactionEntity", mock.Anything).Return(transaction, nil) -} - func getKeyStore(t mock.TestingT) *bridgemocks.KeyStore { return bridgemocks.NewKeyStore(&keyStoreMock{t}) } @@ -313,9 +299,6 @@ func Test_ZCNBridge(t *testing.T) { ethereumClient := getEthereumClient(t) prepareEthereumClientGeneralMockCalls(ðereumClient.Mock) - tx := getTransaction(t) - prepareTransactionGeneralMockCalls(&tx.Mock) - keyStore := getKeyStore(t) prepareKeyStoreGeneralMockCalls(keyStore) @@ -407,45 +390,46 @@ func Test_ZCNBridge(t *testing.T) { )) }) - t.Run("should check configuration used by MintZCN", func(t *testing.T) { - payload := &zcnsc.MintPayload{ - EthereumTxnID: ethereumTxnID, - Amount: sdkcommon.Balance(amount), - Nonce: nonce, - Signatures: zcnScSignatures, - ReceivingClientID: clientId, - } - - _, err := bridgeClient.MintZCN(context.Background(), payload) - require.NoError(t, err) - - require.True(t, tx.AssertCalled( - t, - "ExecuteSmartContract", - context.Background(), - wallet.ZCNSCSmartContractAddress, - wallet.MintFunc, - payload, - uint64(0), - )) - }) - - t.Run("should check configuration used by BurnZCN", func(t *testing.T) { - _, _, err := bridgeClient.BurnZCN(amount) - require.NoError(t, err) - - require.True(t, tx.AssertCalled( - t, - "ExecuteSmartContract", - context.Background(), - wallet.ZCNSCSmartContractAddress, - wallet.BurnFunc, - zcnsc.BurnPayload{ - EthereumAddress: ethereumAddress, - }, - uint64(amount), - )) - }) + //TODO:JAYASHTODO + //t.Run("should check configuration used by MintZCN", func(t *testing.T) { + // payload := &zcnsc.MintPayload{ + // EthereumTxnID: ethereumTxnID, + // Amount: sdkcommon.Balance(amount), + // Nonce: nonce, + // Signatures: zcnScSignatures, + // ReceivingClientID: clientId, + // } + // + // _, err := bridgeClient.MintZCN(context.Background(), payload) + // require.NoError(t, err) + // + // require.True(t, tx.AssertCalled( + // t, + // "ExecuteSmartContract", + // context.Background(), + // wallet.ZCNSCSmartContractAddress, + // wallet.MintFunc, + // payload, + // uint64(0), + // )) + //}) + // + //t.Run("should check configuration used by BurnZCN", func(t *testing.T) { + // _, _, err := bridgeClient.BurnZCN(amount) + // require.NoError(t, err) + // + // require.True(t, tx.AssertCalled( + // t, + // "ExecuteSmartContract", + // context.Background(), + // wallet.ZCNSCSmartContractAddress, + // wallet.BurnFunc, + // zcnsc.BurnPayload{ + // EthereumAddress: ethereumAddress, + // }, + // uint64(amount), + // )) + //}) t.Run("should check configuration used by AddEthereumAuthorizer", func(t *testing.T) { _, err := bridgeClient.AddEthereumAuthorizer(context.Background(), common.HexToAddress(authorizerDelegatedAddress)) From a30e87950a2fe95ca5993d85bfb050863ba139e2 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Thu, 12 Sep 2024 00:33:51 +0530 Subject: [PATCH 067/319] add delete dir and get refs --- core/version/version.go | 2 +- go.mod | 22 +--- go.sum | 52 +-------- zboxcore/fileref/fileref.go | 7 ++ zboxcore/sdk/allocation.go | 26 +++-- zboxcore/sdk/deleteworker.go | 188 +++++++++++++++++++++++++++++++- zboxcore/sdk/filerefsworker.go | 192 +++++++++++++++++++++++++-------- zboxcore/sdk/listworker.go | 5 + 8 files changed, 366 insertions(+), 128 deletions(-) diff --git a/core/version/version.go b/core/version/version.go index fc76fab51..c82609e5d 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,5 +2,5 @@ //====== THIS IS AUTOGENERATED FILE. DO NOT MODIFY ======== package version -const VERSIONSTR = "v1.17.6-6-gdd52546d" +const VERSIONSTR = "v1.17.6-3-g2469b260" diff --git a/go.mod b/go.mod index 10007582c..67821b700 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/0chain/gosdk go 1.21 require ( - github.com/0chain/common v1.13.1-0.20240910144331-3a5093424892 + github.com/0chain/common v1.13.1-0.20240911163240-3f40100a695a github.com/0chain/errors v1.0.3 github.com/Luzifer/go-openssl/v3 v3.1.0 github.com/btcsuite/btcd v0.23.4 @@ -52,20 +52,12 @@ require ( ) require ( - github.com/DataDog/zstd v1.4.5 // indirect github.com/VictoriaMetrics/fastcache v1.6.0 // indirect github.com/andybalholm/brotli v1.0.5 // indirect - github.com/beorn7/perks v1.0.1 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect github.com/cespare/xxhash v1.1.0 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect - github.com/cockroachdb/errors v1.11.3 // indirect - github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce // indirect - github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect - github.com/cockroachdb/pebble v1.1.2 // indirect - github.com/cockroachdb/redact v1.1.5 // indirect - github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/deckarep/golang-set v1.8.0 // indirect @@ -78,7 +70,7 @@ require ( github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/fxamacker/cbor/v2 v2.7.0 // indirect github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect - github.com/getsentry/sentry-go v0.27.0 // indirect + github.com/go-kit/kit v0.9.0 // indirect github.com/go-logfmt/logfmt v0.5.1 // indirect github.com/go-ole/go-ole v1.2.6 // indirect github.com/go-stack/stack v1.8.1 // indirect @@ -97,28 +89,20 @@ require ( github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c // indirect github.com/huin/goupnp v1.0.3 // indirect github.com/jackpal/go-nat-pmp v1.0.2 // indirect - github.com/kr/pretty v0.3.1 // indirect - github.com/kr/text v0.2.0 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/matryer/is v1.4.1 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect - github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect github.com/mitchellh/pointerstructure v1.2.0 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect github.com/patrickmn/go-cache v2.1.0+incompatible // indirect github.com/pelletier/go-toml/v2 v2.0.6 // indirect github.com/philhofer/fwd v1.1.2-0.20210722190033-5c56ac6d0bb9 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prometheus/client_golang v1.12.0 // indirect - github.com/prometheus/client_model v0.2.1-0.20210607210712-147c58e9608a // indirect - github.com/prometheus/common v0.32.1 // indirect - github.com/prometheus/procfs v0.7.3 // indirect github.com/prometheus/tsdb v0.7.1 // indirect github.com/rivo/uniseg v0.4.3 // indirect github.com/rjeczalik/notify v0.9.1 // indirect - github.com/rogpeppe/go-internal v1.9.0 // indirect github.com/rs/cors v1.7.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/shirou/gopsutil v3.21.11+incompatible // indirect @@ -139,10 +123,10 @@ require ( github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect go.dedis.ch/fixbuf v1.0.3 // indirect go.opencensus.io v0.24.0 // indirect - golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect golang.org/x/sys v0.18.0 // indirect golang.org/x/text v0.14.0 // indirect google.golang.org/protobuf v1.33.0 // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect ) diff --git a/go.sum b/go.sum index e49db747d..eba1749d9 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/0chain/common v1.13.1-0.20240910144331-3a5093424892 h1:UA3fxT6u9fZPXtcAMY5asEWATVos/Tk0kyMa6dGYAAM= -github.com/0chain/common v1.13.1-0.20240910144331-3a5093424892/go.mod h1:lcwOe6fIgP7w/0sFSFB13rqDI43BGv5jk4PddArBaBM= +github.com/0chain/common v1.13.1-0.20240911163240-3f40100a695a h1:NzweELzPLiZpaa9lLzEt/zXoxaU1YazA2weFjzeCqEI= +github.com/0chain/common v1.13.1-0.20240911163240-3f40100a695a/go.mod h1:lcwOe6fIgP7w/0sFSFB13rqDI43BGv5jk4PddArBaBM= github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM= github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= @@ -58,10 +58,7 @@ github.com/VictoriaMetrics/fastcache v1.6.0/go.mod h1:0qHz5QP0GMX4pfmMA/zt5RgfNu github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= -github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= -github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= -github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah4HI848JfFxHt+iPb26b4zyfspmqY0/8= github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= @@ -110,7 +107,6 @@ github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= @@ -120,8 +116,6 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= -github.com/cockroachdb/datadriven v1.0.3-0.20230413201302-be42291fc80f h1:otljaYPt5hWxV3MUfO5dFPFiOXg9CyG5/kCfayTqsJ4= -github.com/cockroachdb/datadriven v1.0.3-0.20230413201302-be42291fc80f/go.mod h1:a9RdTaap04u637JoCzcUoIcDmvwSUtcUFtT/C3kJlTU= github.com/cockroachdb/errors v1.11.3 h1:5bA+k2Y6r+oz/6Z/RFlNeVCesGARKuC6YymtcDrbC/I= github.com/cockroachdb/errors v1.11.3/go.mod h1:m4UIW4CDjx+R5cybPsNrRbreomiFqt8o1h1wUVazSd8= github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce h1:giXvy4KSc/6g/esnpM7Geqxka4WSqI1SZc7sMJFd3y4= @@ -140,7 +134,6 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/dave/jennifer v1.2.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg= github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -201,18 +194,14 @@ github.com/getsentry/sentry-go v0.27.0 h1:Pv98CIbtB3LkMWmXi4Joa5OOcwbmnX88sF5qbK github.com/getsentry/sentry-go v0.27.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY= github.com/glycerine/go-unsnap-stream v0.0.0-20180323001048-9f0cb55181dd/go.mod h1:/20jfyN9Y5QPEAprSgKAUr+glWDY39ZiUEAYOEv5dsE= github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24= -github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA= -github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.9.0 h1:wDJmvq38kDhkVxi50ni9ykkdUr1PKgqKOoi01fa0Mdk= github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= -github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= -github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA= github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= @@ -260,7 +249,6 @@ github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QD github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= @@ -287,7 +275,6 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -367,18 +354,13 @@ github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7Bd github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= -github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= -github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= -github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= -github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/jsternberg/zap-logfmt v1.0.0/go.mod h1:uvPs/4X51zdkcm5jXl5SYoN+4RK21K8mysFmDaM/h+o= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= -github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= github.com/jwilder/encoding v0.0.0-20170811194829-b4e1701a28ef/go.mod h1:Ct9fl0F6iIOGgxJ5npU/IUOhOhqlVrGjyIZc8/MagT0= github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= @@ -397,10 +379,10 @@ github.com/klauspost/pgzip v1.0.2-0.20170402124221-0bf5dcad4ada/go.mod h1:Ch1tH6 github.com/klauspost/reedsolomon v1.11.8 h1:s8RpUW5TK4hjr+djiOpbZJB4ksx+TdYbRH7vHQpwPOY= github.com/klauspost/reedsolomon v1.11.8/go.mod h1:4bXRN+cVzMdml6ti7qLouuYi32KHJ5MGv0Qd8a47h6A= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -445,14 +427,10 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/pointerstructure v1.2.0 h1:O+i9nHnXS3l/9Wu7r4NrEdwA2VFTicjUEN1uBnDo34A= github.com/mitchellh/pointerstructure v1.2.0/go.mod h1:BRAsLI5zgXmw97Lf6s25bs8ohIXc3tViBH44KcwB2g4= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE+eKJXWVjKXM4ck2QobLqTDytGJbLLhJg= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= -github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= @@ -482,9 +460,6 @@ github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG github.com/philhofer/fwd v1.1.2-0.20210722190033-5c56ac6d0bb9 h1:6ob53CVz+ja2i7easAStApZJlh7sxyq3Cm7g1Di6iqA= github.com/philhofer/fwd v1.1.2-0.20210722190033-5c56ac6d0bb9/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU= github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= -github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= -github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= -github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -495,27 +470,20 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= -github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M= -github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0= github.com/prometheus/client_golang v1.12.0 h1:C+UIj/QWtmqY13Arb8kwMt5j34/0Z2iKamrJ+ryC0Gg= github.com/prometheus/client_golang v1.12.0/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= -github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.2.1-0.20210607210712-147c58e9608a h1:CmF68hwI0XsOQ5UwlBopMi2Ow4Pbg32akc4KIVCOm+Y= github.com/prometheus/client_model v0.2.1-0.20210607210712-147c58e9608a/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w= github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= github.com/prometheus/common v0.6.0/go.mod h1:eBmuwkDJBwy6iBfxCBob6t6dR6ENT/y+J+Zk0j9GMYc= -github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo= -github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc= github.com/prometheus/common v0.32.1 h1:hWIdL3N2HoUx3B8j3YN9mWor0qhY/NlEKZEaXxuIRh4= github.com/prometheus/common v0.32.1/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= -github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= -github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU= github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/tsdb v0.7.1 h1:YZcsG11NqnK4czYLrWd9mpEuAJIHVQLwdrleYfszMAA= @@ -544,8 +512,6 @@ github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMT github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8= github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= -github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= -github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= @@ -748,7 +714,6 @@ golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -760,7 +725,6 @@ golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43/go.mod h1:KelEdhl1UZF7XfJ golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= -golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -784,7 +748,6 @@ golang.org/x/sys v0.0.0-20190124100055-b90733256f2e/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -797,7 +760,6 @@ golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200107162124-548cf772de50/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -812,8 +774,6 @@ golang.org/x/sys v0.0.0-20200511232937-7e40ca221e25/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -822,14 +782,10 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210104204734-6f8348627aad/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -844,7 +800,6 @@ golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3 golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -1039,7 +994,6 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= diff --git a/zboxcore/fileref/fileref.go b/zboxcore/fileref/fileref.go index 63967ad33..75b35817b 100644 --- a/zboxcore/fileref/fileref.go +++ b/zboxcore/fileref/fileref.go @@ -62,6 +62,7 @@ func (fRef *FileRef) MetaID() string { } type RefEntity interface { + GetAllocationRoot() string GetNumBlocks() int64 GetSize() int64 GetFileMetaHash() string @@ -95,9 +96,11 @@ type Ref struct { ThumbnailSize int64 `json:"thumbnail_size" mapstructure:"thumbnail_size"` ActualThumbnailHash string `json:"actual_thumbnail_hash" mapstructure:"actual_thumbnail_hash"` ActualThumbnailSize int64 `json:"actual_thumbnail_size" mapstructure:"actual_thumbnail_size"` + IsEmpty bool `json:"is_empty" mapstructure:"is_empty"` HashToBeComputed bool ChildrenLoaded bool Children []RefEntity `json:"-" mapstructure:"-"` + AllocationRoot string `json:"allocation_root" mapstructure:"allocation_root"` CreatedAt common.Timestamp `json:"created_at" mapstructure:"created_at"` UpdatedAt common.Timestamp `json:"updated_at" mapstructure:"updated_at"` } @@ -170,6 +173,10 @@ func (r *Ref) GetHashData() string { return fmt.Sprintf("%s:%s:%s", r.AllocationID, r.Path, r.FileID) } +func (r *Ref) GetAllocationRoot() string { + return r.AllocationRoot +} + func (r *Ref) GetType() string { return r.Type } diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 9715c76bb..0c09d0183 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -1090,9 +1090,9 @@ func (a *Allocation) DoMultiOperation(operations []OperationRequest, opts ...Mul case constants.FileOperationDelete: if op.Mask != nil { - operation = NewDeleteOperation(op.RemotePath, *op.Mask, mo.maskMU, mo.consensusThresh, mo.fullconsensus, mo.ctx) + operation = NewDeleteOperation(mo.ctx, op.RemotePath, *op.Mask, mo.maskMU, mo.consensusThresh, mo.fullconsensus) } else { - operation = NewDeleteOperation(op.RemotePath, mo.operationMask, mo.maskMU, mo.consensusThresh, mo.fullconsensus, mo.ctx) + operation = NewDeleteOperation(mo.ctx, op.RemotePath, mo.operationMask, mo.maskMU, mo.consensusThresh, mo.fullconsensus) } case constants.FileOperationUpdate: @@ -1568,7 +1568,7 @@ func (a *Allocation) ListDirFromAuthTicket(authTicket string, lookupHash string, return nil, errors.New("invalid_path", "Invalid path for the list") } - listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} + listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}, storageVersion: a.StorageVersion} listReq.allocationID = a.ID listReq.allocationTx = a.Tx listReq.sig = a.sig @@ -1608,7 +1608,7 @@ func (a *Allocation) ListDir(path string, opts ...ListRequestOptions) (*ListResu if !isabs { return nil, errors.New("invalid_path", "Path should be valid and absolute") } - listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} + listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}, storageVersion: a.StorageVersion} listReq.allocationID = a.ID listReq.allocationTx = a.Tx listReq.sig = a.sig @@ -1631,7 +1631,7 @@ func (a *Allocation) ListDir(path string, opts ...ListRequestOptions) (*ListResu return nil, errors.New("list_request_failed", "Failed to get list response from the blobbers") } -func (a *Allocation) getRefs(path, pathHash, authToken, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int) (*ObjectTreeResult, error) { +func (a *Allocation) getRefs(path, pathHash, authToken, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int, opts ...ObjectTreeRequestOption) (*ObjectTreeResult, error) { if !a.isInitialized() { return nil, notInitialized } @@ -1652,9 +1652,13 @@ func (a *Allocation) getRefs(path, pathHash, authToken, offsetPath, updatedDate, fileType: fileType, refType: refType, ctx: a.ctx, + reqMask: zboxutil.NewUint128(1).Lsh(uint64(len(a.Blobbers))).Sub64(1), } oTreeReq.fullconsensus = a.fullconsensus oTreeReq.consensusThresh = a.DataShards + for _, opt := range opts { + opt(oTreeReq) + } return oTreeReq.GetRefs() } @@ -1774,12 +1778,12 @@ func (a *Allocation) GetRefsWithAuthTicket(authToken, offsetPath, updatedDate, o // - refType: the ref type to get the refs, e.g., file or directory. // - level: the level of the refs to get relative to the path root (strating from 0 as the root path). // - pageLimit: the limit of the refs to get per page. -func (a *Allocation) GetRefs(path, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int) (*ObjectTreeResult, error) { +func (a *Allocation) GetRefs(path, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int, opts ...ObjectTreeRequestOption) (*ObjectTreeResult, error) { if len(path) == 0 || !zboxutil.IsRemoteAbs(path) { return nil, errors.New("invalid_path", fmt.Sprintf("Absolute path required. Path provided: %v", path)) } - return a.getRefs(path, "", "", offsetPath, updatedDate, offsetDate, fileType, refType, level, pageLimit) + return a.getRefs(path, "", "", offsetPath, updatedDate, offsetDate, fileType, refType, level, pageLimit, opts...) } func (a *Allocation) ListObjects(ctx context.Context, path, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int) <-chan ORef { @@ -1885,7 +1889,7 @@ func (a *Allocation) GetFileMeta(path string) (*ConsolidatedFileMeta, error) { } result := &ConsolidatedFileMeta{} - listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} + listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}, storageVersion: a.StorageVersion} listReq.allocationID = a.ID listReq.allocationTx = a.Tx listReq.sig = a.sig @@ -1925,7 +1929,7 @@ func (a *Allocation) GetFileMetaByName(fileName string) ([]*ConsolidatedFileMeta } resultArr := []*ConsolidatedFileMetaByName{} - listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} + listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}, storageVersion: a.StorageVersion} listReq.allocationID = a.ID listReq.allocationTx = a.Tx listReq.blobbers = a.Blobbers @@ -2004,7 +2008,7 @@ func (a *Allocation) GetFileMetaFromAuthTicket(authTicket string, lookupHash str return nil, errors.New("invalid_path", "Invalid path for the list") } - listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} + listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}, storageVersion: a.StorageVersion} listReq.allocationID = a.ID listReq.allocationTx = a.Tx listReq.sig = a.sig @@ -2050,7 +2054,7 @@ func (a *Allocation) GetFileStats(path string) (map[string]*FileStats, error) { if !isabs { return nil, errors.New("invalid_path", "Path should be valid and absolute") } - listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} + listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}, storageVersion: a.StorageVersion} listReq.allocationID = a.ID listReq.allocationTx = a.Tx listReq.sig = a.sig diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index 59a42a3d4..c29b2b634 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "net/http" "net/url" + "strings" "sync" "sync/atomic" "time" @@ -47,6 +48,8 @@ type DeleteRequest struct { const emptyHash = "e" +var errFileDeleted = errors.New("file_deleted", "file is already deleted") + func (req *DeleteRequest) deleteBlobberFile( blobber *blockchain.StorageNode, blobberIdx int) error { @@ -358,6 +361,9 @@ func (dop *DeleteOperation) Process(allocObj *Allocation, connectionID string) ( } deleteReq.consensus.fullconsensus = dop.consensus.fullconsensus deleteReq.consensus.consensusThresh = dop.consensus.consensusThresh + if allocObj.StorageVersion == 1 { + return deleteReq.processDeleteV2() + } numList := len(deleteReq.blobbers) objectTreeRefs := make([]fileref.RefEntity, numList) @@ -409,6 +415,109 @@ func (dop *DeleteOperation) Process(allocObj *Allocation, connectionID string) ( return objectTreeRefs, deleteReq.deleteMask, nil } +func (deleteReq *DeleteRequest) processDeleteV2() ([]fileref.RefEntity, zboxutil.Uint128, error) { + numList := len(deleteReq.blobbers) + objectTreeRefs := make([]fileref.RefEntity, numList) + blobberErrors := make([]error, numList) + versionMap := make(map[string]int) + var ( + pos uint64 + consensusRef *fileref.FileRef + ) + + for i := deleteReq.deleteMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + deleteReq.wg.Add(1) + go func(blobberIdx int) { + defer deleteReq.wg.Done() + refEntity, err := deleteReq.getFileMetaFromBlobber(uint64(blobberIdx)) + if errors.Is(err, constants.ErrNotFound) { + deleteReq.consensus.Done() + return + } else if err != nil { + blobberErrors[blobberIdx] = err + l.Logger.Error(err.Error()) + return + } + deleteReq.consensus.Done() + objectTreeRefs[blobberIdx] = refEntity + deleteReq.maskMu.Lock() + versionMap[refEntity.AllocationRoot] += 1 + if versionMap[refEntity.AllocationRoot] >= deleteReq.consensus.consensusThresh { + consensusRef = refEntity + } + deleteReq.maskMu.Unlock() + }(int(pos)) + } + deleteReq.wg.Wait() + if !deleteReq.consensus.isConsensusOk() { + err := zboxutil.MajorError(blobberErrors) + if err != nil { + return nil, deleteReq.deleteMask, thrown.New("delete_failed", fmt.Sprintf("Delete failed. %s", err.Error())) + } + + return nil, deleteReq.deleteMask, thrown.New("consensus_not_met", + fmt.Sprintf("Delete failed. Required consensus %d, got %d", + deleteReq.consensus.consensusThresh, deleteReq.consensus.consensus)) + } + if consensusRef == nil { + //Already deleted + return nil, deleteReq.deleteMask, errFileDeleted + } + if consensusRef.Type == fileref.DIRECTORY && !consensusRef.IsEmpty { + for ind, refEntity := range objectTreeRefs { + if refEntity == nil { + continue + } + if refEntity.GetAllocationRoot() != consensusRef.AllocationRoot { + deleteReq.deleteMask = deleteReq.deleteMask.And(zboxutil.NewUint128(1).Lsh(uint64(ind)).Not()) + } + } + err := deleteReq.deleteSubDirectories() + if err != nil { + return nil, deleteReq.deleteMask, err + } + } + if deleteReq.remotefilepath == "/" { + return objectTreeRefs, deleteReq.deleteMask, nil + } + pos = 0 + deleteReq.consensus.Reset() + for i := deleteReq.deleteMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + deleteReq.wg.Add(1) + go func(blobberIdx int) { + defer deleteReq.wg.Done() + err := deleteReq.deleteBlobberFile(deleteReq.blobbers[blobberIdx], blobberIdx) + if err != nil { + logger.Logger.Error("error during deleteBlobberFile", err) + blobberErrors[blobberIdx] = err + } + deleteReq.consensus.Done() + if singleClientMode { + lookuphash := fileref.GetReferenceLookup(deleteReq.allocationID, deleteReq.remotefilepath) + cacheKey := fileref.GetCacheKey(lookuphash, deleteReq.blobbers[blobberIdx].ID) + fileref.DeleteFileRef(cacheKey) + } + }(int(pos)) + } + deleteReq.wg.Wait() + + if !deleteReq.consensus.isConsensusOk() { + err := zboxutil.MajorError(blobberErrors) + if err != nil { + return nil, deleteReq.deleteMask, thrown.New("delete_failed", fmt.Sprintf("Delete failed. %s", err.Error())) + } + + return nil, deleteReq.deleteMask, thrown.New("consensus_not_met", + fmt.Sprintf("Delete failed. Required consensus %d, got %d", + deleteReq.consensus.consensusThresh, deleteReq.consensus.consensus)) + } + + l.Logger.Debug("Delete Process Ended ") + return objectTreeRefs, deleteReq.deleteMask, nil +} + func (do *DeleteOperation) buildChange(refs []fileref.RefEntity, uid uuid.UUID) []allocationchange.AllocationChange { changes := make([]allocationchange.AllocationChange, len(refs)) @@ -452,7 +561,7 @@ func (dop *DeleteOperation) Error(allocObj *Allocation, consensus int, err error } -func NewDeleteOperation(remotePath string, deleteMask zboxutil.Uint128, maskMu *sync.Mutex, consensusTh int, fullConsensus int, ctx context.Context) *DeleteOperation { +func NewDeleteOperation(ctx context.Context, remotePath string, deleteMask zboxutil.Uint128, maskMu *sync.Mutex, consensusTh, fullConsensus int) *DeleteOperation { dop := &DeleteOperation{} dop.remotefilepath = zboxutil.RemoteClean(remotePath) dop.deleteMask = deleteMask @@ -463,6 +572,83 @@ func NewDeleteOperation(remotePath string, deleteMask zboxutil.Uint128, maskMu * return dop } +func (req *DeleteRequest) deleteSubDirectories() error { + // list all files + var ( + offsetPath string + pathLevel int + ) + for { + oResult, err := req.allocationObj.GetRefs(req.remotefilepath, offsetPath, "", "", fileref.FILE, fileref.REGULAR, 0, getRefPageLimit, WithObjectContext(req.ctx), WithObjectMask(req.deleteMask), WithObjectConsensusThresh(req.consensus.consensusThresh), WithSingleBlobber(true)) + if err != nil { + return err + } + if len(oResult.Refs) == 0 { + break + } + ops := make([]OperationRequest, 0, len(oResult.Refs)) + for _, ref := range oResult.Refs { + opMask := req.deleteMask + if ref.Type == fileref.DIRECTORY { + continue + } + if ref.PathLevel > pathLevel { + pathLevel = ref.PathLevel + } + op := OperationRequest{ + OperationType: constants.FileOperationDelete, + RemotePath: ref.Path, + Mask: &opMask, + } + ops = append(ops, op) + } + err = req.allocationObj.DoMultiOperation(ops) + if err != nil { + return err + } + offsetPath = oResult.Refs[len(oResult.Refs)-1].Path + if len(oResult.Refs) < getRefPageLimit { + break + } + } + // reset offsetPath + offsetPath = "" + level := len(strings.Split(strings.TrimSuffix(req.remotefilepath, "/"), "/")) + if pathLevel == 0 { + pathLevel = level + 1 + } + // list all directories by descending order of path level + for pathLevel > level { + oResult, err := req.allocationObj.GetRefs(req.remotefilepath, offsetPath, "", "", fileref.DIRECTORY, fileref.REGULAR, pathLevel, getRefPageLimit, WithObjectContext(req.ctx), WithObjectMask(req.deleteMask), WithObjectConsensusThresh(req.consensus.consensusThresh), WithSingleBlobber(true)) + if err != nil { + return err + } + if len(oResult.Refs) == 0 { + pathLevel-- + } else { + ops := make([]OperationRequest, 0, len(oResult.Refs)) + for _, ref := range oResult.Refs { + opMask := req.deleteMask + op := OperationRequest{ + OperationType: constants.FileOperationDelete, + RemotePath: ref.Path, + Mask: &opMask, + } + ops = append(ops, op) + } + err = req.allocationObj.DoMultiOperation(ops) + if err != nil { + return err + } + offsetPath = oResult.Refs[len(oResult.Refs)-1].Path + if len(oResult.Refs) < getRefPageLimit { + pathLevel-- + } + } + } + + return nil +} func (dop *DeleteOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { if dop.refs[changeIndex] == nil { return nil diff --git a/zboxcore/sdk/filerefsworker.go b/zboxcore/sdk/filerefsworker.go index 09d8a1b98..f9778834e 100644 --- a/zboxcore/sdk/filerefsworker.go +++ b/zboxcore/sdk/filerefsworker.go @@ -7,6 +7,7 @@ import ( "fmt" "io/ioutil" "math" + "math/rand" "net/http" "sync" "time" @@ -14,6 +15,7 @@ import ( "github.com/0chain/errors" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/zboxcore/blockchain" + "github.com/0chain/gosdk/zboxcore/logger" l "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/marker" "github.com/0chain/gosdk/zboxcore/zboxutil" @@ -44,7 +46,9 @@ type ObjectTreeRequest struct { offsetPath string updatedDate string // must have "2006-01-02T15:04:05.99999Z07:00" format offsetDate string // must have "2006-01-02T15:04:05.99999Z07:00" format + reqMask zboxutil.Uint128 ctx context.Context + singleBlobber bool Consensus } @@ -55,23 +59,87 @@ type oTreeResponse struct { idx int } +var errTooManyRequests = errors.New("too_many_requests", "Too many requests") + +type ObjectTreeRequestOption func(*ObjectTreeRequest) + +func WithObjectContext(ctx context.Context) ObjectTreeRequestOption { + return func(o *ObjectTreeRequest) { + o.ctx = ctx + } +} + +func WithObjectMask(mask zboxutil.Uint128) ObjectTreeRequestOption { + return func(o *ObjectTreeRequest) { + o.reqMask = mask + } +} + +func WithObjectConsensusThresh(thresh int) ObjectTreeRequestOption { + return func(o *ObjectTreeRequest) { + o.consensusThresh = thresh + } +} + +func WithSingleBlobber(singleBlobber bool) ObjectTreeRequestOption { + return func(o *ObjectTreeRequest) { + o.singleBlobber = singleBlobber + } +} + // Paginated tree should not be collected as this will stall the client // It should rather be handled by application that uses gosdk func (o *ObjectTreeRequest) GetRefs() (*ObjectTreeResult, error) { - totalBlobbersCount := len(o.blobbers) - oTreeResponses := make([]oTreeResponse, totalBlobbersCount) - respChan := make(chan *oTreeResponse, totalBlobbersCount) - for i, blob := range o.blobbers { + activeCount := o.reqMask.CountOnes() + oTreeResponses := make([]oTreeResponse, activeCount) + respChan := make(chan *oTreeResponse, activeCount) + if o.singleBlobber { + var respErr error + for i := 0; i < activeCount; i++ { + var rnd = rand.New(rand.NewSource(time.Now().UnixNano())) + num := rnd.Intn(activeCount) + var blob *blockchain.StorageNode + var pos uint64 + for i := o.reqMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + num-- + if num < 0 { + blob = o.blobbers[pos] + break + } + } + l.Logger.Debug(fmt.Sprintf("Getting file refs for path %v from blobber %v", o.remotefilepath, blob.Baseurl)) + idx := num + baseURL := blob.Baseurl + go o.getFileRefs(baseURL, respChan, idx) + select { + case <-o.ctx.Done(): + return nil, o.ctx.Err() + case oTreeResponse := <-respChan: + if oTreeResponse.err != nil { + respErr = oTreeResponse.err + } else { + return oTreeResponse.oTResult, nil + } + } + } + return nil, respErr + } + var pos uint64 + for i := o.reqMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + blob := o.blobbers[pos] l.Logger.Debug(fmt.Sprintf("Getting file refs for path %v from blobber %v", o.remotefilepath, blob.Baseurl)) - idx := i + idx := int(pos) baseURL := blob.Baseurl go o.getFileRefs(baseURL, respChan, idx) } + hashCount := make(map[string]int) hashRefsMap := make(map[string]*ObjectTreeResult) - oTreeResponseErrors := make([]error, totalBlobbersCount) + oTreeResponseErrors := make([]error, activeCount) var successCount int - for i := 0; i < totalBlobbersCount; i++ { + for i := 0; i < activeCount; i++ { select { case <-o.ctx.Done(): return nil, o.ctx.Err() @@ -83,6 +151,7 @@ func (o *ObjectTreeRequest) GetRefs() (*ObjectTreeResult, error) { } continue } + oTreeResponses[oTreeResponse.idx] = *oTreeResponse successCount++ hash := oTreeResponse.hash if _, ok := hashCount[hash]; ok { @@ -96,6 +165,7 @@ func (o *ObjectTreeRequest) GetRefs() (*ObjectTreeResult, error) { } } } + var selected *ObjectTreeResult if successCount < o.consensusThresh { majorError := zboxutil.MajorError(oTreeResponseErrors) @@ -114,7 +184,7 @@ func (o *ObjectTreeRequest) GetRefs() (*ObjectTreeResult, error) { selected = &ObjectTreeResult{} minPage := int64(math.MaxInt64) for _, oTreeResponse := range oTreeResponses { - if oTreeResponse.err != nil { + if oTreeResponse.err != nil || oTreeResponse.oTResult == nil { continue } if oTreeResponse.oTResult.TotalPages < minPage { @@ -132,6 +202,14 @@ func (o *ObjectTreeRequest) GetRefs() (*ObjectTreeResult, error) { } } if len(selected.Refs) > 0 { + for _, oTreeResponse := range oTreeResponses { + if oTreeResponse.err != nil || oTreeResponse.oTResult == nil { + continue + } + if len(selected.Refs) != len(oTreeResponse.oTResult.Refs) { + l.Logger.Error("Consensus failed for refs: ", o.blobbers[oTreeResponse.idx].Baseurl) + } + } selected.OffsetPath = selected.Refs[len(selected.Refs)-1].Path return selected, nil } @@ -145,53 +223,73 @@ func (o *ObjectTreeRequest) getFileRefs(bUrl string, respChan chan *oTreeRespons defer func() { respChan <- oTR }() - oReq, err := zboxutil.NewRefsRequest( - bUrl, - o.allocationID, - o.sig, - o.allocationTx, - o.remotefilepath, - o.pathHash, - o.authToken, - o.offsetPath, - o.updatedDate, - o.offsetDate, - o.fileType, - o.refType, - o.level, - o.pageLimit, - ) - if err != nil { - oTR.err = err - return - } + oResult := ObjectTreeResult{} - ctx, cncl := context.WithTimeout(o.ctx, 2*time.Minute) - err = zboxutil.HttpDo(ctx, cncl, oReq, func(resp *http.Response, err error) error { + for i := 0; i < 3; i++ { + oReq, err := zboxutil.NewRefsRequest( + bUrl, + o.allocationID, + o.sig, + o.allocationTx, + o.remotefilepath, + o.pathHash, + o.authToken, + o.offsetPath, + o.updatedDate, + o.offsetDate, + o.fileType, + o.refType, + o.level, + o.pageLimit, + ) if err != nil { - l.Logger.Error(err) - return err + oTR.err = err + return } - defer resp.Body.Close() - respBody, err := ioutil.ReadAll(resp.Body) - if err != nil { - l.Logger.Error(err) - return err - } - if resp.StatusCode == http.StatusOK { - err := json.Unmarshal(respBody, &oResult) + ctx, cncl := context.WithTimeout(o.ctx, 2*time.Minute) + defer cncl() + err = zboxutil.HttpDo(ctx, cncl, oReq, func(resp *http.Response, err error) error { if err != nil { l.Logger.Error(err) return err } - return nil - } else { - return errors.New("response_error", fmt.Sprintf("got status %d, err: %s", resp.StatusCode, respBody)) + defer resp.Body.Close() + respBody, err := ioutil.ReadAll(resp.Body) + if err != nil { + l.Logger.Error(err) + return err + } + if resp.StatusCode == http.StatusOK { + err := json.Unmarshal(respBody, &oResult) + if err != nil { + l.Logger.Error(err) + return err + } + return nil + } else { + if resp.StatusCode == http.StatusTooManyRequests { + l.Logger.Error("Too many requests") + r, err := zboxutil.GetRateLimitValue(resp) + if err != nil { + logger.Logger.Error(err) + return err + } + + time.Sleep(time.Duration(r) * time.Second) + return errTooManyRequests + } + + return errors.New("response_error", fmt.Sprintf("got status %d, err: %s", resp.StatusCode, respBody)) + } + }) + if err != nil { + if err == errTooManyRequests && i < 2 { + continue + } + oTR.err = err + return } - }) - if err != nil { - oTR.err = err - return + break } oTR.oTResult = &oResult similarFieldRefs := make([]byte, 0, 32*len(oResult.Refs)) diff --git a/zboxcore/sdk/listworker.go b/zboxcore/sdk/listworker.go index 220974a7e..0458fdc17 100644 --- a/zboxcore/sdk/listworker.go +++ b/zboxcore/sdk/listworker.go @@ -36,6 +36,7 @@ type ListRequest struct { listOnly bool offset int pageLimit int + storageVersion int Consensus } @@ -154,6 +155,7 @@ func (req *ListRequest) getListInfoFromBlobber(blobber *blockchain.StorageNode, if err != nil { return errors.Wrap(err, "error getting the dir tree from list response:") } + ref.AllocationRoot = listResult.AllocationRoot return nil } @@ -182,6 +184,9 @@ func (req *ListRequest) getlistFromBlobbers() ([]*listResponse, error) { continue } hash := listInfos[i].ref.FileMetaHash + if req.storageVersion == 1 { + hash = listInfos[i].ref.AllocationRoot + } consensusMap[hash] = append(consensusMap[hash], req.blobbers[listInfos[i].blobberIdx]) if len(consensusMap[hash]) >= req.consensusThresh { consensusHash = hash From e79118083a99166cb78ddeeb3b4650c7561f9835 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Thu, 12 Sep 2024 00:53:49 +0530 Subject: [PATCH 068/319] fix process change for delete dir --- zboxcore/sdk/deleteworker.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index c29b2b634..70c006788 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -650,7 +650,7 @@ func (req *DeleteRequest) deleteSubDirectories() error { return nil } func (dop *DeleteOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { - if dop.refs[changeIndex] == nil { + if dop.refs[changeIndex] == nil || dop.refs[changeIndex].GetType() == fileref.DIRECTORY { return nil } decodedKey, _ := hex.DecodeString(dop.lookupHash) @@ -663,7 +663,7 @@ func (dop *DeleteOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, chang } func (dop *DeleteOperation) GetLookupHash(changeIndex uint64) string { - if dop.refs[changeIndex] == nil { + if dop.refs[changeIndex] == nil || dop.refs[changeIndex].GetType() == fileref.DIRECTORY { return "" } return dop.lookupHash From 3bd80b5b0cfdcd47ef3803246a10593fd341d78d Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 12 Sep 2024 01:18:46 +0530 Subject: [PATCH 069/319] Fix wasm --- core/version/version.go | 2 +- wasmsdk/bridge.go | 38 +++++++---------- wasmsdk/proxy.go | 5 --- wasmsdk/sdk.go | 37 +++-------------- wasmsdk/wallet_base.go | 2 +- wasmsdk/zcn.go | 17 +++----- zcncore/ethwallet_base.go | 87 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 115 insertions(+), 73 deletions(-) diff --git a/core/version/version.go b/core/version/version.go index 68da08386..e8cd0a9b9 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.6-44-ge92116b7" +const VERSIONSTR = "v1.17.6-69-gc1c10ab1" diff --git a/wasmsdk/bridge.go b/wasmsdk/bridge.go index b1c09ac00..4d9b2a02b 100644 --- a/wasmsdk/bridge.go +++ b/wasmsdk/bridge.go @@ -12,7 +12,6 @@ import ( "github.com/0chain/gosdk/zcnbridge" "github.com/0chain/gosdk/zcnbridge/errors" "github.com/0chain/gosdk/zcnbridge/log" - "github.com/0chain/gosdk/zcnbridge/wallet" "github.com/0chain/gosdk/zcncore" "github.com/ethereum/go-ethereum/ethclient" ) @@ -120,21 +119,17 @@ func getMintWZCNPayload(burnTrxHash string) string { // getNotProcessedWZCNBurnEvents returns all not processed WZCN burn events from the Ethereum network func getNotProcessedWZCNBurnEvents() string { - var mintNonce int64 - cb := wallet.NewZCNStatus(&mintNonce) - - cb.Begin() - - if err := zcncore.GetMintNonce(cb); err != nil { - return errors.Wrap("getNotProcessedWZCNBurnEvents", "failed to retreive last ZCN processed mint nonce", err).Error() - } - - if err := cb.Wait(); err != nil { + 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 !cb.Success { - return errors.New("getNotProcessedWZCNBurnEvents", "failed to retreive last ZCN processed mint nonce").Error() + if err = json.Unmarshal(res, &mintNonce); err != nil { + return errors.Wrap("getNotProcessedWZCNBurnEvents", "failed to unmarshal last ZCN processed mint nonce", err).Error() } log.Logger.Debug("MintNonce = " + strconv.Itoa(int(mintNonce))) @@ -159,21 +154,18 @@ func getNotProcessedZCNBurnTickets() string { return errors.Wrap("getNotProcessedZCNBurnTickets", "failed to retreive user nonce", err).Error() } - var burnTickets []zcncore.BurnTicket - cb := wallet.NewZCNStatus(&burnTickets) - cb.Begin() + var ( + res []byte + burnTickets []zcncore.BurnTicket + ) - err = zcncore.GetNotProcessedZCNBurnTickets(bridge.EthereumAddress, userNonce.String(), cb) + res, err = zcncore.GetNotProcessedZCNBurnTickets(bridge.EthereumAddress, userNonce.String()) if err != nil { return errors.Wrap("getNotProcessedZCNBurnTickets", "failed to retreive ZCN burn tickets", err).Error() } - if err := cb.Wait(); err != nil { - return errors.Wrap("getNotProcessedZCNBurnTickets", "failed to retreive ZCN burn tickets", err).Error() - } - - if !cb.Success { - return errors.New("getNotProcessedZCNBurnTickets", "failed to retreive ZCN burn tickets").Error() + if err = json.Unmarshal(res, &burnTickets); err != nil { + return errors.Wrap("getNotProcessedZCNBurnTickets", "failed to unmarshal ZCN burn tickets", err).Error() } var result []byte diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index ceaf2e4f8..6a7cbfc09 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -16,7 +16,6 @@ import ( "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/core/version" - "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/wasmsdk/jsbridge" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/0chain/gosdk/zcncore" @@ -69,8 +68,6 @@ func main() { } //update sign with js sign - zcncrypto.Sign = signFunc - zcncore.SignFn = signFunc sys.Sign = func(hash, signatureScheme string, keys []sys.KeyPair) (string, error) { // js already has signatureScheme and keys return signFunc(hash) @@ -366,8 +363,6 @@ func main() { return result[0].String(), nil } //update sign with js sign - zcncrypto.Sign = signFunc - zcncore.SignFn = signFunc sys.Sign = func(hash, signatureScheme string, keys []sys.KeyPair) (string, error) { // js already has signatureScheme and keys return signFunc(hash) diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index befd6fdad..55ec66641 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -7,12 +7,7 @@ import ( "context" "encoding/hex" "encoding/json" - "errors" "fmt" - "io" - "os" - "sync" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/encryption" @@ -20,6 +15,8 @@ import ( "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/0chain/gosdk/zcncore" + "io" + "os" ) var CreateObjectURL func(buf []byte, mimeType string) string @@ -167,7 +164,7 @@ func makeSCRestAPICall(scAddress, relativePath, paramsJson string) (string, erro if err != nil { sdkLogger.Error(fmt.Sprintf("Error parsing JSON: %v", err)) } - b, err := client.MakeSCRestAPICall(scAddress, relativePath, params, nil) + b, err := client.MakeSCRestAPICall(scAddress, relativePath, params) return string(b), err } @@ -177,34 +174,10 @@ func makeSCRestAPICall(scAddress, relativePath, paramsJson string) (string, erro // - fee is the transaction fee // - desc is the description of the transaction func send(toClientID string, tokens uint64, fee uint64, desc string) (string, error) { - wg := &sync.WaitGroup{} - cb := &transactionCallback{wg: wg} - txn, err := zcncore.NewTransaction(cb, fee, 0) + _, out, _, _, err := zcncore.Send(toClientID, tokens, desc) if err != nil { return "", err } - wg.Add(1) - err = txn.Send(toClientID, tokens, desc) - if err == nil { - wg.Wait() - } else { - return "", err - } - - if cb.success { - cb.success = false - wg.Add(1) - err := txn.Verify() - if err == nil { - wg.Wait() - } else { - return "", err - } - if cb.success { - return txn.GetVerifyOutput(), nil - } - } - - return "", errors.New(cb.errMsg) + return out, nil } diff --git a/wasmsdk/wallet_base.go b/wasmsdk/wallet_base.go index 265beeed9..3f7e1be44 100644 --- a/wasmsdk/wallet_base.go +++ b/wasmsdk/wallet_base.go @@ -23,7 +23,7 @@ func splitKeys(privateKey string, numSplits int) (string, error) { // // nolint: unused func setWalletInfo(jsonWallet string, splitKeyWallet bool) bool { - err := zcncore.SetWalletInfo(jsonWallet, splitKeyWallet) + err := zcncore.SetWalletInfo(jsonWallet, "bls0chain", splitKeyWallet) if err == nil { return true } else { diff --git a/wasmsdk/zcn.go b/wasmsdk/zcn.go index 0fac9e39f..8f1c47def 100644 --- a/wasmsdk/zcn.go +++ b/wasmsdk/zcn.go @@ -4,6 +4,7 @@ package main import ( + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/0chain/gosdk/zcncore" ) @@ -17,26 +18,20 @@ type Balance struct { // getWalletBalance retrieves the wallet balance of the client from the network. // - clientId is the client id func getWalletBalance(clientId string) (*Balance, error) { - - zcn, nonce, err := zcncore.GetWalletBalance(clientId) - if err != nil { - return nil, err - } - - zcnToken, err := zcn.ToToken() + bal, err := client.GetBalance(clientId) if err != nil { return nil, err } - usd, err := zcncore.ConvertTokenToUSD(zcnToken) + toUsd, err := zcncore.ConvertTokenToUSD(float64(bal.Balance)) if err != nil { return nil, err } return &Balance{ - ZCN: zcnToken, - USD: usd, - Nonce: nonce, + ZCN: float64(bal.Balance), + USD: toUsd, + Nonce: bal.Nonce, }, nil } diff --git a/zcncore/ethwallet_base.go b/zcncore/ethwallet_base.go index e89e5da37..401005e76 100644 --- a/zcncore/ethwallet_base.go +++ b/zcncore/ethwallet_base.go @@ -2,8 +2,14 @@ package zcncore import ( "context" + "crypto/ecdsa" "encoding/json" "fmt" + "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/crypto" + "golang.org/x/crypto/sha3" + "log" "math" "math/big" "regexp" @@ -216,6 +222,87 @@ func SuggestEthGasPrice() (int64, error) { return gasPrice.Int64(), nil } +// TransferEthTokens - transfer ETH tokens to multisign wallet +func TransferEthTokens(fromPrivKey string, amountTokens, gasPrice int64) (string, error) { + var client *ethclient.Client + var err error + if client, err = getEthClient(); err != nil { + return "", err + } + + privateKey, err := crypto.HexToECDSA(fromPrivKey) + if err != nil { + return "", err + } + + publicKey := privateKey.Public() + publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey) + + fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA) + nonce, err := client.PendingNonceAt(context.Background(), fromAddress) + if err != nil { + return "", err + } + + toAddress := common.HexToAddress(walletAddr) + tokenAddress := common.HexToAddress(tokenAddress) + + transferFnSignature := []byte("transfer(address,uint256)") + hash := sha3.NewLegacyKeccak256() + hash.Write(transferFnSignature) + methodID := hash.Sum(nil)[:4] + fmt.Println(hexutil.Encode(methodID)) // 0xa9059cbb + + paddedAddress := common.LeftPadBytes(toAddress.Bytes(), 32) + fmt.Println(hexutil.Encode(paddedAddress)) // 0x0000000000000000000000004592d8f8d7b001e72cb26a73e4fa1806a51ac79d + + amount := new(big.Int) + amount.SetInt64(amountTokens) + + paddedAmount := common.LeftPadBytes(amount.Bytes(), 32) + fmt.Println(hexutil.Encode(paddedAmount)) // 0x00000000000000000000000000000000000000000000003635c9adc5dea00000 + + var data []byte + data = append(data, methodID...) + data = append(data, paddedAddress...) + data = append(data, paddedAmount...) + + gasLimit, err := client.EstimateGas(context.Background(), ethereum.CallMsg{ + To: &tokenAddress, + Data: data, + }) + if err != nil { + log.Fatal(err) + } + + txData := &types.LegacyTx{ + Nonce: nonce, + GasPrice: big.NewInt(gasPrice), + Gas: gasLimit, + To: &tokenAddress, + Value: amount, + Data: data, + } + tx := types.NewTx(txData) + + chainID, err := client.ChainID(context.Background()) + if err != nil { + return "", err + } + + signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey) + if err != nil { + return "", err + } + + err = client.SendTransaction(context.Background(), signedTx) + if err != nil { + return "", err + } + + return signedTx.Hash().Hex(), nil +} + func getBalanceFromEthNode(ethAddr string) (int64, error) { if client, err := getEthClient(); err == nil { account := common.HexToAddress(ethAddr) From 94431da4078b85251455981e48538bc3585fcbb2 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 12 Sep 2024 01:23:12 +0530 Subject: [PATCH 070/319] Fix wasm --- core/version/version.go | 2 +- wasmsdk/demo/main.go | 2 -- wasmsdk/zcn.go | 8 ++++++-- winsdk/wallet.go | 3 ++- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/core/version/version.go b/core/version/version.go index e8cd0a9b9..087550579 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.6-69-gc1c10ab1" +const VERSIONSTR = "v1.17.6-70-g3bd80b5b" diff --git a/wasmsdk/demo/main.go b/wasmsdk/demo/main.go index 6c899cdd7..9e08e1f1d 100644 --- a/wasmsdk/demo/main.go +++ b/wasmsdk/demo/main.go @@ -14,8 +14,6 @@ import ( func main() { - zcncore.InitSignatureScheme("bls0chain") - ctx, cf := context.WithCancel(context.Background()) router := bunrouter.New() diff --git a/wasmsdk/zcn.go b/wasmsdk/zcn.go index 8f1c47def..6a663bfe9 100644 --- a/wasmsdk/zcn.go +++ b/wasmsdk/zcn.go @@ -22,14 +22,18 @@ func getWalletBalance(clientId string) (*Balance, error) { if err != nil { return nil, err } + balance, err := bal.ToToken() + if err != nil { + return nil, err + } - toUsd, err := zcncore.ConvertTokenToUSD(float64(bal.Balance)) + toUsd, err := zcncore.ConvertTokenToUSD(balance) if err != nil { return nil, err } return &Balance{ - ZCN: float64(bal.Balance), + ZCN: balance, USD: toUsd, Nonce: bal.Nonce, }, nil diff --git a/winsdk/wallet.go b/winsdk/wallet.go index be185829e..0e72008f8 100644 --- a/winsdk/wallet.go +++ b/winsdk/wallet.go @@ -8,6 +8,7 @@ import ( ) import ( + "github.com/0chain/gosdk/core/client" "os" "path/filepath" @@ -86,7 +87,7 @@ func RecoverWallet(mnemonic *C.char) *C.char { // //export GetWalletBalance func GetWalletBalance(clientID *C.char) *C.char { - b, _, err := zcncore.GetWalletBalance(C.GoString(clientID)) + b, err := client.GetBalance(C.GoString(clientID)) if err != nil { log.Error("win: ", err) return WithJSON(0, err) From a459f7616f80cfdac53f7c0e8d27b06108a6d334 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Thu, 12 Sep 2024 19:18:25 +0530 Subject: [PATCH 071/319] fix delete dir --- zboxcore/sdk/commitworker.go | 19 ++++++++++++++----- zboxcore/sdk/deleteworker.go | 10 ++++++++-- zboxcore/sdk/multi_operation_worker.go | 10 ++++++---- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index b8254c63d..e1249338d 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -406,11 +406,11 @@ func (commitReq *CommitRequestV2) processCommit() { commitReq.changes[i] = nil } } - if len(paths) == 0 { - l.Logger.Debug("Nothing to commit") - commitReq.result = SuccessCommitResult() - return - } + // if len(paths) == 0 { + // l.Logger.Debug("Nothing to commit") + // commitReq.result = SuccessCommitResult() + // return + // } var ( trie *wmpt.WeightedMerkleTrie err error @@ -587,6 +587,15 @@ func getFormWritter(connectionID string, wmData, fileIDMetaData []byte, body *by } func getReferencePathV2(blobber *blockchain.StorageNode, allocationID, allocationTx, sig string, paths []string) (*wmpt.WeightedMerkleTrie, error) { + if len(paths) == 0 { + var node wmpt.Node + if blobber.LatestWM != nil { + decodedRoot, _ := hex.DecodeString(blobber.LatestWM.AllocationRoot) + node = wmpt.NewHashNode(decodedRoot, uint64(numBlocks(blobber.LatestWM.ChainSize))) + } + trie := wmpt.New(node, nil) + return trie, nil + } req, err := zboxutil.NewReferencePathRequestV2(blobber.Baseurl, allocationID, allocationTx, sig, paths) if err != nil { l.Logger.Error("Creating ref path req", err) diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index 70c006788..d2c0ddb40 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -361,8 +361,15 @@ func (dop *DeleteOperation) Process(allocObj *Allocation, connectionID string) ( } deleteReq.consensus.fullconsensus = dop.consensus.fullconsensus deleteReq.consensus.consensusThresh = dop.consensus.consensusThresh + dop.lookupHash = fileref.GetReferenceLookup(allocObj.ID, dop.remotefilepath) if allocObj.StorageVersion == 1 { - return deleteReq.processDeleteV2() + var ( + objectTreeRefs []fileref.RefEntity + err error + ) + objectTreeRefs, deleteReq.deleteMask, err = deleteReq.processDeleteV2() + dop.refs = objectTreeRefs + return dop.refs, deleteReq.deleteMask, err } numList := len(deleteReq.blobbers) @@ -411,7 +418,6 @@ func (dop *DeleteOperation) Process(allocObj *Allocation, connectionID string) ( } l.Logger.Debug("Delete Process Ended ") dop.refs = objectTreeRefs - dop.lookupHash = fileref.GetReferenceLookup(allocObj.ID, dop.remotefilepath) return objectTreeRefs, deleteReq.deleteMask, nil } diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 0bef25949..7fd989cce 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -190,10 +190,12 @@ func (mo *MultiOperation) Process() error { refs, mask, err := op.Process(mo.allocationObj, mo.connectionID) // Process with each blobber if err != nil { - l.Logger.Error(err) - errsSlice[idx] = errors.New("", err.Error()) - ctxCncl(err) - return + if err != errFileDeleted { + l.Logger.Error(err) + errsSlice[idx] = errors.New("", err.Error()) + ctxCncl(err) + return + } } mo.maskMU.Lock() if mo.allocationObj.StorageVersion == StorageV2 { From 4c690e4a3c9d4326291167ffaa43472ebf47d456 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Thu, 12 Sep 2024 21:29:30 +0530 Subject: [PATCH 072/319] add timming log --- zboxcore/sdk/commitworker.go | 6 ++++-- zboxcore/sdk/upload_worker.go | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index e1249338d..695b74e1d 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -489,7 +489,7 @@ func (commitReq *CommitRequestV2) processCommit() { return } commitReq.allocationObj.allocationRoot = hex.EncodeToString(rootHash) - l.Logger.Info("[commit] ", "elapsedGetRefPath", elapsedGetRefPath.Milliseconds(), " elapsedProcessChanges", elapsedProcessChanges.Milliseconds(), " elapsedCommit", elapsedCommit.Milliseconds(), " total", time.Since(now).Milliseconds()) + l.Logger.Info("[commit] ", "elapsedGetRefPath ", elapsedGetRefPath.Milliseconds(), " elapsedProcessChanges ", elapsedProcessChanges.Milliseconds(), " elapsedCommit ", elapsedCommit.Milliseconds(), " total ", time.Since(now).Milliseconds()) commitReq.result = SuccessCommitResult() } @@ -596,6 +596,7 @@ func getReferencePathV2(blobber *blockchain.StorageNode, allocationID, allocatio trie := wmpt.New(node, nil) return trie, nil } + now := time.Now() req, err := zboxutil.NewReferencePathRequestV2(blobber.Baseurl, allocationID, allocationTx, sig, paths) if err != nil { l.Logger.Error("Creating ref path req", err) @@ -631,7 +632,7 @@ func getReferencePathV2(blobber *blockchain.StorageNode, allocationID, allocatio if err != nil { return nil, err } - + elapsedRefPath := time.Since(now) trie := wmpt.New(nil, nil) if lR.LatestWM != nil { err = lR.LatestWM.VerifySignature(client.GetClientPublicKey()) @@ -643,6 +644,7 @@ func getReferencePathV2(blobber *blockchain.StorageNode, allocationID, allocatio l.Logger.Error("Error deserializing trie", err) return nil, err } + l.Logger.Info("[getReferencePathV2] elapsedRefPath ", elapsedRefPath.Milliseconds(), " elapsedDeserialize ", (time.Since(now) - elapsedRefPath).Milliseconds()) chainBlocks := numBlocks(lR.LatestWM.ChainSize) if trie.Weight() != uint64(chainBlocks) { return nil, errors.New("chain_length_mismatch", fmt.Sprintf("Expected chain length %d, got %d", chainBlocks, trie.Weight())) diff --git a/zboxcore/sdk/upload_worker.go b/zboxcore/sdk/upload_worker.go index fda7d0b3c..00f7df0dd 100644 --- a/zboxcore/sdk/upload_worker.go +++ b/zboxcore/sdk/upload_worker.go @@ -64,7 +64,7 @@ func (uo *UploadOperation) Process(allocObj *Allocation, connectionID string) ([ } } uo.lookupHash = fileref.GetReferenceLookup(uo.chunkedUpload.allocationObj.ID, uo.chunkedUpload.fileMeta.RemotePath) - l.Logger.Info("UploadOperation Success", zap.String("name", uo.chunkedUpload.fileMeta.RemoteName)) + l.Logger.Debug("UploadOperation Success", zap.String("name", uo.chunkedUpload.fileMeta.RemoteName)) return nil, uo.chunkedUpload.uploadMask, nil } From c9723ae708ba7923fe0ee22add2ea28db53f459d Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 13 Sep 2024 02:13:23 +0530 Subject: [PATCH 073/319] Fix --- core/client/cache.go | 4 ++++ core/client/http.go | 22 +++++++++++++++------- core/client/init_node.go | 4 ++-- core/transaction/entity.go | 5 ++++- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/core/client/cache.go b/core/client/cache.go index 5652a3cdb..dc394a930 100644 --- a/core/client/cache.go +++ b/core/client/cache.go @@ -1,6 +1,8 @@ package client import ( + "go.uber.org/zap" + "log" "sync" ) @@ -35,6 +37,8 @@ func (nc *NonceCache) GetNextNonce(clientId string) int64 { } else { nc.cache[clientId] = bal.Nonce } + + log.Println("NonceCache.GetNextNonce: ", clientId, nc.cache[clientId], zap.Any("bal", bal), zap.Error(err)) } nc.cache[clientId] += 1 diff --git a/core/client/http.go b/core/client/http.go index fcf6ede4d..a63e92bd1 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -77,7 +77,7 @@ func NewRetryableClient(retryMax int) *retryablehttp.Client { // MakeSCRestAPICall calls smart contract with provided address // and makes retryable request to smart contract resource with provided relative path using params. -func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string) ([]byte, error) { +func MakeSCRestAPICall(scAddress, relativePath string, params map[string]string, restApiUrls ...string) ([]byte, error) { var ( resMaxCounterBody []byte @@ -89,13 +89,21 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] lastErrMsg string ) + restApiUrl := ScRestApiUrl + if len(restApiUrls) > 0 { + restApiUrl = restApiUrls[0] + } + for _, sharder := range sharders { var ( retryableClient = NewRetryableClient(5) - u = makeScURL(params, sharder, scAddress, relativePath) + u = makeScURL(params, sharder, restApiUrl, scAddress, relativePath) ) - resp, err := retryableClient.Get(u.String()) + url := u.String() + fmt.Println("URL: ", url) + + resp, err := retryableClient.Get(url) if err != nil { lastErrMsg = fmt.Sprintf("error while requesting sharders: %v", err) continue @@ -150,8 +158,8 @@ const ( ) // makeScURL creates url.URL to make smart contract request to sharder. -func makeScURL(params map[string]string, sharder, scAddress, relativePath string) *url.URL { - uString := fmt.Sprintf("%v/%v%v%v", sharder, ScRestApiUrl, scAddress, relativePath) +func makeScURL(params map[string]string, sharder, scAddress, restApiUrl, relativePath string) *url.URL { + uString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath) u, _ := url.Parse(uString) q := u.Query() for k, v := range params { @@ -220,7 +228,7 @@ func getEnvAny(names ...string) string { } func GetBalance(clientIDs ...string) (*GetBalanceResponse, error) { - const GET_BALANCE = "/client/get/balance" + const GET_BALANCE = "client/get/balance" var ( balance GetBalanceResponse err error @@ -236,7 +244,7 @@ func GetBalance(clientIDs ...string) (*GetBalanceResponse, error) { if res, err = MakeSCRestAPICall("", GET_BALANCE, map[string]string{ "client_id": clientID, - }); err != nil { + }, "v1"); err != nil { return nil, err } diff --git a/core/client/init_node.go b/core/client/init_node.go index b218d9e1c..568f9c6e3 100644 --- a/core/client/init_node.go +++ b/core/client/init_node.go @@ -90,7 +90,7 @@ func (n *Node) ShouldUpdateNetwork() (bool, *conf.Network, error) { } network, err := GetNetwork(n.clientCtx, cfg.BlockWorker) if err != nil { - logging.Error("Failed to get network details ", zap.Error(err)) + logging.Error("Failed to get network details ", zap.Error(err), zap.String("block_worker", cfg.BlockWorker)) return false, nil, err } n.networkGuard.RLock() @@ -128,7 +128,7 @@ func Init(ctx context.Context, cfg conf.Config) error { network, err := GetNetwork(ctx, cfg.BlockWorker) if err != nil { - logging.Error("Failed to get network details ", zap.Error(err)) + logging.Error("Failed to get network details ", zap.Error(err), zap.Any("block_worker", cfg.BlockWorker)) return err } diff --git a/core/transaction/entity.go b/core/transaction/entity.go index cc401fe33..abbe9073e 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -9,6 +9,7 @@ import ( "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/core/sys" "go.uber.org/zap" + "log" "net/http" "strings" "sync" @@ -335,7 +336,7 @@ func retriveFromTable(table map[string]map[string]int64, txnName, toAddress stri if txnName == "transfer" { fees = uint64(table["transfer"]["transfer"]) } else { - return 0, fmt.Errorf("invalid transaction") + return 0, fmt.Errorf("Jayash invalid transaction") } } return fees, nil @@ -554,6 +555,8 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, txn.TransactionNonce = client.Cache.GetNextNonce(txn.ClientID) } + log.Println("Nonce: ", txn.TransactionNonce) + if err = txn.ComputeHashAndSign(client.Sign); err != nil { return } From 47852658e253aa0ffaa04cd28edeffecdf904089 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 13 Sep 2024 02:31:19 +0530 Subject: [PATCH 074/319] Fix --- core/client/http.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index a63e92bd1..4b0e5dbe8 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -228,7 +228,7 @@ func getEnvAny(names ...string) string { } func GetBalance(clientIDs ...string) (*GetBalanceResponse, error) { - const GET_BALANCE = "client/get/balance" + const GetBalance = "/client/get/balance" var ( balance GetBalanceResponse err error @@ -242,7 +242,7 @@ func GetBalance(clientIDs ...string) (*GetBalanceResponse, error) { clientID = ClientID() } - if res, err = MakeSCRestAPICall("", GET_BALANCE, map[string]string{ + if res, err = MakeSCRestAPICall("", GetBalance, map[string]string{ "client_id": clientID, }, "v1"); err != nil { return nil, err From 042f96bf0ab33d3986041bda511ff177dbefd78a Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 13 Sep 2024 21:27:12 +0530 Subject: [PATCH 075/319] Fix --- core/client/http.go | 6 ++-- go.mod | 20 +------------ go.sum | 71 --------------------------------------------- 3 files changed, 4 insertions(+), 93 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index 4b0e5dbe8..99617d541 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -158,7 +158,7 @@ const ( ) // makeScURL creates url.URL to make smart contract request to sharder. -func makeScURL(params map[string]string, sharder, scAddress, restApiUrl, relativePath string) *url.URL { +func makeScURL(params map[string]string, sharder, restApiUrl, scAddress, relativePath string) *url.URL { uString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath) u, _ := url.Parse(uString) q := u.Query() @@ -228,7 +228,7 @@ func getEnvAny(names ...string) string { } func GetBalance(clientIDs ...string) (*GetBalanceResponse, error) { - const GetBalance = "/client/get/balance" + const GetBalance = "client/get/balance" var ( balance GetBalanceResponse err error @@ -244,7 +244,7 @@ func GetBalance(clientIDs ...string) (*GetBalanceResponse, error) { if res, err = MakeSCRestAPICall("", GetBalance, map[string]string{ "client_id": clientID, - }, "v1"); err != nil { + }, "v1/"); err != nil { return nil, err } diff --git a/go.mod b/go.mod index 8580f176e..acbf8867f 100644 --- a/go.mod +++ b/go.mod @@ -7,11 +7,8 @@ require ( github.com/0chain/errors v1.0.3 github.com/Luzifer/go-openssl/v3 v3.1.0 github.com/btcsuite/btcd v0.23.4 - github.com/dgraph-io/badger/v3 v3.2103.5 - github.com/didip/tollbooth v4.0.2+incompatible github.com/ethereum/go-ethereum v1.10.26 github.com/google/uuid v1.3.0 - github.com/gorilla/handlers v1.5.1 github.com/gorilla/mux v1.8.0 github.com/h2non/filetype v1.1.4-0.20231228185113-6469358c2bcb github.com/hashicorp/go-retryablehttp v0.7.2 @@ -22,7 +19,6 @@ require ( github.com/klauspost/reedsolomon v1.11.8 github.com/lithammer/shortuuid/v3 v3.0.7 github.com/machinebox/graphql v0.2.2 - github.com/magma/augmented-networks/accounting/protos v0.1.1 github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/mapstructure v1.5.0 github.com/pkg/errors v0.9.1 @@ -36,11 +32,10 @@ require ( golang.org/x/crypto v0.26.0 golang.org/x/image v0.19.0 golang.org/x/sync v0.8.0 - google.golang.org/grpc v1.53.0 gopkg.in/cheggaaa/pb.v1 v1.0.28 gopkg.in/natefinch/lumberjack.v2 v2.2.1 gopkg.in/yaml.v2 v2.4.0 - gopkg.in/yaml.v3 v3.0.1 + gopkg.in/yaml.v3 v3.0.1 // indirect ) require ( @@ -56,29 +51,20 @@ require ( github.com/andybalholm/brotli v1.0.5 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect - github.com/cespare/xxhash v1.1.0 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/deckarep/golang-set v1.8.0 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect - github.com/dgraph-io/ristretto v0.1.1 // indirect - github.com/dustin/go-humanize v1.0.1 // indirect github.com/edsrzf/mmap-go v1.0.0 // indirect - github.com/felixge/httpsnoop v1.0.3 // indirect github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect github.com/go-logfmt/logfmt v0.5.1 // indirect github.com/go-ole/go-ole v1.2.6 // indirect github.com/go-stack/stack v1.8.1 // indirect - github.com/gogo/protobuf v1.3.2 // indirect github.com/golang-jwt/jwt/v4 v4.3.0 // indirect - github.com/golang/glog v1.0.0 // indirect - github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect - github.com/golang/protobuf v1.5.2 // indirect github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect - github.com/google/flatbuffers v22.9.29+incompatible // indirect github.com/google/go-cmp v0.6.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/hashicorp/go-bexpr v0.1.10 // indirect @@ -96,7 +82,6 @@ require ( github.com/mattn/go-runewidth v0.0.14 // indirect github.com/mitchellh/pointerstructure v1.2.0 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect - github.com/patrickmn/go-cache v2.1.0+incompatible // indirect github.com/pelletier/go-toml/v2 v2.0.6 // indirect github.com/philhofer/fwd v1.1.2-0.20210722190033-5c56ac6d0bb9 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect @@ -121,10 +106,8 @@ require ( github.com/valyala/fasthttp v1.51.0 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect go.dedis.ch/fixbuf v1.0.3 // indirect - go.opencensus.io v0.24.0 // indirect golang.org/x/sys v0.23.0 // indirect golang.org/x/text v0.17.0 // indirect - google.golang.org/protobuf v1.28.1 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect @@ -141,7 +124,6 @@ require ( go.uber.org/multierr v1.9.0 // indirect golang.org/x/net v0.28.0 // indirect golang.org/x/time v0.3.0 // indirect - google.golang.org/genproto v0.0.0-20230216225411-c8e22ba71e44 // indirect ) //replace github.com/ethereum/go-ethereum => github.com/certifaction/go-ethereum v1.10.3-wasm diff --git a/go.sum b/go.sum index 324d90b99..fbccaefe4 100644 --- a/go.sum +++ b/go.sum @@ -49,7 +49,6 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/Luzifer/go-openssl/v3 v3.1.0 h1:QqKqo6kYXGGUsvtUoCpRZm8lHw+jDfhbzr36gVj+/gw= github.com/Luzifer/go-openssl/v3 v3.1.0/go.mod h1:liy3FXuuS8hfDlYh1T+l78AwQ/NjZflJz0NDvjKhwDs= -github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/VictoriaMetrics/fastcache v1.6.0 h1:C/3Oi3EiBCqufydp1neRZkqcwmEiuRT9c3fqvvgKm5o= github.com/VictoriaMetrics/fastcache v1.6.0/go.mod h1:0qHz5QP0GMX4pfmMA/zt5RgfNuXJrTP0zS7DqpHGGTw= @@ -63,7 +62,6 @@ github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/apache/arrow/go/arrow v0.0.0-20191024131854-af6fa24be0db/go.mod h1:VTxUBvSJ3s3eHAg65PNgrsn5BtqCRPdmyXh6rAfdxN0= -github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= @@ -100,7 +98,6 @@ github.com/c-bata/go-prompt v0.2.2/go.mod h1:VzqtzE2ksDBcdln8G7mk2RX9QyGjH+OVqOC github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/cp v0.1.0 h1:SE+dxFebS7Iik5LK0tsi1k9ZCxEaFX4AjQmoyA+1dJk= github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s= -github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= @@ -112,10 +109,6 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= -github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= -github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= -github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= -github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= @@ -132,20 +125,9 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeC github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 h1:HbphB4TFFXpv7MNrT52FGrrgVXF1owhMVTHFZIlnvd4= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0/go.mod h1:DZGJHZMqrU4JJqFAWUS2UO1+lbSKsdiOoYi9Zzey7Fc= github.com/decred/dcrd/lru v1.0.0/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218= -github.com/dgraph-io/badger/v3 v3.2103.5 h1:ylPa6qzbjYRQMU6jokoj4wzcaweHylt//CH0AKt0akg= -github.com/dgraph-io/badger/v3 v3.2103.5/go.mod h1:4MPiseMeDQ3FNCYwRbbcBOGJLf5jsE0PPFzRiKjtcdw= -github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8= -github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-bitstream v0.0.0-20180413035011-3522498ce2c8/go.mod h1:VMaSuZ+SZcx/wljOQKvp5srsbCiKDEb6K2wC4+PiBmQ= -github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA= -github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= -github.com/didip/tollbooth v4.0.2+incompatible h1:fVSa33JzSz0hoh2NxpwZtksAzAgd7zjmGO20HCZtF4M= -github.com/didip/tollbooth v4.0.2+incompatible/go.mod h1:A9b0665CE6l1KmzpDws2++elm/CsuWBMa5Jv4WY0PEY= -github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= -github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= -github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/eclipse/paho.mqtt.golang v1.2.0/go.mod h1:H9keYFcgq3Qr5OUJm/JZI/i6U7joQ8SYLhZwfeOo6Ts= github.com/edsrzf/mmap-go v1.0.0 h1:CEBF7HpRnUCSJgGUb5h1Gm7e3VkmVDrR8lvWVLtrOFw= github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= @@ -159,9 +141,6 @@ github.com/ethereum/go-ethereum v1.10.26 h1:i/7d9RBBwiXCEuyduBQzJw/mKmnvzsN14jqB github.com/ethereum/go-ethereum v1.10.26/go.mod h1:EYFyF19u3ezGLD4RqOkLq+ZCXzYbLoNDdZlMt7kyKFg= github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= -github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= -github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk= -github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 h1:FtmdgXiUlNeRsoNMFlKLDt+S+6hbjVMEW6RGQ7aUf7c= github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0= github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= @@ -193,20 +172,14 @@ github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP github.com/gofrs/uuid v3.3.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= -github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= -github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang-jwt/jwt/v4 v4.3.0 h1:kHL1vqdqWNfATmA0FNMdmZNMyZI1U6O31X4rlIPoBog= github.com/golang-jwt/jwt/v4 v4.3.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/golang/geo v0.0.0-20190916061304-5b978397cfec/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= -github.com/golang/glog v1.0.0 h1:nfP3RFugxnNRyKgeWd4oI1nYvXpxrx8ck8ZrcizshdQ= -github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= -github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= -github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y= @@ -228,9 +201,6 @@ github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvq github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= -github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= @@ -239,9 +209,6 @@ github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb/go.mod h1:/XxbfmMg github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/flatbuffers v1.11.0/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8= -github.com/google/flatbuffers v1.12.1/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8= -github.com/google/flatbuffers v22.9.29+incompatible h1:3UBb679lq3V/O9rgzoJmnkP1jJzmC9OdFzITUBkLU/A= -github.com/google/flatbuffers v22.9.29+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= @@ -250,9 +217,7 @@ github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= @@ -277,8 +242,6 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= -github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4= -github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= @@ -344,11 +307,9 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= github.com/jwilder/encoding v0.0.0-20170811194829-b4e1701a28ef/go.mod h1:Ct9fl0F6iIOGgxJ5npU/IUOhOhqlVrGjyIZc8/MagT0= github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= -github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= github.com/klauspost/compress v1.4.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= -github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM= github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= @@ -374,11 +335,8 @@ github.com/lithammer/shortuuid/v3 v3.0.7 h1:trX0KTHy4Pbwo/6ia8fscyHoGA+mf1jWbPJV github.com/lithammer/shortuuid/v3 v3.0.7/go.mod h1:vMk8ke37EmiewwolSO1NLW8vP4ZaKlRuDIi8tWWmAts= github.com/machinebox/graphql v0.2.2 h1:dWKpJligYKhYKO5A2gvNhkJdQMNZeChZYyBbrZkBZfo= github.com/machinebox/graphql v0.2.2/go.mod h1:F+kbVMHuwrQ5tYgU9JXlnskM8nOaFxCAEolaQybkjWA= -github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= -github.com/magma/augmented-networks/accounting/protos v0.1.1 h1:eTl3BC7s/PmYvh/scQSHxlqgt1BYvnOArU0vzPSjVAU= -github.com/magma/augmented-networks/accounting/protos v0.1.1/go.mod h1:Hpfg8aAxldUN7qlVtR5xwlAf8pcetFm8DWwRKZsh2J4= github.com/matryer/is v1.4.1 h1:55ehd8zaGABKLXQUe2awZ99BD/PTc2ls+KV/dXphgEQ= github.com/matryer/is v1.4.1/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= @@ -399,7 +357,6 @@ github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dz github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= -github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= @@ -426,10 +383,7 @@ github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.0.3-0.20180606204148-bd9c31933947/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= -github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= -github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/paulbellamy/ratecounter v0.2.0/go.mod h1:Hfx1hDpSGoqxkVVpBi/IlYD7kChlfo5C6hzIHwPqfFE= -github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU= github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= github.com/peterh/liner v1.0.1-0.20180619022028-8c1271fcf47f/go.mod h1:xIteQHvHuaLYG9IFj6mSxM0fCKrs34IrEQUhOYuGPHc= @@ -472,7 +426,6 @@ github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZV github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= -github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/segmentio/kafka-go v0.1.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo= @@ -486,23 +439,17 @@ github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPx github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= -github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= -github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= -github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk= github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w= github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU= github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= -github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= -github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/spf13/viper v1.15.0 h1:js3yy885G8xwJa6iOISGFwd+qlUo5AvyXb7CiihdtiU= github.com/spf13/viper v1.15.0/go.mod h1:fFcTBJxvhhzSJiZy8n+PeW6t8l+KeT/uTARa0jHOQLA= github.com/status-im/keycard-go v0.2.0 h1:QDLFswOQu1r5jsycloeQh3bVU8n/NatHHaZobtDnDzA= @@ -536,7 +483,6 @@ github.com/tklauser/numcpus v0.6.0 h1:kebhY2Qt+3U6RNK7UqpYNA+tJ23IBEGKkB7JQBfDYm github.com/tklauser/numcpus v0.6.0/go.mod h1:FEZLMke0lhOUG6w2JadTzp0a+Nl8PF/GFkQ5UVIcaL4= github.com/tyler-smith/go-bip39 v1.1.0 h1:5eUemwrMargf3BSLRRCalXT93Ns6pQJIjYQN2nyfOP8= github.com/tyler-smith/go-bip39 v1.1.0/go.mod h1:gUYDtqQw1JS3ZJ8UWVcGTGqqr6YIN3CWg+kkNaLt55U= -github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/uptrace/bunrouter v1.0.20 h1:jNvYNcJxF+lSYBQAaQjnE6I11Zs0m+3M5Ek7fq/Tp4c= github.com/uptrace/bunrouter v1.0.20/go.mod h1:TwT7Bc0ztF2Z2q/ZzMuSVkcb/Ig/d3MQeP2cxn3e1hI= github.com/urfave/cli/v2 v2.17.2-0.20221006022127-8f469abc00aa h1:5SqCsI/2Qya2bCzK15ozrqo2sZxkh0FHynJZOTVoV6Q= @@ -547,7 +493,6 @@ github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1S github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= github.com/willf/bitset v1.1.3/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4= github.com/xlab/treeprint v0.0.0-20180616005107-d6fb6747feb6/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg= -github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= github.com/ybbus/jsonrpc/v3 v3.1.5 h1:0cC/QzS8OCuXYqqDbYnKKhsEe+IZLrNlDx8KPCieeW0= @@ -573,8 +518,6 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= -go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= -go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= @@ -592,7 +535,6 @@ go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60= go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg= golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= -golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -676,7 +618,6 @@ golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81R golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= @@ -709,7 +650,6 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190124100055-b90733256f2e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -755,7 +695,6 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= @@ -816,7 +755,6 @@ golang.org/x/tools v0.0.0-20200501065659-ab2804fb9c9d/go.mod h1:EkVYQZoAsY45+roY golang.org/x/tools v0.0.0-20200512131952-2bc93b1c0c88/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200515010526-7d3b6ebf133d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= @@ -826,7 +764,6 @@ golang.org/x/tools v0.0.0-20201110124207-079ba7bd75cd/go.mod h1:emZCQorbCU4vsT4f golang.org/x/tools v0.0.0-20201201161351-ac6f37ff4c2a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.0.0-20201208233053-a543418bbed2/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.0.0-20210108195828-e2f9c7f1fc8e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -905,8 +842,6 @@ google.golang.org/genproto v0.0.0-20201210142538-e3217bee35cc/go.mod h1:FWY/as6D google.golang.org/genproto v0.0.0-20201214200347-8c77b98c765d/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20210108203827-ffc7fda8c3d7/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20210226172003-ab064af71705/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20230216225411-c8e22ba71e44 h1:EfLuoKW5WfkgVdDy7dTK8qSbH37AX5mj/MFh+bGPz14= -google.golang.org/genproto v0.0.0-20230216225411-c8e22ba71e44/go.mod h1:8B0gmkoRebU8ukX6HP+4wrVQUY1+6PkQ44BSyIlflHA= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= @@ -923,8 +858,6 @@ google.golang.org/grpc v1.31.1/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/grpc v1.34.0/go.mod h1:WotjhfgOW/POjDeRt8vscBtXq+2VjORFy659qA51WJ8= google.golang.org/grpc v1.35.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= -google.golang.org/grpc v1.53.0 h1:LAv2ds7cmFV/XTS3XG1NneeENYrXGmorPxsBbptIjNc= -google.golang.org/grpc v1.53.0/go.mod h1:OnIrk0ipVdj4N5d9IUoFUx72/VlD7+jUsHwZgwSMQpw= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= @@ -935,10 +868,6 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2 google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= -google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From aa521ff941e20c084272485a28d017b3739b3305 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 14 Sep 2024 02:06:25 +0530 Subject: [PATCH 076/319] Fix --- core/client/cache.go | 4 ---- core/client/http.go | 5 +---- core/transaction/entity.go | 3 --- zboxcore/sdk/sdk.go | 2 -- 4 files changed, 1 insertion(+), 13 deletions(-) diff --git a/core/client/cache.go b/core/client/cache.go index dc394a930..5652a3cdb 100644 --- a/core/client/cache.go +++ b/core/client/cache.go @@ -1,8 +1,6 @@ package client import ( - "go.uber.org/zap" - "log" "sync" ) @@ -37,8 +35,6 @@ func (nc *NonceCache) GetNextNonce(clientId string) int64 { } else { nc.cache[clientId] = bal.Nonce } - - log.Println("NonceCache.GetNextNonce: ", clientId, nc.cache[clientId], zap.Any("bal", bal), zap.Error(err)) } nc.cache[clientId] += 1 diff --git a/core/client/http.go b/core/client/http.go index 99617d541..d0c980ee2 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -100,10 +100,7 @@ func MakeSCRestAPICall(scAddress, relativePath string, params map[string]string, u = makeScURL(params, sharder, restApiUrl, scAddress, relativePath) ) - url := u.String() - fmt.Println("URL: ", url) - - resp, err := retryableClient.Get(url) + resp, err := retryableClient.Get(u.String()) if err != nil { lastErrMsg = fmt.Sprintf("error while requesting sharders: %v", err) continue diff --git a/core/transaction/entity.go b/core/transaction/entity.go index abbe9073e..14f4a41a2 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -9,7 +9,6 @@ import ( "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/core/sys" "go.uber.org/zap" - "log" "net/http" "strings" "sync" @@ -555,8 +554,6 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, txn.TransactionNonce = client.Cache.GetNextNonce(txn.ClientID) } - log.Println("Nonce: ", txn.TransactionNonce) - if err = txn.ComputeHashAndSign(client.Sign); err != nil { return } diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index 3699e67dd..33cd8892c 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -12,7 +12,6 @@ import ( "github.com/0chain/gosdk/core/zcncrypto" "gopkg.in/natefinch/lumberjack.v2" "io/ioutil" - "log" "math" "net/http" "strconv" @@ -757,7 +756,6 @@ func getBlobbersInternal(active, stakable bool, limit, offset int) (bs []*Blobbe // - active: if true then only active blobbers are returned // - stakable: if true then only stakable blobbers are returned func GetBlobbers(active, stakable bool) (bs []*Blobber, err error) { - log.Println("GetBlobbers : active ", active, stakable) if !sdkInitialized { return nil, sdkNotInitialized } From e71c555b3b9ce1306cc86ba4c7889f08a5d63649 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 14 Sep 2024 03:13:58 +0530 Subject: [PATCH 077/319] Fix --- core/client/http.go | 5 ++++ core/transaction/get_data.go | 55 ++---------------------------------- zboxcore/sdk/sdk.go | 10 +++---- 3 files changed, 13 insertions(+), 57 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index d0c980ee2..63d570726 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -157,6 +157,11 @@ const ( // makeScURL creates url.URL to make smart contract request to sharder. func makeScURL(params map[string]string, sharder, restApiUrl, scAddress, relativePath string) *url.URL { uString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath) + //log.Println("SC URL:", uString) + //log.Println("Sharders:", sharder) + //log.Println("Rest API URL:", restApiUrl) + //log.Println("SC Address:", scAddress) + //log.Println("Relative Path:", relativePath) u, _ := url.Parse(uString) q := u.Query() for k, v := range params { diff --git a/core/transaction/get_data.go b/core/transaction/get_data.go index 0d5315a68..889f01dd1 100644 --- a/core/transaction/get_data.go +++ b/core/transaction/get_data.go @@ -13,57 +13,8 @@ const ( ZCNSCSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712e0` ) const ( - GET_CLIENT = `/v1/client/get` - PUT_TRANSACTION = `/v1/transaction/put` - GET_BLOCK_INFO = `/v1/block/get?` - GET_MAGIC_BLOCK_INFO = `/v1/block/magic/get?` - GET_LATEST_FINALIZED = `/v1/block/get/latest_finalized` - GET_LATEST_FINALIZED_MAGIC_BLOCK = `/v1/block/get/latest_finalized_magic_block` - GET_FEE_STATS = `/v1/block/get/fee_stats` - GET_CHAIN_STATS = `/v1/chain/get/stats` - - // zcn sc - ZCNSC_PFX = `/v1/screst/` + ZCNSCSmartContractAddress - GET_MINT_NONCE = ZCNSC_PFX + `/v1/mint_nonce` - GET_NOT_PROCESSED_BURN_TICKETS = ZCNSC_PFX + `/v1/not_processed_burn_tickets` - GET_AUTHORIZER = ZCNSC_PFX + `/getAuthorizer` - - // miner SC - - MINERSC_PFX = `/v1/screst/` + MinerSmartContractAddress - GET_MINERSC_NODE = MINERSC_PFX + "/nodeStat" - GET_MINERSC_POOL = MINERSC_PFX + "/nodePoolStat" - GET_MINERSC_CONFIG = MINERSC_PFX + "/configs" - GET_MINERSC_GLOBALS = MINERSC_PFX + "/globalSettings" - GET_MINERSC_USER = MINERSC_PFX + "/getUserPools" - GET_MINERSC_MINERS = MINERSC_PFX + "/getMinerList" - GET_MINERSC_SHARDERS = MINERSC_PFX + "/getSharderList" - GET_MINERSC_EVENTS = MINERSC_PFX + "/getEvents" - - // storage SC - - STORAGESC_PFX = "/v1/screst/" + StorageSmartContractAddress - - STORAGESC_GET_SC_CONFIG = STORAGESC_PFX + "/storage-config" - STORAGESC_GET_CHALLENGE_POOL_INFO = STORAGESC_PFX + "/getChallengePoolStat" - STORAGESC_GET_ALLOCATION = STORAGESC_PFX + "/allocation" - STORAGESC_GET_ALLOCATIONS = STORAGESC_PFX + "/allocations" - STORAGESC_GET_READ_POOL_INFO = STORAGESC_PFX + "/getReadPoolStat" - STORAGESC_GET_STAKE_POOL_INFO = STORAGESC_PFX + "/getStakePoolStat" - STORAGESC_GET_STAKE_POOL_USER_INFO = STORAGESC_PFX + "/getUserStakePoolStat" - STORAGESC_GET_USER_LOCKED_TOTAL = STORAGESC_PFX + "/getUserLockedTotal" - STORAGESC_GET_BLOBBERS = STORAGESC_PFX + "/getblobbers" - STORAGESC_GET_BLOBBER = STORAGESC_PFX + "/getBlobber" - STORAGESC_GET_VALIDATOR = STORAGESC_PFX + "/get_validator" - STORAGESC_GET_TRANSACTIONS = STORAGESC_PFX + "/transactions" - - STORAGE_GET_SNAPSHOT = STORAGESC_PFX + "/replicate-snapshots" - STORAGE_GET_BLOBBER_SNAPSHOT = STORAGESC_PFX + "/replicate-blobber-aggregates" - STORAGE_GET_MINER_SNAPSHOT = STORAGESC_PFX + "/replicate-miner-aggregates" - STORAGE_GET_SHARDER_SNAPSHOT = STORAGESC_PFX + "/replicate-sharder-aggregates" - STORAGE_GET_AUTHORIZER_SNAPSHOT = STORAGESC_PFX + "/replicate-authorizer-aggregates" - STORAGE_GET_VALIDATOR_SNAPSHOT = STORAGESC_PFX + "/replicate-validator-aggregates" - STORAGE_GET_USER_SNAPSHOT = STORAGESC_PFX + "/replicate-user-aggregates" + GET_MINERSC_GLOBALS = "/globalSettings" + STORAGESC_GET_SC_CONFIG = "/storage-config" ) // @@ -101,7 +52,7 @@ func GetConfig(configType string) (conf *InputMap, err error) { conf = new(InputMap) conf.Fields = make(map[string]string) if err = json.Unmarshal(b, conf); err != nil { - return nil, errors.Wrap(err, "error decoding response:") + return nil, errors.Wrap(err, "1 error decoding response:") } return diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index 33cd8892c..bac657b7e 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -194,7 +194,7 @@ func GetReadPoolInfo(clientID string) (info *ReadPool, err error) { info = new(ReadPool) if err = json.Unmarshal(b, info); err != nil { - return nil, errors.Wrap(err, "error decoding response:") + return nil, errors.Wrap(err, "2 error decoding response:") } return @@ -300,7 +300,7 @@ func GetStakePoolInfo(providerType ProviderType, providerID string) (info *Stake info = new(StakePoolInfo) if err = json.Unmarshal(b, info); err != nil { - return nil, errors.Wrap(err, "error decoding response:") + return nil, errors.Wrap(err, "3 error decoding response:") } return @@ -341,7 +341,7 @@ func GetStakePoolUserInfo(clientID string, offset, limit int) (info *StakePoolUs info = new(StakePoolUserInfo) if err = json.Unmarshal(b, info); err != nil { - return nil, errors.Wrap(err, "error decoding response:") + return nil, errors.Wrap(err, "4 error decoding response:") } return @@ -553,7 +553,7 @@ func GetChallengePoolInfo(allocID string) (info *ChallengePoolInfo, err error) { info = new(ChallengePoolInfo) if err = json.Unmarshal(b, info); err != nil { - return nil, errors.Wrap(err, "error decoding response:") + return nil, errors.Wrap(err, "5 error decoding response:") } return @@ -746,7 +746,7 @@ func getBlobbersInternal(active, stakable bool, limit, offset int) (bs []*Blobbe } if err = json.Unmarshal(b, &wrap); err != nil { - return nil, errors.Wrap(err, "error decoding response:") + return nil, errors.Wrap(err, "6 error decoding response:") } return wrap.Nodes, nil From f1e342732f07e917253c281f725cd4fae403ff80 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sat, 14 Sep 2024 19:25:38 +0530 Subject: [PATCH 078/319] fix setting root --- zboxcore/sdk/commitworker.go | 5 ++++- zboxcore/sdk/multi_operation_worker.go | 3 ++- zboxcore/sdk/rollback.go | 6 +++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 695b74e1d..9cdd386eb 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -88,6 +88,7 @@ type CommitRequestV2 struct { consensusThresh int commitMask zboxutil.Uint128 changeIndex uint64 + isRepair bool } var ( @@ -488,7 +489,9 @@ func (commitReq *CommitRequestV2) processCommit() { commitReq.result = ErrorCommitResult(err.Error()) return } - commitReq.allocationObj.allocationRoot = hex.EncodeToString(rootHash) + if !commitReq.isRepair { + commitReq.allocationObj.allocationRoot = hex.EncodeToString(rootHash) + } l.Logger.Info("[commit] ", "elapsedGetRefPath ", elapsedGetRefPath.Milliseconds(), " elapsedProcessChanges ", elapsedProcessChanges.Milliseconds(), " elapsedCommit ", elapsedCommit.Milliseconds(), " total ", time.Since(now).Milliseconds()) commitReq.result = SuccessCommitResult() } diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 7fd989cce..3f2338c52 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -300,7 +300,7 @@ func (mo *MultiOperation) Process() error { for i := mo.operationMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { pos = uint64(i.TrailingZeros()) if mo.allocationObj.Blobbers[pos].AllocationRoot != mo.allocationObj.allocationRoot { - l.Logger.Debug("Blobber allocation root mismatch", mo.allocationObj.Blobbers[pos].Baseurl, mo.allocationObj.Blobbers[pos].AllocationRoot, mo.allocationObj.allocationRoot) + l.Logger.Info("Blobber allocation root mismatch", mo.allocationObj.Blobbers[pos].Baseurl, mo.allocationObj.Blobbers[pos].AllocationRoot, mo.allocationObj.allocationRoot) mo.operationMask = mo.operationMask.And(zboxutil.NewUint128(1).Lsh(pos).Not()) } } @@ -405,6 +405,7 @@ func (mo *MultiOperation) commitV2() error { commitMask: mask, consensusThresh: mo.consensusThresh, changes: changes, + isRepair: mo.isRepair, } commitReqs[counter] = commitReq counter++ diff --git a/zboxcore/sdk/rollback.go b/zboxcore/sdk/rollback.go index 6040427ad..ff28ed7dc 100644 --- a/zboxcore/sdk/rollback.go +++ b/zboxcore/sdk/rollback.go @@ -341,7 +341,11 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { } if len(versionMap[latestVersion]) > req || len(versionMap[prevVersion]) > req { - a.allocationRoot = versionMap[latestVersion][0].lpm.LatestWM.AllocationRoot + if len(versionMap[latestVersion]) > req { + a.allocationRoot = versionMap[latestVersion][0].lpm.LatestWM.AllocationRoot + } else { + a.allocationRoot = versionMap[prevVersion][0].lpm.LatestWM.AllocationRoot + } return Commit, blobberRes, nil } From 23757f0f67edf3c0497ee42719cfb376ab1dd636 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sun, 15 Sep 2024 00:12:54 +0530 Subject: [PATCH 079/319] send ref path request to all --- zboxcore/sdk/commitworker.go | 67 ++++++++++++++++++++++++++---------- zboxcore/zboxutil/http.go | 5 ++- 2 files changed, 53 insertions(+), 19 deletions(-) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 9cdd386eb..2934bb5a8 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -92,8 +92,9 @@ type CommitRequestV2 struct { } var ( - commitChan map[string]chan CommitRequestInterface - initCommitMutex sync.Mutex + commitChan map[string]chan CommitRequestInterface + initCommitMutex sync.Mutex + errAlreadySuccessful = errors.New("alread_successful", "") ) func InitCommitWorker(blobbers []*blockchain.StorageNode) { @@ -394,6 +395,12 @@ func (commitReq *CommitRequestV2) blobberID() string { return "" } +type refPathResp struct { + trie *wmpt.WeightedMerkleTrie + pos uint64 + err error +} + func (commitReq *CommitRequestV2) processCommit() { defer commitReq.wg.Done() l.Logger.Debug("received a commit request") @@ -413,24 +420,44 @@ func (commitReq *CommitRequestV2) processCommit() { // return // } var ( - trie *wmpt.WeightedMerkleTrie - err error - pos uint64 + pos uint64 + mu = &sync.Mutex{} + success bool ) now := time.Now() - + respChan := make(chan refPathResp, commitReq.commitMask.CountOnes()) for i := commitReq.commitMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { pos = uint64(i.TrailingZeros()) - blobber := commitReq.allocationObj.Blobbers[pos] - trie, err = getReferencePathV2(blobber, commitReq.allocationObj.ID, commitReq.allocationObj.Tx, commitReq.sig, paths) - if err != nil { - commitReq.commitMask = commitReq.commitMask.And(zboxutil.NewUint128(1).Lsh(pos).Not()) - l.Logger.Error("Error getting reference path: ", err) - continue + go func(ind uint64) { + blobber := commitReq.allocationObj.Blobbers[ind] + trie, err := getReferencePathV2(blobber, commitReq.allocationObj.ID, commitReq.allocationObj.Tx, commitReq.sig, paths, &success, mu) + resp := refPathResp{ + trie: trie, + err: err, + pos: ind, + } + respChan <- resp + }(pos) + } + + var ( + trie *wmpt.WeightedMerkleTrie + err error + ) + + for { + resp := <-respChan + if resp.err == nil { + trie = resp.trie + break + } else if resp.err != errAlreadySuccessful { + commitReq.commitMask = commitReq.commitMask.And(zboxutil.NewUint128(1).Lsh(resp.pos).Not()) + if commitReq.commitMask.CountOnes() < commitReq.consensusThresh { + commitReq.result = ErrorCommitResult("Failed to get reference path " + resp.err.Error()) + } } - changeIndex = pos - break } + if trie == nil { commitReq.result = ErrorCommitResult("Failed to get reference path") return @@ -459,7 +486,6 @@ func (commitReq *CommitRequestV2) processCommit() { wg := sync.WaitGroup{} errSlice := make([]error, commitReq.commitMask.CountOnes()) counter := 0 - mu := sync.Mutex{} for i := commitReq.commitMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { pos = uint64(i.TrailingZeros()) blobber := commitReq.allocationObj.Blobbers[pos] @@ -589,7 +615,7 @@ func getFormWritter(connectionID string, wmData, fileIDMetaData []byte, body *by return formWriter, nil } -func getReferencePathV2(blobber *blockchain.StorageNode, allocationID, allocationTx, sig string, paths []string) (*wmpt.WeightedMerkleTrie, error) { +func getReferencePathV2(blobber *blockchain.StorageNode, allocationID, allocationTx, sig string, paths []string, success *bool, mu *sync.Mutex) (*wmpt.WeightedMerkleTrie, error) { if len(paths) == 0 { var node wmpt.Node if blobber.LatestWM != nil { @@ -600,7 +626,7 @@ func getReferencePathV2(blobber *blockchain.StorageNode, allocationID, allocatio return trie, nil } now := time.Now() - req, err := zboxutil.NewReferencePathRequestV2(blobber.Baseurl, allocationID, allocationTx, sig, paths) + req, err := zboxutil.NewReferencePathRequestV2(blobber.Baseurl, allocationID, allocationTx, sig, paths, false) if err != nil { l.Logger.Error("Creating ref path req", err) return nil, err @@ -636,6 +662,11 @@ func getReferencePathV2(blobber *blockchain.StorageNode, allocationID, allocatio return nil, err } elapsedRefPath := time.Since(now) + mu.Lock() + defer mu.Unlock() + if *success { + return nil, errAlreadySuccessful + } trie := wmpt.New(nil, nil) if lR.LatestWM != nil { err = lR.LatestWM.VerifySignature(client.GetClientPublicKey()) @@ -656,7 +687,7 @@ func getReferencePathV2(blobber *blockchain.StorageNode, allocationID, allocatio return nil, errors.New("allocation_root_mismatch", fmt.Sprintf("Expected allocation root %s, got %s", lR.LatestWM.AllocationRoot, hex.EncodeToString(trie.Root()))) } } - + *success = true return trie, nil } diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index 4473d837f..5c9fa7bc5 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -292,7 +292,7 @@ func NewReferencePathRequest(baseUrl, allocationID string, allocationTx string, return req, nil } -func NewReferencePathRequestV2(baseUrl, allocationID string, allocationTx string, sig string, paths []string) (*http.Request, error) { +func NewReferencePathRequestV2(baseUrl, allocationID, allocationTx, sig string, paths []string, loadOnly bool) (*http.Request, error) { nurl, err := joinUrl(baseUrl, REFERENCE_ENDPOINT_V2, allocationTx) if err != nil { return nil, err @@ -304,6 +304,9 @@ func NewReferencePathRequestV2(baseUrl, allocationID string, allocationTx string } params := url.Values{} params.Add("paths", string(pathBytes)) + if loadOnly { + params.Add("load", "true") + } nurl.RawQuery = params.Encode() // Escape Query Parameters req, err := http.NewRequest(http.MethodGet, nurl.String(), nil) From 2d0ff4ffa0353fe2aecc2efd4a4be6ef3e832e4c Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 15 Sep 2024 02:09:56 +0530 Subject: [PATCH 080/319] Fix --- core/client/set.go | 2 -- zcncore/get_data.go | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index 58b0b8686..fce37812b 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -2,7 +2,6 @@ package client import ( "errors" - "fmt" "strings" "github.com/0chain/gosdk/constants" @@ -134,7 +133,6 @@ func Wallet() *zcncrypto.Wallet { } func SignatureScheme() string { - fmt.Println("Jayash client.signatureScheme", client.signatureScheme) return client.signatureScheme } diff --git a/zcncore/get_data.go b/zcncore/get_data.go index b994bc8e4..e6fa01080 100644 --- a/zcncore/get_data.go +++ b/zcncore/get_data.go @@ -225,7 +225,7 @@ func GetSharders(active, stakable bool, limit, offset int) ([]byte, error) { // - numSharders: number of sharders // - timeout: request timeout func GetLatestFinalizedMagicBlock(ctx context.Context, numSharders int) (m *block.MagicBlock, err error) { - res, err := client.MakeSCRestAPICall(MinerSmartContractAddress, GET_LATEST_FINALIZED_MAGIC_BLOCK, nil) + res, err := client.MakeSCRestAPICall("", GET_LATEST_FINALIZED_MAGIC_BLOCK, nil, "") if err != nil { return nil, err } From c21417aa01bda57417aad74a67a8e236eabeed2a Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sun, 15 Sep 2024 13:50:39 +0530 Subject: [PATCH 081/319] add merge logs --- go.mod | 2 +- go.sum | 4 ++-- zboxcore/sdk/commitworker.go | 3 ++- zboxcore/sdk/multi_operation_worker.go | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 67821b700..f03f7e144 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/0chain/gosdk go 1.21 require ( - github.com/0chain/common v1.13.1-0.20240911163240-3f40100a695a + github.com/0chain/common v1.13.1-0.20240915081248-6eba55b916e8 github.com/0chain/errors v1.0.3 github.com/Luzifer/go-openssl/v3 v3.1.0 github.com/btcsuite/btcd v0.23.4 diff --git a/go.sum b/go.sum index eba1749d9..d013f8960 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/0chain/common v1.13.1-0.20240911163240-3f40100a695a h1:NzweELzPLiZpaa9lLzEt/zXoxaU1YazA2weFjzeCqEI= -github.com/0chain/common v1.13.1-0.20240911163240-3f40100a695a/go.mod h1:lcwOe6fIgP7w/0sFSFB13rqDI43BGv5jk4PddArBaBM= +github.com/0chain/common v1.13.1-0.20240915081248-6eba55b916e8 h1:RNz7mwQzhmREUt7LULE9ZitEe7eeOE7yNaa2HfdWEeY= +github.com/0chain/common v1.13.1-0.20240915081248-6eba55b916e8/go.mod h1:vHK9Q0/nz28PxmKsLSMLzEs5iK6QVgl1LxdDQdR7y6s= github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM= github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 2934bb5a8..5902ecd94 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -454,6 +454,7 @@ func (commitReq *CommitRequestV2) processCommit() { commitReq.commitMask = commitReq.commitMask.And(zboxutil.NewUint128(1).Lsh(resp.pos).Not()) if commitReq.commitMask.CountOnes() < commitReq.consensusThresh { commitReq.result = ErrorCommitResult("Failed to get reference path " + resp.err.Error()) + return } } } @@ -474,7 +475,7 @@ func (commitReq *CommitRequestV2) processCommit() { } err = change.ProcessChangeV2(trie, changeIndex) if err != nil { - l.Logger.Error("Error processing change", err) + l.Logger.Error("Error processing change ", err) commitReq.result = ErrorCommitResult("Failed to process change " + err.Error()) return } diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 3f2338c52..85e95016e 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -431,7 +431,7 @@ func (mo *MultiOperation) commitV2() error { } if !mo.isConsensusOk() { err := zboxutil.MajorError(errSlice) - if err != nil { + if err == nil { err = errors.New("consensus_not_met", fmt.Sprintf("Successfully committed to %d blobbers, but required %d", mo.consensus, len(mo.allocationObj.Blobbers))) } if mo.getConsensus() != 0 { From 68afe5d077caa3d93dfb58d391379c3a16777146 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sun, 15 Sep 2024 16:21:39 +0530 Subject: [PATCH 082/319] fix collapse for short node --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index f03f7e144..88b183f25 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/0chain/gosdk go 1.21 require ( - github.com/0chain/common v1.13.1-0.20240915081248-6eba55b916e8 + github.com/0chain/common v1.13.1-0.20240915104526-dd036b153d4b github.com/0chain/errors v1.0.3 github.com/Luzifer/go-openssl/v3 v3.1.0 github.com/btcsuite/btcd v0.23.4 diff --git a/go.sum b/go.sum index d013f8960..1a09b7fc6 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/0chain/common v1.13.1-0.20240915081248-6eba55b916e8 h1:RNz7mwQzhmREUt7LULE9ZitEe7eeOE7yNaa2HfdWEeY= -github.com/0chain/common v1.13.1-0.20240915081248-6eba55b916e8/go.mod h1:vHK9Q0/nz28PxmKsLSMLzEs5iK6QVgl1LxdDQdR7y6s= +github.com/0chain/common v1.13.1-0.20240915104526-dd036b153d4b h1:Upb/QpkqMzwg/gmY67/BUohSqBz1Dpdh9YmIF6HCj3g= +github.com/0chain/common v1.13.1-0.20240915104526-dd036b153d4b/go.mod h1:vHK9Q0/nz28PxmKsLSMLzEs5iK6QVgl1LxdDQdR7y6s= github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM= github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= From f4a0c22a85ee6144572659e187a5cffe4944d94d Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sun, 15 Sep 2024 20:16:47 +0530 Subject: [PATCH 083/319] check refs in delete --- go.mod | 2 +- go.sum | 4 ++-- zboxcore/sdk/commitworker.go | 6 +----- zboxcore/sdk/deleteworker.go | 2 +- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 88b183f25..3aaf12743 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/0chain/gosdk go 1.21 require ( - github.com/0chain/common v1.13.1-0.20240915104526-dd036b153d4b + github.com/0chain/common v1.13.1-0.20240915144523-0e4d37ab39a1 github.com/0chain/errors v1.0.3 github.com/Luzifer/go-openssl/v3 v3.1.0 github.com/btcsuite/btcd v0.23.4 diff --git a/go.sum b/go.sum index 1a09b7fc6..3ea776362 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/0chain/common v1.13.1-0.20240915104526-dd036b153d4b h1:Upb/QpkqMzwg/gmY67/BUohSqBz1Dpdh9YmIF6HCj3g= -github.com/0chain/common v1.13.1-0.20240915104526-dd036b153d4b/go.mod h1:vHK9Q0/nz28PxmKsLSMLzEs5iK6QVgl1LxdDQdR7y6s= +github.com/0chain/common v1.13.1-0.20240915144523-0e4d37ab39a1 h1:jHfgxejNdfybNzNvYc1K+lZQ6yDT2BGNXQvQBhB3ChA= +github.com/0chain/common v1.13.1-0.20240915144523-0e4d37ab39a1/go.mod h1:vHK9Q0/nz28PxmKsLSMLzEs5iK6QVgl1LxdDQdR7y6s= github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM= github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 5902ecd94..92d62f0b3 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -414,11 +414,7 @@ func (commitReq *CommitRequestV2) processCommit() { commitReq.changes[i] = nil } } - // if len(paths) == 0 { - // l.Logger.Debug("Nothing to commit") - // commitReq.result = SuccessCommitResult() - // return - // } + var ( pos uint64 mu = &sync.Mutex{} diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index d2c0ddb40..84cf0b595 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -669,7 +669,7 @@ func (dop *DeleteOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, chang } func (dop *DeleteOperation) GetLookupHash(changeIndex uint64) string { - if dop.refs[changeIndex] == nil || dop.refs[changeIndex].GetType() == fileref.DIRECTORY { + if dop.refs == nil || dop.refs[changeIndex] == nil || dop.refs[changeIndex].GetType() == fileref.DIRECTORY { return "" } return dop.lookupHash From c5abb8f3fdc1ea243c34fc664ca76e5de3d8f37f Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sun, 15 Sep 2024 20:25:07 +0530 Subject: [PATCH 084/319] add changesv2 --- zboxcore/sdk/multi_operation_worker.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 85e95016e..73a7e4320 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -62,8 +62,9 @@ type MultiOperation struct { operationMask zboxutil.Uint128 maskMU *sync.Mutex Consensus - changes [][]allocationchange.AllocationChange - isRepair bool + changes [][]allocationchange.AllocationChange + changesV2 []allocationchange.AllocationChangeV2 + isRepair bool } func (mo *MultiOperation) createConnectionObj(blobberIdx int) (err error) { @@ -166,7 +167,11 @@ func (mo *MultiOperation) createConnectionObj(blobberIdx int) (err error) { func (mo *MultiOperation) Process() error { l.Logger.Debug("MultiOperation Process start") wg := &sync.WaitGroup{} - mo.changes = make([][]allocationchange.AllocationChange, len(mo.operations)) + if mo.allocationObj.StorageVersion == 0 { + mo.changes = make([][]allocationchange.AllocationChange, len(mo.operations)) + } else { + mo.changesV2 = make([]allocationchange.AllocationChangeV2, 0, len(mo.operations)) + } ctx := mo.ctx ctxCncl := mo.ctxCncl defer ctxCncl(nil) @@ -204,6 +209,7 @@ func (mo *MultiOperation) Process() error { } else { mo.operationMask = mo.operationMask.And(mask) } + mo.changesV2 = append(mo.changesV2, op) mo.maskMU.Unlock() } else { mo.operationMask = mo.operationMask.Or(mask) @@ -392,9 +398,12 @@ func (mo *MultiOperation) commitV2() error { wg := &sync.WaitGroup{} for _, mask := range rootMap { wg.Add(1) - changes := make([]allocationchange.AllocationChangeV2, 0, len(mo.operations)) - for _, op := range mo.operations { - changes = append(changes, op) + var changes []allocationchange.AllocationChangeV2 + if len(rootMap) > 1 { + changes = make([]allocationchange.AllocationChangeV2, 0, len(mo.operations)) + changes = append(changes, mo.changesV2...) + } else { + changes = mo.changesV2 } commitReq := &CommitRequestV2{ allocationObj: mo.allocationObj, From 85cb823dbda5176c79cdd019e48f82604d1b13f4 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 15 Sep 2024 20:29:48 +0530 Subject: [PATCH 085/319] Fix --- zcncore/get_data.go | 12 +++++++++--- zcncore/wallet_base.go | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/zcncore/get_data.go b/zcncore/get_data.go index e6fa01080..1197f6ae0 100644 --- a/zcncore/get_data.go +++ b/zcncore/get_data.go @@ -224,18 +224,24 @@ func GetSharders(active, stakable bool, limit, offset int) ([]byte, error) { // GetLatestFinalizedMagicBlock gets latest finalized magic block // - numSharders: number of sharders // - timeout: request timeout -func GetLatestFinalizedMagicBlock(ctx context.Context, numSharders int) (m *block.MagicBlock, err error) { +func GetLatestFinalizedMagicBlock() (m *block.MagicBlock, err error) { res, err := client.MakeSCRestAPICall("", GET_LATEST_FINALIZED_MAGIC_BLOCK, nil, "") if err != nil { return nil, err } - err = json.Unmarshal(res, &m) + type respObj struct { + MagicBlock *block.MagicBlock `json:"magic_block"` + } + + var resp respObj + + err = json.Unmarshal(res, &resp) if err != nil { return nil, err } - return m, nil + return resp.MagicBlock, nil } // GetMinerSCUserInfo retrieve user stake pools for the providers related to the Miner SC (miners/sharders). diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 59a94941b..071fa9c1b 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -52,7 +52,7 @@ const ( GET_MINERSC_GLOBALS = MINERSC_PFX + "/globalSettings" GET_MINERSC_USER = MINERSC_PFX + "/getUserPools" GET_MINERSC_MINERS = MINERSC_PFX + "/getMinerList" - GET_MINERSC_SHARDERS = MINERSC_PFX + "/getSharderList" + GET_MINERSC_SHARDERS = "/getSharderList" GET_MINERSC_EVENTS = MINERSC_PFX + "/getEvents" // storage SC From 41d28b92725c6d6d5956fa9eaecd9890dbe8ed97 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sun, 15 Sep 2024 20:58:28 +0530 Subject: [PATCH 086/319] add eg for signature --- zboxcore/sdk/commitworker.go | 49 ++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 92d62f0b3..fb13e34c3 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -28,6 +28,7 @@ import ( "github.com/0chain/gosdk/zboxcore/marker" "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/minio/sha256-simd" + "golang.org/x/sync/errgroup" ) type ReferencePathResult struct { @@ -470,7 +471,7 @@ func (commitReq *CommitRequestV2) processCommit() { continue } err = change.ProcessChangeV2(trie, changeIndex) - if err != nil { + if err != nil && err != wmpt.ErrNotFound { l.Logger.Error("Error processing change ", err) commitReq.result = ErrorCommitResult("Failed to process change " + err.Error()) return @@ -521,23 +522,39 @@ func (commitReq *CommitRequestV2) processCommit() { func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, changeIndex uint64, blobber *blockchain.StorageNode) (err error) { hashSignatureMap := make(map[string]string) - for _, change := range req.changes { - if change == nil { - continue - } - hash := change.GetHash(changeIndex, blobber.ID) - if hash == "" { - return errors.New("hash_signature_failed", "Failed to add hash signature") - } - if hash == emptyHash { + mu := sync.Mutex{} + eg, _ := errgroup.WithContext(context.TODO()) + eg.SetLimit(10) + for i := 0; i < len(req.changes); i++ { + if req.changes[i] == nil { continue } - sig, err := client.Sign(hash) - if err != nil { - l.Logger.Error("Error signing hash", err) - return err - } - hashSignatureMap[change.GetLookupHash(changeIndex)] = sig + change := req.changes[i] + eg.Go(func() error { + hash := change.GetHash(changeIndex, blobber.ID) + if hash == "" { + return errors.New("hash_signature_failed", "Failed to add hash signature") + } + if hash == emptyHash { + return nil + } + + sig, sigErr := client.Sign(hash) + if sigErr != nil { + l.Logger.Error("Error signing hash", sigErr) + mu.Lock() + err = sigErr + mu.Unlock() + } + mu.Lock() + hashSignatureMap[change.GetLookupHash(changeIndex)] = sig + mu.Unlock() + return nil + }) + } + err = eg.Wait() + if err != nil { + return err } hasher := sha256.New() var prevChainSize int64 From ed19f05a329d3506c96fd8ef67b7a366978247e1 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 15 Sep 2024 22:26:18 +0530 Subject: [PATCH 087/319] Fix --- core/transaction/get_data.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/transaction/get_data.go b/core/transaction/get_data.go index 889f01dd1..0e67f2824 100644 --- a/core/transaction/get_data.go +++ b/core/transaction/get_data.go @@ -13,7 +13,7 @@ const ( ZCNSCSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712e0` ) const ( - GET_MINERSC_GLOBALS = "/globalSettings" + GET_MINERSC_GLOBALS = "/configs" STORAGESC_GET_SC_CONFIG = "/storage-config" ) From f45e9760f8602b1e81d0cbafbcb3d74aaf099e07 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sun, 15 Sep 2024 22:50:02 +0530 Subject: [PATCH 088/319] set limit to 4 --- zboxcore/sdk/commitworker.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index fb13e34c3..77aba8c10 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -521,10 +521,10 @@ func (commitReq *CommitRequestV2) processCommit() { } func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, changeIndex uint64, blobber *blockchain.StorageNode) (err error) { - hashSignatureMap := make(map[string]string) + hashSignatureMap := make(map[string]string, len(req.changes)) mu := sync.Mutex{} eg, _ := errgroup.WithContext(context.TODO()) - eg.SetLimit(10) + eg.SetLimit(4) for i := 0; i < len(req.changes); i++ { if req.changes[i] == nil { continue From d6b867518a2a11b868101667635b5e9824b35083 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 15 Sep 2024 23:46:19 +0530 Subject: [PATCH 089/319] Fix --- core/transaction/entity.go | 6 +++--- zcncore/execute_transactions.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 14f4a41a2..183387671 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -303,7 +303,7 @@ func SendTransactionSync(txn *Transaction, miners []string) error { } if failureCount == len(miners) { - return errors.New("transaction_send_error", dominant) + return fmt.Errorf(dominant) } return nil @@ -319,7 +319,7 @@ func sendTransactionToURL(url string, txn *Transaction, wg *sync.WaitGroup) ([]b if postResponse.StatusCode >= 200 && postResponse.StatusCode <= 299 { return []byte(postResponse.Body), nil } - return nil, errors.Wrap(err, errors.New("transaction_send_error", postResponse.Body)) + return nil, errors.Wrap(err, errors.New("submit transaction failed", postResponse.Body)) } type cachedObject struct { @@ -335,7 +335,7 @@ func retriveFromTable(table map[string]map[string]int64, txnName, toAddress stri if txnName == "transfer" { fees = uint64(table["transfer"]["transfer"]) } else { - return 0, fmt.Errorf("Jayash invalid transaction") + return 0, fmt.Errorf("failed to get fees for txn %s", txnName) } } return fees, nil diff --git a/zcncore/execute_transactions.go b/zcncore/execute_transactions.go index 85d78805c..5fd0de64e 100644 --- a/zcncore/execute_transactions.go +++ b/zcncore/execute_transactions.go @@ -203,8 +203,8 @@ type SendTxnData struct { } func Send(toClientID string, tokens uint64, desc string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { - return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ - Name: transaction.ZCNSC_COLLECT_REWARD, + return transaction.SmartContractTxnValue(MinerSmartContractAddress, transaction.SmartContractTxnData{ + Name: "transfer", InputArgs: SendTxnData{Note: desc}, - }, toClientID) + }, tokens, toClientID) } From b8ef8b747b65d524995a00541f00025853636a98 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Mon, 16 Sep 2024 00:09:48 +0530 Subject: [PATCH 090/319] Fix --- core/transaction/entity.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 183387671..5c5372dc0 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -536,6 +536,7 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, if len(toClient) > 0 { txn.ToClientID = toClient[0] + txn.TransactionType = TxnTypeSend } // adjust fees if not set From c2c16df3c02556fac75594c996e94bc9ae74c1b5 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Mon, 16 Sep 2024 00:11:52 +0530 Subject: [PATCH 091/319] remove group --- zboxcore/sdk/commitworker.go | 51 ++++++++++++++---------------------- 1 file changed, 19 insertions(+), 32 deletions(-) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 77aba8c10..ce2b7468e 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -28,7 +28,6 @@ import ( "github.com/0chain/gosdk/zboxcore/marker" "github.com/0chain/gosdk/zboxcore/zboxutil" "github.com/minio/sha256-simd" - "golang.org/x/sync/errgroup" ) type ReferencePathResult struct { @@ -521,41 +520,27 @@ func (commitReq *CommitRequestV2) processCommit() { } func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, changeIndex uint64, blobber *blockchain.StorageNode) (err error) { + now := time.Now() hashSignatureMap := make(map[string]string, len(req.changes)) - mu := sync.Mutex{} - eg, _ := errgroup.WithContext(context.TODO()) - eg.SetLimit(4) - for i := 0; i < len(req.changes); i++ { - if req.changes[i] == nil { + for _, change := range req.changes { + if change == nil { continue } - change := req.changes[i] - eg.Go(func() error { - hash := change.GetHash(changeIndex, blobber.ID) - if hash == "" { - return errors.New("hash_signature_failed", "Failed to add hash signature") - } - if hash == emptyHash { - return nil - } - - sig, sigErr := client.Sign(hash) - if sigErr != nil { - l.Logger.Error("Error signing hash", sigErr) - mu.Lock() - err = sigErr - mu.Unlock() - } - mu.Lock() - hashSignatureMap[change.GetLookupHash(changeIndex)] = sig - mu.Unlock() - return nil - }) - } - err = eg.Wait() - if err != nil { - return err + hash := change.GetHash(changeIndex, blobber.ID) + if hash == "" { + return errors.New("hash_signature_failed", "Failed to add hash signature") + } + if hash == emptyHash { + continue + } + sig, err := client.Sign(hash) + if err != nil { + l.Logger.Error("Error signing hash", err) + return err + } + hashSignatureMap[change.GetLookupHash(changeIndex)] = sig } + elapsedSign := time.Since(now) hasher := sha256.New() var prevChainSize int64 if blobber.LatestWM != nil { @@ -606,6 +591,8 @@ func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, changeInd } blobber.LatestWM = wm blobber.AllocationRoot = allocationRoot + elapsedSubmit := time.Since(now) - elapsedSign + l.Logger.Info("[commit] ", "elapsedSign: ", elapsedSign.Milliseconds(), " elapsedSubmit: ", elapsedSubmit.Milliseconds()) return } From d27b0cf8a7a830d36ed8e40dff005027bdb68a41 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Mon, 16 Sep 2024 02:07:04 +0530 Subject: [PATCH 092/319] Fix --- core/client/http.go | 12 +++++++----- zcncore/execute_transactions.go | 7 +++++++ zcncore/wallet_base.go | 4 ++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index 63d570726..050072db6 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -157,11 +157,7 @@ const ( // makeScURL creates url.URL to make smart contract request to sharder. func makeScURL(params map[string]string, sharder, restApiUrl, scAddress, relativePath string) *url.URL { uString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath) - //log.Println("SC URL:", uString) - //log.Println("Sharders:", sharder) - //log.Println("Rest API URL:", restApiUrl) - //log.Println("SC Address:", scAddress) - //log.Println("Relative Path:", relativePath) + u, _ := url.Parse(uString) q := u.Query() for k, v := range params { @@ -169,6 +165,12 @@ func makeScURL(params map[string]string, sharder, restApiUrl, scAddress, relativ } u.RawQuery = q.Encode() + //log.Println("SC URL:", u.RawQuery) + //log.Println("Sharders:", sharder) + //log.Println("Rest API URL:", restApiUrl) + //log.Println("SC Address:", scAddress) + //log.Println("Relative Path:", relativePath) + return u } diff --git a/zcncore/execute_transactions.go b/zcncore/execute_transactions.go index 5fd0de64e..267054664 100644 --- a/zcncore/execute_transactions.go +++ b/zcncore/execute_transactions.go @@ -208,3 +208,10 @@ func Send(toClientID string, tokens uint64, desc string) (hash, out string, nonc InputArgs: SendTxnData{Note: desc}, }, tokens, toClientID) } + +func Faucet(tokens uint64, input string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { + return transaction.SmartContractTxnValue(FaucetSmartContractAddress, transaction.SmartContractTxnData{ + Name: "pour", + InputArgs: input, + }, tokens) +} diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 071fa9c1b..5f5b3e084 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -47,11 +47,11 @@ const ( MINERSC_PFX = `/v1/screst/` + MinerSmartContractAddress GET_MINERSC_NODE = MINERSC_PFX + "/nodeStat" - GET_MINERSC_POOL = MINERSC_PFX + "/nodePoolStat" + GET_MINERSC_POOL = "/nodePoolStat" GET_MINERSC_CONFIG = MINERSC_PFX + "/configs" GET_MINERSC_GLOBALS = MINERSC_PFX + "/globalSettings" GET_MINERSC_USER = MINERSC_PFX + "/getUserPools" - GET_MINERSC_MINERS = MINERSC_PFX + "/getMinerList" + GET_MINERSC_MINERS = "/getMinerList" GET_MINERSC_SHARDERS = "/getSharderList" GET_MINERSC_EVENTS = MINERSC_PFX + "/getEvents" From 86559618c129e11b1784926513296d9e5dbcd3a1 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Mon, 16 Sep 2024 02:29:30 +0530 Subject: [PATCH 093/319] Fix --- zcncore/wallet_base.go | 44 +++--------------------------------------- 1 file changed, 3 insertions(+), 41 deletions(-) diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 5f5b3e084..9307f3616 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -32,53 +32,15 @@ const ( GET_FEE_STATS = `/v1/block/get/fee_stats` GET_CHAIN_STATS = `/v1/chain/get/stats` - // faucet sc - - FAUCETSC_PFX = `/v1/screst/` + FaucetSmartContractAddress - GET_FAUCETSC_CONFIG = FAUCETSC_PFX + `/faucet-config` - - // ZCNSC_PFX zcn sc - ZCNSC_PFX = `/v1/screst/` + ZCNSCSmartContractAddress - GET_MINT_NONCE = ZCNSC_PFX + `/v1/mint_nonce` - GET_NOT_PROCESSED_BURN_TICKETS = ZCNSC_PFX + `/v1/not_processed_burn_tickets` - GET_AUTHORIZER = ZCNSC_PFX + `/getAuthorizer` + GET_MINT_NONCE = `/v1/mint_nonce` // miner SC - MINERSC_PFX = `/v1/screst/` + MinerSmartContractAddress - GET_MINERSC_NODE = MINERSC_PFX + "/nodeStat" + GET_MINERSC_NODE = "/nodeStat" GET_MINERSC_POOL = "/nodePoolStat" - GET_MINERSC_CONFIG = MINERSC_PFX + "/configs" - GET_MINERSC_GLOBALS = MINERSC_PFX + "/globalSettings" - GET_MINERSC_USER = MINERSC_PFX + "/getUserPools" + GET_MINERSC_USER = "/getUserPools" GET_MINERSC_MINERS = "/getMinerList" GET_MINERSC_SHARDERS = "/getSharderList" - GET_MINERSC_EVENTS = MINERSC_PFX + "/getEvents" - - // storage SC - - STORAGESC_PFX = "/v1/screst/" + StorageSmartContractAddress - - STORAGESC_GET_SC_CONFIG = STORAGESC_PFX + "/storage-config" - STORAGESC_GET_CHALLENGE_POOL_INFO = STORAGESC_PFX + "/getChallengePoolStat" - STORAGESC_GET_ALLOCATION = STORAGESC_PFX + "/allocation" - STORAGESC_GET_ALLOCATIONS = STORAGESC_PFX + "/allocations" - STORAGESC_GET_READ_POOL_INFO = STORAGESC_PFX + "/getReadPoolStat" - STORAGESC_GET_STAKE_POOL_INFO = STORAGESC_PFX + "/getStakePoolStat" - STORAGESC_GET_STAKE_POOL_USER_INFO = STORAGESC_PFX + "/getUserStakePoolStat" - STORAGESC_GET_USER_LOCKED_TOTAL = STORAGESC_PFX + "/getUserLockedTotal" - STORAGESC_GET_BLOBBERS = STORAGESC_PFX + "/getblobbers" - STORAGESC_GET_BLOBBER = STORAGESC_PFX + "/getBlobber" - STORAGESC_GET_VALIDATOR = STORAGESC_PFX + "/get_validator" - STORAGESC_GET_TRANSACTIONS = STORAGESC_PFX + "/transactions" - - STORAGE_GET_SNAPSHOT = STORAGESC_PFX + "/replicate-snapshots" - STORAGE_GET_BLOBBER_SNAPSHOT = STORAGESC_PFX + "/replicate-blobber-aggregates" - STORAGE_GET_MINER_SNAPSHOT = STORAGESC_PFX + "/replicate-miner-aggregates" - STORAGE_GET_SHARDER_SNAPSHOT = STORAGESC_PFX + "/replicate-sharder-aggregates" - STORAGE_GET_AUTHORIZER_SNAPSHOT = STORAGESC_PFX + "/replicate-authorizer-aggregates" - STORAGE_GET_VALIDATOR_SNAPSHOT = STORAGESC_PFX + "/replicate-validator-aggregates" - STORAGE_GET_USER_SNAPSHOT = STORAGESC_PFX + "/replicate-user-aggregates" ) const ( From b96e59e567a09e428060305f15d8c114c4d155fd Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Mon, 16 Sep 2024 03:15:53 +0530 Subject: [PATCH 094/319] Fix --- core/zcncrypto/factory.go | 2 +- zcncore/wallet_base.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/zcncrypto/factory.go b/core/zcncrypto/factory.go index 92e287b50..d528d1dc5 100644 --- a/core/zcncrypto/factory.go +++ b/core/zcncrypto/factory.go @@ -24,7 +24,7 @@ func NewSignatureScheme(sigScheme string) SignatureScheme { } } -// UnmarshalThresholdSignatureSchemes unmarshal SignatureScheme from json string +// UnmarshalSignatureSchemes unmarshal SignatureScheme from json string func UnmarshalSignatureSchemes(sigScheme string, obj interface{}) ([]SignatureScheme, error) { switch sigScheme { diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 9307f3616..62c9342b6 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -191,7 +191,7 @@ func SetLogFile(logFile string, verbose bool) { // CreateWalletOffline creates the wallet for the config signature scheme. func CreateWalletOffline() (string, error) { - sigScheme := zcncrypto.NewSignatureScheme(client.SignatureScheme()) + sigScheme := zcncrypto.NewSignatureScheme("bls0chain") wallet, err := sigScheme.GenerateKeys() if err != nil { return "", errors.New("failed to generate keys: " + err.Error()) From 03ae61489c3ae1968dc32ab05379a23c20c344be Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Mon, 16 Sep 2024 03:23:39 +0530 Subject: [PATCH 095/319] Fix --- core/transaction/get_data.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/transaction/get_data.go b/core/transaction/get_data.go index 0e67f2824..cf7a60065 100644 --- a/core/transaction/get_data.go +++ b/core/transaction/get_data.go @@ -13,7 +13,8 @@ const ( ZCNSCSmartContractAddress = `6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712e0` ) const ( - GET_MINERSC_GLOBALS = "/configs" + GET_MINERSC_CONFIGS = "/configs" + GET_MINERSC_GLOBALS = "/globalSettings" STORAGESC_GET_SC_CONFIG = "/storage-config" ) @@ -39,6 +40,9 @@ func GetConfig(configType string) (conf *InputMap, err error) { } else if configType == "miner_sc_globals" { scAddress = MinerSmartContractAddress relativePath = GET_MINERSC_GLOBALS + } else if configType == "miner_sc_configs" { + scAddress = MinerSmartContractAddress + relativePath = GET_MINERSC_CONFIGS } b, err = coreHttp.MakeSCRestAPICall(scAddress, relativePath, nil) From a38a0141c7850474e111e4a0ceb731e9d8d6dcc4 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Mon, 16 Sep 2024 18:44:07 +0530 Subject: [PATCH 096/319] fix path --- wasmsdk/jsbridge/template_data.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wasmsdk/jsbridge/template_data.go b/wasmsdk/jsbridge/template_data.go index d9512d9d7..cc3c2c7d7 100644 --- a/wasmsdk/jsbridge/template_data.go +++ b/wasmsdk/jsbridge/template_data.go @@ -51,7 +51,7 @@ func buildJS(args, env []string, wasmPath string, tpl []byte) (string, error) { if suffix == "" { suffix = "dev" } - cdnPath := fmt.Sprintf("https://d2os1u2xwjukgrs.cloudfront.net/%s/zcn.wasm", suffix) + cdnPath := fmt.Sprintf("https://d2os1u2xwjukgr.cloudfront.net/%s/zcn.wasm", suffix) data := templateData{ Path: cdnPath, Args: args, From e93ee658d27dad14652e90c4ebe5e3cd3c528918 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Tue, 17 Sep 2024 02:15:54 +0200 Subject: [PATCH 097/319] feature: provided required changes for other kms components --- wasmsdk/proxy.go | 12 ++--------- zcncore/zauth.go | 52 ++++++++---------------------------------------- 2 files changed, 10 insertions(+), 54 deletions(-) diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index 1253c106c..0ba39b483 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -82,11 +82,7 @@ func main() { return "", fmt.Errorf("failed to sign with split key: %v", err) } - data, err := json.Marshal(struct { - Hash string `json:"hash"` - Signature string `json:"signature"` - ClientID string `json:"client_id"` - }{ + data, err := json.Marshal(zauth.AuthMessage{ Hash: hash, Signature: sig, ClientID: client.GetClient().ClientID, @@ -384,11 +380,7 @@ func main() { return "", fmt.Errorf("failed to sign with split key: %v", err) } - data, err := json.Marshal(struct { - Hash string `json:"hash"` - Signature string `json:"signature"` - ClientID string `json:"client_id"` - }{ + data, err := json.Marshal(zauth.AuthMessage{ Hash: hash, Signature: sig, ClientID: client.GetClient().ClientID, diff --git a/zcncore/zauth.go b/zcncore/zauth.go index 0ab96d5c0..eacd470f3 100644 --- a/zcncore/zauth.go +++ b/zcncore/zauth.go @@ -15,13 +15,14 @@ import ( // SplitWallet represents wallet info for split wallet // The client id and client key are the same as the primary wallet client id and client key type SplitWallet struct { - ClientID string `json:"client_id"` - ClientKey string `json:"client_key"` - PublicKey string `json:"public_key"` - PrivateKey string `json:"private_key"` - PeerPublicKey string `json:"peer_public_key"` - IsRevoked bool `json:"is_revoked"` - ExpiredAt int64 `json:"expired_at"` + ClientID string `json:"client_id"` + ClientKey string `json:"client_key"` + PublicKey string `json:"public_key"` + PrivateKey string `json:"private_key"` + PeerPublicKey string `json:"peer_public_key"` + Roles []string `json:"roles"` + IsRevoked bool `json:"is_revoked"` + ExpiredAt int64 `json:"expired_at"` } // CallZauthSetup calls the zauth setup endpoint @@ -452,7 +453,6 @@ func ZauthSignTxn(serverAddr string) sys.AuthorizeFunc { func ZauthAuthCommon(serverAddr string) sys.AuthorizeFunc { return func(msg string) (string, error) { - // return func(msg string) (string, error) { req, err := http.NewRequest("POST", serverAddr+"/sign/msg", bytes.NewBuffer([]byte(msg))) if err != nil { return "", errors.Wrap(err, "failed to create HTTP request") @@ -496,39 +496,3 @@ type AuthMessage struct { type AuthResponse struct { Sig string `json:"sig"` } - -func ZauthSignMsg(serverAddr string) sys.SignFunc { - return func(hash string, signatureScheme string, keys []sys.KeyPair) (string, error) { - sig, err := SignWithKey(keys[0].PrivateKey, hash) - if err != nil { - return "", err - } - - data, err := json.Marshal(AuthMessage{ - Hash: hash, - Signature: sig, - ClientID: client.GetClient().ClientID, - }) - if err != nil { - return "", err - } - - // fmt.Println("auth - sys.AuthCommon:", sys.AuthCommon) - if sys.AuthCommon == nil { - return "", errors.New("authCommon is not set") - } - - rsp, err := sys.AuthCommon(string(data)) - if err != nil { - return "", err - } - - var ar AuthResponse - err = json.Unmarshal([]byte(rsp), &ar) - if err != nil { - return "", err - } - - return AddSignature(client.GetClientPrivateKey(), ar.Sig, hash) - } -} From 09a82f19311aa3b8eb2bb26a1ef2e50e8b50bbe0 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Tue, 17 Sep 2024 15:56:29 +0200 Subject: [PATCH 098/319] fix: fixed bugs --- wasmsdk/auth_txn.go | 5 +++-- zcncore/zauth.go | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/wasmsdk/auth_txn.go b/wasmsdk/auth_txn.go index 5e2357787..184b52018 100644 --- a/wasmsdk/auth_txn.go +++ b/wasmsdk/auth_txn.go @@ -48,7 +48,7 @@ func zvaultNewWallet(serverAddr, token string) (string, error) { } // zvaultNewSplit generates new split wallet from existing clientID -func zvaultNewSplit(clientID, serverAddr, token string) (string, error) { +func zvaultNewSplit(clientID, serverAddr, token string, roles []string) (string, error) { return zcncore.CallZvaultNewWalletString(serverAddr, token, clientID) } @@ -92,7 +92,8 @@ func registerAuthCommon(this js.Value, args []js.Value) interface{} { } // authResponse Publishes the response to the authorization request. -// `response` is the response to the authorization request. +// +// `response` is the response to the authorization request. func authResponse(response string) { authResponseC <- response } diff --git a/zcncore/zauth.go b/zcncore/zauth.go index eacd470f3..b10cd7bc7 100644 --- a/zcncore/zauth.go +++ b/zcncore/zauth.go @@ -153,14 +153,25 @@ func CallZauthDelete(serverAddr, token, clientID string) error { return nil } -func CallZvaultNewWalletString(serverAddr, token, clientID string) (string, error) { +type newWalletRequest struct { + Roles []string `json:"roles"` +} + +func CallZvaultNewWalletString(serverAddr, token, clientID string, roles []string) (string, error) { // Add your code here endpoint := serverAddr + "/generate" if clientID != "" { endpoint = endpoint + "/" + clientID } - req, err := http.NewRequest("POST", endpoint, nil) + data, err := json.Marshal(newWalletRequest{ + Roles: roles, + }) + if err != nil { + return "", errors.Wrap(err, "failed to serialize request") + } + + req, err := http.NewRequest("POST", endpoint, bytes.NewReader(data)) if err != nil { return "", errors.Wrap(err, "failed to create HTTP request") } From 5e19f7a24a72ca5617761a80c3d730cb18228338 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 18 Sep 2024 03:48:34 +0530 Subject: [PATCH 099/319] Fix sc rest api call --- core/client/http.go | 136 ++++++++++++++++++++++++++++++++------------ 1 file changed, 100 insertions(+), 36 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index 050072db6..8f2bcb1d2 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -6,16 +6,19 @@ import ( "encoding/json" "fmt" "github.com/0chain/errors" + "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/util" "github.com/hashicorp/go-retryablehttp" "github.com/shopspring/decimal" "io" "io/ioutil" + "log" "math" "net" "net/http" "net/url" "os" + "sync" "time" ) @@ -75,18 +78,14 @@ func NewRetryableClient(retryMax int) *retryablehttp.Client { return client } -// MakeSCRestAPICall calls smart contract with provided address -// and makes retryable request to smart contract resource with provided relative path using params. -func MakeSCRestAPICall(scAddress, relativePath string, params map[string]string, restApiUrls ...string) ([]byte, error) { - var ( - resMaxCounterBody []byte - - hashMaxCounter int - hashCounters = make(map[string]int) - - sharders = extractSharders() - - lastErrMsg string +// MakeSCRestAPICall makes a rest api call to the sharders. +// - scAddress is the address of the smart contract +// - relativePath is the relative path of the api +// - params is the query parameters +// - handler is the handler function to handle the response +func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string, restApiUrls ...string) ([]byte, error) { + const ( + consensusThresh = float32(25.0) ) restApiUrl := ScRestApiUrl @@ -94,40 +93,105 @@ func MakeSCRestAPICall(scAddress, relativePath string, params map[string]string, restApiUrl = restApiUrls[0] } + sharders := nodeClient.Network().Sharders + responses := make(map[int]int) + mu := &sync.Mutex{} + entityResult := make(map[string][]byte) + var retObj []byte + maxCount := 0 + dominant := 200 + wg := sync.WaitGroup{} + + cfg, err := conf.GetClientConfig() + if err != nil { + return nil, err + } + for _, sharder := range sharders { - var ( - retryableClient = NewRetryableClient(5) - u = makeScURL(params, sharder, restApiUrl, scAddress, relativePath) - ) + wg.Add(1) + go func(sharder string) { + defer wg.Done() + urlString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath) + urlObj, err := url.Parse(urlString) + if err != nil { + log.Println(err) + return + } + q := urlObj.Query() + for k, v := range params { + q.Add(k, v) + } + urlObj.RawQuery = q.Encode() + client := &http.Client{Transport: DefaultTransport} + response, err := client.Get(urlObj.String()) + if err != nil { + nodeClient.sharders.Fail(sharder) + return + } + + defer response.Body.Close() + entityBytes, _ := ioutil.ReadAll(response.Body) + mu.Lock() + if response.StatusCode > http.StatusBadRequest { + nodeClient.sharders.Fail(sharder) + } else { + nodeClient.sharders.Success(sharder) + } + responses[response.StatusCode]++ + if responses[response.StatusCode] > maxCount { + maxCount = responses[response.StatusCode] + } + + if isCurrentDominantStatus(response.StatusCode, responses, maxCount) { + dominant = response.StatusCode + retObj = entityBytes + } + + entityResult[sharder] = entityBytes + nodeClient.sharders.Success(sharder) + mu.Unlock() + }(sharder) + } + wg.Wait() - resp, err := retryableClient.Get(u.String()) - if err != nil { - lastErrMsg = fmt.Sprintf("error while requesting sharders: %v", err) - continue - } - hash, resBody, err := hashAndBytesOfReader(resp.Body) - _ = resp.Body.Close() + rate := float32(maxCount*100) / float32(cfg.SharderConsensous) + if rate < consensusThresh { + err = errors.New("consensus_failed", "consensus failed on sharders") + } + + if dominant != 200 { + var objmap map[string]json.RawMessage + err := json.Unmarshal(retObj, &objmap) if err != nil { - lastErrMsg = fmt.Sprintf("error while reading response body: %v", err) - continue - } - if resp.StatusCode != http.StatusOK { - lastErrMsg = fmt.Sprintf("response status is not OK; response body: %s", string(resBody)) - continue + return nil, errors.New("", string(retObj)) } - hashCounters[hash]++ - if hashCounters[hash] > hashMaxCounter { - hashMaxCounter = hashCounters[hash] - resMaxCounterBody = resBody + var parsed string + err = json.Unmarshal(objmap["error"], &parsed) + if err != nil || parsed == "" { + return nil, errors.New("", string(retObj)) } + + return nil, errors.New("", parsed) } - if hashMaxCounter == 0 { - return nil, errors.New("request_sharders", "no valid responses, last err: "+lastErrMsg) + if rate > consensusThresh { + return retObj, nil } + return nil, err +} - return resMaxCounterBody, nil +// isCurrentDominantStatus determines whether the current response status is the dominant status among responses. +// +// The dominant status is where the response status is counted the most. +// On tie-breakers, 200 will be selected if included. +// +// Function assumes runningTotalPerStatus can be accessed safely concurrently. +func isCurrentDominantStatus(respStatus int, currentTotalPerStatus map[int]int, currentMax int) bool { + // mark status as dominant if + // - running total for status is the max and response is 200 or + // - running total for status is the max and count for 200 is lower + return currentTotalPerStatus[respStatus] == currentMax && (respStatus == 200 || currentTotalPerStatus[200] < currentMax) } // hashAndBytesOfReader computes hash of readers data and returns hash encoded to hex and bytes of reader data. From 52035865e7f8a5b506e6631941e7630f2e01d9b0 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Wed, 18 Sep 2024 11:57:41 +0530 Subject: [PATCH 100/319] remove hash signature map --- wasmsdk/jsbridge/webworker.go | 4 ++- zboxcore/sdk/allocation.go | 1 + zboxcore/sdk/chunked_upload.go | 2 +- zboxcore/sdk/chunked_upload_form_builder.go | 21 +++++++---- .../chunked_upload_form_builder_bench_test.go | 4 +-- zboxcore/sdk/chunked_upload_process.go | 2 +- zboxcore/sdk/chunked_upload_process_js.go | 7 ++-- zboxcore/sdk/commitworker.go | 35 ++++--------------- zboxcore/sdk/downloadworker.go | 11 ++++-- 9 files changed, 43 insertions(+), 44 deletions(-) diff --git a/wasmsdk/jsbridge/webworker.go b/wasmsdk/jsbridge/webworker.go index d889de621..3f9fb864b 100644 --- a/wasmsdk/jsbridge/webworker.go +++ b/wasmsdk/jsbridge/webworker.go @@ -80,7 +80,9 @@ func NewWasmWebWorker(blobberID, blobberURL, clientID, clientKey, peerPublicKey, "PUBLIC_KEY=" + publicKey, "IS_SPLIT=" + strconv.FormatBool(isSplit), "MNEMONIC=" + mnemonic, - "ZAUTH_SERVER=" + gZauthServer}, + "ZAUTH_SERVER=" + gZauthServer, + "BLOBBER_ID=" + blobberID, + }, Path: "zcn.wasm", subscribers: make(map[string]chan worker.MessageEvent), } diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 0c09d0183..a77a00c04 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -1419,6 +1419,7 @@ func (a *Allocation) processReadMarker(drs []*DownloadRequest) { now := time.Now() for _, dr := range drs { + dr.storageVersion = a.StorageVersion wg.Add(1) go func(dr *DownloadRequest) { defer wg.Done() diff --git a/zboxcore/sdk/chunked_upload.go b/zboxcore/sdk/chunked_upload.go index 29d31a8f3..65a53caf2 100644 --- a/zboxcore/sdk/chunked_upload.go +++ b/zboxcore/sdk/chunked_upload.go @@ -292,7 +292,7 @@ func CreateChunkedUpload( su.chunkReader = cReader - su.formBuilder = CreateChunkedUploadFormBuilder() + su.formBuilder = CreateChunkedUploadFormBuilder(su.allocationObj.StorageVersion) su.isRepair = isRepair uploadWorker, uploadRequest := calculateWorkersAndRequests(su.allocationObj.DataShards, len(su.blobbers), su.chunkNumber) diff --git a/zboxcore/sdk/chunked_upload_form_builder.go b/zboxcore/sdk/chunked_upload_form_builder.go index 1de228fa5..bd3800afc 100644 --- a/zboxcore/sdk/chunked_upload_form_builder.go +++ b/zboxcore/sdk/chunked_upload_form_builder.go @@ -4,10 +4,12 @@ import ( "bytes" "encoding/hex" "encoding/json" + "fmt" "io" "mime/multipart" "sync" + "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/zboxcore/client" "golang.org/x/crypto/sha3" @@ -17,7 +19,7 @@ import ( type ChunkedUploadFormBuilder interface { // build form data Build( - fileMeta *FileMeta, hasher Hasher, connectionID string, + fileMeta *FileMeta, hasher Hasher, connectionID, blobberID string, chunkSize int64, chunkStartIndex, chunkEndIndex int, isFinal bool, encryptedKey, encryptedKeyPoint string, fileChunksData [][]byte, thumbnailChunkData []byte, shardSize int64, @@ -35,17 +37,20 @@ type ChunkedUploadFormMetadata struct { } // CreateChunkedUploadFormBuilder create ChunkedUploadFormBuilder instance -func CreateChunkedUploadFormBuilder() ChunkedUploadFormBuilder { - return &chunkedUploadFormBuilder{} +func CreateChunkedUploadFormBuilder(storageVersion int) ChunkedUploadFormBuilder { + return &chunkedUploadFormBuilder{ + storageVersion, + } } type chunkedUploadFormBuilder struct { + storageVersion int } const MAX_BLOCKS = 80 // 5MB(CHUNK_SIZE*80) func (b *chunkedUploadFormBuilder) Build( - fileMeta *FileMeta, hasher Hasher, connectionID string, + fileMeta *FileMeta, hasher Hasher, connectionID, blobberID string, chunkSize int64, chunkStartIndex, chunkEndIndex int, isFinal bool, encryptedKey, encryptedKeyPoint string, fileChunksData [][]byte, thumbnailChunkData []byte, shardSize int64, @@ -163,8 +168,12 @@ func (b *chunkedUploadFormBuilder) Build( if err != nil { return res, err } - - validationRootSignature, err := client.Sign(actualHashSignature + formData.ValidationRoot) + hash := actualHashSignature + formData.ValidationRoot + if b.storageVersion == StorageV2 { + hashData := fmt.Sprintf("%s:%s:%s:%s", fileMeta.ActualHash, formData.ValidationRoot, formData.FixedMerkleRoot, blobberID) + hash = encryption.Hash(hashData) + } + validationRootSignature, err := client.Sign(hash) if err != nil { return res, err } diff --git a/zboxcore/sdk/chunked_upload_form_builder_bench_test.go b/zboxcore/sdk/chunked_upload_form_builder_bench_test.go index c021d6528..156910efe 100644 --- a/zboxcore/sdk/chunked_upload_form_builder_bench_test.go +++ b/zboxcore/sdk/chunked_upload_form_builder_bench_test.go @@ -53,7 +53,7 @@ func BenchmarkChunkedUploadFormBuilder(b *testing.B) { for i := 0; i < b.N; i++ { - builder := CreateChunkedUploadFormBuilder() + builder := CreateChunkedUploadFormBuilder(0) isFinal := false @@ -68,7 +68,7 @@ func BenchmarkChunkedUploadFormBuilder(b *testing.B) { fileBytes := buf[begin:end] - _, err := builder.Build(fileMeta, hasher, "connectionID", int64(bm.ChunkSize), chunkIndex, chunkIndex, isFinal, "", "", [][]byte{fileBytes}, nil, getShardSize(fileMeta.ActualSize, 1, false)) + _, err := builder.Build(fileMeta, hasher, "connectionID", "blobberID", int64(bm.ChunkSize), chunkIndex, chunkIndex, isFinal, "", "", [][]byte{fileBytes}, nil, getShardSize(fileMeta.ActualSize, 1, false)) if err != nil { b.Fatal(err) return diff --git a/zboxcore/sdk/chunked_upload_process.go b/zboxcore/sdk/chunked_upload_process.go index da58f19de..72a7560b7 100644 --- a/zboxcore/sdk/chunked_upload_process.go +++ b/zboxcore/sdk/chunked_upload_process.go @@ -106,7 +106,7 @@ func (su *ChunkedUpload) processUpload(chunkStartIndex, chunkEndIndex int, go func(b *ChunkedUploadBlobber, thumbnailChunkData []byte, pos uint64) { defer wg.Done() uploadData, err := su.formBuilder.Build( - &su.fileMeta, blobber.progress.Hasher, su.progress.ConnectionID, + &su.fileMeta, blobber.progress.Hasher, su.progress.ConnectionID, blobber.blobber.ID, su.chunkSize, chunkStartIndex, chunkEndIndex, isFinal, su.encryptedKey, su.progress.EncryptedKeyPoint, fileShards[pos], thumbnailChunkData, su.shardSize) if err != nil { diff --git a/zboxcore/sdk/chunked_upload_process_js.go b/zboxcore/sdk/chunked_upload_process_js.go index 092c4c66b..660de22ba 100644 --- a/zboxcore/sdk/chunked_upload_process_js.go +++ b/zboxcore/sdk/chunked_upload_process_js.go @@ -51,6 +51,7 @@ type ChunkedUploadFormInfo struct { AllocationID string AllocationTx string OnlyHash bool + StorageVersion int } // createUploadProgress create a new UploadProgress @@ -113,6 +114,7 @@ func (su *ChunkedUpload) processUpload(chunkStartIndex, chunkEndIndex int, AllocationID: su.allocationObj.ID, AllocationTx: su.allocationObj.Tx, OnlyHash: chunkEndIndex <= su.progress.ChunkIndex, + StorageVersion: su.allocationObj.StorageVersion, } formInfoJSON, err := json.Marshal(formInfo) if err != nil { @@ -447,8 +449,9 @@ func ProcessEventData(data safejs.Value) { if formInfo.IsFinal { defer delete(hasherMap, fileMeta.RemotePath) } - formBuilder := CreateChunkedUploadFormBuilder() - uploadData, err := formBuilder.Build(fileMeta, wp.hasher, formInfo.ConnectionID, formInfo.ChunkSize, formInfo.ChunkStartIndex, formInfo.ChunkEndIndex, formInfo.IsFinal, formInfo.EncryptedKey, formInfo.EncryptedKeyPoint, + blobberID := os.Getenv("BLOBBER_ID") + formBuilder := CreateChunkedUploadFormBuilder(formInfo.StorageVersion) + uploadData, err := formBuilder.Build(fileMeta, wp.hasher, formInfo.ConnectionID, blobberID, formInfo.ChunkSize, formInfo.ChunkStartIndex, formInfo.ChunkEndIndex, formInfo.IsFinal, formInfo.EncryptedKey, formInfo.EncryptedKeyPoint, fileShards, thumbnailChunkData, formInfo.ShardSize) if err != nil { selfPostMessage(false, false, err.Error(), remotePath, formInfo.ChunkEndIndex, nil) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index ce2b7468e..67ab4f687 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -521,25 +521,6 @@ func (commitReq *CommitRequestV2) processCommit() { func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, changeIndex uint64, blobber *blockchain.StorageNode) (err error) { now := time.Now() - hashSignatureMap := make(map[string]string, len(req.changes)) - for _, change := range req.changes { - if change == nil { - continue - } - hash := change.GetHash(changeIndex, blobber.ID) - if hash == "" { - return errors.New("hash_signature_failed", "Failed to add hash signature") - } - if hash == emptyHash { - continue - } - sig, err := client.Sign(hash) - if err != nil { - l.Logger.Error("Error signing hash", err) - return err - } - hashSignatureMap[change.GetLookupHash(changeIndex)] = sig - } elapsedSign := time.Since(now) hasher := sha256.New() var prevChainSize int64 @@ -578,13 +559,8 @@ func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, changeInd l.Logger.Error("Error marshalling writemarker data", err) return err } - hashSignatureData, err := json.Marshal(hashSignatureMap) - if err != nil { - l.Logger.Error("Error marshalling hash signature data", err) - return err - } - err = submitWriteMarker(wmData, hashSignatureData, blobber, req.connectionID, req.allocationObj.ID, req.allocationObj.Tx, req.allocationObj.StorageVersion) + err = submitWriteMarker(wmData, nil, blobber, req.connectionID, req.allocationObj.ID, req.allocationObj.Tx, req.allocationObj.StorageVersion) if err != nil { l.Logger.Error("Error submitting writemarker", err) return err @@ -607,10 +583,11 @@ func getFormWritter(connectionID string, wmData, fileIDMetaData []byte, body *by if err != nil { return nil, err } - - err = formWriter.WriteField("file_id_meta", string(fileIDMetaData)) - if err != nil { - return nil, err + if len(fileIDMetaData) > 0 { + err = formWriter.WriteField("file_id_meta", string(fileIDMetaData)) + if err != nil { + return nil, err + } } formWriter.Close() return formWriter, nil diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index c1a83374a..ef9f102cb 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -21,6 +21,7 @@ import ( "github.com/0chain/errors" "github.com/0chain/gosdk/core/common" + encrypt "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/zboxcore/blockchain" "github.com/0chain/gosdk/zboxcore/client" @@ -114,6 +115,7 @@ type DownloadRequest struct { downloadQueue downloadQueue // Always initialize this queue with max time taken isResume bool isEnterprise bool + storageVersion int } type downloadPriority struct { @@ -1209,11 +1211,16 @@ func (req *DownloadRequest) getFileMetaConsensus(fMetaResp []*fileMetaResponse) if selected.fileref.ActualFileHashSignature != fRef.ActualFileHashSignature { continue } - if !req.isEnterprise { + if !req.isEnterprise && req.shouldVerify { + hash := fRef.ActualFileHashSignature + fRef.ValidationRoot + if req.storageVersion == StorageV2 { + hashData := fmt.Sprintf("%s:%s:%s:%s", fRef.ActualFileHash, fRef.ValidationRoot, fRef.FixedMerkleRoot, req.blobbers[i].ID) + hash = encrypt.Hash(hashData) + } isValid, err := sys.VerifyWith( req.allocOwnerPubKey, fRef.ValidationRootSignature, - fRef.ActualFileHashSignature+fRef.ValidationRoot, + hash, ) if err != nil { l.Logger.Error(err, "allocOwnerPubKey: ", req.allocOwnerPubKey, " validationRootSignature: ", fRef.ValidationRootSignature, " actualFileHashSignature: ", fRef.ActualFileHashSignature, " validationRoot: ", fRef.ValidationRoot) From 88cd6905b15a2863b64c1fcb9689980978bd94f1 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Wed, 18 Sep 2024 12:04:09 +0530 Subject: [PATCH 101/319] remove sign timing --- zboxcore/sdk/commitworker.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 67ab4f687..0f769bc10 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -520,8 +520,6 @@ func (commitReq *CommitRequestV2) processCommit() { } func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, changeIndex uint64, blobber *blockchain.StorageNode) (err error) { - now := time.Now() - elapsedSign := time.Since(now) hasher := sha256.New() var prevChainSize int64 if blobber.LatestWM != nil { @@ -567,8 +565,6 @@ func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, changeInd } blobber.LatestWM = wm blobber.AllocationRoot = allocationRoot - elapsedSubmit := time.Since(now) - elapsedSign - l.Logger.Info("[commit] ", "elapsedSign: ", elapsedSign.Milliseconds(), " elapsedSubmit: ", elapsedSubmit.Milliseconds()) return } From 3cdf57b9ea87bebdcbdcb8b4924609cd768e4e5c Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Wed, 18 Sep 2024 14:01:01 +0530 Subject: [PATCH 102/319] check timestamp --- zboxcore/sdk/commitworker.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 0f769bc10..64219b32a 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -445,6 +445,10 @@ func (commitReq *CommitRequestV2) processCommit() { resp := <-respChan if resp.err == nil { trie = resp.trie + latestWM := commitReq.allocationObj.Blobbers[resp.pos].LatestWM + if commitReq.timestamp <= latestWM.Timestamp { + commitReq.timestamp = latestWM.Timestamp + 1 + } break } else if resp.err != errAlreadySuccessful { commitReq.commitMask = commitReq.commitMask.And(zboxutil.NewUint128(1).Lsh(resp.pos).Not()) From 13671a760882143f370f601649546df2fa177e76 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Wed, 18 Sep 2024 14:21:15 +0530 Subject: [PATCH 103/319] add check for lwm --- zboxcore/sdk/commitworker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 64219b32a..5558d56ba 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -446,7 +446,7 @@ func (commitReq *CommitRequestV2) processCommit() { if resp.err == nil { trie = resp.trie latestWM := commitReq.allocationObj.Blobbers[resp.pos].LatestWM - if commitReq.timestamp <= latestWM.Timestamp { + if latestWM != nil && commitReq.timestamp <= latestWM.Timestamp { commitReq.timestamp = latestWM.Timestamp + 1 } break From ae5837afc9f33320033c23e575890d667ae207e2 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 19 Sep 2024 00:19:13 +0530 Subject: [PATCH 104/319] Multi wallet --- core/client/http.go | 2 +- core/client/set.go | 51 +++++++++++++++++++------ core/transaction/entity.go | 4 +- mobilesdk/zboxapi/client.go | 4 +- winsdk/zboxapi.go | 2 +- zboxcore/allocationchange/updatefile.go | 2 +- zboxcore/marker/deletetoken.go | 4 +- zboxcore/marker/writemarker.go | 4 +- zboxcore/sdk/allocation_test.go | 2 +- zboxcore/sdk/blockdownloadworker.go | 4 +- zboxcore/sdk/chunked_upload_blobber.go | 4 +- zboxcore/sdk/commitworker.go | 2 +- zboxcore/sdk/downloadworker.go | 2 +- zboxcore/sdk/playlist.go | 6 +-- zboxcore/sdk/rollback.go | 2 +- zboxcore/sdk/sdk.go | 12 +++--- zboxcore/sdk/sharerequest.go | 4 +- zboxcore/zboxutil/http.go | 6 +-- zcnbridge/authorizers_query.go | 4 +- zcnbridge/bridge.go | 26 ++++++------- zcnbridge/bridge_test.go | 2 +- zcncore/get_data.go | 6 +-- 22 files changed, 91 insertions(+), 64 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index 8f2bcb1d2..88b142507 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -307,7 +307,7 @@ func GetBalance(clientIDs ...string) (*GetBalanceResponse, error) { if len(clientIDs) > 0 { clientID = clientIDs[0] } else { - clientID = ClientID() + clientID = Id() } if res, err = MakeSCRestAPICall("", GetBalance, map[string]string{ diff --git a/core/client/set.go b/core/client/set.go index fce37812b..6409f2bbd 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -13,11 +13,13 @@ var ( client Client ) -type SignFunc func(hash string) (string, error) +type SignFunc func(hash string, clientId ...string) (string, error) -// maintains client's information +// Client maintains client's information type Client struct { wallet *zcncrypto.Wallet + wallets map[string]*zcncrypto.Wallet + useMultiWallets bool signatureScheme string splitKeyWallet bool authUrl string @@ -30,10 +32,18 @@ func init() { sys.Sign = signHash client = Client{ wallet: &zcncrypto.Wallet{}, - sign: func(hash string) (string, error) { + sign: func(hash string, clientId ...string) (string, error) { + if len(clientId) > 0 { + w, ok := client.wallets[clientId[0]] + if !ok { + return "", errors.New("invalid client id") + } + return sys.Sign(hash, client.signatureScheme, GetClientSysKeys(w.ClientID)) + } return sys.Sign(hash, client.signatureScheme, GetClientSysKeys()) }, } + sys.Verify = verifySignature sys.VerifyWith = verifySignatureWith } @@ -77,9 +87,18 @@ func verifySignatureWith(pubKey, signature, hash string) (bool, error) { return sch.Verify(signature, hash) } -func GetClientSysKeys() []sys.KeyPair { +func GetClientSysKeys(clientId ...string) []sys.KeyPair { + wallet := client.wallet + if len(clientId) > 0 { + w, ok := client.wallets[clientId[0]] + if !ok { + return nil + } + wallet = w + } + var keys []sys.KeyPair - for _, kv := range client.wallet.Keys { + for _, kv := range wallet.Keys { keys = append(keys, sys.KeyPair{ PrivateKey: kv.PrivateKey, PublicKey: kv.PublicKey, @@ -91,9 +110,14 @@ func GetClientSysKeys() []sys.KeyPair { // SetWallet should be set before any transaction or client specific APIs func SetWallet(w zcncrypto.Wallet) { client.wallet = &w + + if client.wallets == nil { + client.wallets = make(map[string]*zcncrypto.Wallet) + } + client.wallets[w.ClientID] = &w } -// splitKeyWallet parameter is valid only if SignatureScheme is "BLS0Chain" +// SetSplitKeyWallet parameter is valid only if SignatureScheme is "BLS0Chain" func SetSplitKeyWallet(isSplitKeyWallet bool) error { if client.signatureScheme == constants.BLS0CHAIN.String() { client.splitKeyWallet = isSplitKeyWallet @@ -152,7 +176,14 @@ func TxnFee() uint64 { return client.txnFee } -func Sign(hash string) (string, error) { +func Sign(hash string, clientId ...string) (string, error) { + if len(clientId) > 0 { + w, ok := client.wallets[clientId[0]] + if !ok { + return "", errors.New("invalid client id") + } + return client.sign(hash, w.ClientID) + } return client.sign(hash) } @@ -175,14 +206,10 @@ func PrivateKey() string { return "" } -func ClientID() string { +func Id() string { return client.wallet.ClientID } -func GetWallet() *zcncrypto.Wallet { - return client.wallet -} - func GetClient() *zcncrypto.Wallet { return client.wallet } diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 5c5372dc0..2db3d8ede 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -182,7 +182,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) @@ -525,7 +525,7 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, return } - txn := NewTransactionEntity(client.ClientID(), + txn := NewTransactionEntity(client.Id(), cfg.ChainID, client.PublicKey(), nonce) txn.TransactionData = string(requestBytes) diff --git a/mobilesdk/zboxapi/client.go b/mobilesdk/zboxapi/client.go index 9dc4bd272..358fdc430 100644 --- a/mobilesdk/zboxapi/client.go +++ b/mobilesdk/zboxapi/client.go @@ -13,9 +13,9 @@ import ( "context" "errors" + "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" ) @@ -33,7 +33,7 @@ func Init(baseUrl, appType string) { 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)) } diff --git a/winsdk/zboxapi.go b/winsdk/zboxapi.go index 392001a81..b25bf4b80 100644 --- a/winsdk/zboxapi.go +++ b/winsdk/zboxapi.go @@ -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 diff --git a/zboxcore/allocationchange/updatefile.go b/zboxcore/allocationchange/updatefile.go index 0b4e46a83..e4399f0d4 100644 --- a/zboxcore/allocationchange/updatefile.go +++ b/zboxcore/allocationchange/updatefile.go @@ -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" ) diff --git a/zboxcore/marker/deletetoken.go b/zboxcore/marker/deletetoken.go index 37594c003..4b9a37dda 100644 --- a/zboxcore/marker/deletetoken.go +++ b/zboxcore/marker/deletetoken.go @@ -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 { @@ -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 } diff --git a/zboxcore/marker/writemarker.go b/zboxcore/marker/writemarker.go index 60e328c1b..55a4ad3b4 100644 --- a/zboxcore/marker/writemarker.go +++ b/zboxcore/marker/writemarker.go @@ -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 { @@ -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 } diff --git a/zboxcore/sdk/allocation_test.go b/zboxcore/sdk/allocation_test.go index f73a5a830..63d3fd620 100644 --- a/zboxcore/sdk/allocation_test.go +++ b/zboxcore/sdk/allocation_test.go @@ -517,7 +517,7 @@ func TestAllocation_isInitialized(t *testing.T) { // client := zclient.GetClient() // client.Wallet = &zcncrypto.Wallet{ -// ClientID: mockClientId, +// Id: mockClientId, // ClientKey: mockClientKey, // } diff --git a/zboxcore/sdk/blockdownloadworker.go b/zboxcore/sdk/blockdownloadworker.go index 0ebddceec..33b128cff 100644 --- a/zboxcore/sdk/blockdownloadworker.go +++ b/zboxcore/sdk/blockdownloadworker.go @@ -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)) } @@ -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 diff --git a/zboxcore/sdk/chunked_upload_blobber.go b/zboxcore/sdk/chunked_upload_blobber.go index 3a4190c20..903671fca 100644 --- a/zboxcore/sdk/chunked_upload_blobber.go +++ b/zboxcore/sdk/chunked_upload_blobber.go @@ -15,9 +15,9 @@ import ( "github.com/0chain/errors" thrown "github.com/0chain/errors" "github.com/0chain/gosdk/constants" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/marker" @@ -200,7 +200,7 @@ func (sb *ChunkedUploadBlobber) processCommit(ctx context.Context, su *ChunkedUp wm.BlobberID = sb.blobber.ID wm.Timestamp = timestamp - wm.ClientID = client.ClientID() + wm.ClientID = client.Id() err = wm.Sign() if err != nil { logger.Logger.Error("Signing writemarker failed: ", err) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 5b57a12ba..f86388574 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -247,7 +247,7 @@ func (req *CommitRequest) commitBlobber( wm.Size = size wm.BlobberID = req.blobber.ID wm.Timestamp = req.timestamp - wm.ClientID = client.ClientID() + wm.ClientID = client.Id() err = wm.Sign() if err != nil { l.Logger.Error("Signing writemarker failed: ", err) diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index b5f97e181..bee063522 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -827,7 +827,7 @@ func (req *DownloadRequest) attemptSubmitReadMarker(blobber *blockchain.StorageN lockBlobberReadCtr(req.allocationID, blobber.ID) defer unlockBlobberReadCtr(req.allocationID, blobber.ID) rm := &marker.ReadMarker{ - ClientID: client.ClientID(), + ClientID: client.Id(), ClientPublicKey: client.PublicKey(), BlobberID: blobber.ID, AllocationID: req.allocationID, diff --git a/zboxcore/sdk/playlist.go b/zboxcore/sdk/playlist.go index b6e6e9d25..8e1b424a4 100644 --- a/zboxcore/sdk/playlist.go +++ b/zboxcore/sdk/playlist.go @@ -7,10 +7,10 @@ import ( "net/url" "strings" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/resty" "github.com/0chain/gosdk/core/sys" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/zboxutil" @@ -65,7 +65,7 @@ func getPlaylistFromBlobbers(ctx context.Context, alloc *Allocation, query strin opts = append(opts, resty.WithRetry(resty.DefaultRetry)) opts = append(opts, resty.WithRequestInterceptor(func(req *http.Request) error { - req.Header.Set("X-App-Client-ID", client.ClientID()) + req.Header.Set("X-App-Client-ID", client.Id()) req.Header.Set("X-App-Client-Key", client.PublicKey()) hash := encryption.Hash(alloc.ID) @@ -146,7 +146,7 @@ func getPlaylistFileFromBlobbers(ctx context.Context, alloc *Allocation, query s opts = append(opts, resty.WithRetry(resty.DefaultRetry)) opts = append(opts, resty.WithRequestInterceptor(func(req *http.Request) error { - req.Header.Set("X-App-Client-ID", client.ClientID()) + req.Header.Set("X-App-Client-ID", client.Id()) req.Header.Set("X-App-Client-Key", client.PublicKey()) hash := encryption.Hash(alloc.ID) diff --git a/zboxcore/sdk/rollback.go b/zboxcore/sdk/rollback.go index b5812a7ea..8d9a2c791 100644 --- a/zboxcore/sdk/rollback.go +++ b/zboxcore/sdk/rollback.go @@ -124,7 +124,7 @@ func (rb *RollbackBlobber) processRollback(ctx context.Context, tx string) error wm.AllocationID = rb.lpm.LatestWM.AllocationID wm.Timestamp = rb.lpm.LatestWM.Timestamp wm.BlobberID = rb.lpm.LatestWM.BlobberID - wm.ClientID = client.ClientID() + wm.ClientID = client.Id() wm.Size = -rb.lpm.LatestWM.Size wm.ChainSize = wm.Size + rb.lpm.LatestWM.ChainSize diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index bac657b7e..8d790171a 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -179,7 +179,7 @@ func GetReadPoolInfo(clientID string) (info *ReadPool, err error) { } if clientID == "" { - clientID = client.ClientID() + clientID = client.Id() } var b []byte @@ -321,7 +321,7 @@ func GetStakePoolUserInfo(clientID string, offset, limit int) (info *StakePoolUs return nil, sdkNotInitialized } if clientID == "" { - clientID = client.ClientID() + clientID = client.Id() } var b []byte @@ -974,7 +974,7 @@ func SetNumBlockDownloads(num int) { // // returns the list of allocations and error if any func GetAllocations() ([]*Allocation, error) { - return GetAllocationsForClient(client.ClientID()) + return GetAllocationsForClient(client.Id()) } func getAllocationsInternal(clientID string, limit, offset int) ([]*Allocation, error) { @@ -1069,7 +1069,7 @@ type CreateAllocationOptions struct { func CreateAllocationWith(options CreateAllocationOptions) ( string, int64, *transaction.Transaction, error) { - return CreateAllocationForOwner(client.ClientID(), + return CreateAllocationForOwner(client.Id(), client.PublicKey(), options.DataShards, options.ParityShards, options.Size, options.ReadPrice, options.WritePrice, options.Lock, options.BlobberIds, options.BlobberAuthTickets, options.ThirdPartyExtendable, options.IsEnterprise, options.Force, options.FileOptionsParams) @@ -1385,7 +1385,7 @@ func UpdateAllocation( } updateAllocationRequest := make(map[string]interface{}) - updateAllocationRequest["owner_id"] = client.ClientID() + updateAllocationRequest["owner_id"] = client.Id() updateAllocationRequest["owner_public_key"] = "" updateAllocationRequest["id"] = allocationID updateAllocationRequest["size"] = size @@ -1760,7 +1760,7 @@ func GetUpdateAllocationMinLock( addBlobberId, removeBlobberId string) (int64, error) { updateAllocationRequest := make(map[string]interface{}) - updateAllocationRequest["owner_id"] = client.ClientID() + updateAllocationRequest["owner_id"] = client.Id() updateAllocationRequest["owner_public_key"] = "" updateAllocationRequest["id"] = allocationID updateAllocationRequest["size"] = size diff --git a/zboxcore/sdk/sharerequest.go b/zboxcore/sdk/sharerequest.go index 227870446..ad23e4df7 100644 --- a/zboxcore/sdk/sharerequest.go +++ b/zboxcore/sdk/sharerequest.go @@ -6,9 +6,9 @@ import ( "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/zboxcore/blockchain" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/zboxcore/encryption" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/marker" @@ -54,7 +54,7 @@ func (req *ShareRequest) getAuthTicket(clientID, encPublicKey string) (*marker.A at := &marker.AuthTicket{ AllocationID: req.allocationID, - OwnerID: client.ClientID(), + OwnerID: client.Id(), ClientID: clientID, FileName: req.remotefilename, FilePathHash: fileref.GetReferenceLookup(req.allocationID, req.remotefilepath), diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index 3f4759ba4..338f0f462 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -186,7 +186,7 @@ func NewHTTPRequest(method string, url string, data []byte) (*http.Request, cont } func setClientInfo(req *http.Request) { - req.Header.Set("X-App-Client-ID", client.ClientID()) + req.Header.Set("X-App-Client-ID", client.Id()) req.Header.Set("X-App-Client-Key", client.PublicKey()) } @@ -589,7 +589,7 @@ func NewFastUploadRequest(baseURL, allocationID string, allocationTx string, bod } func setFastClientInfoWithSign(req *fasthttp.Request, allocation string) error { - req.Header.Set("X-App-Client-ID", client.ClientID()) + req.Header.Set("X-App-Client-ID", client.Id()) req.Header.Set("X-App-Client-Key", client.PublicKey()) sign, err := client.Sign(encryption.Hash(allocation)) @@ -745,7 +745,7 @@ func NewFastDownloadRequest(baseUrl, allocationID, allocationTx string) (*fastht // } req := fasthttp.AcquireRequest() req.SetRequestURI(u.String()) - req.Header.Set("X-App-Client-ID", client.ClientID()) + req.Header.Set("X-App-Client-ID", client.Id()) req.Header.Set("X-App-Client-Key", client.PublicKey()) req.Header.Set(ALLOCATION_ID_HEADER, allocationID) diff --git a/zcnbridge/authorizers_query.go b/zcnbridge/authorizers_query.go index ea8aeacaf..5f2c5ecd8 100644 --- a/zcnbridge/authorizers_query.go +++ b/zcnbridge/authorizers_query.go @@ -120,7 +120,7 @@ func (b *BridgeClient) QueryEthereumBurnEvents(startNonce string) ([]*ethereum.B var ( totalWorkers = len(authorizers) values = map[string]string{ - "clientid": coreClient.ClientID(), + "clientid": coreClient.Id(), "ethereumaddress": b.EthereumAddress, "startnonce": startNonce, } @@ -179,7 +179,7 @@ func (b *BridgeClient) QueryZChainMintPayload(ethBurnHash string) (*zcnsc.MintPa totalWorkers = len(authorizers) values = map[string]string{ "hash": ethBurnHash, - "clientid": coreClient.ClientID(), + "clientid": coreClient.Id(), } ) diff --git a/zcnbridge/bridge.go b/zcnbridge/bridge.go index 4a803a9d9..8d1a3e70f 100644 --- a/zcnbridge/bridge.go +++ b/zcnbridge/bridge.go @@ -127,8 +127,8 @@ func (b *BridgeClient) AddEthereumAuthorizer(ctx context.Context, address common tran, err := instance.AddAuthorizers(transactOpts, address) if err != nil { - msg := "failed to execute AddAuthorizers transaction to ClientID = %s with amount = %s" - return nil, errors.Wrapf(err, msg, coreClient.ClientID(), address.String()) + msg := "failed to execute AddAuthorizers transaction to Id = %s with amount = %s" + return nil, errors.Wrapf(err, msg, coreClient.Id(), address.String()) } return tran, err @@ -145,8 +145,8 @@ func (b *BridgeClient) RemoveEthereumAuthorizer(ctx context.Context, address com tran, err := instance.RemoveAuthorizers(transactOpts, address) if err != nil { - msg := "failed to execute RemoveAuthorizers transaction to ClientID = %s with amount = %s" - return nil, errors.Wrapf(err, msg, coreClient.ClientID(), address.String()) + msg := "failed to execute RemoveAuthorizers transaction to Id = %s with amount = %s" + return nil, errors.Wrapf(err, msg, coreClient.Id(), address.String()) } return tran, err @@ -277,8 +277,8 @@ func (b *BridgeClient) NFTConfigSetUint256Raw(ctx context.Context, key common.Ha tran, err := instance.SetUint256(transactOpts, key, v) if err != nil { - msg := "failed to execute setUint256 transaction to ClientID = %s with key = %s, value = %v" - return nil, errors.Wrapf(err, msg, coreClient.ClientID(), key, v) + msg := "failed to execute setUint256 transaction to Id = %s with key = %s, value = %v" + return nil, errors.Wrapf(err, msg, coreClient.Id(), key, v) } return tran, err @@ -330,8 +330,8 @@ func (b *BridgeClient) NFTConfigSetAddress(ctx context.Context, key, address str tran, err := instance.SetAddress(transactOpts, kkey, addr) if err != nil { - msg := "failed to execute setAddress transaction to ClientID = %s with key = %s, value = %v" - return nil, errors.Wrapf(err, msg, coreClient.ClientID(), key, address) + msg := "failed to execute setAddress transaction to Id = %s with key = %s, value = %v" + return nil, errors.Wrapf(err, msg, coreClient.Id(), key, address) } return tran, err @@ -587,7 +587,7 @@ func (b *BridgeClient) BurnWZCN(ctx context.Context, amountTokens uint64) (*type } // 1. Data Parameter (amount to burn) - clientID := DefaultClientIDEncoder(coreClient.ClientID()) + clientID := DefaultClientIDEncoder(coreClient.Id()) // 2. Data Parameter (signature) amount := new(big.Int) @@ -605,13 +605,13 @@ func (b *BridgeClient) BurnWZCN(ctx context.Context, amountTokens uint64) (*type tran, err := bridgeInstance.Burn(transactOpts, amount, clientID) if err != nil { - msg := "failed to execute Burn WZCN transaction to ClientID = %s with amount = %s" - return nil, errors.Wrapf(err, msg, coreClient.ClientID(), amount) + msg := "failed to execute Burn WZCN transaction to Id = %s with amount = %s" + return nil, errors.Wrapf(err, msg, coreClient.Id(), amount) } Logger.Info( "Posted Burn WZCN", - zap.String("clientID", coreClient.ClientID()), + zap.String("clientID", coreClient.Id()), zap.Int64("amount", amount.Int64()), ) @@ -1047,7 +1047,7 @@ func (b *BridgeClient) EstimateBurnWZCNGasAmount(ctx context.Context, from, to, return 0, errors.Wrap(err, "failed to get ABI") } - clientID := DefaultClientIDEncoder(coreClient.ClientID()) + clientID := DefaultClientIDEncoder(coreClient.Id()) amount := new(big.Int) amount.SetString(amountTokens, 10) diff --git a/zcnbridge/bridge_test.go b/zcnbridge/bridge_test.go index 0d09846d6..f438048e2 100644 --- a/zcnbridge/bridge_test.go +++ b/zcnbridge/bridge_test.go @@ -375,7 +375,7 @@ func Test_ZCNBridge(t *testing.T) { rawAbi, err := binding.BridgeMetaData.GetAbi() require.NoError(t, err) - pack, err := rawAbi.Pack("burn", big.NewInt(amount), DefaultClientIDEncoder(coreClient.ClientID())) + pack, err := rawAbi.Pack("burn", big.NewInt(amount), DefaultClientIDEncoder(coreClient.Id())) require.NoError(t, err) require.True(t, ethereumClient.AssertCalled( diff --git a/zcncore/get_data.go b/zcncore/get_data.go index 1197f6ae0..f0c0719cb 100644 --- a/zcncore/get_data.go +++ b/zcncore/get_data.go @@ -191,7 +191,7 @@ func GetMintNonce() ([]byte, error) { } return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINT_NONCE, Params{ - "client_id": client.ClientID(), + "client_id": client.Id(), }) } @@ -251,7 +251,7 @@ func GetMinerSCUserInfo(clientID string) ([]byte, error) { return nil, err } if clientID == "" { - clientID = client.ClientID() + clientID = client.Id() } return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_USER, Params{ @@ -268,7 +268,7 @@ func GetMinerSCNodePool(id string) ([]byte, error) { return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_POOL, Params{ "id": id, - "pool_id": client.ClientID(), + "pool_id": client.Id(), }) } From 77d333f1b33308d40f7661c6af3fab754326a5e0 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 19 Sep 2024 02:25:29 +0530 Subject: [PATCH 105/319] Added multiwallet in http.go --- core/client/set.go | 42 ++++--- zboxcore/sdk/allocation.go | 11 +- zboxcore/sdk/chunked_upload_blobber.go | 6 +- zboxcore/sdk/chunked_upload_process_js.go | 12 +- zboxcore/sdk/commitworker.go | 7 +- zboxcore/sdk/common.go | 8 +- zboxcore/sdk/copyworker.go | 6 +- zboxcore/sdk/deleteworker.go | 6 +- zboxcore/sdk/dirworker.go | 2 +- zboxcore/sdk/downloadworker.go | 4 +- zboxcore/sdk/filemetaworker.go | 4 +- zboxcore/sdk/filerefsworker.go | 5 +- zboxcore/sdk/filestatsworker.go | 2 +- zboxcore/sdk/listworker.go | 3 +- zboxcore/sdk/moveworker.go | 5 +- zboxcore/sdk/multi_operation_worker.go | 3 +- zboxcore/sdk/reader.go | 1 + zboxcore/sdk/renameworker.go | 5 +- zboxcore/sdk/rollback.go | 10 +- zboxcore/sdk/sharerequest.go | 2 + zboxcore/sdk/writemarker_mutex.go | 4 +- zboxcore/zboxutil/http.go | 134 +++++++++++----------- 22 files changed, 159 insertions(+), 123 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index 6409f2bbd..3d0901eba 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -177,39 +177,53 @@ func TxnFee() uint64 { } func Sign(hash string, clientId ...string) (string, error) { - if len(clientId) > 0 { - w, ok := client.wallets[clientId[0]] - if !ok { - return "", errors.New("invalid client id") - } - return client.sign(hash, w.ClientID) - } - return client.sign(hash) + return client.sign(hash, clientId...) } -func IsWalletSet() bool { +func IsWalletSet(clientId ...string) bool { + if len(clientId) > 0 { + _, ok := client.wallets[clientId[0]] + return ok + } return client.wallet.ClientID != "" } -func PublicKey() string { +func PublicKey(clientId ...string) string { + if len(clientId) > 0 { + return client.wallets[clientId[0]].ClientKey + } return client.wallet.ClientKey } -func Mnemonic() string { +func Mnemonic(clientId ...string) string { + if len(clientId) > 0 { + return client.wallets[clientId[0]].Mnemonic + } return client.wallet.Mnemonic } -func PrivateKey() string { +func PrivateKey(clientId ...string) string { + if len(clientId) > 0 { + for _, kv := range client.wallets[clientId[0]].Keys { + return kv.PrivateKey + } + } for _, kv := range client.wallet.Keys { return kv.PrivateKey } return "" } -func Id() string { +func Id(clientId ...string) string { + if len(clientId) > 0 { + return clientId[0] + } return client.wallet.ClientID } -func GetClient() *zcncrypto.Wallet { +func GetClient(clientId ...string) *zcncrypto.Wallet { + if len(clientId) > 0 { + return client.wallets[clientId[0]] + } return client.wallet } diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 20de41bc9..ec0638d2a 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -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)) @@ -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)) @@ -1629,6 +1629,7 @@ func (a *Allocation) getRefs(path, pathHash, authToken, offsetPath, updatedDate, } oTreeReq := &ObjectTreeRequest{ + ClientId: a.Owner, allocationID: a.ID, allocationTx: a.Tx, sig: a.sig, @@ -1850,6 +1851,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, @@ -2179,7 +2181,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 } @@ -2272,6 +2274,7 @@ func (a *Allocation) GetAuthTicket(path, filename string, } shareReq := &ShareRequest{ + ClientId: a.Owner, expirationSeconds: expiration, allocationID: a.ID, allocationTx: a.Tx, @@ -2342,7 +2345,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 } diff --git a/zboxcore/sdk/chunked_upload_blobber.go b/zboxcore/sdk/chunked_upload_blobber.go index 903671fca..2dcf20160 100644 --- a/zboxcore/sdk/chunked_upload_blobber.go +++ b/zboxcore/sdk/chunked_upload_blobber.go @@ -79,7 +79,7 @@ func (sb *ChunkedUploadBlobber) sendUploadRequest( var req *fasthttp.Request for i := 0; i < 3; i++ { req, err = zboxutil.NewFastUploadRequest( - sb.blobber.Baseurl, su.allocationObj.ID, su.allocationObj.Tx, dataBuffers[ind].Bytes(), su.httpMethod) + sb.blobber.Baseurl, su.allocationObj.ID, su.allocationObj.Tx, dataBuffers[ind].Bytes(), su.httpMethod, su.allocationObj.Owner) if err != nil { return err } @@ -237,7 +237,7 @@ func (sb *ChunkedUploadBlobber) processCommit(ctx context.Context, su *ChunkedUp formWriter.Close() - req, err := zboxutil.NewCommitRequest(sb.blobber.Baseurl, su.allocationObj.ID, su.allocationObj.Tx, body) + req, err := zboxutil.NewCommitRequest(sb.blobber.Baseurl, su.allocationObj.ID, su.allocationObj.Tx, body, su.allocationObj.Owner) if err != nil { logger.Logger.Error("Error creating commit req: ", err) return err @@ -326,7 +326,7 @@ func (sb *ChunkedUploadBlobber) processWriteMarker( } var lR ReferencePathResult - req, err := zboxutil.NewReferencePathRequest(sb.blobber.Baseurl, su.allocationObj.ID, su.allocationObj.Tx, su.allocationObj.sig, paths) + req, err := zboxutil.NewReferencePathRequest(sb.blobber.Baseurl, su.allocationObj.ID, su.allocationObj.Tx, su.allocationObj.sig, paths, su.allocationObj.Owner) if err != nil || len(paths) == 0 { logger.Logger.Error("Creating ref path req", err) return nil, nil, 0, nil, err diff --git a/zboxcore/sdk/chunked_upload_process_js.go b/zboxcore/sdk/chunked_upload_process_js.go index ee7998f6b..be8d5095a 100644 --- a/zboxcore/sdk/chunked_upload_process_js.go +++ b/zboxcore/sdk/chunked_upload_process_js.go @@ -39,6 +39,7 @@ type workerProcess struct { } type ChunkedUploadFormInfo struct { + ClientId string ConnectionID string ChunkSize int64 ChunkStartIndex int @@ -101,6 +102,7 @@ func (su *ChunkedUpload) processUpload(chunkStartIndex, chunkEndIndex int, ) formInfo := ChunkedUploadFormInfo{ + ClientId: su.allocationObj.Owner, ConnectionID: su.progress.ConnectionID, ChunkSize: su.chunkSize, ChunkStartIndex: chunkStartIndex, @@ -473,13 +475,13 @@ func ProcessEventData(data safejs.Value) { } go func(blobberData BlobberData, remotePath string, wg *sync.WaitGroup) { if formInfo.IsFinal && len(blobberData.dataBuffers) > 1 { - err = sendUploadRequest(blobberData.dataBuffers[:len(blobberData.dataBuffers)-1], blobberData.contentSlice[:len(blobberData.contentSlice)-1], blobberURL, formInfo.AllocationID, formInfo.AllocationTx, formInfo.HttpMethod) + err = sendUploadRequest(blobberData.dataBuffers[:len(blobberData.dataBuffers)-1], blobberData.contentSlice[:len(blobberData.contentSlice)-1], blobberURL, formInfo.AllocationID, formInfo.AllocationTx, formInfo.HttpMethod, formInfo.ClientId) if err != nil { selfPostMessage(false, true, err.Error(), remotePath, formInfo.ChunkEndIndex, nil) return } wg.Wait() - err = sendUploadRequest(blobberData.dataBuffers[len(blobberData.dataBuffers)-1:], blobberData.contentSlice[len(blobberData.contentSlice)-1:], blobberURL, formInfo.AllocationID, formInfo.AllocationTx, formInfo.HttpMethod) + err = sendUploadRequest(blobberData.dataBuffers[len(blobberData.dataBuffers)-1:], blobberData.contentSlice[len(blobberData.contentSlice)-1:], blobberURL, formInfo.AllocationID, formInfo.AllocationTx, formInfo.HttpMethod, formInfo.ClientId) if err != nil { selfPostMessage(false, true, err.Error(), remotePath, formInfo.ChunkEndIndex, nil) return @@ -490,7 +492,7 @@ func ProcessEventData(data safejs.Value) { } else { defer wg.Done() } - err = sendUploadRequest(blobberData.dataBuffers, blobberData.contentSlice, blobberURL, formInfo.AllocationID, formInfo.AllocationTx, formInfo.HttpMethod) + err = sendUploadRequest(blobberData.dataBuffers, blobberData.contentSlice, blobberURL, formInfo.AllocationID, formInfo.AllocationTx, formInfo.HttpMethod, formInfo.ClientId) if err != nil { selfPostMessage(false, formInfo.IsFinal, err.Error(), remotePath, formInfo.ChunkEndIndex, nil) return @@ -603,7 +605,7 @@ func parseEventData(data safejs.Value) (*FileMeta, *ChunkedUploadFormInfo, [][]b return fileMeta, formInfo, fileShards, thumbnailChunkData, nil } -func sendUploadRequest(dataBuffers []*bytes.Buffer, contentSlice []string, blobberURL, allocationID, allocationTx, httpMethod string) (err error) { +func sendUploadRequest(dataBuffers []*bytes.Buffer, contentSlice []string, blobberURL, allocationID, allocationTx, httpMethod string, clientId ...string) (err error) { eg, _ := errgroup.WithContext(context.TODO()) for dataInd := 0; dataInd < len(dataBuffers); dataInd++ { ind := dataInd @@ -614,7 +616,7 @@ func sendUploadRequest(dataBuffers []*bytes.Buffer, contentSlice []string, blobb var req *fasthttp.Request for i := 0; i < 3; i++ { req, err = zboxutil.NewFastUploadRequest( - blobberURL, allocationID, allocationTx, dataBuffers[ind].Bytes(), httpMethod) + blobberURL, allocationID, allocationTx, dataBuffers[ind].Bytes(), httpMethod, clientId...) if err != nil { return err } diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index f86388574..4865b26dd 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -52,6 +52,7 @@ func SuccessCommitResult() *CommitResult { const MARKER_VERSION = "v2" type CommitRequest struct { + ClientId string changes []allocationchange.AllocationChange blobber *blockchain.StorageNode allocationID string @@ -112,7 +113,7 @@ func (commitreq *CommitRequest) processCommit() { } var req *http.Request var lR ReferencePathResult - req, err := zboxutil.NewReferencePathRequest(commitreq.blobber.Baseurl, commitreq.allocationID, commitreq.allocationTx, commitreq.sig, paths) + req, err := zboxutil.NewReferencePathRequest(commitreq.blobber.Baseurl, commitreq.allocationID, commitreq.allocationTx, commitreq.sig, paths, commitreq.ClientId) if err != nil { l.Logger.Error("Creating ref path req", err) return @@ -272,7 +273,7 @@ func (req *CommitRequest) commitBlobber( l.Logger.Error("Creating form writer failed: ", err) return } - httpreq, err := zboxutil.NewCommitRequest(req.blobber.Baseurl, req.allocationID, req.allocationTx, body) + httpreq, err := zboxutil.NewCommitRequest(req.blobber.Baseurl, req.allocationID, req.allocationTx, body, req.ClientId) if err != nil { l.Logger.Error("Error creating commit req: ", err) return @@ -352,7 +353,7 @@ func AddCommitRequest(req *CommitRequest) { func (commitreq *CommitRequest) calculateHashRequest(ctx context.Context, paths []string) error { //nolint var req *http.Request - req, err := zboxutil.NewCalculateHashRequest(commitreq.blobber.Baseurl, commitreq.allocationID, commitreq.allocationTx, paths) + req, err := zboxutil.NewCalculateHashRequest(commitreq.blobber.Baseurl, commitreq.allocationID, commitreq.allocationTx, paths, commitreq.ClientId) if err != nil || len(paths) == 0 { l.Logger.Error("Creating calculate hash req", err) return err diff --git a/zboxcore/sdk/common.go b/zboxcore/sdk/common.go index 61eb45a00..5aaf2faf5 100644 --- a/zboxcore/sdk/common.go +++ b/zboxcore/sdk/common.go @@ -19,8 +19,8 @@ import ( "github.com/0chain/gosdk/zboxcore/zboxutil" ) -func getObjectTreeFromBlobber(ctx context.Context, allocationID, allocationTx, sig string, remoteFilePath string, blobber *blockchain.StorageNode) (fileref.RefEntity, error) { - httpreq, err := zboxutil.NewObjectTreeRequest(blobber.Baseurl, allocationID, allocationTx, sig, remoteFilePath) +func getObjectTreeFromBlobber(ctx context.Context, allocationID, allocationTx, sig string, remoteFilePath string, blobber *blockchain.StorageNode, clientId ...string) (fileref.RefEntity, error) { + httpreq, err := zboxutil.NewObjectTreeRequest(blobber.Baseurl, allocationID, allocationTx, sig, remoteFilePath, clientId...) if err != nil { l.Logger.Error(blobber.Baseurl, "Error creating object tree request", err) return nil, err @@ -62,9 +62,9 @@ func getObjectTreeFromBlobber(ctx context.Context, allocationID, allocationTx, s return lR.GetRefFromObjectTree(allocationID) } -func getAllocationDataFromBlobber(blobber *blockchain.StorageNode, allocationId string, allocationTx string, respCh chan<- *BlobberAllocationStats, wg *sync.WaitGroup) { +func getAllocationDataFromBlobber(blobber *blockchain.StorageNode, allocationId string, allocationTx string, respCh chan<- *BlobberAllocationStats, wg *sync.WaitGroup, clientId ...string) { defer wg.Done() - httpreq, err := zboxutil.NewAllocationRequest(blobber.Baseurl, allocationId, allocationTx) + httpreq, err := zboxutil.NewAllocationRequest(blobber.Baseurl, allocationId, allocationTx, clientId...) if err != nil { l.Logger.Error(blobber.Baseurl, "Error creating allocation request", err) return diff --git a/zboxcore/sdk/copyworker.go b/zboxcore/sdk/copyworker.go index 0b5e21b0b..a80c6764f 100644 --- a/zboxcore/sdk/copyworker.go +++ b/zboxcore/sdk/copyworker.go @@ -44,7 +44,7 @@ type CopyRequest struct { } func (req *CopyRequest) getObjectTreeFromBlobber(blobber *blockchain.StorageNode) (fileref.RefEntity, error) { - return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.sig, req.remotefilepath, blobber) + return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.sig, req.remotefilepath, blobber, req.allocationObj.Owner) } func (req *CopyRequest) copyBlobberObject( @@ -99,7 +99,7 @@ func (req *CopyRequest) copyBlobberObject( cncl context.CancelFunc ) - httpreq, err = zboxutil.NewCopyRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body) + httpreq, err = zboxutil.NewCopyRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body, req.allocationObj.Owner) if err != nil { l.Logger.Error(blobber.Baseurl, "Error creating rename request", err) return @@ -252,10 +252,12 @@ func (req *CopyRequest) ProcessCopy() error { Uuid: uid, ObjectTree: objectTreeRefs[pos], } + newChange.NumBlocks = 0 newChange.Operation = constants.FileOperationCopy newChange.Size = 0 commitReq := &CommitRequest{ + ClientId: req.allocationObj.Owner, allocationID: req.allocationID, allocationTx: req.allocationTx, sig: req.sig, diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index 601ec2137..21de9ff8d 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -61,7 +61,7 @@ func (req *DeleteRequest) deleteBlobberFile( query.Add("connection_id", req.connectionID) query.Add("path", req.remotefilepath) - httpreq, err := zboxutil.NewDeleteRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, query) + httpreq, err := zboxutil.NewDeleteRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, query, req.allocationObj.Owner) if err != nil { l.Logger.Error(blobber.Baseurl, "Error creating delete request", err) return err @@ -154,7 +154,7 @@ func (req *DeleteRequest) getObjectTreeFromBlobber(pos uint64) ( fRefEntity, err = getObjectTreeFromBlobber( req.ctx, req.allocationID, req.allocationTx, req.sig, - req.remotefilepath, req.blobbers[pos]) + req.remotefilepath, req.blobbers[pos], req.allocationObj.Owner) return } @@ -167,6 +167,7 @@ func (req *DeleteRequest) getFileMetaFromBlobber(pos uint64) (fileRef *fileref.F } }() listReq := &ListRequest{ + ClientId: req.allocationObj.Owner, allocationID: req.allocationID, allocationTx: req.allocationTx, blobbers: req.blobbers, @@ -286,6 +287,7 @@ func (req *DeleteRequest) ProcessDelete() (err error) { newChange.Operation = constants.FileOperationDelete newChange.Size = newChange.FileMetaRef.GetSize() commitReq := &CommitRequest{ + ClientId: req.allocationObj.Owner, allocationID: req.allocationID, allocationTx: req.allocationTx, sig: req.sig, diff --git a/zboxcore/sdk/dirworker.go b/zboxcore/sdk/dirworker.go index 80aea731a..84a80a53b 100644 --- a/zboxcore/sdk/dirworker.go +++ b/zboxcore/sdk/dirworker.go @@ -186,7 +186,7 @@ func (req *DirRequest) createDirInBlobber(blobber *blockchain.StorageNode, pos u } formWriter.Close() - httpreq, err := zboxutil.NewCreateDirRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body) + httpreq, err := zboxutil.NewCreateDirRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body, req.allocationObj.Owner) if err != nil { l.Logger.Error(blobber.Baseurl, "Error creating dir request", err) return err, false diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index bee063522..9500f358a 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -67,6 +67,7 @@ func WithFileCallback(cb func()) DownloadRequestOption { } type DownloadRequest struct { + ClientId string allocationID string allocationTx string sig string @@ -845,7 +846,7 @@ func (req *DownloadRequest) attemptSubmitReadMarker(blobber *blockchain.StorageN if err != nil { return fmt.Errorf("error marshaling read marker: %w", err) } - httpreq, err := zboxutil.NewRedeemRequest(blobber.Baseurl, req.allocationID, req.allocationTx) + httpreq, err := zboxutil.NewRedeemRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.allocOwnerID) if err != nil { return fmt.Errorf("error creating download request: %w", err) } @@ -1117,6 +1118,7 @@ func GetFileRefFromBlobber(allocationID, blobberId, remotePath string) (fRef *fi func (req *DownloadRequest) getFileRef() (fRef *fileref.FileRef, err error) { listReq := &ListRequest{ + ClientId: req.ClientId, remotefilepath: req.remotefilepath, remotefilepathhash: req.remotefilepathhash, allocationID: req.allocationID, diff --git a/zboxcore/sdk/filemetaworker.go b/zboxcore/sdk/filemetaworker.go index 2a8595e2b..54975705a 100644 --- a/zboxcore/sdk/filemetaworker.go +++ b/zboxcore/sdk/filemetaworker.go @@ -76,7 +76,7 @@ func (req *ListRequest) getFileMetaInfoFromBlobber(blobber *blockchain.StorageNo } formWriter.Close() - httpreq, err := zboxutil.NewFileMetaRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body) + httpreq, err := zboxutil.NewFileMetaRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body, req.ClientId) if err != nil { l.Logger.Error("File meta info request error: ", err.Error()) return @@ -141,7 +141,7 @@ func (req *ListRequest) getFileMetaByNameInfoFromBlobber(blobber *blockchain.Sto } } formWriter.Close() - httpreq, err := zboxutil.NewFileMetaRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body) + httpreq, err := zboxutil.NewFileMetaRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body, req.ClientId) if err != nil { l.Logger.Error("File meta info request error: ", err.Error()) return diff --git a/zboxcore/sdk/filerefsworker.go b/zboxcore/sdk/filerefsworker.go index 09d8a1b98..49c10bbf7 100644 --- a/zboxcore/sdk/filerefsworker.go +++ b/zboxcore/sdk/filerefsworker.go @@ -30,6 +30,7 @@ type ObjectTreeResult struct { const INVALID_PATH = "invalid_path" type ObjectTreeRequest struct { + ClientId string allocationID string allocationTx string sig string @@ -160,6 +161,7 @@ func (o *ObjectTreeRequest) getFileRefs(bUrl string, respChan chan *oTreeRespons o.refType, o.level, o.pageLimit, + o.ClientId, ) if err != nil { oTR.err = err @@ -235,6 +237,7 @@ type SimilarField struct { type RecentlyAddedRefRequest struct { ctx context.Context + ClientId string allocationID string allocationTx string sig string @@ -314,7 +317,7 @@ func (r *RecentlyAddedRefRequest) GetRecentlyAddedRefs() (*RecentlyAddedRefResul func (r *RecentlyAddedRefRequest) getRecentlyAddedRefs(resp *RecentlyAddedRefResponse, bUrl string) { defer r.wg.Done() - req, err := zboxutil.NewRecentlyAddedRefsRequest(bUrl, r.allocationID, r.allocationTx, r.sig, r.fromDate, r.offset, r.pageLimit) + req, err := zboxutil.NewRecentlyAddedRefsRequest(bUrl, r.allocationID, r.allocationTx, r.sig, r.fromDate, r.offset, r.pageLimit, r.ClientId) if err != nil { resp.err = err return diff --git a/zboxcore/sdk/filestatsworker.go b/zboxcore/sdk/filestatsworker.go index 568d1cf49..2ae4c4b2b 100644 --- a/zboxcore/sdk/filestatsworker.go +++ b/zboxcore/sdk/filestatsworker.go @@ -70,7 +70,7 @@ func (req *ListRequest) getFileStatsInfoFromBlobber(blobber *blockchain.StorageN } formWriter.Close() - httpreq, err := zboxutil.NewFileStatsRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body) + httpreq, err := zboxutil.NewFileStatsRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body, req.ClientId) if err != nil { l.Logger.Error("File meta info request error: ", err.Error()) return diff --git a/zboxcore/sdk/listworker.go b/zboxcore/sdk/listworker.go index 220974a7e..454fe770c 100644 --- a/zboxcore/sdk/listworker.go +++ b/zboxcore/sdk/listworker.go @@ -23,6 +23,7 @@ import ( const CHUNK_SIZE = 64 * 1024 type ListRequest struct { + ClientId string allocationID string allocationTx string sig string @@ -125,7 +126,7 @@ func (req *ListRequest) getListInfoFromBlobber(blobber *blockchain.StorageNode, if req.forRepair { req.listOnly = true } - httpreq, err := zboxutil.NewListRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.remotefilepath, req.remotefilepathhash, string(authTokenBytes), req.listOnly, req.offset, req.pageLimit) + httpreq, err := zboxutil.NewListRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.remotefilepath, req.remotefilepathhash, string(authTokenBytes), req.listOnly, req.offset, req.pageLimit, req.ClientId) if err != nil { l.Logger.Error("List info request error: ", err.Error()) return diff --git a/zboxcore/sdk/moveworker.go b/zboxcore/sdk/moveworker.go index a70c814ce..541979ab8 100644 --- a/zboxcore/sdk/moveworker.go +++ b/zboxcore/sdk/moveworker.go @@ -44,7 +44,7 @@ type MoveRequest struct { } func (req *MoveRequest) getObjectTreeFromBlobber(blobber *blockchain.StorageNode) (fileref.RefEntity, error) { - return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.sig, req.remotefilepath, blobber) + return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.sig, req.remotefilepath, blobber, req.allocationObj.Owner) } func (req *MoveRequest) moveBlobberObject( @@ -96,7 +96,7 @@ func (req *MoveRequest) moveBlobberObject( cncl context.CancelFunc ) - httpreq, err = zboxutil.NewMoveRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body) + httpreq, err = zboxutil.NewMoveRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body, req.allocationObj.Owner) if err != nil { l.Logger.Error(blobber.Baseurl, "Error creating rename request", err) return @@ -250,6 +250,7 @@ func (req *MoveRequest) ProcessMove() error { moveChange.Operation = constants.FileOperationMove moveChange.Size = 0 commitReq := &CommitRequest{ + ClientId: req.allocationObj.Owner, allocationID: req.allocationID, allocationTx: req.allocationTx, sig: req.sig, diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 115cb8482..95f581ae1 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -91,7 +91,7 @@ func (mo *MultiOperation) createConnectionObj(blobberIdx int) (err error) { formWriter.Close() var httpreq *http.Request - httpreq, err = zboxutil.NewConnectionRequest(blobber.Baseurl, mo.allocationObj.ID, mo.allocationObj.Tx, mo.allocationObj.sig, body) + httpreq, err = zboxutil.NewConnectionRequest(blobber.Baseurl, mo.allocationObj.ID, mo.allocationObj.Tx, mo.allocationObj.sig, body, mo.allocationObj.Owner) if err != nil { l.Logger.Error(blobber.Baseurl, "Error creating new connection request", err) return @@ -284,6 +284,7 @@ func (mo *MultiOperation) Process() error { for i := mo.operationMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { pos = uint64(i.TrailingZeros()) commitReq := &CommitRequest{ + ClientId: mo.allocationObj.Owner, allocationID: mo.allocationObj.ID, allocationTx: mo.allocationObj.Tx, sig: mo.allocationObj.sig, diff --git a/zboxcore/sdk/reader.go b/zboxcore/sdk/reader.go index ca380c00f..aacb06cdd 100644 --- a/zboxcore/sdk/reader.go +++ b/zboxcore/sdk/reader.go @@ -168,6 +168,7 @@ func GetDStorageFileReader(alloc *Allocation, ref *ORef, sdo *StreamDownloadOpti sd := &StreamDownload{ DownloadRequest: &DownloadRequest{ + ClientId: alloc.Owner, allocationID: alloc.ID, allocationTx: alloc.Tx, allocOwnerID: alloc.Owner, diff --git a/zboxcore/sdk/renameworker.go b/zboxcore/sdk/renameworker.go index 3260104fa..caa38a28f 100644 --- a/zboxcore/sdk/renameworker.go +++ b/zboxcore/sdk/renameworker.go @@ -44,7 +44,7 @@ type RenameRequest struct { } func (req *RenameRequest) getObjectTreeFromBlobber(blobber *blockchain.StorageNode) (fileref.RefEntity, error) { - return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.sig, req.remotefilepath, blobber) + return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.sig, req.remotefilepath, blobber, req.allocationObj.Owner) } func (req *RenameRequest) renameBlobberObject( @@ -93,7 +93,7 @@ func (req *RenameRequest) renameBlobberObject( formWriter.Close() var httpreq *http.Request - httpreq, err = zboxutil.NewRenameRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body) + httpreq, err = zboxutil.NewRenameRequest(blobber.Baseurl, req.allocationID, req.allocationTx, req.sig, body, req.allocationObj.Owner) if err != nil { l.Logger.Error(blobber.Baseurl, "Error creating rename request", err) return @@ -250,6 +250,7 @@ func (req *RenameRequest) ProcessRename() error { newChange.Size = 0 commitReq := &CommitRequest{ + ClientId: req.allocationObj.Owner, allocationID: req.allocationID, allocationTx: req.allocationTx, sig: req.sig, diff --git a/zboxcore/sdk/rollback.go b/zboxcore/sdk/rollback.go index 8d9a2c791..4e60ac77b 100644 --- a/zboxcore/sdk/rollback.go +++ b/zboxcore/sdk/rollback.go @@ -60,11 +60,11 @@ type BlobberStatus struct { Status string } -func GetWritemarker(allocID, allocTx, sig, id, baseUrl string) (*LatestPrevWriteMarker, error) { +func GetWritemarker(allocID, allocTx, sig, id, baseUrl string, clientId ...string) (*LatestPrevWriteMarker, error) { var lpm LatestPrevWriteMarker - req, err := zboxutil.NewWritemarkerRequest(baseUrl, allocID, allocTx, sig) + req, err := zboxutil.NewWritemarkerRequest(baseUrl, allocID, allocTx, sig, clientId...) if err != nil { return nil, err } @@ -164,7 +164,7 @@ func (rb *RollbackBlobber) processRollback(ctx context.Context, tx string) error formWriter.WriteField("connection_id", connID) formWriter.Close() - req, err := zboxutil.NewRollbackRequest(rb.blobber.Baseurl, wm.AllocationID, tx, body) + req, err := zboxutil.NewRollbackRequest(rb.blobber.Baseurl, wm.AllocationID, tx, body, wm.ClientID) if err != nil { l.Logger.Error("Creating rollback request failed: ", err) return err @@ -270,7 +270,7 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { ID: blobber.ID, Status: "available", } - 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) markerError = err @@ -397,7 +397,7 @@ func (a *Allocation) RollbackWithMask(mask zboxutil.Uint128) { 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 { l.Logger.Error("error during getWritemarker", zap.Error(err)) } diff --git a/zboxcore/sdk/sharerequest.go b/zboxcore/sdk/sharerequest.go index ad23e4df7..b362d6532 100644 --- a/zboxcore/sdk/sharerequest.go +++ b/zboxcore/sdk/sharerequest.go @@ -15,6 +15,7 @@ import ( ) type ShareRequest struct { + ClientId string allocationID string allocationTx string sig string @@ -31,6 +32,7 @@ func (req *ShareRequest) GetFileRef() (*fileref.FileRef, error) { var fileRef *fileref.FileRef listReq := &ListRequest{ + ClientId: req.ClientId, remotefilepathhash: filePathHash, allocationID: req.allocationID, allocationTx: req.allocationTx, diff --git a/zboxcore/sdk/writemarker_mutex.go b/zboxcore/sdk/writemarker_mutex.go index 501f1f1a6..b0458f94c 100644 --- a/zboxcore/sdk/writemarker_mutex.go +++ b/zboxcore/sdk/writemarker_mutex.go @@ -100,7 +100,7 @@ func (wmMu *WriteMarkerMutex) UnlockBlobber( var req *http.Request req, err = zboxutil.NewWriteMarkerUnLockRequest( - b.Baseurl, wmMu.allocationObj.ID, wmMu.allocationObj.Tx, wmMu.allocationObj.sig, connID, "") + b.Baseurl, wmMu.allocationObj.ID, wmMu.allocationObj.Tx, wmMu.allocationObj.sig, connID, "", wmMu.allocationObj.Owner) if err != nil { return } @@ -281,7 +281,7 @@ func (wmMu *WriteMarkerMutex) lockBlobber( var req *http.Request req, err = zboxutil.NewWriteMarkerLockRequest( - b.Baseurl, wmMu.allocationObj.ID, wmMu.allocationObj.Tx, wmMu.allocationObj.sig, connID) + b.Baseurl, wmMu.allocationObj.ID, wmMu.allocationObj.Tx, wmMu.allocationObj.sig, connID, wmMu.allocationObj.Owner) if err != nil { return } diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index 338f0f462..bfefb6a0e 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -185,17 +185,17 @@ func NewHTTPRequest(method string, url string, data []byte) (*http.Request, cont return req, ctx, cncl, err } -func setClientInfo(req *http.Request) { - req.Header.Set("X-App-Client-ID", client.Id()) - req.Header.Set("X-App-Client-Key", client.PublicKey()) +func setClientInfo(req *http.Request, clientId ...string) { + req.Header.Set("X-App-Client-ID", client.Id(clientId...)) + req.Header.Set("X-App-Client-Key", client.PublicKey(clientId...)) } -func setClientInfoWithSign(req *http.Request, sig, allocation, baseURL string) error { - setClientInfo(req) +func setClientInfoWithSign(req *http.Request, sig, allocation, baseURL string, clientId ...string) error { + setClientInfo(req, clientId...) req.Header.Set(CLIENT_SIGNATURE_HEADER, sig) hashData := allocation + baseURL - sig2, err := client.Sign(encryption.Hash(hashData)) + sig2, err := client.Sign(encryption.Hash(hashData), clientId...) if err != nil { return err } @@ -203,7 +203,7 @@ func setClientInfoWithSign(req *http.Request, sig, allocation, baseURL string) e return nil } -func NewCommitRequest(baseUrl, allocationID string, allocationTx string, body io.Reader) (*http.Request, error) { +func NewCommitRequest(baseUrl, allocationID string, allocationTx string, body io.Reader, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, COMMIT_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -213,14 +213,14 @@ func NewCommitRequest(baseUrl, allocationID string, allocationTx string, body io if err != nil { return nil, err } - setClientInfo(req) + setClientInfo(req, clientId...) req.Header.Set(ALLOCATION_ID_HEADER, allocationID) return req, nil } -func NewReferencePathRequest(baseUrl, allocationID string, allocationTx string, sig string, paths []string) (*http.Request, error) { +func NewReferencePathRequest(baseUrl, allocationID string, allocationTx string, sig string, paths []string, clientId ...string) (*http.Request, error) { nurl, err := joinUrl(baseUrl, REFERENCE_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -240,7 +240,7 @@ func NewReferencePathRequest(baseUrl, allocationID string, allocationTx string, return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -249,7 +249,7 @@ func NewReferencePathRequest(baseUrl, allocationID string, allocationTx string, return req, nil } -func NewCalculateHashRequest(baseUrl, allocationID string, allocationTx string, paths []string) (*http.Request, error) { +func NewCalculateHashRequest(baseUrl, allocationID string, allocationTx string, paths []string, clientId ...string) (*http.Request, error) { nurl, err := joinUrl(baseUrl, CALCULATE_HASH_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -265,14 +265,14 @@ func NewCalculateHashRequest(baseUrl, allocationID string, allocationTx string, if err != nil { return nil, err } - setClientInfo(req) + setClientInfo(req, clientId...) req.Header.Set(ALLOCATION_ID_HEADER, allocationID) return req, nil } -func NewObjectTreeRequest(baseUrl, allocationID string, allocationTx string, sig string, path string) (*http.Request, error) { +func NewObjectTreeRequest(baseUrl, allocationID string, allocationTx string, sig string, path string, clientId ...string) (*http.Request, error) { nurl, err := joinUrl(baseUrl, OBJECT_TREE_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -286,7 +286,7 @@ func NewObjectTreeRequest(baseUrl, allocationID string, allocationTx string, sig return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -295,7 +295,7 @@ func NewObjectTreeRequest(baseUrl, allocationID string, allocationTx string, sig return req, nil } -func NewRefsRequest(baseUrl, allocationID, sig, allocationTx, path, pathHash, authToken, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int) (*http.Request, error) { +func NewRefsRequest(baseUrl, allocationID, sig, allocationTx, path, pathHash, authToken, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int, clientId ...string) (*http.Request, error) { nUrl, err := joinUrl(baseUrl, REFS_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -319,14 +319,14 @@ func NewRefsRequest(baseUrl, allocationID, sig, allocationTx, path, pathHash, au req.Header.Set(ALLOCATION_ID_HEADER, allocationID) - if err := setClientInfoWithSign(req, sig, allocationID, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationID, baseUrl, clientId...); err != nil { return nil, err } return req, nil } -func NewRecentlyAddedRefsRequest(bUrl, allocID, allocTx, sig string, fromDate, offset int64, pageLimit int) (*http.Request, error) { +func NewRecentlyAddedRefsRequest(bUrl, allocID, allocTx, sig string, fromDate, offset int64, pageLimit int, clientId ...string) (*http.Request, error) { nUrl, err := joinUrl(bUrl, RECENT_REFS_ENDPOINT, allocID) if err != nil { return nil, err @@ -345,14 +345,14 @@ func NewRecentlyAddedRefsRequest(bUrl, allocID, allocTx, sig string, fromDate, o req.Header.Set(ALLOCATION_ID_HEADER, allocID) - if err = setClientInfoWithSign(req, sig, allocTx, bUrl); err != nil { + if err = setClientInfoWithSign(req, sig, allocTx, bUrl, clientId...); err != nil { return nil, err } return req, nil } -func NewAllocationRequest(baseUrl, allocationID, allocationTx string) (*http.Request, error) { +func NewAllocationRequest(baseUrl, allocationID, allocationTx string, clientId ...string) (*http.Request, error) { nurl, err := joinUrl(baseUrl, ALLOCATION_ENDPOINT) if err != nil { return nil, err @@ -364,13 +364,13 @@ func NewAllocationRequest(baseUrl, allocationID, allocationTx string) (*http.Req if err != nil { return nil, err } - setClientInfo(req) + setClientInfo(req, clientId...) req.Header.Set(ALLOCATION_ID_HEADER, allocationID) return req, nil } -func NewCollaboratorRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { +func NewCollaboratorRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, COLLABORATOR_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -380,7 +380,7 @@ func NewCollaboratorRequest(baseUrl, allocationID, allocationTx, sig string, bod return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -389,7 +389,7 @@ func NewCollaboratorRequest(baseUrl, allocationID, allocationTx, sig string, bod return req, nil } -func GetCollaboratorsRequest(baseUrl, allocationID, allocationTx, sig string, query *url.Values) (*http.Request, error) { +func GetCollaboratorsRequest(baseUrl, allocationID, allocationTx, sig string, query *url.Values, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, COLLABORATOR_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -400,7 +400,7 @@ func GetCollaboratorsRequest(baseUrl, allocationID, allocationTx, sig string, qu return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -409,7 +409,7 @@ func GetCollaboratorsRequest(baseUrl, allocationID, allocationTx, sig string, qu return req, nil } -func DeleteCollaboratorRequest(baseUrl, allocationID, allocationTx, sig string, query *url.Values) (*http.Request, error) { +func DeleteCollaboratorRequest(baseUrl, allocationID, allocationTx, sig string, query *url.Values, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, COLLABORATOR_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -421,7 +421,7 @@ func DeleteCollaboratorRequest(baseUrl, allocationID, allocationTx, sig string, return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -430,7 +430,7 @@ func DeleteCollaboratorRequest(baseUrl, allocationID, allocationTx, sig string, return req, nil } -func NewFileMetaRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { +func NewFileMetaRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, FILE_META_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -440,7 +440,7 @@ func NewFileMetaRequest(baseUrl, allocationID, allocationTx, sig string, body io return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -449,7 +449,7 @@ func NewFileMetaRequest(baseUrl, allocationID, allocationTx, sig string, body io return req, nil } -func NewFileStatsRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { +func NewFileStatsRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, FILE_STATS_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -459,7 +459,7 @@ func NewFileStatsRequest(baseUrl, allocationID, allocationTx, sig string, body i return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -468,7 +468,7 @@ func NewFileStatsRequest(baseUrl, allocationID, allocationTx, sig string, body i return req, nil } -func NewListRequest(baseUrl, allocationID, allocationTx, path, pathHash, auth_token string, list bool, offset, pageLimit int) (*http.Request, error) { +func NewListRequest(baseUrl, allocationID, allocationTx, path, pathHash, auth_token string, list bool, offset, pageLimit int, clientId ...string) (*http.Request, error) { nurl, err := joinUrl(baseUrl, LIST_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -487,7 +487,7 @@ func NewListRequest(baseUrl, allocationID, allocationTx, path, pathHash, auth_to if err != nil { return nil, err } - setClientInfo(req) + setClientInfo(req, clientId...) req.Header.Set(ALLOCATION_ID_HEADER, allocationID) @@ -495,7 +495,7 @@ func NewListRequest(baseUrl, allocationID, allocationTx, path, pathHash, auth_to } // NewUploadRequestWithMethod create a http request of upload -func NewUploadRequestWithMethod(baseURL, allocationID, allocationTx, sig string, body io.Reader, method string) (*http.Request, error) { +func NewUploadRequestWithMethod(baseURL, allocationID, allocationTx, sig string, body io.Reader, method string, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseURL, UPLOAD_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -510,7 +510,7 @@ func NewUploadRequestWithMethod(baseURL, allocationID, allocationTx, sig string, } // set header: X-App-Client-Signature - if err := setClientInfoWithSign(req, sig, allocationTx, baseURL); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseURL, clientId...); err != nil { return nil, err } @@ -520,7 +520,7 @@ func NewUploadRequestWithMethod(baseURL, allocationID, allocationTx, sig string, } func NewWriteMarkerLockRequest( - baseURL, allocationID, allocationTx, sig, connID string) (*http.Request, error) { + baseURL, allocationID, allocationTx, sig, connID string, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseURL, WM_LOCK_ENDPOINT, allocationTx) if err != nil { @@ -536,7 +536,7 @@ func NewWriteMarkerLockRequest( return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseURL); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseURL, clientId...); err != nil { return nil, err } @@ -546,7 +546,7 @@ func NewWriteMarkerLockRequest( } func NewWriteMarkerUnLockRequest( - baseURL, allocationID, allocationTx, sig, connID, requestTime string) (*http.Request, error) { + baseURL, allocationID, allocationTx, sig, connID, requestTime string, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseURL, WM_LOCK_ENDPOINT, allocationTx, connID) if err != nil { @@ -558,7 +558,7 @@ func NewWriteMarkerUnLockRequest( return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseURL); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseURL, clientId...); err != nil { return nil, err } @@ -567,7 +567,7 @@ func NewWriteMarkerUnLockRequest( return req, nil } -func NewFastUploadRequest(baseURL, allocationID string, allocationTx string, body []byte, method string) (*fasthttp.Request, error) { +func NewFastUploadRequest(baseURL, allocationID string, allocationTx string, body []byte, method string, clientId ...string) (*fasthttp.Request, error) { u, err := joinUrl(baseURL, UPLOAD_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -580,7 +580,7 @@ func NewFastUploadRequest(baseURL, allocationID string, allocationTx string, bod req.SetBodyRaw(body) // set header: X-App-Client-Signature - if err := setFastClientInfoWithSign(req, allocationTx); err != nil { + if err := setFastClientInfoWithSign(req, allocationTx, clientId...); err != nil { return nil, err } @@ -588,7 +588,7 @@ func NewFastUploadRequest(baseURL, allocationID string, allocationTx string, bod return req, nil } -func setFastClientInfoWithSign(req *fasthttp.Request, allocation string) error { +func setFastClientInfoWithSign(req *fasthttp.Request, allocation string, clientId ...string) error { req.Header.Set("X-App-Client-ID", client.Id()) req.Header.Set("X-App-Client-Key", client.PublicKey()) @@ -601,7 +601,7 @@ func setFastClientInfoWithSign(req *fasthttp.Request, allocation string) error { return nil } -func NewUploadRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader, update bool) (*http.Request, error) { +func NewUploadRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader, update bool, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, UPLOAD_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -617,7 +617,7 @@ func NewUploadRequest(baseUrl, allocationID, allocationTx, sig string, body io.R return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -626,7 +626,7 @@ func NewUploadRequest(baseUrl, allocationID, allocationTx, sig string, body io.R return req, nil } -func NewConnectionRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { +func NewConnectionRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, CREATE_CONNECTION_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -636,7 +636,7 @@ func NewConnectionRequest(baseUrl, allocationID, allocationTx, sig string, body return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -645,7 +645,7 @@ func NewConnectionRequest(baseUrl, allocationID, allocationTx, sig string, body return req, nil } -func NewRenameRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { +func NewRenameRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, RENAME_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -657,7 +657,7 @@ func NewRenameRequest(baseUrl, allocationID, allocationTx, sig string, body io.R return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -666,7 +666,7 @@ func NewRenameRequest(baseUrl, allocationID, allocationTx, sig string, body io.R return req, nil } -func NewCopyRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { +func NewCopyRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, COPY_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -677,7 +677,7 @@ func NewCopyRequest(baseUrl, allocationID, allocationTx, sig string, body io.Rea return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -686,7 +686,7 @@ func NewCopyRequest(baseUrl, allocationID, allocationTx, sig string, body io.Rea return req, nil } -func NewMoveRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { +func NewMoveRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, MOVE_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -697,7 +697,7 @@ func NewMoveRequest(baseUrl, allocationID, allocationTx, sig string, body io.Rea return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -706,7 +706,7 @@ func NewMoveRequest(baseUrl, allocationID, allocationTx, sig string, body io.Rea return req, nil } -func NewDownloadRequest(baseUrl, allocationID, allocationTx string) (*http.Request, error) { +func NewDownloadRequest(baseUrl, allocationID, allocationTx string, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, DOWNLOAD_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -723,7 +723,7 @@ func NewDownloadRequest(baseUrl, allocationID, allocationTx string) (*http.Reque return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -753,7 +753,7 @@ func NewFastDownloadRequest(baseUrl, allocationID, allocationTx string) (*fastht return req, nil } -func NewRedeemRequest(baseUrl, allocationID, allocationTx string) (*http.Request, error) { +func NewRedeemRequest(baseUrl, allocationID, allocationTx string, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, REDEEM_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -763,12 +763,12 @@ func NewRedeemRequest(baseUrl, allocationID, allocationTx string) (*http.Request if err != nil { return nil, err } - setClientInfo(req) + setClientInfo(req, clientId...) req.Header.Set(ALLOCATION_ID_HEADER, allocationID) return req, nil } -func NewDeleteRequest(baseUrl, allocationID, allocationTx, sig string, query *url.Values) (*http.Request, error) { +func NewDeleteRequest(baseUrl, allocationID, allocationTx, sig string, query *url.Values, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, UPLOAD_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -780,7 +780,7 @@ func NewDeleteRequest(baseUrl, allocationID, allocationTx, sig string, query *ur return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -789,7 +789,7 @@ func NewDeleteRequest(baseUrl, allocationID, allocationTx, sig string, query *ur return req, nil } -func NewCreateDirRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { +func NewCreateDirRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, DIR_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -800,7 +800,7 @@ func NewCreateDirRequest(baseUrl, allocationID, allocationTx, sig string, body i return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -809,7 +809,7 @@ func NewCreateDirRequest(baseUrl, allocationID, allocationTx, sig string, body i return req, nil } -func NewShareRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader) (*http.Request, error) { +func NewShareRequest(baseUrl, allocationID, allocationTx, sig string, body io.Reader, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, SHARE_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -820,7 +820,7 @@ func NewShareRequest(baseUrl, allocationID, allocationTx, sig string, body io.Re return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -829,7 +829,7 @@ func NewShareRequest(baseUrl, allocationID, allocationTx, sig string, body io.Re return req, nil } -func NewRevokeShareRequest(baseUrl, allocationID, allocationTx, sig string, query *url.Values) (*http.Request, error) { +func NewRevokeShareRequest(baseUrl, allocationID, allocationTx, sig string, query *url.Values, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, SHARE_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -840,7 +840,7 @@ func NewRevokeShareRequest(baseUrl, allocationID, allocationTx, sig string, quer return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -849,7 +849,7 @@ func NewRevokeShareRequest(baseUrl, allocationID, allocationTx, sig string, quer return req, nil } -func NewWritemarkerRequest(baseUrl, allocationID, allocationTx, sig string) (*http.Request, error) { +func NewWritemarkerRequest(baseUrl, allocationID, allocationTx, sig string, clientId ...string) (*http.Request, error) { nurl, err := joinUrl(baseUrl, LATEST_WRITE_MARKER_ENDPOINT, allocationTx) if err != nil { @@ -861,7 +861,7 @@ func NewWritemarkerRequest(baseUrl, allocationID, allocationTx, sig string) (*ht return nil, err } - if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl, clientId...); err != nil { return nil, err } @@ -870,7 +870,7 @@ func NewWritemarkerRequest(baseUrl, allocationID, allocationTx, sig string) (*ht return req, nil } -func NewRollbackRequest(baseUrl, allocationID string, allocationTx string, body io.Reader) (*http.Request, error) { +func NewRollbackRequest(baseUrl, allocationID string, allocationTx string, body io.Reader, clientId ...string) (*http.Request, error) { u, err := joinUrl(baseUrl, ROLLBACK_ENDPOINT, allocationTx) if err != nil { return nil, err @@ -880,7 +880,7 @@ func NewRollbackRequest(baseUrl, allocationID string, allocationTx string, body if err != nil { return nil, err } - setClientInfo(req) + setClientInfo(req, clientId...) req.Header.Set(ALLOCATION_ID_HEADER, allocationID) From bd578128034efb1ae9f051729e0a7accd2604369 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 19 Sep 2024 21:43:23 +0530 Subject: [PATCH 106/319] Fix --- core/client/set.go | 5 +- core/transaction/entity.go | 32 +++++--- zboxcore/sdk/allocation_file_delete_test.go | 1 + zboxcore/sdk/deleteworker_test.go | 1 + zboxcore/sdk/filemetaworker_test.go | 2 + zboxcore/sdk/filestatsworker_test.go | 2 + zboxcore/sdk/listworker_test.go | 1 + zcncore/execute_transactions.go | 83 +++++++++++---------- 8 files changed, 75 insertions(+), 52 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index 3d0901eba..d4bca9cce 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -152,7 +152,10 @@ func SetSignatureScheme(signatureScheme string) { client.signatureScheme = signatureScheme } -func Wallet() *zcncrypto.Wallet { +func Wallet(clientId ...string) *zcncrypto.Wallet { + if len(clientId) > 0 { + return client.wallets[clientId[0]] + } return client.wallet } diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 2db3d8ede..d67283f07 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -486,29 +486,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() + if len(clients) > 0 && clients[0] != "" { + clientId = clients[0] + } var requestBytes []byte if requestBytes, err = json.Marshal(sn); err != nil { @@ -525,8 +530,8 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, return } - txn := NewTransactionEntity(client.Id(), - cfg.ChainID, client.PublicKey(), nonce) + txn := NewTransactionEntity(client.Id(clientId), + cfg.ChainID, client.PublicKey(clientId), nonce) txn.TransactionData = string(requestBytes) txn.ToClientID = scAddress @@ -534,8 +539,11 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, 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 } diff --git a/zboxcore/sdk/allocation_file_delete_test.go b/zboxcore/sdk/allocation_file_delete_test.go index e9be2c349..179f7d01c 100644 --- a/zboxcore/sdk/allocation_file_delete_test.go +++ b/zboxcore/sdk/allocation_file_delete_test.go @@ -50,6 +50,7 @@ func TestAllocation_DeleteFile(t *testing.T) { DataShards: 2, ParityShards: 2, FileOptions: 63, + Owner: mockClientId, } a.InitAllocation() sdkInitialized = true diff --git a/zboxcore/sdk/deleteworker_test.go b/zboxcore/sdk/deleteworker_test.go index 7b0de8e5a..33e75a206 100644 --- a/zboxcore/sdk/deleteworker_test.go +++ b/zboxcore/sdk/deleteworker_test.go @@ -379,6 +379,7 @@ func TestDeleteRequest_ProcessDelete(t *testing.T) { a := &Allocation{ DataShards: numBlobbers, + Owner: mockClientId, } for i := 0; i < tt.numBlobbers; i++ { diff --git a/zboxcore/sdk/filemetaworker_test.go b/zboxcore/sdk/filemetaworker_test.go index 395abb6c4..6f51e300c 100644 --- a/zboxcore/sdk/filemetaworker_test.go +++ b/zboxcore/sdk/filemetaworker_test.go @@ -162,6 +162,7 @@ func TestListRequest_getFileMetaInfoFromBlobber(t *testing.T) { Baseurl: tt.name, } req := &ListRequest{ + ClientId: mockClientId, allocationID: mockAllocationId, allocationTx: mockAllocationTxId, ctx: context.TODO(), @@ -272,6 +273,7 @@ func TestListRequest_getFileConsensusFromBlobbers(t *testing.T) { t.Run(tt.name, func(t *testing.T) { tt.setup(t, tt.name, tt.numBlobbers, tt.numCorrect) req := &ListRequest{ + ClientId: mockClientId, allocationID: mockAllocationId, allocationTx: mockAllocationTxId, ctx: context.TODO(), diff --git a/zboxcore/sdk/filestatsworker_test.go b/zboxcore/sdk/filestatsworker_test.go index a5981f389..303f69808 100644 --- a/zboxcore/sdk/filestatsworker_test.go +++ b/zboxcore/sdk/filestatsworker_test.go @@ -152,6 +152,7 @@ func TestListRequest_getFileStatsInfoFromBlobber(t *testing.T) { Baseurl: tt.name, } req := &ListRequest{ + ClientId: mockClientId, allocationID: mockAllocationId, allocationTx: mockAllocationTxId, remotefilepath: mockRemoteFilePath, @@ -248,6 +249,7 @@ func TestListRequest_getFileStatsFromBlobbers(t *testing.T) { t.Run(tt.name, func(t *testing.T) { tt.setup(t, tt.name, tt.numBlobbers, tt.numCorrect) req := &ListRequest{ + ClientId: mockClientId, allocationID: mockAllocationId, allocationTx: mockAllocationTxId, ctx: context.TODO(), diff --git a/zboxcore/sdk/listworker_test.go b/zboxcore/sdk/listworker_test.go index 0145304f1..46548442e 100644 --- a/zboxcore/sdk/listworker_test.go +++ b/zboxcore/sdk/listworker_test.go @@ -178,6 +178,7 @@ func TestListRequest_getListInfoFromBlobber(t *testing.T) { Baseurl: tt.name, } req := &ListRequest{ + ClientId: mockClientId, allocationID: mockAllocationId, allocationTx: mockAllocationTxId, ctx: context.TODO(), diff --git a/zcncore/execute_transactions.go b/zcncore/execute_transactions.go index 267054664..bac4b4909 100644 --- a/zcncore/execute_transactions.go +++ b/zcncore/execute_transactions.go @@ -32,40 +32,39 @@ type MinerSCMinerInfo struct { MinerSCDelegatePool `json:"stake_pool"` } -func MinerSCLock(providerId string, providerType Provider, lock uint64) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func MinerSCLock(providerId string, providerType Provider, lock uint64, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxnValue(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_LOCK, InputArgs: &stakePoolRequest{ ProviderID: providerId, ProviderType: providerType, }, - }, lock) + }, lock, client...) } -func MinerSCUnlock(providerId string, providerType Provider) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func MinerSCUnlock(providerId string, providerType Provider, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_UNLOCK, InputArgs: &stakePoolRequest{ ProviderID: providerId, ProviderType: providerType, }, - }) - + }, client...) } -func MinerSCCollectReward(providerId string, providerType Provider) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func MinerSCCollectReward(providerId string, providerType Provider, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_COLLECT_REWARD, InputArgs: &scCollectReward{ ProviderId: providerId, ProviderType: int(providerType), }, - }) + }, client...) } -func MinerSCKill(providerId string, providerType Provider) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func MinerSCKill(providerId string, providerType Provider, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { pr := &scCollectReward{ ProviderId: providerId, ProviderType: int(providerType), @@ -83,110 +82,110 @@ func MinerSCKill(providerId string, providerType Provider) (hash, out string, no return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: name, InputArgs: pr, - }) + }, client...) } -func StorageSCCollectReward(providerId string, providerType Provider) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func StorageSCCollectReward(providerId string, providerType Provider, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(StorageSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.STORAGESC_COLLECT_REWARD, InputArgs: &scCollectReward{ ProviderId: providerId, ProviderType: int(providerType), }, - }) + }, client...) } -func MinerScUpdateConfig(input interface{}) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func MinerScUpdateConfig(input interface{}, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_UPDATE_SETTINGS, InputArgs: input, - }) + }, client...) } -func MinerScUpdateGlobals(input interface{}) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func MinerScUpdateGlobals(input interface{}, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_UPDATE_GLOBALS, InputArgs: input, - }) + }, client...) } -func StorageScUpdateConfig(input interface{}) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func StorageScUpdateConfig(input interface{}, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(StorageSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.STORAGESC_UPDATE_SETTINGS, InputArgs: input, - }) + }, client...) } -func AddHardfork(input interface{}) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func AddHardfork(input interface{}, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ADD_HARDFORK, InputArgs: input, - }) + }, client...) } -func ZCNSCUpdateGlobalConfig(input *InputMap) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func ZCNSCUpdateGlobalConfig(input *InputMap, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_UPDATE_GLOBAL_CONFIG, InputArgs: input, - }) + }, client...) } -func MinerSCMinerSettings(input *MinerSCMinerInfo) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func MinerSCMinerSettings(input *MinerSCMinerInfo, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_MINER_SETTINGS, InputArgs: input, - }) + }, client...) } -func MinerSCSharderSettings(input *MinerSCMinerInfo) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func MinerSCSharderSettings(input *MinerSCMinerInfo, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_SHARDER_SETTINGS, InputArgs: input, - }) + }, client...) } -func ZCNSCUpdateAuthorizerConfig(input *AuthorizerNode) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func ZCNSCUpdateAuthorizerConfig(input *AuthorizerNode, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_UPDATE_AUTHORIZER_CONFIG, InputArgs: input, - }) + }, client...) } -func ZCNSCAddAuthorizer(input *AddAuthorizerPayload) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func ZCNSCAddAuthorizer(input *AddAuthorizerPayload, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_ADD_AUTHORIZER, InputArgs: input, - }) + }, client...) } -func ZCNSCAuthorizerHealthCheck(input *AuthorizerHealthCheckPayload) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func ZCNSCAuthorizerHealthCheck(input *AuthorizerHealthCheckPayload, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_AUTHORIZER_HEALTH_CHECK, InputArgs: input, - }) + }, client...) } -func ZCNSCDeleteAuthorizer(input *DeleteAuthorizerPayload) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func ZCNSCDeleteAuthorizer(input *DeleteAuthorizerPayload, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_DELETE_AUTHORIZER, InputArgs: input, - }) + }, client...) } -func ZCNSCCollectReward(providerId string, providerType Provider) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func ZCNSCCollectReward(providerId string, providerType Provider, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { pr := &scCollectReward{ ProviderId: providerId, ProviderType: int(providerType), @@ -195,23 +194,29 @@ func ZCNSCCollectReward(providerId string, providerType Provider) (hash, out str return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_COLLECT_REWARD, InputArgs: pr, - }) + }, client...) } type SendTxnData struct { Note string `json:"note"` } -func Send(toClientID string, tokens uint64, desc string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func Send(toClientID string, tokens uint64, desc string, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { + if len(client) == 0 { + client = append(client, "") + client = append(client, toClientID) + } else { + client = append(client, toClientID) + } return transaction.SmartContractTxnValue(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: "transfer", InputArgs: SendTxnData{Note: desc}, - }, tokens, toClientID) + }, tokens, client...) } -func Faucet(tokens uint64, input string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { +func Faucet(tokens uint64, input string, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxnValue(FaucetSmartContractAddress, transaction.SmartContractTxnData{ Name: "pour", InputArgs: input, - }, tokens) + }, tokens, client...) } From 6e54cf7f83f0b1ff1ff64f64a0094071bb1dd059 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Thu, 19 Sep 2024 22:25:56 +0530 Subject: [PATCH 107/319] implement rename move and copy for v2 --- zboxcore/allocationchange/change.go | 3 +- zboxcore/fileref/fileref.go | 5 + zboxcore/sdk/allocation.go | 3 +- zboxcore/sdk/commitworker.go | 6 +- zboxcore/sdk/common.go | 109 +++++++++++ zboxcore/sdk/copyworker.go | 249 +++++++++++++++++++++++-- zboxcore/sdk/deleteworker.go | 10 +- zboxcore/sdk/dirworker.go | 8 +- zboxcore/sdk/moveworker.go | 173 +++++++++++++++-- zboxcore/sdk/multi_operation_worker.go | 9 +- zboxcore/sdk/renameworker.go | 177 +++++++++++++++--- zboxcore/sdk/upload_worker.go | 15 +- 12 files changed, 674 insertions(+), 93 deletions(-) diff --git a/zboxcore/allocationchange/change.go b/zboxcore/allocationchange/change.go index f5ab83508..cfd8ffb33 100644 --- a/zboxcore/allocationchange/change.go +++ b/zboxcore/allocationchange/change.go @@ -19,8 +19,7 @@ type AllocationChange interface { type AllocationChangeV2 interface { ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error - GetLookupHash(changeIndex uint64) string - GetHash(changeIndex uint64, id string) string + GetLookupHash(changeIndex uint64) []string } type EmptyFileChange struct { diff --git a/zboxcore/fileref/fileref.go b/zboxcore/fileref/fileref.go index 75b35817b..2fafdd1ed 100644 --- a/zboxcore/fileref/fileref.go +++ b/zboxcore/fileref/fileref.go @@ -66,6 +66,7 @@ type RefEntity interface { GetNumBlocks() int64 GetSize() int64 GetFileMetaHash() string + GetFileMetaHashV2() []byte GetHash() string CalculateHash() string GetType() string @@ -165,6 +166,10 @@ func (r *Ref) GetFileMetaHash() string { return r.FileMetaHash } +func (r *Ref) GetFileMetaHashV2() []byte { + return nil +} + func (r *Ref) GetHash() string { return r.Hash } diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index a77a00c04..3df6820b5 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -327,6 +327,7 @@ type OperationRequest struct { StreamUpload bool // Required for streaming file when actualSize is not available CancelCauseFunc context.CancelCauseFunc Opts []ChunkedUploadOption + CopyDirOnly bool } // GetReadPriceRange returns the read price range from the global configuration. @@ -1077,7 +1078,7 @@ func (a *Allocation) DoMultiOperation(operations []OperationRequest, opts ...Mul operation = NewRenameOperation(op.RemotePath, op.DestName, mo.operationMask, mo.maskMU, mo.consensusThresh, mo.fullconsensus, mo.ctx) case constants.FileOperationCopy: - operation = NewCopyOperation(op.RemotePath, op.DestPath, mo.operationMask, mo.maskMU, mo.consensusThresh, mo.fullconsensus, mo.ctx) + operation = NewCopyOperation(mo.ctx, op.RemotePath, op.DestPath, mo.operationMask, mo.maskMU, mo.consensusThresh, mo.fullconsensus, op.CopyDirOnly) case constants.FileOperationMove: operation = NewMoveOperation(op.RemotePath, op.DestPath, mo.operationMask, mo.maskMU, mo.consensusThresh, mo.fullconsensus, mo.ctx) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 5558d56ba..e72eeffba 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -407,9 +407,9 @@ func (commitReq *CommitRequestV2) processCommit() { paths := make([]string, 0) changeIndex := commitReq.changeIndex for i := 0; i < len(commitReq.changes); i++ { - lookupHash := commitReq.changes[i].GetLookupHash(changeIndex) - if lookupHash != "" { - paths = append(paths, lookupHash) + lookupHashes := commitReq.changes[i].GetLookupHash(changeIndex) + if lookupHashes != nil { + paths = append(paths, lookupHashes...) } else { commitReq.changes[i] = nil } diff --git a/zboxcore/sdk/common.go b/zboxcore/sdk/common.go index 61eb45a00..af5337b22 100644 --- a/zboxcore/sdk/common.go +++ b/zboxcore/sdk/common.go @@ -8,6 +8,7 @@ import ( "net/http" "path" "strconv" + "strings" "sync" "time" @@ -120,3 +121,111 @@ func ValidateRemoteFileName(remotePath string) error { return nil } + +type subDirRequest struct { + opType string + subOpType string + remotefilepath string + destPath string + allocationObj *Allocation + ctx context.Context + consensusThresh int + mask zboxutil.Uint128 +} + +func (req *subDirRequest) processSubDirectories() error { + var ( + offsetPath string + pathLevel int + ) + + for { + oResult, err := req.allocationObj.GetRefs(req.remotefilepath, offsetPath, "", "", fileref.FILE, fileref.REGULAR, 0, getRefPageLimit, WithObjectContext(req.ctx), WithObjectConsensusThresh(req.consensusThresh), WithSingleBlobber(true), WithObjectMask(req.mask)) + if err != nil { + return err + } + if len(oResult.Refs) == 0 { + break + } + ops := make([]OperationRequest, 0, len(oResult.Refs)) + for _, ref := range oResult.Refs { + opMask := req.mask + if ref.Type == fileref.DIRECTORY { + continue + } + if ref.PathLevel > pathLevel { + pathLevel = ref.PathLevel + } + var destPath string + if req.subOpType == constants.FileOperationRename { + destPath = path.Dir(strings.Replace(ref.Path, req.remotefilepath, req.destPath, 1)) + } else { + basePath := strings.TrimPrefix(path.Dir(ref.Path), path.Dir(req.remotefilepath)) + destPath = path.Join(req.destPath, basePath) + } + op := OperationRequest{ + OperationType: req.opType, + RemotePath: ref.Path, + DestPath: destPath, + Mask: &opMask, + } + ops = append(ops, op) + } + err = req.allocationObj.DoMultiOperation(ops) + if err != nil { + return err + } + offsetPath = oResult.Refs[len(oResult.Refs)-1].Path + if len(oResult.Refs) < getRefPageLimit { + break + } + } + + offsetPath = "" + level := len(strings.Split(strings.TrimSuffix(req.remotefilepath, "/"), "/")) + if pathLevel == 0 { + pathLevel = level + 1 + } + + for pathLevel > level { + oResult, err := req.allocationObj.GetRefs(req.remotefilepath, offsetPath, "", "", fileref.DIRECTORY, fileref.REGULAR, pathLevel, getRefPageLimit, WithObjectContext(req.ctx), WithObjectMask(req.mask), WithObjectConsensusThresh(req.consensusThresh), WithSingleBlobber(true)) + if err != nil { + return err + } + if len(oResult.Refs) == 0 { + pathLevel-- + } else { + ops := make([]OperationRequest, 0, len(oResult.Refs)) + for _, ref := range oResult.Refs { + opMask := req.mask + if ref.Type == fileref.FILE { + continue + } + var destPath string + if req.subOpType == constants.FileOperationRename { + destPath = path.Dir(strings.Replace(ref.Path, req.remotefilepath, req.destPath, 1)) + } else { + basePath := strings.TrimPrefix(path.Dir(ref.Path), path.Dir(req.remotefilepath)) + destPath = path.Join(req.destPath, basePath) + } + op := OperationRequest{ + OperationType: req.opType, + RemotePath: ref.Path, + DestPath: destPath, + Mask: &opMask, + } + ops = append(ops, op) + } + err = req.allocationObj.DoMultiOperation(ops) + if err != nil { + return err + } + offsetPath = oResult.Refs[len(oResult.Refs)-1].Path + if len(oResult.Refs) < getRefPageLimit { + pathLevel-- + } + } + } + + return nil +} diff --git a/zboxcore/sdk/copyworker.go b/zboxcore/sdk/copyworker.go index 4310e7e30..9330ab610 100644 --- a/zboxcore/sdk/copyworker.go +++ b/zboxcore/sdk/copyworker.go @@ -3,10 +3,12 @@ package sdk import ( "bytes" "context" + "encoding/hex" "fmt" "io/ioutil" "mime/multipart" "net/http" + "path" "strings" "sync" "time" @@ -14,6 +16,7 @@ import ( "github.com/0chain/common/core/util/wmpt" "github.com/0chain/errors" "github.com/google/uuid" + "go.uber.org/zap" "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/common" @@ -42,15 +45,39 @@ type CopyRequest struct { maskMU *sync.Mutex connectionID string timestamp int64 + dirOnly bool Consensus } +var errNoChange = errors.New("no_change", "No change in the operation") + +const objAlreadyExists = "Object Already exists" + func (req *CopyRequest) getObjectTreeFromBlobber(blobber *blockchain.StorageNode) (fileref.RefEntity, error) { return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.sig, req.remotefilepath, blobber) } +func (req *CopyRequest) getFileMetaFromBlobber(pos int) (fileRef *fileref.FileRef, err error) { + listReq := &ListRequest{ + allocationID: req.allocationID, + allocationTx: req.allocationTx, + blobbers: req.blobbers, + remotefilepath: req.remotefilepath, + ctx: req.ctx, + } + respChan := make(chan *fileMetaResponse) + go listReq.getFileMetaInfoFromBlobber(req.blobbers[pos], int(pos), respChan) + refRes := <-respChan + if refRes.err != nil { + err = refRes.err + return + } + fileRef = refRes.fileref + return +} + func (req *CopyRequest) copyBlobberObject( - blobber *blockchain.StorageNode, blobberIdx int) (refEntity fileref.RefEntity, err error) { + blobber *blockchain.StorageNode, blobberIdx int, fetchObjectTree bool) (refEntity fileref.RefEntity, err error) { defer func() { if err != nil { @@ -60,9 +87,11 @@ func (req *CopyRequest) copyBlobberObject( req.maskMU.Unlock() } }() - refEntity, err = req.getObjectTreeFromBlobber(req.blobbers[blobberIdx]) - if err != nil { - return nil, err + if fetchObjectTree { + refEntity, err = req.getObjectTreeFromBlobber(req.blobbers[blobberIdx]) + if err != nil { + return nil, err + } } var resp *http.Response @@ -128,7 +157,7 @@ func (req *CopyRequest) copyBlobberObject( } if resp.StatusCode == http.StatusOK { - l.Logger.Info(blobber.Baseurl, " "+req.remotefilepath, " copied.") + l.Logger.Debug(blobber.Baseurl, " "+req.remotefilepath, " copied.") req.Consensus.Done() return } @@ -165,7 +194,7 @@ func (req *CopyRequest) copyBlobberObject( fmt.Sprintf("last status code: %d, last response message: %s", latestStatusCode, latestRespMsg)) } -func (req *CopyRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) { +func (req *CopyRequest) ProcessWithBlobbers() ([]fileref.RefEntity, error) { var pos uint64 numList := len(req.blobbers) objectTreeRefs := make([]fileref.RefEntity, numList) @@ -177,7 +206,7 @@ func (req *CopyRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) { wg.Add(1) go func(blobberIdx int) { defer wg.Done() - refEntity, err := req.copyBlobberObject(req.blobbers[blobberIdx], blobberIdx) + refEntity, err := req.copyBlobberObject(req.blobbers[blobberIdx], blobberIdx, true) if err != nil { blobberErrors[blobberIdx] = err l.Logger.Debug(err.Error()) @@ -187,7 +216,80 @@ func (req *CopyRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) { }(int(pos)) } wg.Wait() - return objectTreeRefs, blobberErrors + return objectTreeRefs, zboxutil.MajorError(blobberErrors) +} + +func (req *CopyRequest) ProcessWithBlobbersV2() ([]fileref.RefEntity, error) { + var ( + pos uint64 + consensusRef *fileref.FileRef + ) + numList := len(req.blobbers) + objectTreeRefs := make([]fileref.RefEntity, numList) + blobberErrors := make([]error, numList) + versionMap := make(map[string]int) + + wg := &sync.WaitGroup{} + for i := req.copyMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + wg.Add(1) + go func(blobberIdx int) { + defer wg.Done() + // refEntity, err := req.copyBlobberObject(req.blobbers[blobberIdx], blobberIdx) + refEntity, err := req.getFileMetaFromBlobber(blobberIdx) + if err != nil { + blobberErrors[blobberIdx] = err + l.Logger.Debug(err.Error()) + return + } + objectTreeRefs[blobberIdx] = refEntity + req.maskMU.Lock() + versionMap[refEntity.AllocationRoot] += 1 + if versionMap[refEntity.AllocationRoot] >= req.consensusThresh { + consensusRef = refEntity + } + req.maskMU.Unlock() + }(int(pos)) + } + wg.Wait() + if consensusRef == nil { + return nil, zboxutil.MajorError(blobberErrors) + } + + if consensusRef.Type == fileref.DIRECTORY && !consensusRef.IsEmpty { + for ind, refEntity := range objectTreeRefs { + if refEntity.GetAllocationRoot() != consensusRef.AllocationRoot { + req.copyMask = req.copyMask.And(zboxutil.NewUint128(1).Lsh(uint64(ind)).Not()) + } + } + err := req.copySubDirectoriees(req.dirOnly) + if err != nil { + return nil, err + } + req.consensus = req.copyMask.CountOnes() + return objectTreeRefs, errNoChange + } + + for i := req.copyMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + wg.Add(1) + go func(blobberIdx int) { + defer wg.Done() + _, err := req.copyBlobberObject(req.blobbers[blobberIdx], blobberIdx, false) + if err != nil { + blobberErrors[blobberIdx] = err + l.Logger.Debug(err.Error()) + return + } + }(int(pos)) + } + wg.Wait() + err := zboxutil.MajorError(blobberErrors) + if err != nil && strings.Contains(err.Error(), objAlreadyExists) && consensusRef.Type == fileref.DIRECTORY { + return nil, errNoChange + } + + return objectTreeRefs, err } func (req *CopyRequest) ProcessCopy() error { @@ -196,10 +298,9 @@ func (req *CopyRequest) ProcessCopy() error { wg := &sync.WaitGroup{} var pos uint64 - objectTreeRefs, blobberErrors := req.ProcessWithBlobbers() + objectTreeRefs, err := req.ProcessWithBlobbers() if !req.isConsensusOk() { - err := zboxutil.MajorError(blobberErrors) if err != nil { return errors.New("copy_failed", fmt.Sprintf("Copy failed. %s", err.Error())) } @@ -298,10 +399,13 @@ func (req *CopyRequest) ProcessCopy() error { type CopyOperation struct { remotefilepath string destPath string + destLookupHash string + dirOnly bool ctx context.Context ctxCncl context.CancelFunc copyMask zboxutil.Uint128 maskMU *sync.Mutex + objectTreeRefs []fileref.RefEntity Consensus } @@ -321,17 +425,21 @@ func (co *CopyOperation) Process(allocObj *Allocation, connectionID string) ([]f ctxCncl: co.ctxCncl, copyMask: co.copyMask, maskMU: co.maskMU, + dirOnly: co.dirOnly, Consensus: Consensus{RWMutex: &sync.RWMutex{}}, } cR.consensusThresh = co.consensusThresh cR.fullconsensus = co.fullconsensus - - objectTreeRefs, blobberErrors := cR.ProcessWithBlobbers() + var err error + if allocObj.StorageVersion == StorageV2 { + co.objectTreeRefs, err = cR.ProcessWithBlobbersV2() + } else { + co.objectTreeRefs, err = cR.ProcessWithBlobbers() + } if !cR.isConsensusOk() { l.Logger.Error("copy failed: ", cR.remotefilepath, cR.destPath) - err := zboxutil.MajorError(blobberErrors) if err != nil { return nil, cR.copyMask, errors.New("copy_failed", fmt.Sprintf("Copy failed. %s", err.Error())) } @@ -340,7 +448,8 @@ func (co *CopyOperation) Process(allocObj *Allocation, connectionID string) ([]f fmt.Sprintf("Copy failed. Required consensus %d, got %d", cR.Consensus.consensusThresh, cR.Consensus.consensus)) } - return objectTreeRefs, cR.copyMask, nil + co.destLookupHash = fileref.GetReferenceLookup(cR.allocationID, co.destPath) + return co.objectTreeRefs, cR.copyMask, nil } @@ -394,7 +503,7 @@ func (co *CopyOperation) Error(allocObj *Allocation, consensus int, err error) { } -func NewCopyOperation(remotePath string, destPath string, copyMask zboxutil.Uint128, maskMU *sync.Mutex, consensusTh int, fullConsensus int, ctx context.Context) *CopyOperation { +func NewCopyOperation(ctx context.Context, remotePath string, destPath string, copyMask zboxutil.Uint128, maskMU *sync.Mutex, consensusTh, fullConsensus int, copyDirOnly bool) *CopyOperation { co := &CopyOperation{} co.remotefilepath = zboxutil.RemoteClean(remotePath) co.copyMask = copyMask @@ -406,18 +515,120 @@ func NewCopyOperation(remotePath string, destPath string, copyMask zboxutil.Uint } co.destPath = destPath co.ctx, co.ctxCncl = context.WithCancel(ctx) + co.dirOnly = copyDirOnly return co } func (co *CopyOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { + if co.objectTreeRefs == nil || co.objectTreeRefs[changeIndex] == nil || co.objectTreeRefs[changeIndex].GetType() == fileref.DIRECTORY { + return nil + } + decodedDestHash, _ := hex.DecodeString(co.destLookupHash) + ref := co.objectTreeRefs[changeIndex] + numBlocks := uint64(ref.GetNumBlocks()) + fileMetaRawHash := ref.GetFileMetaHashV2() + err := trie.Update(decodedDestHash, fileMetaRawHash, numBlocks) + if err != nil { + l.Logger.Error("Error updating trie", zap.Error(err)) + return err + } return nil } -func (co *CopyOperation) GetLookupHash(changeIndex uint64) string { - return "" +func (co *CopyOperation) GetLookupHash(changeIndex uint64) []string { + if co.objectTreeRefs == nil || co.objectTreeRefs[changeIndex] == nil || co.objectTreeRefs[changeIndex].GetType() == fileref.DIRECTORY { + return nil + } + return []string{co.destLookupHash} } -func (co *CopyOperation) GetHash(changeIndex uint64, id string) string { - return "" +func (req *CopyRequest) copySubDirectoriees(dirOnly bool) error { + var ( + offsetPath string + pathLevel int + ) + + for { + if !dirOnly { + oResult, err := req.allocationObj.GetRefs(req.remotefilepath, offsetPath, "", "", fileref.FILE, fileref.REGULAR, 0, getRefPageLimit, WithObjectContext(req.ctx), WithObjectConsensusThresh(req.consensusThresh), WithSingleBlobber(true)) + if err != nil { + return err + } + if len(oResult.Refs) == 0 { + break + } + ops := make([]OperationRequest, 0, len(oResult.Refs)) + for _, ref := range oResult.Refs { + opMask := req.copyMask + if ref.Type == fileref.DIRECTORY { + continue + } + if ref.PathLevel > pathLevel { + pathLevel = ref.PathLevel + } + basePath := strings.TrimPrefix(path.Dir(ref.Path), path.Dir(req.remotefilepath)) + destPath := path.Join(req.destPath, basePath) + op := OperationRequest{ + OperationType: constants.FileOperationCopy, + RemotePath: ref.Path, + DestPath: destPath, + Mask: &opMask, + } + ops = append(ops, op) + } + err = req.allocationObj.DoMultiOperation(ops) + if err != nil { + return err + } + offsetPath = oResult.Refs[len(oResult.Refs)-1].Path + if len(oResult.Refs) < getRefPageLimit { + break + } + } + } + + offsetPath = "" + level := len(strings.Split(strings.TrimSuffix(req.remotefilepath, "/"), "/")) + if pathLevel == 0 { + pathLevel = level + 1 + } + + for pathLevel > level { + oResult, err := req.allocationObj.GetRefs(req.remotefilepath, offsetPath, "", "", fileref.DIRECTORY, fileref.REGULAR, pathLevel, getRefPageLimit, WithObjectContext(req.ctx), WithObjectMask(req.copyMask), WithObjectConsensusThresh(req.consensusThresh), WithSingleBlobber(true)) + if err != nil { + return err + } + if len(oResult.Refs) == 0 { + pathLevel-- + } else { + ops := make([]OperationRequest, 0, len(oResult.Refs)) + for _, ref := range oResult.Refs { + opMask := req.copyMask + if ref.Type == fileref.FILE { + continue + } + basePath := strings.TrimPrefix(path.Dir(ref.Path), path.Dir(req.remotefilepath)) + destPath := path.Join(req.destPath, basePath) + op := OperationRequest{ + OperationType: constants.FileOperationCopy, + RemotePath: ref.Path, + DestPath: destPath, + Mask: &opMask, + CopyDirOnly: true, + } + ops = append(ops, op) + } + err = req.allocationObj.DoMultiOperation(ops) + if err != nil { + return err + } + offsetPath = oResult.Refs[len(oResult.Refs)-1].Path + if len(oResult.Refs) < getRefPageLimit { + pathLevel-- + } + } + } + + return nil } diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index 84cf0b595..ebb75e1ba 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -668,13 +668,9 @@ func (dop *DeleteOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, chang return nil } -func (dop *DeleteOperation) GetLookupHash(changeIndex uint64) string { +func (dop *DeleteOperation) GetLookupHash(changeIndex uint64) []string { if dop.refs == nil || dop.refs[changeIndex] == nil || dop.refs[changeIndex].GetType() == fileref.DIRECTORY { - return "" + return nil } - return dop.lookupHash -} - -func (dop *DeleteOperation) GetHash(changeIndex uint64, id string) string { - return emptyHash + return []string{dop.lookupHash} } diff --git a/zboxcore/sdk/dirworker.go b/zboxcore/sdk/dirworker.go index a2e296494..9c712bfa3 100644 --- a/zboxcore/sdk/dirworker.go +++ b/zboxcore/sdk/dirworker.go @@ -377,10 +377,6 @@ func (dirOp *DirOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, change return nil } -func (dirOp *DirOperation) GetLookupHash(changeIndex uint64) string { - return "" -} - -func (dirOp *DirOperation) GetHash(changeIndex uint64, id string) string { - return emptyHash +func (dirOp *DirOperation) GetLookupHash(changeIndex uint64) []string { + return nil } diff --git a/zboxcore/sdk/moveworker.go b/zboxcore/sdk/moveworker.go index 4d32d3201..94ba599f8 100644 --- a/zboxcore/sdk/moveworker.go +++ b/zboxcore/sdk/moveworker.go @@ -3,6 +3,7 @@ package sdk import ( "bytes" "context" + "encoding/hex" "fmt" "io/ioutil" "mime/multipart" @@ -15,6 +16,7 @@ import ( "github.com/0chain/errors" thrown "github.com/0chain/errors" "github.com/google/uuid" + "go.uber.org/zap" "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/common" @@ -49,8 +51,27 @@ func (req *MoveRequest) getObjectTreeFromBlobber(blobber *blockchain.StorageNode return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.sig, req.remotefilepath, blobber) } +func (req *MoveRequest) getFileMetaFromBlobber(pos int) (fileRef *fileref.FileRef, err error) { + listReq := &ListRequest{ + allocationID: req.allocationID, + allocationTx: req.allocationTx, + blobbers: req.blobbers, + remotefilepath: req.remotefilepath, + ctx: req.ctx, + } + respChan := make(chan *fileMetaResponse) + go listReq.getFileMetaInfoFromBlobber(req.blobbers[pos], int(pos), respChan) + refRes := <-respChan + if refRes.err != nil { + err = refRes.err + return + } + fileRef = refRes.fileref + return +} + func (req *MoveRequest) moveBlobberObject( - blobber *blockchain.StorageNode, blobberIdx int) (refEntity fileref.RefEntity, err error) { + blobber *blockchain.StorageNode, blobberIdx int, fetchObjectTree bool) (refEntity fileref.RefEntity, err error) { defer func() { if err != nil { @@ -60,9 +81,11 @@ func (req *MoveRequest) moveBlobberObject( req.maskMU.Unlock() } }() - refEntity, err = req.getObjectTreeFromBlobber(req.blobbers[blobberIdx]) - if err != nil { - return nil, err + if fetchObjectTree { + refEntity, err = req.getObjectTreeFromBlobber(req.blobbers[blobberIdx]) + if err != nil { + return nil, err + } } var resp *http.Response @@ -162,7 +185,7 @@ func (req *MoveRequest) moveBlobberObject( fmt.Sprintf("last status code: %d, last response message: %s", latestStatusCode, latestRespMsg)) } -func (req *MoveRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) { +func (req *MoveRequest) ProcessWithBlobbers() ([]fileref.RefEntity, error) { var pos uint64 numList := len(req.blobbers) objectTreeRefs := make([]fileref.RefEntity, numList) @@ -173,7 +196,7 @@ func (req *MoveRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) { wg.Add(1) go func(blobberIdx int) { defer wg.Done() - refEntity, err := req.moveBlobberObject(req.blobbers[blobberIdx], blobberIdx) + refEntity, err := req.moveBlobberObject(req.blobbers[blobberIdx], blobberIdx, true) if err != nil { blobberErrors[blobberIdx] = err l.Logger.Error(err.Error()) @@ -183,7 +206,92 @@ func (req *MoveRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) { }(int(pos)) } wg.Wait() - return objectTreeRefs, blobberErrors + return objectTreeRefs, zboxutil.MajorError(blobberErrors) +} + +func (req *MoveRequest) ProcessWithBlobbersV2() ([]fileref.RefEntity, error) { + + var ( + pos uint64 + consensusRef *fileref.FileRef + ) + numList := len(req.blobbers) + objectTreeRefs := make([]fileref.RefEntity, numList) + blobberErrors := make([]error, numList) + versionMap := make(map[string]int) + wg := &sync.WaitGroup{} + for i := req.moveMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + wg.Add(1) + go func(blobberIdx int) { + defer wg.Done() + refEntity, err := req.getFileMetaFromBlobber(blobberIdx) + if err != nil { + blobberErrors[blobberIdx] = err + l.Logger.Debug(err.Error()) + return + } + objectTreeRefs[blobberIdx] = refEntity + req.maskMU.Lock() + versionMap[refEntity.AllocationRoot] += 1 + if versionMap[refEntity.AllocationRoot] >= req.consensusThresh { + consensusRef = refEntity + } + req.maskMU.Unlock() + }(int(pos)) + } + wg.Wait() + if consensusRef == nil { + return nil, zboxutil.MajorError(blobberErrors) + } + + if consensusRef.Type == fileref.DIRECTORY && !consensusRef.IsEmpty { + for ind, refEntity := range objectTreeRefs { + if refEntity.GetAllocationRoot() != consensusRef.AllocationRoot { + req.moveMask = req.moveMask.And(zboxutil.NewUint128(1).Lsh(uint64(ind)).Not()) + } + } + subRequest := &subDirRequest{ + allocationObj: req.allocationObj, + remotefilepath: req.remotefilepath, + destPath: req.destPath, + ctx: req.ctx, + consensusThresh: req.consensusThresh, + opType: constants.FileOperationMove, + subOpType: constants.FileOperationMove, + mask: req.moveMask, + } + err := subRequest.processSubDirectories() + if err != nil { + return nil, err + } + op := OperationRequest{ + OperationType: constants.FileOperationDelete, + RemotePath: req.remotefilepath, + } + err = req.allocationObj.DoMultiOperation([]OperationRequest{op}) + if err != nil { + return nil, err + } + req.consensus = req.moveMask.CountOnes() + return nil, errNoChange + } + + for i := req.moveMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + wg.Add(1) + go func(blobberIdx int) { + defer wg.Done() + _, err := req.moveBlobberObject(req.blobbers[blobberIdx], blobberIdx, false) + if err != nil { + blobberErrors[blobberIdx] = err + l.Logger.Debug(err.Error()) + return + } + }(int(pos)) + } + wg.Wait() + return objectTreeRefs, zboxutil.MajorError(blobberErrors) } func (req *MoveRequest) ProcessMove() error { @@ -192,10 +300,9 @@ func (req *MoveRequest) ProcessMove() error { wg := &sync.WaitGroup{} var pos uint64 - objectTreeRefs, blobberErrors := req.ProcessWithBlobbers() + objectTreeRefs, err := req.ProcessWithBlobbers() if !req.isConsensusOk() { - err := zboxutil.MajorError(blobberErrors) if err != nil { return errors.New("move_failed", fmt.Sprintf("Move failed. %s", err.Error())) } @@ -292,11 +399,14 @@ func (req *MoveRequest) ProcessMove() error { type MoveOperation struct { remotefilepath string destPath string + srcLookupHash string + destLookupHash string ctx context.Context ctxCncl context.CancelFunc moveMask zboxutil.Uint128 maskMU *sync.Mutex consensus Consensus + objectTreeRefs []fileref.RefEntity } func (mo *MoveOperation) Process(allocObj *Allocation, connectionID string) ([]fileref.RefEntity, zboxutil.Uint128, error) { @@ -317,12 +427,18 @@ func (mo *MoveOperation) Process(allocObj *Allocation, connectionID string) ([]f } mR.Consensus.fullconsensus = mo.consensus.fullconsensus mR.Consensus.consensusThresh = mo.consensus.consensusThresh - - objectTreeRefs, blobberErrors := mR.ProcessWithBlobbers() + var err error + if allocObj.StorageVersion == StorageV2 { + mo.objectTreeRefs, err = mR.ProcessWithBlobbersV2() + } else { + mo.objectTreeRefs, err = mR.ProcessWithBlobbers() + } if !mR.Consensus.isConsensusOk() { - err := zboxutil.MajorError(blobberErrors) if err != nil { + if err == errNoChange { + return nil, mR.moveMask, err + } return nil, mR.moveMask, thrown.New("move_failed", fmt.Sprintf("Move failed. %s", err.Error())) } @@ -330,7 +446,9 @@ func (mo *MoveOperation) Process(allocObj *Allocation, connectionID string) ([]f fmt.Sprintf("Move failed. Required consensus %d, got %d", mR.Consensus.consensusThresh, mR.Consensus.consensus)) } - return objectTreeRefs, mR.moveMask, nil + mo.destLookupHash = fileref.GetReferenceLookup(mR.allocationID, mR.destPath) + mo.srcLookupHash = fileref.GetReferenceLookup(mR.allocationID, mR.remotefilepath) + return mo.objectTreeRefs, mR.moveMask, nil } func (mo *MoveOperation) buildChange(refs []fileref.RefEntity, uid uuid.UUID) []allocationchange.AllocationChange { @@ -401,13 +519,30 @@ func NewMoveOperation(remotePath string, destPath string, moveMask zboxutil.Uint } func (mo *MoveOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { + if mo.objectTreeRefs == nil || mo.objectTreeRefs[changeIndex] == nil || mo.objectTreeRefs[changeIndex].GetType() == fileref.DIRECTORY { + return nil + } + decodedSrcHash, _ := hex.DecodeString(mo.srcLookupHash) + err := trie.Update(decodedSrcHash, nil, 0) + if err != nil { + l.Logger.Error("Error updating trie", zap.Error(err)) + return err + } + decodedDestHash, _ := hex.DecodeString(mo.destLookupHash) + ref := mo.objectTreeRefs[changeIndex] + numBlocks := uint64(ref.GetNumBlocks()) + fileMetaRawHash := ref.GetFileMetaHashV2() + err = trie.Update(decodedDestHash, fileMetaRawHash, numBlocks) + if err != nil { + l.Logger.Error("Error updating trie", zap.Error(err)) + return err + } return nil } -func (mo *MoveOperation) GetLookupHash(changeIndex uint64) string { - return "" -} - -func (mo *MoveOperation) GetHash(changeIndex uint64, id string) string { - return "" +func (mo *MoveOperation) GetLookupHash(changeIndex uint64) []string { + if mo.objectTreeRefs == nil || mo.objectTreeRefs[changeIndex] == nil || mo.objectTreeRefs[changeIndex].GetType() == fileref.DIRECTORY { + return nil + } + return []string{mo.destLookupHash, mo.srcLookupHash} } diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 73a7e4320..72965c642 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -49,8 +49,7 @@ type Operationer interface { Completed(allocObj *Allocation) Error(allocObj *Allocation, consensus int, err error) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error - GetLookupHash(changeIndex uint64) string - GetHash(changeIndex uint64, id string) string + GetLookupHash(changeIndex uint64) []string } type MultiOperation struct { @@ -195,7 +194,7 @@ func (mo *MultiOperation) Process() error { refs, mask, err := op.Process(mo.allocationObj, mo.connectionID) // Process with each blobber if err != nil { - if err != errFileDeleted { + if err != errFileDeleted && err != errNoChange { l.Logger.Error(err) errsSlice[idx] = errors.New("", err.Error()) ctxCncl(err) @@ -237,6 +236,10 @@ func (mo *MultiOperation) Process() error { return nil } + if mo.allocationObj.StorageVersion == StorageV2 && len(mo.changesV2) == 0 { + return nil + } + // Take transpose of mo.change because it will be easier to iterate mo if it contains blobber changes // in row instead of column. Currently mo.change[0] contains allocationChange for operation 1 and so on. // But we want mo.changes[0] to have allocationChange for blobber 1 and mo.changes[1] to have allocationChange for diff --git a/zboxcore/sdk/renameworker.go b/zboxcore/sdk/renameworker.go index 4070e8579..bc1d6cc33 100644 --- a/zboxcore/sdk/renameworker.go +++ b/zboxcore/sdk/renameworker.go @@ -3,10 +3,12 @@ package sdk import ( "bytes" "context" + "encoding/hex" "fmt" "io/ioutil" "mime/multipart" "net/http" + "path" "path/filepath" "sync" "time" @@ -14,6 +16,7 @@ import ( "github.com/0chain/common/core/util/wmpt" "github.com/0chain/errors" "github.com/google/uuid" + "go.uber.org/zap" "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/common" @@ -49,8 +52,27 @@ func (req *RenameRequest) getObjectTreeFromBlobber(blobber *blockchain.StorageNo return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.sig, req.remotefilepath, blobber) } +func (req *RenameRequest) getFileMetaFromBlobber(pos int) (fileRef *fileref.FileRef, err error) { + listReq := &ListRequest{ + allocationID: req.allocationID, + allocationTx: req.allocationTx, + blobbers: req.blobbers, + remotefilepath: req.remotefilepath, + ctx: req.ctx, + } + respChan := make(chan *fileMetaResponse) + go listReq.getFileMetaInfoFromBlobber(req.blobbers[pos], int(pos), respChan) + refRes := <-respChan + if refRes.err != nil { + err = refRes.err + return + } + fileRef = refRes.fileref + return +} + func (req *RenameRequest) renameBlobberObject( - blobber *blockchain.StorageNode, blobberIdx int) (refEntity fileref.RefEntity, err error) { + blobber *blockchain.StorageNode, blobberIdx int, fetchObjectTree bool) (refEntity fileref.RefEntity, err error) { defer func() { if err != nil { @@ -59,10 +81,11 @@ func (req *RenameRequest) renameBlobberObject( req.maskMU.Unlock() } }() - - refEntity, err = req.getObjectTreeFromBlobber(req.blobbers[blobberIdx]) - if err != nil { - return nil, err + if fetchObjectTree { + refEntity, err = req.getObjectTreeFromBlobber(req.blobbers[blobberIdx]) + if err != nil { + return nil, err + } } var ( @@ -161,7 +184,7 @@ func (req *RenameRequest) renameBlobberObject( return } -func (req *RenameRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) { +func (req *RenameRequest) ProcessWithBlobbers() ([]fileref.RefEntity, error) { var pos uint64 numList := len(req.blobbers) objectTreeRefs := make([]fileref.RefEntity, numList) @@ -172,26 +195,110 @@ func (req *RenameRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) { req.wg.Add(1) go func(blobberIdx int) { defer req.wg.Done() - refEntity, err := req.renameBlobberObject(req.blobbers[blobberIdx], blobberIdx) + refEntity, err := req.renameBlobberObject(req.blobbers[blobberIdx], blobberIdx, true) + if err != nil { + blobberErrors[blobberIdx] = err + l.Logger.Error(err.Error()) + return + } + objectTreeRefs[blobberIdx] = refEntity + }(int(pos)) + } + req.wg.Wait() + return objectTreeRefs, zboxutil.MajorError(blobberErrors) +} + +func (req *RenameRequest) ProcessWithBlobbersV2() ([]fileref.RefEntity, error) { + var ( + pos uint64 + consensusRef *fileref.FileRef + ) + numList := len(req.blobbers) + objectTreeRefs := make([]fileref.RefEntity, numList) + blobberErrors := make([]error, numList) + versionMap := make(map[string]int) + req.wg = &sync.WaitGroup{} + for i := req.renameMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + req.wg.Add(1) + go func(blobberIdx int) { + defer req.wg.Done() + refEntity, err := req.getFileMetaFromBlobber(blobberIdx) if err != nil { blobberErrors[blobberIdx] = err l.Logger.Error(err.Error()) return } objectTreeRefs[blobberIdx] = refEntity + req.maskMU.Lock() + versionMap[refEntity.AllocationRoot] += 1 + if versionMap[refEntity.AllocationRoot] >= req.consensus.consensusThresh { + consensusRef = refEntity + } + req.maskMU.Unlock() + }(int(pos)) + } + req.wg.Wait() + if consensusRef == nil { + return nil, zboxutil.MajorError(blobberErrors) + } + if consensusRef.Type == fileref.DIRECTORY && !consensusRef.IsEmpty { + for ind, refEntity := range objectTreeRefs { + if refEntity.GetAllocationRoot() != consensusRef.AllocationRoot { + req.renameMask = req.renameMask.And(zboxutil.NewUint128(1).Lsh(uint64(ind)).Not()) + } + } + subRequest := &subDirRequest{ + allocationObj: req.allocationObj, + remotefilepath: req.remotefilepath, + destPath: path.Join(path.Dir(req.remotefilepath), req.newName), + ctx: req.ctx, + consensusThresh: req.consensus.consensusThresh, + opType: constants.FileOperationMove, + subOpType: constants.FileOperationRename, + mask: req.renameMask, + } + err := subRequest.processSubDirectories() + if err != nil { + return nil, err + } + op := OperationRequest{ + OperationType: constants.FileOperationDelete, + RemotePath: req.remotefilepath, + Mask: &req.renameMask, + } + err = req.allocationObj.DoMultiOperation([]OperationRequest{op}) + if err != nil { + return nil, err + } + req.consensus.consensus = req.renameMask.CountOnes() + return nil, errNoChange + } + + for i := req.renameMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + req.wg.Add(1) + go func(blobberIdx int) { + defer req.wg.Done() + _, err := req.renameBlobberObject(req.blobbers[blobberIdx], blobberIdx, false) + if err != nil { + blobberErrors[blobberIdx] = err + l.Logger.Debug(err.Error()) + return + } }(int(pos)) } req.wg.Wait() - return objectTreeRefs, blobberErrors + + return objectTreeRefs, zboxutil.MajorError(blobberErrors) } func (req *RenameRequest) ProcessRename() error { defer req.ctxCncl() - objectTreeRefs, blobberErrors := req.ProcessWithBlobbers() + objectTreeRefs, err := req.ProcessWithBlobbers() if !req.consensus.isConsensusOk() { - err := zboxutil.MajorError(blobberErrors) if err != nil { return errors.New("rename_failed", fmt.Sprintf("Rename failed. %s", err.Error())) @@ -295,11 +402,14 @@ func (req *RenameRequest) ProcessRename() error { type RenameOperation struct { remotefilepath string + srcLookupHash string + destLookupHash string ctx context.Context ctxCncl context.CancelFunc renameMask zboxutil.Uint128 newName string maskMU *sync.Mutex + objectTreeRefs []fileref.RefEntity consensus Consensus } @@ -327,12 +437,18 @@ func (ro *RenameOperation) Process(allocObj *Allocation, connectionID string) ([ } rR.consensus.fullconsensus = ro.consensus.fullconsensus rR.consensus.consensusThresh = ro.consensus.consensusThresh - - objectTreeRefs, blobberErrors := rR.ProcessWithBlobbers() + var err error + if allocObj.StorageVersion == StorageV2 { + ro.objectTreeRefs, err = rR.ProcessWithBlobbersV2() + } else { + ro.objectTreeRefs, err = rR.ProcessWithBlobbers() + } if !rR.consensus.isConsensusOk() { - err := zboxutil.MajorError(blobberErrors) if err != nil { + if err == errNoChange { + return nil, rR.renameMask, err + } return nil, rR.renameMask, errors.New("rename_failed", fmt.Sprintf("Renamed failed. %s", err.Error())) } @@ -340,8 +456,10 @@ func (ro *RenameOperation) Process(allocObj *Allocation, connectionID string) ([ fmt.Sprintf("Rename failed. Required consensus %d, got %d", rR.consensus.consensusThresh, rR.consensus.consensus)) } - l.Logger.Info("Rename Processs Ended ") - return objectTreeRefs, rR.renameMask, nil + ro.srcLookupHash = fileref.GetReferenceLookup(rR.allocationID, rR.remotefilepath) + destPath := path.Join(path.Dir(rR.remotefilepath), rR.newName) + ro.destLookupHash = fileref.GetReferenceLookup(rR.allocationID, destPath) + return ro.objectTreeRefs, rR.renameMask, nil } func (ro *RenameOperation) buildChange(refs []fileref.RefEntity, uid uuid.UUID) []allocationchange.AllocationChange { @@ -414,13 +532,30 @@ func NewRenameOperation(remotePath string, destName string, renameMask zboxutil. } func (ro *RenameOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { + if ro.objectTreeRefs == nil || ro.objectTreeRefs[changeIndex] == nil || ro.objectTreeRefs[changeIndex].GetType() == fileref.DIRECTORY { + return nil + } + decodedSrcHash, _ := hex.DecodeString(ro.srcLookupHash) + err := trie.Update(decodedSrcHash, nil, 0) + if err != nil { + l.Logger.Error("Error updating trie", zap.Error(err)) + return err + } + decodedDestHash, _ := hex.DecodeString(ro.destLookupHash) + ref := ro.objectTreeRefs[changeIndex] + numBlocks := uint64(ref.GetNumBlocks()) + fileMetaRawHash := ref.GetFileMetaHashV2() + err = trie.Update(decodedDestHash, fileMetaRawHash, numBlocks) + if err != nil { + l.Logger.Error("Error updating trie", zap.Error(err)) + return err + } return nil } -func (ro *RenameOperation) GetLookupHash(changeIndex uint64) string { - return "" -} - -func (ro *RenameOperation) GetHash(changeIndex uint64, id string) string { - return "" +func (ro *RenameOperation) GetLookupHash(changeIndex uint64) []string { + if ro.objectTreeRefs == nil || ro.objectTreeRefs[changeIndex] == nil || ro.objectTreeRefs[changeIndex].GetType() == fileref.DIRECTORY { + return nil + } + return []string{ro.destLookupHash, ro.srcLookupHash} } diff --git a/zboxcore/sdk/upload_worker.go b/zboxcore/sdk/upload_worker.go index 00f7df0dd..6d1315824 100644 --- a/zboxcore/sdk/upload_worker.go +++ b/zboxcore/sdk/upload_worker.go @@ -10,7 +10,6 @@ import ( "github.com/0chain/common/core/util/wmpt" "github.com/0chain/gosdk/constants" - "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk/zboxcore/fileref" @@ -163,17 +162,9 @@ func (uo *UploadOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, change return nil } -func (uo *UploadOperation) GetLookupHash(changeIndex uint64) string { +func (uo *UploadOperation) GetLookupHash(changeIndex uint64) []string { if uo.refs[changeIndex] == nil { - return "" - } - return uo.lookupHash -} - -func (uo *UploadOperation) GetHash(changeIndex uint64, id string) string { - if uo.refs[changeIndex] == nil { - return "" + return nil } - ref := uo.refs[changeIndex] - return encryption.Hash(ref.GetFileHashDataV2(id)) + return []string{uo.lookupHash} } From ec62905d75b6d7d4f3925fabdd1212aaf773dad7 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 20 Sep 2024 01:30:29 +0530 Subject: [PATCH 108/319] Fix unit tests --- core/client/set.go | 18 ++++---- core/transaction/entity.go | 2 +- zboxcore/sdk/allocation.go | 6 +++ zboxcore/sdk/allocation_file_delete_test.go | 1 + zboxcore/sdk/allocation_file_test.go | 9 ++++ zboxcore/sdk/allocation_test.go | 50 ++++++++++++++------- zboxcore/sdk/chunked_upload_bench_test.go | 1 + zboxcore/sdk/chunked_upload_blobber.go | 2 +- zboxcore/sdk/commitworker.go | 2 +- zboxcore/sdk/copyworker_test.go | 4 ++ zboxcore/sdk/deleteworker_test.go | 3 ++ zboxcore/sdk/downloadworker.go | 2 +- zboxcore/sdk/listworker.go | 3 ++ zboxcore/sdk/listworker_test.go | 2 + zboxcore/sdk/renameworker_test.go | 4 ++ zboxcore/sdk/rollback.go | 3 ++ zboxcore/sdk/sharerequest.go | 2 +- zboxcore/sdk/writemarker_mutex_test.go | 3 ++ 18 files changed, 86 insertions(+), 31 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index d4bca9cce..9eec43bd9 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -33,7 +33,7 @@ func init() { client = Client{ wallet: &zcncrypto.Wallet{}, sign: func(hash string, clientId ...string) (string, error) { - if len(clientId) > 0 { + if len(clientId) > 0 && client.wallets[clientId[0]].ClientKey != "" { w, ok := client.wallets[clientId[0]] if !ok { return "", errors.New("invalid client id") @@ -89,7 +89,7 @@ func verifySignatureWith(pubKey, signature, hash string) (bool, error) { func GetClientSysKeys(clientId ...string) []sys.KeyPair { wallet := client.wallet - if len(clientId) > 0 { + if len(clientId) > 0 && client.wallets[clientId[0]].ClientKey != "" { w, ok := client.wallets[clientId[0]] if !ok { return nil @@ -153,7 +153,7 @@ func SetSignatureScheme(signatureScheme string) { } func Wallet(clientId ...string) *zcncrypto.Wallet { - if len(clientId) > 0 { + if len(clientId) > 0 && client.wallets[clientId[0]].ClientKey != "" { return client.wallets[clientId[0]] } return client.wallet @@ -184,7 +184,7 @@ func Sign(hash string, clientId ...string) (string, error) { } func IsWalletSet(clientId ...string) bool { - if len(clientId) > 0 { + if len(clientId) > 0 && client.wallets[clientId[0]].ClientKey != "" { _, ok := client.wallets[clientId[0]] return ok } @@ -192,21 +192,21 @@ func IsWalletSet(clientId ...string) bool { } func PublicKey(clientId ...string) string { - if len(clientId) > 0 { + if len(clientId) > 0 && client.wallets[clientId[0]].ClientKey != "" { return client.wallets[clientId[0]].ClientKey } return client.wallet.ClientKey } func Mnemonic(clientId ...string) string { - if len(clientId) > 0 { + if len(clientId) > 0 && client.wallets[clientId[0]].ClientKey != "" { return client.wallets[clientId[0]].Mnemonic } return client.wallet.Mnemonic } func PrivateKey(clientId ...string) string { - if len(clientId) > 0 { + if len(clientId) > 0 && client.wallets[clientId[0]].ClientKey != "" { for _, kv := range client.wallets[clientId[0]].Keys { return kv.PrivateKey } @@ -218,14 +218,14 @@ func PrivateKey(clientId ...string) string { } func Id(clientId ...string) string { - if len(clientId) > 0 { + if len(clientId) > 0 && client.wallets[clientId[0]].ClientKey != "" { return clientId[0] } return client.wallet.ClientID } func GetClient(clientId ...string) *zcncrypto.Wallet { - if len(clientId) > 0 { + if len(clientId) > 0 && client.wallets[clientId[0]].ClientKey != "" { return client.wallets[clientId[0]] } return client.wallet diff --git a/core/transaction/entity.go b/core/transaction/entity.go index d67283f07..1e47c71fe 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -510,7 +510,7 @@ func SmartContractTxnValueFeeWithRetry(scAddress string, sn SmartContractTxnData func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, value, fee uint64, clients ...string) (hash, out string, nonce int64, t *Transaction, err error) { - clientId := client.Id() + clientId := client.Id(clients...) if len(clients) > 0 && clients[0] != "" { clientId = clients[0] } diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index ec0638d2a..1eb85d592 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -857,6 +857,7 @@ func (a *Allocation) GetCurrentVersion() (bool, error) { markerChan <- nil } else { markerChan <- &RollbackBlobber{ + ClientId: a.Owner, blobber: blobber, lpm: wr, commitResult: &CommitResult{}, @@ -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 @@ -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 @@ -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 @@ -1880,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 @@ -1999,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 diff --git a/zboxcore/sdk/allocation_file_delete_test.go b/zboxcore/sdk/allocation_file_delete_test.go index 179f7d01c..81a0580d8 100644 --- a/zboxcore/sdk/allocation_file_delete_test.go +++ b/zboxcore/sdk/allocation_file_delete_test.go @@ -167,6 +167,7 @@ func TestAllocation_deleteFile(t *testing.T) { DataShards: 2, ParityShards: 2, FileOptions: 63, + Owner: mockClientId, } a.InitAllocation() sdkInitialized = true diff --git a/zboxcore/sdk/allocation_file_test.go b/zboxcore/sdk/allocation_file_test.go index ed3920133..ed530defd 100644 --- a/zboxcore/sdk/allocation_file_test.go +++ b/zboxcore/sdk/allocation_file_test.go @@ -221,6 +221,7 @@ func TestAllocation_UpdateFile(t *testing.T) { ParityShards: 2, DataShards: 2, Size: 2 * GB, + Owner: mockClientId, } setupMockAllocation(t, a) @@ -290,6 +291,7 @@ func TestAllocation_UploadFile(t *testing.T) { ParityShards: 2, DataShards: 2, Size: 2 * GB, + Owner: mockClientId, } setupMockAllocation(t, a) @@ -328,6 +330,7 @@ func TestAllocation_UpdateFileWithThumbnail(t *testing.T) { ParityShards: 2, DataShards: 2, Size: 2 * GB, + Owner: mockClientId, } setupMockAllocation(t, a) @@ -407,6 +410,7 @@ func TestAllocation_UploadFileWithThumbnail(t *testing.T) { ParityShards: 2, DataShards: 2, Size: 2 * GB, + Owner: mockClientId, } setupMockAllocation(t, a) @@ -442,6 +446,7 @@ func TestAllocation_EncryptAndUpdateFile(t *testing.T) { ParityShards: 2, DataShards: 2, Size: 2 * GB, + Owner: mockClientId, } setupMockAllocation(t, a) @@ -513,6 +518,7 @@ func TestAllocation_EncryptAndUploadFile(t *testing.T) { ParityShards: 2, DataShards: 2, Size: 2 * GB, + Owner: mockClientId, } setupMockAllocation(t, a) @@ -561,6 +567,7 @@ func TestAllocation_EncryptAndUpdateFileWithThumbnail(t *testing.T) { ParityShards: 2, DataShards: 2, Size: 2 * GB, + Owner: mockClientId, } setupMockAllocation(t, a) @@ -637,6 +644,7 @@ func TestAllocation_EncryptAndUploadFileWithThumbnail(t *testing.T) { DataShards: 2, Size: 2 * GB, ctx: context.TODO(), + Owner: mockClientId, } setupMockAllocation(t, a) @@ -890,6 +898,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) diff --git a/zboxcore/sdk/allocation_test.go b/zboxcore/sdk/allocation_test.go index 63d3fd620..aa72a070c 100644 --- a/zboxcore/sdk/allocation_test.go +++ b/zboxcore/sdk/allocation_test.go @@ -392,6 +392,7 @@ func TestAllocation_GetStats(t *testing.T) { stats := &AllocationStats{} a := &Allocation{ Stats: stats, + Owner: mockClientId, } got := a.GetStats() require.New(t).Same(stats, got) @@ -434,8 +435,9 @@ func TestAllocation_GetBlobberStats(t *testing.T) { require := require.New(t) tt.setup(t, tt.name) a := &Allocation{ - ID: mockAllocationId, - Tx: mockAllocationTxId, + ID: mockAllocationId, + Tx: mockAllocationTxId, + Owner: mockClientId, } a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ ID: tt.name + mockBlobberId, @@ -487,7 +489,7 @@ func TestAllocation_isInitialized(t *testing.T) { originalSDKInitialized := sdkInitialized defer func() { sdkInitialized = originalSDKInitialized }() sdkInitialized = tt.sdkInitialized - a := &Allocation{initialized: tt.allocationInitialized} + a := &Allocation{initialized: tt.allocationInitialized, Owner: mockClientId} got := a.isInitialized() require := require.New(t) if tt.want { @@ -657,6 +659,7 @@ func TestAllocation_RepairRequired(t *testing.T) { DataShards: 2, ParityShards: 2, FileOptions: 63, + Owner: mockClientId, } a.InitAllocation() sdkInitialized = true @@ -740,7 +743,7 @@ func TestAllocation_DownloadFileToFileHandler(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { require := require.New(t) - a := &Allocation{} + a := &Allocation{Owner: mockClientId} setupMockAllocation(t, a) for i := 0; i < numBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ @@ -783,6 +786,7 @@ func TestAllocation_DownloadFile(t *testing.T) { a := &Allocation{ ParityShards: 2, DataShards: 2, + Owner: mockClientId, } setupMockAllocation(t, a) @@ -830,6 +834,7 @@ func TestAllocation_DownloadFileByBlock(t *testing.T) { a := &Allocation{ ParityShards: 2, DataShards: 2, + Owner: mockClientId, } setupMockAllocation(t, a) @@ -850,7 +855,7 @@ func TestAllocation_DownloadThumbnail(t *testing.T) { mockRemoteFilePath = "1.txt" ) require := require.New(t) - a := &Allocation{} + a := &Allocation{Owner: mockClientId} setupMockAllocation(t, a) for i := 0; i < numBlobbers; i++ { @@ -966,7 +971,7 @@ func TestAllocation_downloadFile(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { require := require.New(t) - a := &Allocation{} + a := &Allocation{Owner: mockClientId} a.downloadChan = make(chan *DownloadRequest, 10) a.repairChan = make(chan *RepairRequest, 1) a.ctx, a.ctxCancelF = context.WithCancel(context.Background()) @@ -1023,6 +1028,7 @@ func TestAllocation_GetRefs(t *testing.T) { a := &Allocation{ DataShards: 2, ParityShards: 2, + Owner: mockClientId, } testCaseName := "Test_Get_Refs_Returns_Slice_Of_Length_0_When_File_Not_Present" a.InitAllocation() @@ -1123,6 +1129,7 @@ func TestAllocation_GetFileMeta(t *testing.T) { DataShards: 2, ParityShards: 2, FileOptions: 63, + Owner: mockClientId, } a.InitAllocation() sdkInitialized = true @@ -1180,7 +1187,7 @@ func TestAllocation_GetAuthTicketForShare(t *testing.T) { ClientKey: mockClientKey, }) require := require.New(t) - a := &Allocation{DataShards: 1, ParityShards: 1, FileOptions: 63} + a := &Allocation{DataShards: 1, ParityShards: 1, FileOptions: 63, Owner: mockClientId} a.InitAllocation() for i := 0; i < numberBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{}) @@ -1390,6 +1397,7 @@ func TestAllocation_GetAuthTicket(t *testing.T) { DataShards: 1, ParityShards: 1, FileOptions: 63, + Owner: mockClientId, } a.InitAllocation() sdkInitialized = true @@ -1453,7 +1461,7 @@ func TestAllocation_CancelDownload(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { require := require.New(t) - a := &Allocation{FileOptions: 63} + a := &Allocation{FileOptions: 63, Owner: mockClientId} a.InitAllocation() sdkInitialized = true if tt.setup != nil { @@ -1559,8 +1567,9 @@ func TestAllocation_ListDirFromAuthTicket(t *testing.T) { authTicket: authTicket, lookupHash: mockLookupHash, expectedResult: &ListResult{ - Type: mockType, - Size: 0, + ClientId: mockClientId, + Type: mockType, + Size: 0, }, }, setup: func(t *testing.T, testCaseName string, a *Allocation, mockClient *mocks.HttpClient) (teardown func(t *testing.T)) { @@ -1594,6 +1603,7 @@ func TestAllocation_ListDirFromAuthTicket(t *testing.T) { ClientKey: mockClientKey, }) a := &Allocation{ + Owner: mockClientId, ID: mockAllocationId, Tx: mockAllocationTxId, FileOptions: 63, @@ -1652,6 +1662,7 @@ func TestAllocation_downloadFromAuthTicket(t *testing.T) { Tx: mockAllocationTxId, DataShards: 2, ParityShards: 2, + Owner: mockClientId, } setupMockAllocation(t, a) setupMockGetFileInfoResponse(t, &mockClient) @@ -1844,8 +1855,9 @@ func TestAllocation_listDir(t *testing.T) { parameters: parameters{ path: mockPath, expectedResult: &ListResult{ - Type: mockType, - Size: 0, + ClientId: mockClientId, + Type: mockType, + Size: 0, }, }, setup: func(t *testing.T, testCaseName string, a *Allocation, mockClient *mocks.HttpClient) (teardown func(t *testing.T)) { @@ -1879,6 +1891,7 @@ func TestAllocation_listDir(t *testing.T) { FileOptions: 63, DataShards: 2, ParityShards: 2, + Owner: mockClientId, } if tt.parameters.expectedResult != nil { tt.parameters.expectedResult.deleteMask = zboxutil.NewUint128(1).Lsh(uint64(a.DataShards + a.ParityShards)).Sub64(1) @@ -2011,6 +2024,7 @@ func TestAllocation_GetFileMetaFromAuthTicket(t *testing.T) { DataShards: 2, ParityShards: 2, FileOptions: 63, + Owner: mockClientId, } a.InitAllocation() sdkInitialized = true @@ -2055,7 +2069,7 @@ func TestAllocation_DownloadToFileHandlerFromAuthTicket(t *testing.T) { require := require.New(t) - a := &Allocation{} + a := &Allocation{Owner: mockClientId} setupMockAllocation(t, a) for i := 0; i < numBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ @@ -2090,7 +2104,7 @@ func TestAllocation_DownloadThumbnailFromAuthTicket(t *testing.T) { require := require.New(t) - a := &Allocation{} + a := &Allocation{Owner: mockClientId} setupMockAllocation(t, a) for i := 0; i < numBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ @@ -2132,7 +2146,7 @@ func TestAllocation_DownloadFromAuthTicket(t *testing.T) { require := require.New(t) - a := &Allocation{} + a := &Allocation{Owner: mockClientId} setupMockAllocation(t, a) for i := 0; i < numBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ @@ -2168,7 +2182,7 @@ func TestAllocation_DownloadFromAuthTicketByBlocks(t *testing.T) { require := require.New(t) - a := &Allocation{} + a := &Allocation{Owner: mockClientId} setupMockAllocation(t, a) for i := 0; i < numBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ @@ -2248,6 +2262,7 @@ func TestAllocation_StartRepair(t *testing.T) { a := &Allocation{ DataShards: 2, ParityShards: 2, + Owner: mockClientId, } setupMockAllocation(t, a) for i := 0; i < numBlobbers; i++ { @@ -2295,7 +2310,7 @@ func TestAllocation_CancelRepair(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { require := require.New(t) - a := &Allocation{} + a := &Allocation{Owner: mockClientId} setupMockAllocation(t, a) if tt.setup != nil { if teardown := tt.setup(t, a); teardown != nil { @@ -2391,6 +2406,7 @@ func getMockAuthTicket(t *testing.T) string { DataShards: 1, ParityShards: 1, FileOptions: 63, + Owner: mockClientId, } a.InitAllocation() diff --git a/zboxcore/sdk/chunked_upload_bench_test.go b/zboxcore/sdk/chunked_upload_bench_test.go index 19ec6c6b3..6c4195628 100644 --- a/zboxcore/sdk/chunked_upload_bench_test.go +++ b/zboxcore/sdk/chunked_upload_bench_test.go @@ -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++ { diff --git a/zboxcore/sdk/chunked_upload_blobber.go b/zboxcore/sdk/chunked_upload_blobber.go index 2dcf20160..19aa492c7 100644 --- a/zboxcore/sdk/chunked_upload_blobber.go +++ b/zboxcore/sdk/chunked_upload_blobber.go @@ -200,7 +200,7 @@ func (sb *ChunkedUploadBlobber) processCommit(ctx context.Context, su *ChunkedUp wm.BlobberID = sb.blobber.ID wm.Timestamp = timestamp - wm.ClientID = client.Id() + wm.ClientID = client.Id(su.allocationObj.Owner) err = wm.Sign() if err != nil { logger.Logger.Error("Signing writemarker failed: ", err) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 4865b26dd..f6b18c613 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -248,7 +248,7 @@ func (req *CommitRequest) commitBlobber( wm.Size = size wm.BlobberID = req.blobber.ID wm.Timestamp = req.timestamp - wm.ClientID = client.Id() + wm.ClientID = client.Id(req.ClientId) err = wm.Sign() if err != nil { l.Logger.Error("Signing writemarker failed: ", err) diff --git a/zboxcore/sdk/copyworker_test.go b/zboxcore/sdk/copyworker_test.go index 3baa183e1..296bfb348 100644 --- a/zboxcore/sdk/copyworker_test.go +++ b/zboxcore/sdk/copyworker_test.go @@ -211,6 +211,9 @@ func TestCopyRequest_copyBlobberObject(t *testing.T) { maskMU: &sync.Mutex{}, ctx: context.TODO(), connectionID: mockConnectionId, + allocationObj: &Allocation{ + Owner: mockClientId, + }, } req.blobbers = append(req.blobbers, &blockchain.StorageNode{ Baseurl: tt.name, @@ -441,6 +444,7 @@ func TestCopyRequest_ProcessCopy(t *testing.T) { Tx: "TestCopyRequest_ProcessCopy", DataShards: numBlobbers, FileOptions: 63, + Owner: mockClientId, } a.InitAllocation() diff --git a/zboxcore/sdk/deleteworker_test.go b/zboxcore/sdk/deleteworker_test.go index 33e75a206..cf40f4bb9 100644 --- a/zboxcore/sdk/deleteworker_test.go +++ b/zboxcore/sdk/deleteworker_test.go @@ -179,6 +179,9 @@ func TestDeleteRequest_deleteBlobberFile(t *testing.T) { ctx: context.TODO(), connectionID: mockConnectionId, wg: func() *sync.WaitGroup { wg.Add(1); return &wg }(), + allocationObj: &Allocation{ + Owner: mockClientId, + }, } req.blobbers = append(req.blobbers, &blockchain.StorageNode{ Baseurl: tt.name, diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index 9500f358a..03215c41a 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -828,7 +828,7 @@ func (req *DownloadRequest) attemptSubmitReadMarker(blobber *blockchain.StorageN lockBlobberReadCtr(req.allocationID, blobber.ID) defer unlockBlobberReadCtr(req.allocationID, blobber.ID) rm := &marker.ReadMarker{ - ClientID: client.Id(), + ClientID: client.Id(req.ClientId), ClientPublicKey: client.PublicKey(), BlobberID: blobber.ID, AllocationID: req.allocationID, diff --git a/zboxcore/sdk/listworker.go b/zboxcore/sdk/listworker.go index 454fe770c..16d819504 100644 --- a/zboxcore/sdk/listworker.go +++ b/zboxcore/sdk/listworker.go @@ -50,6 +50,7 @@ type listResponse struct { // ListResult a wrapper around the result of directory listing command. // It can represent a file or a directory. type ListResult struct { + ClientId string `json:"client_id"` Name string `json:"name"` Path string `json:"path,omitempty"` Type string `json:"type"` @@ -227,6 +228,7 @@ func (req *ListRequest) GetListFromBlobbers() (*ListResult, error) { return nil, err } result := &ListResult{ + ClientId: req.ClientId, deleteMask: zboxutil.NewUint128(1).Lsh(uint64(len(req.blobbers))).Sub64(1), } selected := make(map[string]*ListResult) @@ -294,6 +296,7 @@ func (lr *ListResult) populateChildren(children []fileref.RefEntity, childResult var childResult *ListResult if _, ok := childResultMap[actualHash]; !ok { childResult = &ListResult{ + ClientId: lr.ClientId, Name: child.GetName(), Path: child.GetPath(), Type: child.GetType(), diff --git a/zboxcore/sdk/listworker_test.go b/zboxcore/sdk/listworker_test.go index 46548442e..1d2cc03c8 100644 --- a/zboxcore/sdk/listworker_test.go +++ b/zboxcore/sdk/listworker_test.go @@ -281,6 +281,7 @@ func TestListRequest_GetListFromBlobbers(t *testing.T) { tt.setup(t, tt.name, tt.numBlobbers) } req := &ListRequest{ + ClientId: mockClientId, allocationID: mockAllocationId, allocationTx: mockAllocationTxId, ctx: context.TODO(), @@ -299,6 +300,7 @@ func TestListRequest_GetListFromBlobbers(t *testing.T) { } got, _ := req.GetListFromBlobbers() expectedResult := &ListResult{ + ClientId: mockClientId, Type: mockType, Size: 0, deleteMask: zboxutil.NewUint128(1).Lsh(uint64(len(req.blobbers))).Sub64(1), diff --git a/zboxcore/sdk/renameworker_test.go b/zboxcore/sdk/renameworker_test.go index b14eaab10..68cb5dcd5 100644 --- a/zboxcore/sdk/renameworker_test.go +++ b/zboxcore/sdk/renameworker_test.go @@ -227,6 +227,9 @@ func TestRenameRequest_renameBlobberObject(t *testing.T) { require := require.New(t) tt.setup(t, tt.name, tt.parameters) req := &RenameRequest{ + allocationObj: &Allocation{ + Owner: mockClientId, + }, allocationID: mockAllocationId, allocationTx: mockAllocationTxId, remotefilepath: mockRemoteFilePath, @@ -462,6 +465,7 @@ func TestRenameRequest_ProcessRename(t *testing.T) { a := &Allocation{ Tx: "TestRenameRequest_ProcessRename", DataShards: numBlobbers, + Owner: mockClientId, } setupMockAllocation(t, a) diff --git a/zboxcore/sdk/rollback.go b/zboxcore/sdk/rollback.go index 4e60ac77b..7470b2bb4 100644 --- a/zboxcore/sdk/rollback.go +++ b/zboxcore/sdk/rollback.go @@ -49,6 +49,7 @@ var ( ) type RollbackBlobber struct { + ClientId string blobber *blockchain.StorageNode commitResult *CommitResult lpm *LatestPrevWriteMarker @@ -281,6 +282,7 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { markerChan <- nil } else { markerChan <- &RollbackBlobber{ + ClientId: a.Owner, blobber: blobber, lpm: wr, commitResult: &CommitResult{}, @@ -405,6 +407,7 @@ func (a *Allocation) RollbackWithMask(mask zboxutil.Uint128) { markerChan <- nil } else { markerChan <- &RollbackBlobber{ + ClientId: a.Owner, blobber: blobber, lpm: wr, commitResult: &CommitResult{}, diff --git a/zboxcore/sdk/sharerequest.go b/zboxcore/sdk/sharerequest.go index b362d6532..d8a45038f 100644 --- a/zboxcore/sdk/sharerequest.go +++ b/zboxcore/sdk/sharerequest.go @@ -56,7 +56,7 @@ func (req *ShareRequest) getAuthTicket(clientID, encPublicKey string) (*marker.A at := &marker.AuthTicket{ AllocationID: req.allocationID, - OwnerID: client.Id(), + OwnerID: client.Id(req.ClientId), ClientID: clientID, FileName: req.remotefilename, FilePathHash: fileref.GetReferenceLookup(req.allocationID, req.remotefilepath), diff --git a/zboxcore/sdk/writemarker_mutex_test.go b/zboxcore/sdk/writemarker_mutex_test.go index 8645810e4..d1ccd7853 100644 --- a/zboxcore/sdk/writemarker_mutex_test.go +++ b/zboxcore/sdk/writemarker_mutex_test.go @@ -34,6 +34,7 @@ func TestWriteMarkerMutext_Should_Lock(t *testing.T) { Tx: "TestWriteMarkerMutext", DataShards: 2, ParityShards: 1, + Owner: mockClientId, } setupMockAllocation(t, a) @@ -102,6 +103,7 @@ func TestWriteMarkerMutext_Some_Blobbers_Down_Should_Lock(t *testing.T) { Tx: "TestWriteMarkerMutext", DataShards: 2, ParityShards: 2, + Owner: mockClientId, } setupMockAllocation(t, a) @@ -171,6 +173,7 @@ func TestWriteMarkerMutext_Too_Less_Blobbers_Response_Should_Not_Lock(t *testing Tx: "TestWriteMarkerMutext", DataShards: 2, ParityShards: 1, + Owner: mockClientId, } setupMockAllocation(t, a) From 101e3b2e98d686770e4b946c587886fe77be9ed8 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 20 Sep 2024 02:06:52 +0530 Subject: [PATCH 109/319] Fix --- core/client/set.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/client/set.go b/core/client/set.go index 9eec43bd9..90478929b 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -217,6 +217,13 @@ func PrivateKey(clientId ...string) string { return "" } +func ClientID(clientId ...string) string { + if len(clientId) > 0 && client.wallets[clientId[0]].ClientKey != "" { + return clientId[0] + } + return client.wallet.ClientID +} + func Id(clientId ...string) string { if len(clientId) > 0 && client.wallets[clientId[0]].ClientKey != "" { return clientId[0] From fa5e1cec2d8997f8a68d66c0d1e8fa6a1d4f6db4 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 20 Sep 2024 03:47:30 +0530 Subject: [PATCH 110/319] Fix --- zcncore/wallet_base.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 62c9342b6..8740e9c85 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -208,7 +208,7 @@ func RecoverOfflineWallet(mnemonic string) (string, error) { if !zcncrypto.IsMnemonicValid(mnemonic) { return "", errors.New("Invalid mnemonic") } - sigScheme := zcncrypto.NewSignatureScheme(client.SignatureScheme()) + sigScheme := zcncrypto.NewSignatureScheme("bls0chain") wallet, err := sigScheme.RecoverKeys(mnemonic) if err != nil { return "", err From d82518f49fa19dccb8f57d2c7b5ed38fe6dba872 Mon Sep 17 00:00:00 2001 From: Yash Verma Date: Sat, 21 Sep 2024 05:33:02 +0530 Subject: [PATCH 111/319] Fix/refactor zboxcore burn zcn (#1622) * fix: error message MakeSCRestAPICall. debug: some gosdk method calling for updating blobber settings method check. * fix: burn ZCN pass burn amount as well * revert: play.go --- zcnbridge/bridge.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zcnbridge/bridge.go b/zcnbridge/bridge.go index 4a803a9d9..fccfa6515 100644 --- a/zcnbridge/bridge.go +++ b/zcnbridge/bridge.go @@ -660,10 +660,10 @@ func (b *BridgeClient) BurnZCN(amount uint64) (string, string, error) { zap.Uint64("burn amount", amount), ) - hash, out, _, _, err := coreTransaction.SmartContractTxn(wallet.ZCNSCSmartContractAddress, coreTransaction.SmartContractTxnData{ - Name: wallet.MintFunc, + hash, out, _, _, err := coreTransaction.SmartContractTxnValue(wallet.ZCNSCSmartContractAddress, coreTransaction.SmartContractTxnData{ + Name: wallet.BurnFunc, InputArgs: payload, - }) + }, amount) if err != nil { Logger.Error("Burn ZCN transaction FAILED", zap.Error(err)) return hash, out, errors.Wrap(err, fmt.Sprintf("failed to execute smart contract, hash = %s", hash)) From 4768e9c9624ef8f933eb3375409a914d8003f6a7 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 21 Sep 2024 20:18:22 +0530 Subject: [PATCH 112/319] Fix get mint nonce --- zcncore/get_data.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zcncore/get_data.go b/zcncore/get_data.go index 1197f6ae0..436432736 100644 --- a/zcncore/get_data.go +++ b/zcncore/get_data.go @@ -190,7 +190,7 @@ func GetMintNonce() ([]byte, error) { return nil, err } - return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINT_NONCE, Params{ + return client.MakeSCRestAPICall(ZCNSCSmartContractAddress, GET_MINT_NONCE, Params{ "client_id": client.ClientID(), }) } From bf0ab98c904d38fad6e3e03c675870d37dae006b Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 21 Sep 2024 20:20:29 +0530 Subject: [PATCH 113/319] Fix --- zcncore/get_data.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/zcncore/get_data.go b/zcncore/get_data.go index 436432736..09d5d1df8 100644 --- a/zcncore/get_data.go +++ b/zcncore/get_data.go @@ -281,7 +281,10 @@ func GetNotProcessedZCNBurnTickets(ethereumAddress, startNonce string) ([]byte, if err != nil { return nil, err } - return client.MakeSCRestAPICall(MinerSmartContractAddress, GET_MINERSC_POOL, Params{ + + const GET_NOT_PROCESSED_BURN_TICKETS = `/v1/not_processed_burn_tickets` + + return client.MakeSCRestAPICall(ZCNSCSmartContractAddress, GET_NOT_PROCESSED_BURN_TICKETS, Params{ "ethereum_address": ethereumAddress, "nonce": startNonce, }) From 0a7da38725259f7a14d00ce67f0fc11921e249c5 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sat, 21 Sep 2024 22:11:42 +0530 Subject: [PATCH 114/319] add repair for v2 --- core/sys/util.go | 39 +++++++- zboxcore/sdk/allocation.go | 21 ++-- zboxcore/sdk/filerefsworker.go | 1 + zboxcore/sdk/repairworker.go | 172 +++++++++++++++++++++++++++++++-- zboxcore/sdk/rollback.go | 7 ++ zboxcore/sdk/upload_worker.go | 2 +- 6 files changed, 226 insertions(+), 16 deletions(-) diff --git a/core/sys/util.go b/core/sys/util.go index e911ff3f2..1ac7a6990 100644 --- a/core/sys/util.go +++ b/core/sys/util.go @@ -146,7 +146,7 @@ func (f *MemChanFile) Stat() (fs.FileInfo, error) { // Read reads data from the file through the buffer channel // It returns io.EOF when the buffer channel is closed. -// - p: file in bytes loaded from the buffer channel +// - p: file in bytes loaded from the buffer channel func (f *MemChanFile) Read(p []byte) (int, error) { select { case err := <-f.ErrChan: @@ -166,7 +166,7 @@ func (f *MemChanFile) Read(p []byte) (int, error) { // Write writes data to the file through the buffer channel // It writes the data to the buffer channel in chunks of ChunkWriteSize. // If ChunkWriteSize is 0, it writes the data as a whole. -// - p: file in bytes to write to the buffer channel +// - p: file in bytes to write to the buffer channel func (f *MemChanFile) Write(p []byte) (n int, err error) { if f.ChunkWriteSize == 0 { data := make([]byte, len(p)) @@ -253,3 +253,38 @@ func (i *MemFileChanInfo) Sys() interface{} { func (i *MemFileChanInfo) Info() (fs.FileInfo, error) { return i, nil } + +// wrapper over io pipe to implement File interface +type PipeFile struct { + w *io.PipeWriter + r *io.PipeReader +} + +func NewPipeFile() *PipeFile { + r, w := io.Pipe() + return &PipeFile{w: w, r: r} +} + +func (pf *PipeFile) Write(p []byte) (int, error) { + return pf.w.Write(p) +} + +func (pf *PipeFile) Close() error { + return pf.w.Close() +} + +func (pf *PipeFile) Read(p []byte) (int, error) { + return pf.r.Read(p) +} + +func (pf *PipeFile) Stat() (fs.FileInfo, error) { + return nil, nil +} + +func (pf *PipeFile) Sync() error { + return nil +} + +func (pf *PipeFile) Seek(offset int64, whence int) (int64, error) { + return 0, nil +} diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 3df6820b5..12b516df9 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -1788,7 +1788,7 @@ func (a *Allocation) GetRefs(path, offsetPath, updatedDate, offsetDate, fileType return a.getRefs(path, "", "", offsetPath, updatedDate, offsetDate, fileType, refType, level, pageLimit, opts...) } -func (a *Allocation) ListObjects(ctx context.Context, path, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int) <-chan ORef { +func (a *Allocation) ListObjects(ctx context.Context, path, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int, opts ...ObjectTreeRequestOption) <-chan ORef { oRefChan := make(chan ORef, 1) sendObjectRef := func(ref ORef) { select { @@ -1807,7 +1807,7 @@ func (a *Allocation) ListObjects(ctx context.Context, path, offsetPath, updatedD }() continuationPath := offsetPath for { - oRefs, err := a.GetRefs(path, continuationPath, updatedDate, offsetDate, fileType, refType, level, pageLimit) + oRefs, err := a.GetRefs(path, continuationPath, updatedDate, offsetDate, fileType, refType, level, pageLimit, opts...) if err != nil { sendObjectRef(ORef{ Err: err, @@ -2825,18 +2825,25 @@ func (a *Allocation) StartRepair(localRootPath, pathToRepair string, statusCB St return notInitialized } - listDir, err := a.ListDir(pathToRepair, - WithListRequestForRepair(true), - WithListRequestPageLimit(-1), + var ( + listDir *ListResult + err error ) - if err != nil { - return err + if a.StorageVersion == 0 { + listDir, err = a.ListDir(pathToRepair, + WithListRequestForRepair(true), + WithListRequestPageLimit(-1), + ) + if err != nil { + return err + } } repairReq := &RepairRequest{ listDir: listDir, localRootPath: localRootPath, statusCB: statusCB, + repairPath: pathToRepair, } repairReq.completedCallback = func() { diff --git a/zboxcore/sdk/filerefsworker.go b/zboxcore/sdk/filerefsworker.go index f9778834e..924ae7cf1 100644 --- a/zboxcore/sdk/filerefsworker.go +++ b/zboxcore/sdk/filerefsworker.go @@ -323,6 +323,7 @@ type SimilarField struct { PathLevel int `json:"level"` Size int64 `json:"size"` EncryptedKey string `json:"encrypted_key"` + EncryptedKeyPoint string `json:"encrypted_key_point"` ActualFileSize int64 `json:"actual_file_size"` ActualFileHash string `json:"actual_file_hash"` MimeType string `json:"mimetype"` diff --git a/zboxcore/sdk/repairworker.go b/zboxcore/sdk/repairworker.go index bfb42fd8c..258fe18d7 100644 --- a/zboxcore/sdk/repairworker.go +++ b/zboxcore/sdk/repairworker.go @@ -2,6 +2,7 @@ package sdk import ( "context" + "errors" "fmt" "io" "sync" @@ -23,6 +24,7 @@ type RepairRequest struct { filesRepaired int wg *sync.WaitGroup allocation *Allocation + repairPath string } type RepairStatusCB struct { @@ -77,7 +79,12 @@ func (r *RepairRequest) processRepair(ctx context.Context, a *Allocation) { SetSingleClietnMode(true) defer SetSingleClietnMode(false) } - r.iterateDir(a, r.listDir) + r.allocation = a + if a.StorageVersion == StorageV2 { + r.iterateDirV2(ctx) + } else { + r.iterateDir(a, r.listDir) + } if r.statusCB != nil { r.statusCB.RepairCompleted(r.filesRepaired) } @@ -234,11 +241,8 @@ func (r *RepairRequest) repairFile(a *Allocation, file *ListResult) []OperationR if r.checkForCancel(a) { return nil } - memFile := &sys.MemChanFile{ - Buffer: make(chan []byte, 100), - ChunkWriteSize: int(a.GetChunkReadSize(ref.EncryptedKey != "")), - } - op = a.RepairFile(memFile, file.Path, statusCB, found, ref) + pipeFile := sys.NewPipeFile() + op = a.RepairFile(pipeFile, file.Path, statusCB, found, ref) if op.FileMeta.ActualSize > 0 { op.DownloadFile = true } @@ -325,3 +329,159 @@ func (r *RepairRequest) checkForCancel(a *Allocation) bool { } return false } + +type diffRef struct { + tgtRef ORef + tgtChan <-chan ORef + tgtEOF bool + mask zboxutil.Uint128 +} + +func (r *RepairRequest) iterateDirV2(ctx context.Context) { + versionMap := make(map[string]*diffRef) + r.allocation.CheckAllocStatus() //nolint:errcheck + latestRoot := r.allocation.allocationRoot + for idx, blobber := range r.allocation.Blobbers { + if versionMap[blobber.AllocationRoot] == nil { + versionMap[blobber.AllocationRoot] = &diffRef{} + } + versionMap[blobber.AllocationRoot].mask = versionMap[blobber.AllocationRoot].mask.Or(zboxutil.NewUint128(1).Lsh(uint64(idx))) + } + if versionMap[latestRoot].mask.CountOnes() < r.allocation.DataShards { + l.Logger.Error("No consensus on latest allocation root: ", latestRoot) + if r.statusCB != nil { + r.statusCB.Error(r.allocation.ID, r.repairPath, OpRepair, errors.New("no consensus on latest allocation root")) + } + return + } + if len(versionMap) == 1 { + return + } + // get the src list channel + srcChan := r.allocation.ListObjects(ctx, r.repairPath, "", "", "", fileref.FILE, fileref.REGULAR, 0, getRefPageLimit, WithSingleBlobber(true), WithObjectMask(versionMap[latestRoot].mask), WithObjectContext(ctx)) + + for root, diff := range versionMap { + if root == latestRoot { + continue + } + diff.tgtChan = r.allocation.ListObjects(ctx, r.repairPath, "", "", "", fileref.FILE, fileref.REGULAR, 0, getRefPageLimit, WithSingleBlobber(true), WithObjectMask(diff.mask), WithObjectContext(ctx)) + diff.tgtRef, diff.tgtEOF = <-diff.tgtChan + } + var ( + toNextRef = true + srcRef ORef + srcEOF bool + ops []OperationRequest + ) + for { + if toNextRef { + if srcEOF { + break + } + srcRef, srcEOF = <-srcChan + if srcRef.Err != nil { + l.Logger.Error("Failed to get source file reference ", srcRef.Err.Error()) + if r.statusCB != nil { + r.statusCB.Error(r.allocation.ID, r.repairPath, OpRepair, srcRef.Err) + } + return + } + } + toNextRef = true + var ( + uploadMask zboxutil.Uint128 + deleteMask zboxutil.Uint128 + ) + for root, diff := range versionMap { + if root == latestRoot { + continue + } + + // check if both target and source are at EOF + if srcEOF && diff.tgtEOF { + continue + } + if diff.tgtEOF { + uploadMask = uploadMask.Or(diff.mask) + continue + } + if srcEOF { + delMask := diff.mask + op := OperationRequest{ + OperationType: constants.FileOperationDelete, + RemotePath: diff.tgtRef.Path, + Mask: &delMask, + } + ops = append(ops, op) + diff.tgtRef, diff.tgtEOF = <-diff.tgtChan + toNextRef = false + continue + } + if diff.tgtRef.Err != nil { + l.Logger.Error("Failed to get target file reference ", diff.tgtRef.Err.Error()) + if r.statusCB != nil { + r.statusCB.Error(r.allocation.ID, r.repairPath, OpRepair, diff.tgtRef.Err) + } + continue + } + if diff.tgtRef.Path == srcRef.Path { + if diff.tgtRef.ActualFileHash != srcRef.ActualFileHash { + deleteMask = deleteMask.Or(diff.mask) + uploadMask = uploadMask.Or(diff.mask) + } + diff.tgtRef, diff.tgtEOF = <-diff.tgtChan + } else if diff.tgtRef.Path < srcRef.Path { + deleteMask = deleteMask.Or(diff.mask) + toNextRef = false + diff.tgtRef, diff.tgtEOF = <-diff.tgtChan + } + } + if deleteMask.CountOnes() > 0 { + op := OperationRequest{ + OperationType: constants.FileOperationDelete, + RemotePath: srcRef.Path, + Mask: &deleteMask, + } + ops = append(ops, op) + } + if uploadMask.CountOnes() > 0 { + op := r.uploadFileOp(srcRef, uploadMask) + ops = append(ops, op) + } + if len(ops) >= RepairBatchSize { + r.repairOperation(r.allocation, ops) + ops = nil + } + } + if len(ops) > 0 { + r.repairOperation(r.allocation, ops) + ops = nil + } + +} + +func (r *RepairRequest) uploadFileOp(file ORef, opMask zboxutil.Uint128) OperationRequest { + var wg sync.WaitGroup + wg.Add(1) + statusCB := &RepairStatusCB{ + wg: &wg, + statusCB: r.statusCB, + } + + ref := &fileref.FileRef{ + ActualFileSize: file.ActualFileSize, + MimeType: file.MimeType, + CustomMeta: file.CustomMeta, + Ref: fileref.Ref{ + Name: file.Name, + }, + EncryptedKey: file.EncryptedKey, + EncryptedKeyPoint: file.EncryptedKeyPoint, + } + pipeFile := sys.NewPipeFile() + op := r.allocation.RepairFile(pipeFile, file.Path, statusCB, opMask, ref) + if op.FileMeta.ActualSize > 0 { + op.DownloadFile = true + } + return *op +} diff --git a/zboxcore/sdk/rollback.go b/zboxcore/sdk/rollback.go index ff28ed7dc..fe865e88d 100644 --- a/zboxcore/sdk/rollback.go +++ b/zboxcore/sdk/rollback.go @@ -244,6 +244,8 @@ func (rb *RollbackBlobber) processRollback(ctx context.Context, tx string) error if shouldContinue { continue } + rb.blobber.LatestWM = wm + rb.blobber.AllocationRoot = wm.AllocationRoot return nil } @@ -350,6 +352,11 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { } if len(versionMap[latestVersion]) >= req || len(versionMap[prevVersion]) >= req || len(versionMap) > 2 { + if len(versionMap[latestVersion]) >= req { + a.allocationRoot = versionMap[latestVersion][0].lpm.LatestWM.AllocationRoot + } else if len(versionMap[prevVersion]) > 0 { + a.allocationRoot = versionMap[prevVersion][0].lpm.LatestWM.AllocationRoot + } for _, rb := range versionMap[prevVersion] { blobberRes[rb.blobIndex].Status = "repair" } diff --git a/zboxcore/sdk/upload_worker.go b/zboxcore/sdk/upload_worker.go index 6d1315824..34cf88de4 100644 --- a/zboxcore/sdk/upload_worker.go +++ b/zboxcore/sdk/upload_worker.go @@ -32,7 +32,7 @@ var ErrPauseUpload = errors.New("upload paused by user") func (uo *UploadOperation) Process(allocObj *Allocation, connectionID string) ([]fileref.RefEntity, zboxutil.Uint128, error) { if uo.isDownload { - if f, ok := uo.chunkedUpload.fileReader.(*sys.MemChanFile); ok { + if f, ok := uo.chunkedUpload.fileReader.(sys.File); ok { err := allocObj.DownloadFileToFileHandler(f, uo.chunkedUpload.fileMeta.RemotePath, false, nil, true, WithFileCallback(func() { f.Close() //nolint:errcheck })) From 527afc129b671cb6a104b7eec3727ebe4b287102 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sun, 22 Sep 2024 00:05:18 +0530 Subject: [PATCH 115/319] check for cancel --- zboxcore/sdk/repairworker.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/zboxcore/sdk/repairworker.go b/zboxcore/sdk/repairworker.go index 258fe18d7..093bba6f6 100644 --- a/zboxcore/sdk/repairworker.go +++ b/zboxcore/sdk/repairworker.go @@ -69,6 +69,8 @@ func (r *RepairRequest) processRepair(ctx context.Context, a *Allocation) { if r.checkForCancel(a) { return } + ctx, cancel := context.WithCancel(ctx) + defer cancel() SetNumBlockDownloads(RepairBlocks) currBatchSize := BatchSize BatchSize = BatchSize / 2 @@ -320,14 +322,7 @@ func checkFileExists(localPath string) bool { } func (r *RepairRequest) checkForCancel(a *Allocation) bool { - if r.isRepairCanceled { - l.Logger.Info("Repair Cancelled by the user") - if r.statusCB != nil { - r.statusCB.RepairCompleted(r.filesRepaired) - } - return true - } - return false + return r.isRepairCanceled } type diffRef struct { @@ -374,6 +369,9 @@ func (r *RepairRequest) iterateDirV2(ctx context.Context) { ops []OperationRequest ) for { + if r.checkForCancel(r.allocation) { + return + } if toNextRef { if srcEOF { break From 6557b5eaea4be183476af9937a0e24382195552b Mon Sep 17 00:00:00 2001 From: Jayash Satolia <73050737+Jayashsatolia403@users.noreply.github.com> Date: Sun, 22 Sep 2024 03:18:19 +0530 Subject: [PATCH 116/319] Fix --- wasmsdk/bridge.go | 15 ++++++--------- zcnbridge/bridge.go | 2 +- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/wasmsdk/bridge.go b/wasmsdk/bridge.go index 4d9b2a02b..d027decc6 100644 --- a/wasmsdk/bridge.go +++ b/wasmsdk/bridge.go @@ -5,15 +5,13 @@ import ( "encoding/base64" "encoding/json" "github.com/0chain/gosdk/core/client" - "path" - "strconv" - "time" - "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" ) var bridge *zcnbridge.BridgeClient @@ -85,15 +83,14 @@ 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() } - c, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second) - defer cancel() - - hash, err := bridge.MintZCN(c, mintPayload) + hash, err := bridge.MintZCN(mintPayload) if err != nil { return errors.Wrap("mintZCN", "failed to MintZCN for txn "+hash, err).Error() } diff --git a/zcnbridge/bridge.go b/zcnbridge/bridge.go index fccfa6515..b259fb5ce 100644 --- a/zcnbridge/bridge.go +++ b/zcnbridge/bridge.go @@ -621,7 +621,7 @@ func (b *BridgeClient) BurnWZCN(ctx context.Context, amountTokens uint64) (*type // MintZCN mints ZCN tokens after receiving proof-of-burn of WZCN tokens // - ctx go context instance to run the transaction // - payload received from authorizers -func (b *BridgeClient) MintZCN(ctx context.Context, payload *zcnsc.MintPayload) (string, error) { +func (b *BridgeClient) MintZCN(payload *zcnsc.MintPayload) (string, error) { Logger.Info( "Starting MINT smart contract", zap.String("sc address", wallet.ZCNSCSmartContractAddress), From aaac5172da4fd3e9643ee4d21f164b001ac9b2e0 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 22 Sep 2024 17:22:18 +0530 Subject: [PATCH 117/319] Debug --- core/client/set.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/client/set.go b/core/client/set.go index fce37812b..1bea43d0e 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -176,6 +176,9 @@ func PrivateKey() string { } func ClientID() string { + if client.wallet.ClientID == "" { + panic("Wallet not initialised, client ID empty") + } return client.wallet.ClientID } From c649c27ca1504347eaf00354659a645de980c461 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 22 Sep 2024 19:05:17 +0530 Subject: [PATCH 118/319] Fix --- zcnbridge/bridge_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/zcnbridge/bridge_test.go b/zcnbridge/bridge_test.go index 0d09846d6..ee114fa22 100644 --- a/zcnbridge/bridge_test.go +++ b/zcnbridge/bridge_test.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "encoding/json" coreClient "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zcnbridge/ethereum/uniswapnetwork" "github.com/ethereum/go-ethereum/accounts/abi" "log" @@ -366,6 +367,9 @@ func Test_ZCNBridge(t *testing.T) { }) t.Run("should check configuration formating in BurnWZCN", func(t *testing.T) { + coreClient.SetWallet(zcncrypto.Wallet{ + ClientID: clientId, + }) _, err := bridgeClient.BurnWZCN(context.Background(), amount) require.NoError(t, err) From bab6ab968a048459f5211a8fe74f608fa85f1352 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 22 Sep 2024 23:09:38 +0530 Subject: [PATCH 119/319] Fix sdk builds --- .github/workflows/build-sdks.yml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/build-sdks.yml b/.github/workflows/build-sdks.yml index 91d194090..d2850771d 100644 --- a/.github/workflows/build-sdks.yml +++ b/.github/workflows/build-sdks.yml @@ -24,10 +24,10 @@ jobs: - name: Checkout code uses: actions/checkout@v2 - - name: Set up Go 1.x + - name: Set up Go 1.22 uses: actions/setup-go@v3 with: - go-version: 1.21.5 + go-version: 1.22 - name: Clean build run: make clean-mobilesdk @@ -96,10 +96,10 @@ jobs: - name: Checkout code uses: actions/checkout@v2 - - name: Set up Go 1.x + - name: Set up Go 1.22 uses: actions/setup-go@v3 with: - go-version: 1.21.5 + go-version: 1.22 - name: Install deps run: | @@ -199,10 +199,10 @@ jobs: - name: Checkout code uses: actions/checkout@v2 - - name: Set up Go 1.x + - name: Set up Go 1.22 uses: actions/setup-go@v3 with: - go-version: 1.21.5 + go-version: 1.22 - name: Clean build run: make clean-mobilesdk @@ -271,10 +271,10 @@ jobs: - name: Checkout code uses: actions/checkout@v2 - - name: Set up Go 1.x + - name: Set up Go 1.22 uses: actions/setup-go@v3 with: - go-version: 1.21.5 + go-version: 1.22 - name: Install deps run: | @@ -355,4 +355,4 @@ jobs: uses: actions/upload-artifact@v3 with: name: zcn.wasm - path: zcn.wasm + path: zcn.wasm \ No newline at end of file From 0145dea84fa0b29f033cc6e7dbe38c04a7bccb9e Mon Sep 17 00:00:00 2001 From: Jayash Satolia <73050737+Jayashsatolia403@users.noreply.github.com> Date: Mon, 23 Sep 2024 00:25:51 +0530 Subject: [PATCH 120/319] Remove readpool and add blobber_operations.go --- docs/uml/read pool lock.png | Bin 14198 -> 0 bytes docs/uml/read pool lock.puml | 10 - docs/uml/read pool unlock.png | Bin 14781 -> 0 bytes docs/uml/read pool unlock.puml | 10 - docs/uml/sign.png | Bin 23708 -> 0 bytes docs/uml/sign.puml | 19 -- mobilesdk/zcn/readpool.go | 41 ---- wasmsdk/allocation.go | 15 -- zboxcore/sdk/blobber_operations.go | 306 ++++++++++++++++++++++++++ zboxcore/sdk/sdk.go | 332 ----------------------------- 10 files changed, 306 insertions(+), 427 deletions(-) delete mode 100644 docs/uml/read pool lock.png delete mode 100644 docs/uml/read pool lock.puml delete mode 100644 docs/uml/read pool unlock.png delete mode 100644 docs/uml/read pool unlock.puml delete mode 100644 docs/uml/sign.png delete mode 100644 docs/uml/sign.puml delete mode 100644 mobilesdk/zcn/readpool.go create mode 100644 zboxcore/sdk/blobber_operations.go diff --git a/docs/uml/read pool lock.png b/docs/uml/read pool lock.png deleted file mode 100644 index a8350165d47a77f5ea67f1e8514dda8c7ee9fe40..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 14198 zcmbVzby!r5=!?F0+JFUNJa*>;)RXAg{9*gLn9lAq~TjbD?J-S1DaQkG$uASmVB(NmT&al+Sr=EVbQlR zzs1H&39ex9TIGe!KaN9Cz-1gh+lDHbp|am>-uAq}_xAe&y)u`181Uiec+{L%*_vxt zFjtJ+n@6`M6sB{O7)GZaZC~8CFltHcf0t=<7k@V;jW1cx1h@M13-e}>FSl&266SA{ z(eKT-Y$|jpoKfvf%pM%!iNKVy#ig=2rJoNwjH>Hw5#Eyco-A1LlZ+Xwv%J`em?HkW z_~%8P8W;L4S7|%vC$^sAuO-(J3BeH5y7c?X8 z?+kR|52qjy&KPNNgo=aqW&*b6C)=7Uby;=s@F5z>>aX4lzbkNW-Db%g?BZ10G9lqC z+*{&|zW-SHN_y!L=?4!2t$rmYbxkSF%x87&(%p1FrRg>lDo}e{L-bpSjwUA zeu-ZK?d^wkwX4sH<`9Jv59Tsmv*m2nYUiKNtRMe$Jv+QjOq|GP`zl&cy z&1+-B;hh$3M1-L8&VIQy_ZumxfFSLK-!_x+g@gnI8fB=EHoY8Z`#baTBDbrH_Ja&X zH-VBj>MXed239|eVd>Q*)o-=t;tc!ysT%b!?T7akJ6|GC>c@rk#BqAU-%KZ_C5t*J z{xax=zlr%xe??O1fY=F@u&BCCK{3&A8zK^Hx{<*(@6+Tr}YU;GELI}J&ts*wI6CF)AXnV9ETt)uj!U8Rm zJ-z(-1v5A4)1xh_;E%Bb<;E-qeL zNSquCtSv1)vRbHPt<_xXV_@~cPEYIF9_^nV8)KxYvp?BWXO8IT1&@c5^UaIeqM~L^ zct7Pp!{ z)+XJ?+~2AsoG7&@)oax0N$3=G*ty{!hcT3$ee^{9{lWUk@_2Feoc0>$!AvCMl|=ef zjo?S40wcMi+S*dF)T)L1FEp!u_4mJ99at^$<=|(ki;H8Que~_0dU2v4bmC4cp38Xe zdLvLI zcp8Y{G?|)S_H6r1^ORbYQ?_r>5|)Ayi@i*U++EEecw{)F@OgBy)WY6B=IxXK4NGG< z7o3)?pSf*Bv&VT`&dMY-x&gi2RmIoqEkgPGOW_9As~&#V!t}MEL>>VwjwfcYM84vo z=C8khufr~1^RgHkFk9Q(3tAbVMK^NcVi0NK`!Gd{`DI<44KA=0WE^;KeT#@7{Iqa% z*W~kEMMSV>JO>xcZf67(jH%VVo(^GeGe9X+RlD~~+FYQ`2!VJI-Drf^Q;MMp5};jR zROl_4Z@n@$TKZ9G4;$g0JP2j(J<^RJ^n-e~m~7UE2sxRp^jW&$uZEn9GA$VYz`HjU z+v2d3bTv7*wA64h{_OK{hK5_Pf&Fj1t)=i}44?WBK{PAD!C$ns6NS%1Sy<}m9tt{K zp63lt7wCT#Q--It+S)>`tq7)FtZ}KXB!ekmOs^nzpY92cE;P*|lG!z!cTSd_ziVFz z9Yx|d*ia^NZ*wBpOY{Z?h^=|8m#P}@zir&4xA*;;FqWavC&OBOllrPBHN=U*L}~+p zd>SKO4Z3jxB+9mNW&KasQlVqB+=7C>ow58!s(zS`jIyCg4DhtQ;^MQh@|fE2LSqF3 zxgiR{cMdmA0t?`ig~oW*ALC_(uQX{grBF~4l*YI+GuzCDI!@-AwI)>uUV*jF_r*#l zCf}t*ufp!4yTtq>k!FrMpp=|H*y+*4Jr!lGg)&Rb{3Tmf@d0 z!tKJS_x@Jbdi_LcZ4&Qe|D4UGrMP>Dq2{0MAJaokNp+VcXP%kCi`L?9y_jexmIbV2}zRsU6E2 z)Sq)PCqQlxWmq`+iGAO9`g(7o$Y(Qlr#ABMcJ2I+Cx2;O?M0K#y&9)QKFf4?(KtFR z9O`~lsXL&P{k%s(Xe^ADp(gK}x))g6b>7btIQElO>A>IO0>;LMzlVBaB9CrNdYgq4 zs&ywpVW`$?En>XLVY8888*%3-vrf za{5>?GS)?MNno_$svQpDr4=|1k6_#_S-z+5;YChMzXw)sh z<9>OLaf7&Teq-ZJoSUAWp1OMMHB)25?yA33>i6b~G#QqbmJT3c4Gs=Y7go#Dma`Ui z*rAcD{r2rCQgPIY!K$;~B3N{^jT=t-Y*u#np- zt>}b>U_QqIQ>FtGZEUzwmfXO4Mv_~F-R3x_*>GBVdTz<1aEjyt^U1Pz+f&sX@BGoF zy-F>niRH09h-ualr!!w4W0?z#v#Hk&^kpZ{W%WH5TKHKk46$l+3Z1emT3or1ED(eL z{f44XFkahDmA=6YWP+hLPhW$Zf0UWrx<0}p;5%k;mUtDgGeTSVXm9HGU+Ys z7RuCxYJ`@J_l79J`6dJshYm-FjPmc$KR}3HEt!G*cR~_TD;wMd+X5{&$BU&S?q8hMURCCYN@EGMRg_sRc7QXS zkG38%o1>FE?ED_laX?0xl@bK`D^T^1^aAvHuf#&o#iC=bcaxu zf<2Y!2h)Ydy?{X&7Pwat?HcHYzJ7kuQBec>eBnvL?ullz@fJ0!;Gx;sacO;V=^#2+ zv($0F;4Du}(x(m2#`E}{Y7KjO8SYKyS;B{5e~_%4A^#cc+qZ9&R8#@}{-c8l{Pw5E z$LAV_Vdv*AYHDguBG2v=Tkb7LMTe`NE=8^Ty;R zo^{h$p>fvJw{LS(W~1pY&W?<0QZv$U`3}KKe6FIRSeLnh5No^;&vnVut~TmT{2t1> zGgE9f@|M$TxreG;qiLl-^|h|-ir1Mp35({+RP|XP*<&d$UtelmEBo7ItW`T+uZ8bK zYMMiGs1uwO_j;;dD1pWGwX)J#RrOQK?A2j{fa8yl5EzwPrR_Q~HFa-CQ&W@Tig@gu zJ9iiv89zg1WMrhDbWD@%-zg0yWp4mWy|dG7?Pu1cd*_2#)*YIpck?anLytII*1Nm! z^6*7nHG7&C70poEp=D?w0X6c zr~bDez-!)?scYNX+1mXwJTD_(A#}J&DgnSfo;v9GZCBN|YM3h?o(H}M)U!bIblp>S zIUlz*qtq7utmrP)?43Tm9U2qNw1%MXN!^wXPKCxzPe$CZV4gyK*Z5@1b6csy^fxnh z*bP|0AJiJNlXzrdh4WRITbPdltu`K|k@R?;QTs04xspTwoP5?;qHDDiKSq$H>7v!kDBFEG$9NO9)Ho3|HyQA z=E!>!ij69Q@)v?dZtomPd(>QmExrI2B;WqpOAyoO*L0W6hk zu>N;Ux7k8WmXCxReJ^QnpM51D$FD+^$u^%T;YfUz`b4YB;o0P)2h!a?h_UGDGgekiyw7bq zuF;nHm|BMddU<1ach}R?lcU7L!=tQ>CyG)1dNP24Zb(tj&dz@Q`qkI(O6MdqGc#K) zZ>q-Cx#2x_ufRU;14=S7UaN(+wKda6ZOI7lbcLkcNi6eAi<*;kF+cpVv9TbkORu`; zb6d2wExj476D8*S)=Rj8;6ll}xD;HnDR=J)3)g~jhh&J*ajD}%I^nHbh{qN0<}uZI zXlcba>chhcIwq@~OH*cRhlK1lVnmzoN24+%Tf?~4djw7HSY>Oet4qI(l=k%AoUmT* zDae?1J2MBRh7|r0FjXE?c*a`LD~DL!2ud_m}$m z6#chN*lhUH&jF9NtDc`MCnY(|VQDjYMnHED&<1fSkOuF`STXmH;QlGd!@2yRdgi@C%J#r5qa~V z01BfPVrCIhP*;pfOzd$Zi-?Y<&up_O&&>^%-vVr7zA;V1O;w)1HLFxBq9rLQ>Fn%G zvG9PaoJa%(Yzp57(-62|+#Gp2TK#vy=N^QFf^Z=h2Nji{i=EMUcoS|Txmt2uRU?OPVZfr1M6t4lBc^QhJk>MK9t zbI&i$Etk|_0C6$~1_tKl<`NPZ&GC~5y#4_JS#0H>=2}NbUkD2e`}oVTU?@W1=-*QE z$2mDUBj^UQeOg)?gF`61uORxY!=XK#4)bI+GAincxHyB0j;4x= zc-;QZj-{>b*Q^5Tf`2~sj$h@_CCL9O@IIsBiPzoKfj(GqK@<%rY zg z9S2aHjJDC)y}3O#>e_Tw$(X=F-~rDeJ~Wl;EZH+PCzxi{dux{S6Y?Z*HgT=Bg_=|@ z7ly}G;KeJmnLdE++!gQR6}&JVZpI*WC>WmCP&#C1g2dRc*ik6^x3zl6dviyQh8zNp z510^-Fr*+IHMK^Wc~ZI)=v%vASfyM}Gts4cy^pxfcLUoF$#W0_SRTkRh1+j=#@@`aTby=lqvrYTiAR+*wlEB1LVq66 zBZ$R}PXZS@t{EMwnkGThB?m?h3kP;|u9vRvE)32GqfzcUJ(7kXrw!5F2G=`I6#s`z z-mN5iSZ|6(fdSTwC!mV|KzY6+nypE*YvBz(LLeO2e~Chum6FexL$FdPOXq(2N77p_V!0JGcyehq9wM20|Q8(Q(?OfNPPI`PuVz*_Rh}E!9nY} zrjNzN#W`=EK7FdFs0aX)(fw!UGZI$qD!a|N?Cb}s_7*g}ys`f6s>?H-U%q_tird@S z**QE+Qw3zVtr-&cD6oXcTHmz%p3F6cx{NB8U@6SE?z^V1W| z6SC&kRwyRs01i4jdQ)?=bS|a|n5rV4&(QtV_kTp}{9uLxQpK7d7wES>S5up=a*Wu! zckdpkuShGrzJif+n4BK0=4=5a-}9MQ-z1b|5k|q)MROYkIX<-%RE{(X?zfs1Hd)5K ziPlS9dWH)v(#16=OY!(rdejXOv-;0MM`MfYgy*Fg*m z6~A3zwvR)f0n%ap0sG1WpvTT-OKjUYH(vh}`dxfEJ$7nIkc=y6Yuh_~S>UAj62fqhont;r=(( zYzAC|=rWC+`L?T>QVPHr9UI~X%_=xKor14geXXqx zA8eX!^aJygWPx{ebuF}dhtH=qXjRz63cCwk5!8xA+zShUhENO3!x&EWPe94j_{3_{ zvd>HW8r=u=>rFkDfCxf;pa%*hPW+Z|<&WFsmJG;BTwl4Q1 zfJ&3_AbO>o-CPs);+o5`AizoSCr|#!=!3N(v%`(CoWKSiR?V`$YUd-e$3{O#Mmjk= zLn*L}yKJA{Vu{C6O|!HBOhL4uWKT3#;8Jb}P<|DmrA5{3TToB{l#p*ryzluCq#G5E zuAvB&kK>*JH8nL~HwF|sn4*vq0+dFsZ0XxybMy1+ZnCn0d3mgce{kF~E{4yRiT|I`12=$q(HsQE-p%J=Iajj{p4ZP-S|~`l$}@8A^eU)IBZ}*X)kZ)>{Ul^ zdwYs*#GZ6;R#ui~jRC)egoH}qldZM2r`~$Yue?n-MG8HjDEW{{SCLr^U2GsHnL#^7 z6=04PcwoGBrWCNhKh`cl{!jx_PJS zk^>yde=q|+0H}lgf%6q80B1}AtOGOwELpK-c+qi!b7e!!P;RBBtDWs(_nyvWETb*Z zmq<7rCDr&+HLmI$;>K-vNxXgMlXEauUe^0P2qY8@eCeI?aUuxucd6-NM{!^Ml@_jE zB>2aoK>BEro4(#u7z@(cHL)^oQD`iluDr5jht|!6@HT-!NC7q?!JGsVe)&T~P$qW# z59$NfFzHHgP9`oIUo1a+Bz|@1870kg9}*}6oX5D>?hNelJhw1;*eq$sWF#RE$3$zO zP6(t=6wHx@WohfotnC3=k6v2CLv8OAQQcxdYgIc9jTzkKzrArhZ=-^iO7aey`;1EA zrQ1!&An+Nxf*+$u3&)Kv&S)VqzJRk>Zx*BfF$e!+J*H){9BPWB05DoC2t*EC7TM}U zQbwIO1mX-_Qd5F6k}M1XBrVL$SRQR@&0@$P5gw?!FN>yyRG>Nba26xr)z-7D(Hc=@ zoM;WH2snq%NCy%iWVJUdD){uCE59&pWDqH2JIW*)x7Z?Q;`nNr%({4_)83V7>yAc3xpD4bSMeprdeLDtnwGg$a1Pv3n%zis@fu`J!=Z~Y1HItEv zi;Qe?R1{EoHxl0bo+=Z$`o;8$SE(rBaBU5Zry=0Fu9xSIB>^qLR4=u(T=$pcIzj>i zDS54E1?f`p{+&}4G5>N3Pn+s810!P!6X4ABR+Wv>0)5{WJ8h@cuMgC6Cu>~exg0a( z6F`}!RnT>-eEEf~kDg`W%@F?(hg&NsoDwJ8Zzs!aR+V}a1(&;G^-bPzF)^*J4P|o# zViVpM*zbC*DKt{!fJX;q94l^6Y>o!}JOs)KKxn!hju~HGUY_?tc4;sG=2;TrVq?z= zFz{YaQRVLv3VXQ;iY`vp)PhctN$pd+Mf78X-@Y+PO16Iaa`zB)@DhZCglePS(h3VF ziDG)2G?FSO%#j-#FRNxhe>Pcf_`|Lzfxk6TDOp^B@%?OKiRqC4gRVQInA|sZxt2mT z(Ft%2!*xan0|NuWLM0VXJw2ST7-ORqZ2QgPL6K%Kf+O~+iinCz?iqdachD0&e>I2L zyCuu^;6W#1O_TZP2hNOt7N|X9<)Dmv9xSDQ@JM2GXe|Vcrg$(rH)nNq=~_{AZB6

qq*wPhphb%FNndO{x|HSvsb- zi^cC}g*CNnV`4Rf@rsi9gN%YgMg)oASKasKX^nm`3S}(;;48*L;GFDi-@M65N=iz}>h2Q!gdCTHic3>~u1T3I zF&UZq%a<>oJ?jT%85kdm1xY#+9Yxu>`U(bwn6)9$j>uZl zp8l&>pd7~dn3fi(JD&+`;OGo9X23pwk-{el?C+-7c*{n>%u-?io!^~AA8`AS!&E%{ zaO<9ling}4N;HNLPU>7K>z0;PsFl_O+)wTwzp#y2K!}r3>vch4N4_GI#!UO|50)Og9wo}2$a76AEt)-UkNb5xo^&X zYZE6RkLwTrHMR5+{Ht$b&(^XpKiyc{h+kA~xM8Z>1@b>2%Q2*=z4@(L@+jnGezUU_ zC~-BIF{KDVAPZpCnO>V9i3bCv_CZ~ixW7i19QURBXdZ(?z4%{(70n-KhD@$Zxtq-d z#LB0{PoBl9{=+6=75UwO6uP?Cjy=AcI-7T4N`pXhGhlU7rNqjRkG)FLJT#C%;8U5b z3#-=Zj#q--Uhr@)VuoCF937E2c|(wV-ql=NbL=G97lYfXsH7*~N`mCSzYd00?QCSH zZ+BJh?FBgM1CEyIrO|a{%gxSi?Od_1Z$nX#mM7DfJ==V^Rsr?e3SD;!fDSPITitp( zRSq>m{a-EWf7gR}sCM=*Ee6Oip7ZS6Ulnv!eJ)YV>T`>Wi(6aTii$K5qDy6u%Hnz5SSXdtd0`y~%X8*q2m(-w)jOC_} zgl1-DctK|df9hl^HGoWj;W1wP_x6D_%%A@i9zc9{uxZu+qD7ORuVv?D-eMXC^g-=@ z9yTN=EuA6{9G1*H8K>#b<^YoNwb!p-gW8SxuL@4pl#DGrT6+5Z%KN$hbZ`|nWqD&1LgpjXg?(`Ux6Tjv2dR2o0r%&)+7 z=-btC$kj|DF$oDjFYl+wNK&Gah;zD|cdxwV_JZo^`T67Hy=rS~MQ@k8pdghYriohL zOFN`1DyyhC1=PnZ{0QVM+QK5mEQbFyE`IfuQ3FSqt6lAMcDVVM)N))v^TIp>xUjxh z24qPM(H{K5o3;9202ISA5#zWCe75A=Zz+!s%9j)Mhf2*S?3TL7#JtN*1R8zu=D&qd z@kfzYIqs)h@rX6vfWcTFKK$AWOrenbrGQ{^dYRtQ#+WtmewZh(pN@O3|Dt&e(El3c z%tmqnnu6N*USY4$y~3RYIyf!0MhlnuINLno4Pxfx=#PR?l;7ClkuLgJH&vpu??$tN zePiU;fM;xMj6=ylF#9O`-}@k>evG=uFSmg&RAG70w;2X z(I;wBdAP;{v`qYYaD#^0nVEIo+zbZ%Tw!yt@(g&nQ8F?zDn1*w4^Nuqln3xNw^7Rw zPwl?5skF=V_xBUoDow^kN4K)xho^b4>$h~puzSOSn;A3)0U(z<%y%>tFkl_k?C3k$ zovLA-Bmi_Cj#fE7rYc_fKtxR3Qu8ZSPG(!b_Xz{(yLa!(-LGmU50gK5!xdGU_dr>& zcaOsP7~`F!6`p^R(yzWgB;yY1W@2DpS|=^uL=#0#KQKB|neaEqLF!eR2F{=)~Xx1-fg zzvBd*OAu}EY0nEI043$G^=0Md z?}QD)D40g>BiPA*%aDKVS32+EWI1WMNke))5wJs(qk#1T^8iJ|@h`ucYmye-ersrH z2?7Jlr_`U4^|JIWsIp`BpT*Z6J(ZDZXT+H%k+DnbOP+cC$HwB_zh-cWd<*y0M%`;K zYwQZVtmggH^mO0&XQ0kmsV6yM(e&XK2NUQ9eCwRM=ksiLg5-#WSE?LrxsF=#dUCvw zvniMUFA|`KupsV(z~Ql7BhqqO%gQ*84HsK1jg5#H{3=7@gjiLTk&{~_k4A32Ksn?p zfgm00f7giqFPDpktV*Jji`_{9RZWrWBHy($1@`h31&*E$bkzj}hKjUm$d)y8%{bD= zf-X3P81e^VYRI2f`TtY~{TDi~JvWnCdxhk4V8k@ed#bfkuD$IRHFfnrIURf-^1D2n zQy}>!5~n>|`i~Msxl0^t?p}rFzjyjmC)H(n0V!+Op9xN8r_f58UU{XBO%6l`6EMAG zERacZ9-EiZVPtsjn`&tP{6)mxq^qn;)YAqO6kT-iZ1=z-WqDJa?JjUuD%{iMvY_50 z=h&-o*|CkW?<$BYaszahZ_$`wyBDu-PhTxdCRwyw^0sS+8`sEeI43iVJTL!RTZ444l~Y&72P)E#gOS&XRrTAzkc<30P&z~ zaq4StZ?DJtQ8?dh8zw7%xKZD=;`p-kzJ;_V(fly?>!m7S?TRfiAfb&fVBzy1yl`=O z7O|?3a_;bE?1!c%N&5oGrk=ZOP2y1y47^GuaTKIFTH1L3{ylLn1Y*U}=v`N>athCoP>-v|Set!wQdk^}1i^^to;O#rf>si&I!awsL4Pg`d{%BoXtb9>nIUB#TGZpzl zXo3x7z}~G7zjwbdTV5O5jz}ENz3|Wi!YVM&!ns|hyu6(@VY}v~+RI8|p5tk4N5e7) zNY1WywcAU@Pz|LY-X&kHfW4gV#qoAL`t=dicqj5_S(kej;R44P65U>FLqpE@?uwrN zUbo&H*M7(P`+7X}sBl_Pupy#vqvcg1YH1dK)0m@E`{99zJT}gnEG$?1B1)FN>|kPA zsH|xKJSVLR^l%i$o7q_>{Iw)*^9C#kkgg2>I03%(PHPyU@P&$Kdd}%b!g!z^g+Mkp zo*VX|Ge0dBYB7Cz`MvauiwoS=+8VTbXQFjjY)vAgMeE|jAJythx*Hp3ctEBXJBw9DS%%#?n9WHx3U`Z4A<{e2v!r5aPbv zFDXdGL3@~td~9YQ;whIHki{pbsqQoG5IUxL&Mva2bW>2`$DKP0j!q?1!k5C2A4mVH zb}p-#m0@e&bAfUFnR4LonM6Ue6}d@+)01UXgv2;0kx;(5;>fVbeQ#(`d7K8gD*9@Y;PqI!JzSG9in5no^8D@AGG9G(*JI5c)|rSOaTYmlyX~ zt8Nb0x=%YF!8a7#<~V5E!q(+KA3tQk_5CarC!-J&c9xh{Gux6`T57D9(iAshwqC!# z?EZTunCe0Nfv#&~2EN?q=gF>TcK0Olb1V*$)0EbO@ZTfTy8vnznx^;vGjAH}^D`=x zEk0W!1zkeP)6;FEN03C9m?$mWc%Q;OR_gih-{!Rj?Hq0)n)9z<9kw9TPP~ArJiSv% zwpI-QjW12v6DItbDOY>i66=$43K!hKPiK!a8l<&Apjx!9Q-KJkbTU4Ev7y-&k^_2J zZE@eY`EG)iY)i~9?{o2tl$e;G->5{&%wkT!}t{crEGm>frcIBOU7g>PI5_ zb+8`nUZT|U^75+VSH~bBIn(EG{ON`R$b$=5>VEL^<6l&!l+!Rcv3YR`60l+O|C)dm zx*n^^8X{Y6CzR%-k^@mo2Q#ew14@;fmbbP}qiefv zk__Ij2ipVr5{QP=j(zQo`xnKgi{}dwRMK8^+hd$E|8;)RH*`>{M{@o3>l4T~K_U{I zE#<7{zMbp?rS%qh5xdQLjDnAaB+PI=pcI#$GO_keKgZ6h6PWWJrM#ZCxi;#h-mtF>AMM8$0;Qf^TP&|w?O`^|{ zkwp%>b083N3f?Oiq6Ja|Hqa^pdWy)LtcRaKo1c*Fs<~uRUD3rbnv7VU=#A7n@iz|J zy#ejpP-=shQC_{e+uXom9}YsZe_zw_$N>UMGO4;~mjSaKHOg1phxmz@RsH2DZ@@@( zIp`n02HEf>QB8Le8t*YD!90I4{VZZmR_P%8yCqPwz31Li9s_)AX@} zKhCDWKtpPvkkP04d2fkuJt+Sxg!(VJsJ|~i{j(#oe1(>$9sBO5=tVnt0}diBAupaQ Is_XTC0F^^^6#xJL diff --git a/docs/uml/read pool lock.puml b/docs/uml/read pool lock.puml deleted file mode 100644 index 25617d841..000000000 --- a/docs/uml/read pool lock.puml +++ /dev/null @@ -1,10 +0,0 @@ -@startuml -participant "<&terminal> ./zbox rp-lock" as cli -collections gosdk - -cli -> gosdk: ReadPoolLock() -gosdk -> gosdk: check initialized sdk -gosdk -> gosdk: create lock request -gosdk -> gosdk: create smart contract txn data -gosdk -> gosdk: send smart contract txn value fee -@enduml diff --git a/docs/uml/read pool unlock.png b/docs/uml/read pool unlock.png deleted file mode 100644 index 775de4f0297966596e7e5f78ff2f9a5c978e43e7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 14781 zcmb8W1z42b-Zeg=fRuHub zqtA2B^Pcy7-*^4FE-z%}j=lH2_gd?>_PPU=6r^!5p_mW|1V>gzLInc3(F*=?-bMkR zu$5;Mf?xEGlA4ajwy)f*OwAl2(xx`1_J)q8CXbBV9>EX&5nPob37H};S@kj3{F2h6!1tNz1O5F@XmYJD4NG7(S+g+8 zsSaGRrpOsT_MrRga81$kLDuhh>@B<~Pu;PWPW8X@*zt#tm5G1*vV+E8a?$I-KB|9! z=k#a-kJ^YZ;>gG-W3ha!og{S0a|YVs=NKJ|9%b_oo8aWe8uf?a&6m+3G-a^amV#OQ z*}Qg=1{{TZYTcEp)355Z6&n`t3?E__y<>m+V)n(L-`)?Mx9ARPeTna|gX5mty<3>H zcPpD|9oM+jX;+F2E;mqp?=fXKKq8$}5?xUF08#EkwG3bPb>gI?+P30+MP9BE#G2ez zItclJ@e%^znUj?eRda=HrC{maA8qP%3lxy}SrT4Q!}&N&`iTnrh>~K>^q|`IHx^kl zM6aa~pWFA5A`_uj5tZoG{1_GbN6#_}NF^GyUkZr{TKu>fO34sOPlqXV)a0X8L1KP{ zoz(07z07C6|Li28(X7#I3^sqM?@_&;jt+qg+#+dv2p%x>CMTFj&rzVL5MP=bAtJZD=e;XQ0XJIEv9Z;$(yYxMQLfG(+Ez}$=Z_2W0 zO+hzH_BNzt5vUIt6q{*iwSK&Vcy#zmag}q2FGV^Px6Cn&q$nImbq}9fKUDur@g?<1 zy7Kd4oX52$yq33{_vz186<8(7sg+#{pMQ$;Uf?m2+uPHtcfL3^~ZVY?5ns-^DH5*MF8EwknB zMulD)OE^vNK(C?VW6;QsK{trnX%N?B_=eLA)F)U)_!ju}rI zv$G}sdBcnv=}R8so$FuX?pHx=?aZyh2_$K;vDGg}3yX`H&fpoh_5MEm9*2yX=RJ#& zhOvQx>8vXn`RnDRk;CAX$F2wR37lSQLpjTh6cDx#qxn%$QGR$!OONF?D7835v+6wN zt>kqY-Id*qE=cBSBj=JU;DeO=b9W&RI$lN-OjhJOR3_QcAc#E%4co1`dnXLpV(2^qHtm|DNu2q?e+LH zd7VRetHc8g+)v}0vF{Ofb)OBmhBWO;eb1IHAM#B7pwFf)`@Dg2u}D0N4^E&NCk2n* zl-)62R-&Z*S>7+4){Bo?DD^U0cc!{Tn2TDlJ!%&#cWtdB#A`^tGLHrpu{)ZfOv;H_ zEJwxp61xNL*q`dCuWw7kYUhQHuKtcW-sggfnY}`%C-x(*;0O)1DTD3n^}hJsDL-7a zPw*c@wGQWq9`o{xy}b)=>x-X@^9HQAWZ$#v5)!Z^ZW@I6oNwJbdz?;a??A-pVqV6l z|9!}*$yDy8t80FdWpF1s?l~gva7f#JG0$;h?CR}XSzO_ZM=6=ONyl@;L$;&+~#*MEO@PHOHJ7$6d2xZ_ln#^TRZ<4*Jb<#Na6?=H&9f^_Ac z)dpm4*|R+JOlRZG+{`-MQJY6=c{}Bmiye)3?=Bes8d>fwsM7U0F*J2L+%7Bg@j%2- z4BaT}sCV(HN;hzm{Z>=O6jRTksVADHD0JGXSFcVgSl8sTBYd!P{kbZ(^<>`7JUUEq zW3)*4sng5-xki1Jc-BIgRxJk~U#9K+ubw!UXPvo~5mKF@mx_^B9U*{#7}eX*QBQY0 zC|%0i+lfg@Nmu&QL{vpIo}p^0;?rJf7^Jf@UVo##mJ|AC2ctJ*Q&JvxKdw>1?xMn~ zHc&4~{E8K>FDA}E(2^s^FB6@q8YbkvLLe7K)9$?aKnx`({$t^~haqcg$7vmc7p*=j z7mX`1?b^kefy(%Ga4<)3y&JRtZWL)3dzzP-$b9ix1l5N7(WcmwsZW+3*`}HmO;X=X z9VN59Q}?&)vnBkfDBMC#?0X${i*@5)iP z;uk4+nCt8Bq5q9R27}{tF35!WM30+A@US`o~wOejz2i46);~P(U@RKvZg?AFI2IA{VJ~S zxtp-G)GYYRH||YjHk4~3g)`!~;MZW*#$=CVaPCtXU3x2Z$7hBYI5z7Ae6x(EftvbR zRx3?r&r$jlxVK(7|G*?-lqU#LIbC0$wNhnGeowC|CA(v2Y=gv~&~oT(lNV}ZAhCS& zrTU{nnO#)t@=10&)_xZ|J0TaAur%r-Daph1OD>ECc;3D zT+U=rFZ^_613N$>-(*k5Xayyuobgkg!XFiX0svJC)3f*y@sNIX@srKWg!JxhW`QGO zDB0YQa}sA`$rVw-V^i(7`^zCjM_t5mFV#NsTVzI>C^9@GB^Awx^~-ST(|@M;Eks}A z*pC+aD&S5_U!Q{7!sT%%M$%8_=JRbhZ{UPP*1;gVg$k1w#MSll~*73nG9t@Tmtyq&%+`VTZygT09B%!ggJ-0V$e%^JtAZxz)+*j&#d--Q6e~Yeg zZw!p6>{D(xHY0-L6y+kD%bJ@QFcYRs85ZKm>s=xC*Ckg+TpUr~Nq3P8JPK6p4sdq{%F#@5Fdh zTld}yL9zTz`}iS+zppKl)eC+j$%hCj8sKWQpD`F!h*(pekb>oo3bnQY6uXusw>?ieF^0icw4w6ba6RS?X`a8@gkO^;>@Nz6>C>v0u4=%^1E2k{bFlkr8k2WWV z^Ayu~9cU~Z57$R)YisMBw;1T@f2$88ku0E;2J7$2W(DTEC$OeIp zMt^i`-RRAh7~o6HOoCu_e>e6KHY&%1Aukeo{>F%{Zq*v z{``5!NhHu_S|LLL+}1vDyrGxhP@r#V`s_V4OFX-^4=Qg#-W5x&2}<{b&3O{IEZxKr z!#A%LELImXWj@nFfrzVTRsCAEmS!x=&R){2G#=L3Z`bO*llZh}%aHkizTSS*(N2B1 zY}FhCf~^(8Z40#OUGvu+1{sQzK(DoHJ^z}SMh#(0S5Bv7V-*P&$r6z?fcX->mU$ZAAQJN=HF{Jy9Ta1M)>zBnxQe$LY!t2>jjv9R$LO!9PkzW}1)x9czHn zxls9{Oe*D{S3wCux&PYjhW0HeDg(+sO4a+6mjq$#BN)r5a6JCe{KLr_+cZDFuf0h; z?X>d#T&|aAPP-UXY$YGXO2%Ox)`OXnL&lKJ8}I|6czS^i_-x%_x~QM`)v381MbrZH zzVnZ*t;-pQeo|p?UEQhMpGUHtEtMvR7&;zlZ2bE1wWX!y)2C0Hhyv9-PcN@s+s8N0 zZ$KMyGw50Ln*`hsR)sHq;Qkb&p%|ju_<20MMXu$>znBAuC9(-Q(V;4l7-4%dPE8e3Y(uB8j=e=+}uu2PeaIsQ#)PI z_Cp5g_`CZ0a(~p;*9Qays4meJGmbyCLv+WK8+W3ip{b0J5E80uYSI-;ZaJLLheM0^8c!@&H%G-xT5KP~c)XrW1z;1rxD0UF`J$3G-N(7$2X*aaC#RW27QF z_5(J<)?2a7hF{;|(<-cPL`$6dq2rpJ4h{~QW$wWC8%Eb~Di<@YD|YTa&#^nzK1@Yeh)FN&Ey5P4^+#Z+!iS{uv;oH{>$4eNEUww{)zHIouFsF+&CaOI2hR|A{O2B3(&VQ9yfG#%Wsj?U`}`(Z5k#7+#$`B z$JFzucG;SeOsKJ)Cp3fl%``yL&C&-tD+jFrsJ%*4}= zmFSEYX|R%$lXsh3H9Bn&ZoOIa%@k1gI^LcY`-D-_86Hb($+)!de23(z2>Jyv+RsjM z9}`0SL+T+$L=RUVOY90(5jrYrt=qshgj;aH18jJvgMZ3^k}uX zvT{F>%W}d;AR#TSUoD3u9c1=w+Xs$~N9r;g}rg>FF6CcbWNYcX9oK zC!W=6rDon6D0>Y!Hs?qHnLGUKV2#IS=5EXnR!L*FiO)^j<`@@7Ut3$@Nvy?^-P?m% zokgWh?&n9=nA+lEVkcDI_gJ``0DK=+D0lT@L0*D&h50@Bmn}=<)!7EQ*FnUu%}Dek5ohyB(( zC*w<_Owe|K=m3j1T1e~0vh-thMvlMc1vL@>^)aK}imX=;zv8<8kG$J|A-OH8h1p&A!+TbKeI#bGz`hWkx|asO z?RPCs1SbIeGJwt2Q^6xg^`V1%bYibtB#Rr2xpq5K&m2<-8+2ODxQFp($kbgp0o;#K zFDjf*Bj^kdi0QjSzpx~@tSXju;ejIC2X5?3c(gn;V zYmuYCpdbcj=A1>KXw!te5}D5@CMM+KSVl)i+S=O>w`Y^?hXw{doWhzVweWpvgvF6dlTBNzH}V_Q^$rsh&~_+)@q3PuZ0%O`wyq9t@n3# zd0l_?3BRXy;RP7)D5+5jqYjjokdWchR#6d40LY$WTnfk1c&>z`%hLWa(vTZ_2DHLQYp!j4y(8Tc2R*;f2!cd=4ONA z+E7m%>%M&L-;E#;E>@)_%VgMYf67^uY+>nlE>uqmsHh4O6j( z)myR3P*VQB_$T6^mm&=O1yaX5^Q_DwtXeXk&3d6nQMnfvp30JjJwST^He2inDcaFz zb>X!aMO@}=i|KWCcAlS}t|1uzbtlYycF&VnL51SI$==f7vt0>MX_qrmeGJ|LpQ?vbmj8a_&V(08|gSL>) z%5AAD;`(amTA}=<*R@M{JxN_`e0==+`ug6UeUS4h!XYR)xPYx7A#)sPhc915{yckh zPE578tqp1~UW7d4=$M~RJIVvgr)o52Ke1`s4wH4J)&UrYCdSZ=SD&QA$V!{e7-aLB zrh!+*P9J2VvlAwKr5T?V9)9=kU6B+i35o2S9C~>49e95q`B;Avk0N;ZIXO9C7Gf%{ z2L}apMZ#dP?d@$x3sAOdl(*>ISSnKu=@#sURHObS7$K1FsQ-W(uyPR8h8GL z+pBS(UfmTAB-sCSFavgRE&#w!TUpqT0vus<7r+7S?Fa}keR|YwA1IJ^oR#nU5P3v^2I~`VKO2K3Z~LNFZio!jcfV5x$icU+sZ_kN5OJ8)9-+;4M{%~ z!MVMXW3R@c@Z{?^4^>h|vL?s*C+x_WxHa}8QA zUU0b|!1aL%YP+MHCG|o>qp}XbcSc4=iT0<=i$D0iy@%)l6HsmTH@-8_XrxftqkG6+ zR`_jhTCMvh;^5*6d!Cf#djd>#+Cm@@D=R$S7p@sh|q*wyV~j z?ryX@B&$Du41Tg4kB%nz0ngFMh-<$2hXWiffclh`l||c06}$;5qYO6IrS$y#{GlNg zJ|XXmleOWz$usgNMiQo`2yi}~Fb$2_>OjUpq$8Wb7heo~nzA}#ZmXwuKi@X>nMMn{ z{xWE63&b4dwMQ{R|4&7pFTf=e_X?ER`z!q}8)Me%BL&5HwHuk*^Ipe#N!-@<_V(M( z`eD07WTd3M{2}k(!W{f%2OT_?!S6z_14Cz=U8Bvw^YbeijKnT4?cWm9LJJ z72Grhn+sW4#>&cg9<_joe0;7wayBNbt%)U7+yifaC}5#|yjAx>B z{Oy*aS=h*lR_jJFIFOl>|16$f`&;pce-Ht8igt~SUMZ~6)6Bp z{1(rtLafeak7JvAv9R5`H*em|d7oPw3Uzix$nn1*E79dYt0SHOBv9uR5fOokiaPXU zf)~uPC~{LsFeMKw(@W!N*-$w3a8nEnxDOaTs~dg=T$W=4&Pgz^)xu%9Io~`VzbA1v@VGA>RU9SQSBNj$c>##1iGK%8s& zc_Cbuy6_O|V)?9?!)E=d&s&&qe8o7%)clY_{rN+Dd{sR?Js51R(c_q2HMgu4bKGh9 zdjgAIoqY0B<-Flh+#j%W9N1G)=6wZvGV`p+ddym9Kn!c?)zz_-!+)5;>yjw9wcflR z01xrMiJ>|cNenk*rm`z4BIDKoG1%L)_bAH9+`fG~{rmg(@2jdfZ9?o8JIZl3K@n7} zq$guz!-5(}Mn-0mgXoEqb1H}%q|;hET}eCM+uJ)l%<V2;QZh3s8KkIOTwL;6%APmE?2k7e0ySFi+oR`+!uk8Z5a=fo@ce3} zAAYGefH9$SzT^d{VlP~L<2R-JhOXXV>nK+d$b%b5wzFPouWr1#g)}P+r`^h}cSP!u zs7EKBGT{tEQUbUdBwHlw3ny?sw!vXYX#9-DiHStZ1(rYTk#7y=3okS|ZYnqilmxo` z9>?Tv9Dh>_;RinD__&}HbeFIX`aAr;{t$;)h6{uMP)bmEAC`ZbBzei{(O|O)-(k1C zTZPqE1?3wYlv|__W8@Kc`>?QtD|HU%2kbOsl zO8Nf6kvWZN?a?>K(6_R$69GUM{%Emyw}_vu7Eh3mRMs~MlRtVGK;$t;-XYzb_z_vZ zeWStV_FKM4e%qB-|9?3^RW0Dj&>(FhKqx2>-`YP!^G|OH47fnI5-lhlzW?D2o4E#h zAwyO098U~G_{x_X%23^Yn?*@RYsv}bUjDfR3S1FrTrX=s-bW4>n;YzClXfEgG89MK^Wz<# zjWK3Fzoo5L-M4r8eRGm-ask(}Ii(3ojTBW- zM7*fBnSIu&wmFa?es<;z^W1Hp_qlG&&3#8Q2p(Y^%ssGqb2tyT=p!4p_x&MdjEK2g zeCfG#*XyH2U?Xg2tYmtmK=tsjx1*y2R_7S_4v&avyxzq&wSPKpPTbFrbK+C0QcfPg z2>|q72djf%%mnD@WKQrXNweRtuEM=vNa%4Z8`~2$a>iF4srS&v7rshH9p_#l>P$AfPL{g73ZokP^ zGM`gWV>Vj|Oa;=eU-$X*uU3*lwv!h!%wL|k;&(|$%eot}acRas=keRv6~BdZ5! zT-_c-E$9|ymi`$~f&1j-iceT2dx6i_YBVH@iKl0vt`7DUo;S`^1ue9H4+g6Uj-~yj zikprM&n$=#n{iPOw_V7_#s;`jxvIc;0^5vnVPUwYL(8E-yQDc!M*Wpkt0;1$D54;nD4N{P(2=NglQun!`gPuQ@RvFbUV%J}KYZ7`%!Qqs@w^M)<_t5_G3>Z&7P zDs*jQKdvs%Lkeu}z6>&A1hmy?^1vP%L;Nnk4z4DKJ$FmFEH)v5{)1Fb*1b=x3^Y98 zo{ZQp+=GO|(Qu%go+rP6kLP{1mdB)O6W$ODvwtlB&MwTitTj69O+rUPi}q^*H1^(a zUOYVipuSjr7>tZGS->qa$a=0JA=Kh}qEuhFI(&jCMLLv3`6EmA=g;x%mX?;Xn1O+T zuV24Dd#C$FzZ*wfO^=U(VkjxS^n9I-h9(#EyrH4N<9J&&SAMW3?Ab_De*Da%!*_M+ z4+;-PHYY3V9oNJ}5nyyI^soDUu2fmqX49oG*5mXawa0WHtB!oqePVe2936@}m0no* z@_YOfFn6k0v9PeRtaH_}Vq#(%rCNX({WdsbO4e{@X1%51ywo0Mp(v z2uMD8gcEjcWZ0L?3j}Pon!hL}J@5{~FxR5oYcqnRs5QQS004|2Glfk5<--*I`U*7= z|8Mp_lIui4N%S`>AlHh~wA)`r%8l^NO^YO<}j{iDKy zzk5?|pvUZL){WbG8HiTj*^o-`|SuK~LFVeY)K+UxUPZ~Sac zeq}C?WB-;I3PHkKUcb$-PA^yy54o2n;DJ~6l5c&R>78e9OFw)=;NhX)5tbaZ#qEU}R# z*;q1>j>X6aLbmr0r#0ZuenjUy(HIAK>*DBmvD-=BP74L&ruLR=rJV*^Qc}Vdhq|N$ zPje?Sr_UUSZl#wq{`e7>gwFD7DNF5eblSZ&CY%>l3ztNwg6ZL7_j*^pb98x zDQ&(=gk~5Cx*wbXO{g^s^y4~mGv#lmEmT8MQE_LaAgZW{@kK^ls#A7uE@nN=N8MU> z=gkS=VDQP!f{c{P{~}MnI5%M9DxYW0t-0geQJFdfZZhH>!xjQ8dbQnP)+Pf~ILq0; zX_GCMMenRr_$s2AiG{`4-X3gH4l>Ru4yASFwf0N;*;42}p54Mu#qhNH8L6J{}!jV{~+UlDS+k34_vq8>sV?S{&sVubl2b2<{ z2xM0QK%k`?bWTntv=RpSxtjfF73kx2x%c$ndoWm}1FgB9+AB=LjEuphe4#C&5;!XH zQmpi42v)6gO60!IZfH{zUtl0Y4z;cJ)en>uKG$FAY^t1_A3l8Gww_w0&iWZ=p~TYQ zg6A2IRu7Q6MRj*~mtbFIBsndOm5BzBKM~B;@Ml|~WBTygD%E-qYcoMy_>3ZINO$8C ziQ7dzFCuz~kZx<4%`D#(?a&nqJA0&{Mh=%^&zE{KO#hjg8Jqbg=9)UXxs01ZYnoTU z7}WCi!gYyoemtIjKs~zkhkbG0X{UQOmEV8Cvr6ME>G>9Wt<1u3gL>te@V2uXIhI5~mcde;6YPamua&NsMmAR}7DRk-N|Cd!pCv;2nVwH=7L2ghgl(dNGg zH8}B&)a%&v5DI?*Z*T9jI@*#uZ}P*bYU(}&O7XUaENrLRF$;PjVD# zIC|2*^?!T8i1iLWk&uwk3RmM}02AEE{%@ru!{P^6SIE;9_`kqhSB<6Tk%0w^+GAs2 zP*{YL@a~21%o!xuizBRo8jZh0XJCQ=g=-gP7jo1(X*48R^xvkuRQsWfRz_jJucE7) z92cjdqa$a@_2kJD;Ho2VAF@9Nllf?I$AEJh^70o_+Y#;kB; zl6pj3S0&I^ExcQefKU>ANnu$pn2x|*9(p@e%&gy( z%wzK_ghGsyyOzQJRO~-DQNUXKd#fI7o!zj)`ioJNXbD25xc}lf$PBPQbHV(71uW5c zG)~MfdVf{4$O5Z61^Fqmj>D2d7Q0r{`~#q#Rl);bOds8;0yXacD31ph8vbR8)?afc zx>+EZ9cb-8-Y2k)Hw8X>m|VCwixO3n-%aDd|I1n(z$EY{^r|vbop=wtN-gHIWbMn8 z&u>qGWm|0BJ|M;;=L;@ExC%?+9z2-HqMK~vW zL5VA#^+gRiAbmD-F%+jOV#NtBV+W04WESOEM@1~zPnT(7ezvZxWW;#$WpJ~uM1DWDwp^%|^X7N4T zNlJ?+T4(b+12a19Lp--8IM6uH{sfR^pwE#BNUG6ypisYmK_E~-%Dfw_^W0}B&ANZV+yYW z_9eY@p6}rX(X)T0k^>a}dn!3(U=^fP9n0*DgWkwyC@0av)m(PY2To!biApu^lFzd+ z{P3Y+)%`rsz;g>CMa8+kxp{zy+jk72@t3krO;?`v;`tHPOxu@opBvssY)ef;lXwh< z1^!klaCcF9z|L;BBRu@e`K23?gN5UCwdu%|Kh|`19%o{rt7L~pfRq!6KeB*EXbN-$X#EiE6Aj6ral~?wT}kC8mP-9BnVB$IGAhAL=(ISVZfc z>@;7K3)?LHj<11>gtR;e*`AEr-q`3}qW-wE@TI#9bwA7}8|3jUCx;F*0;aOFPnkfGmpY#tqVIV@yoqS>OHkwCaCi-e6z|$L>xre^8WFSE zHbQJ;tFHuW)%Dd_$pTm2$;o;bWZIf9Dqz}jHW$%0vr}o|Y-?bfFI)A%aDUFjIA^p} ze;#-k>C#~X`ztk==pFa|^tb%ARNiwBE|zUxXU`)mGCA`;Pon#ye)oFJv6dsGr@Z!i zGkq>Chbv4uzS*RCpONwN+st6yxFLk5Xg!zw4CU-cK+Nc9{nys3tr(4WGU#8RJ9C$V zZ{8rNspp3()VBK!oKQoAPM64CwzQoNR?p@duLqj17l*Vl7kySzl!b&!G)k`)Mhbd8 zj=g0KoATXF5q+zx@0+~7vg*&#lP2APmdJsJNy9xrH_6P;FB>gulk*@vK*Oq~Seq+= z=uz|Ach2d4a&gY#bdq^wUI%^ju{6(vwkDpuz4h#g-iy)WR@mE+sCJ3}bNrW5w%~un zf7xAM&0CHi$@G+&;D?NjT%3&QlwV!pf;1xz>DhsXe(%b$1jT`ZT+-l2z3|FP)mN{+ zwE9PB(Zr+)@P=|pY9yu#X*hZiKa2#p{8LjWt25MVae3Jt(VgPc@_}|JBros$_01x; zJ34Fx#k@(4#HS)o4^!O8nh{>J|Gq4V0AsNO-#XFb^xErBoA-oM5SQCs21)PqzW=EZSXN zU{q10e@^l#e4c!w|N3=P@da(#1?#EWKbr_rsbdcPk{AwMsoAvb1#D5w52hyeNEW%y zdRRB;!!sTP7*QG~Wp?;`i2MKVq_7jqUxD10(N>6!jmu$0%WPEF-{ENji;u9&8~tv_ zvxA$h_r+{Me45vlO?4!?$P&x&wE1$&bWqUBMCMKiO0cnYh|r;$3#ROe9Oew1RoLsy zGSkSaW#C=tihRl$=Co<0BxB8u(vn$XGP1Lu!TpbEueV;KHxNWW9?fsr&JJu)^baG` z>xZ#ubGD2Ad}dggsfxL1`$|=DbPYzIG;#(tCC47ZrKo%5-;W(sKt)Nme`LdDzvN?L z#`4+Cq50$XZdS`)tH(rFI0-V&tr-HO@SC|doZNYD#=9@F{Y3}SbC+vlZuMtTn z{gGd}u;4VpIkCLFf2i7VI5!8pE|5MF(`uHJN6?}8j9TuaR@5V8O!!!^A}!6uFypnw z(4<0Ep5pxQYRx>0g;G{sUB5sbh9i%sr#3F>S=C2a7FtMgQ8Pve_BW6{{SyZMd?l+G z5i$GLob>T+OY6zVsUi)H3hac4H$t#rN<7SNF)@@$Rjhwzff2rtG`IZ@ERUrSIWC6r z6i#K?3fhptKfE;sv z7Zu@tior@OjoXd>ftkt-`g)B?*6aS$Z~jVUi`gUJUI?~Si-)tuhO@~g+U`HTU*sHh z$W>V6(MCW*A|gtM8c85pW8tuZhwo8ML*%ZykPyj!xH@gt@qc6%q;L@G=7erqixT4kyibct>_- z5Mn!2(qn>`j&I}QXc5CSzQ}xX-Ze6x9OUR?d%9WawdDOPn}ho?Fj3me3#DEpr7Ih= zU2Q%2pP6K&3;r~}q%!zmCfIW9kAGr3R;=Zi92F(* zl2uk#R$MHPe2=Lcsm4ybfnlY|AoKj%{VI}+Ls;I&g)DM;sIba3TX`Q%A39b1D_nq@ z+H`xS7F4JQD;1_aT^XRF$-#{Qj`J?!h5-Y3L+rMf(XdCTP@@I?$KP?^hL*TxZpQ3E z5G|VOVbv@%*!|f~5IKZLAE~o8o zmt{{Ogx9mQOY3U)p}lrF)eX&ZqxQd|#wa;DqIXIP3VtW7kyrUTxR<49J9TgQPnvi^ zjxp(eWxx_dCSsJ1S>S9Y*99gk`|U}(DEucAktgYJms$HJ1usO!gbULbMSHSXlk+jU zFDNEd2fEGLGr>1tTlPPaV1MEn|9ea48rS|t-;xlt7Y^RDgUCuMNPHACc=P`OQxJTy diff --git a/docs/uml/read pool unlock.puml b/docs/uml/read pool unlock.puml deleted file mode 100644 index c3dbfdfc0..000000000 --- a/docs/uml/read pool unlock.puml +++ /dev/null @@ -1,10 +0,0 @@ -@startuml -participant "<&terminal> ./zbox rp-unlock" as cli -collections gosdk - -cli -> gosdk: ReadPoolUnlock() -gosdk -> gosdk: check initialized sdk -gosdk -> gosdk: create unlock request -gosdk -> gosdk: create smart contract txn data -gosdk -> gosdk: send smart contract txn value fee -@enduml diff --git a/docs/uml/sign.png b/docs/uml/sign.png deleted file mode 100644 index ce935f35f201194f1b6221b9f1f6f64be3f92b32..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 23708 zcmbrm1z6N=_bxhsz$+k#loDdmrNGcBpd#HRpc2AR(jaXBVjv+gbVwuJsVGPcAl;x6 z($WoQ4f?+C_x<-i`|RsFUU~sDKc0DFJ@;DcUib33s`AZKM6^T*1mcwZEm?I00%r{V z4g7@*uUNl3{~W%sImzidnbC3(Lfka7HFLP#rXnkt8eGM?Qve8z1H*#|4D%A=i83)^A0CP9paz(&0gtPot+fR zP4+vp^~T4~(kRwU&V_V7M&aya$St4ThzT2~&_rzsA!nL^yEnfRx&GDv^+c~4P6JmB z+f7w2qoDk&qEk7uLd5pY1`^W0hW)e3z9D?X>3no>*d3~=M5@lquy!NC&s__R$U=Q>9jPll13=xNetWqw_^p9OKr`bDOWD>fv*|xB-aEeaA zdb~D{RKe0R(_stIc}}%x{z?q$%X3?o0y40ao#Xsp3Dm)NB zNiqhVLzR$|vI(bL=w*@VwJS^CKfjpG?~J52RHvk*Wc|-i{8^;tXfku;y?xQS;p5Xu z|53?DNC_sUsiF>v)-PWY?Kmr=RF}>pMDS29ai7AA`Mn7;2j^%Vk~?l-?4$O3bsQsE z>i9-yUJvpp#q&LQ-5Y!oQQDx66tw89$KYz`+~OanZk9NSGS8fDY)@rUFn`DVsg9z@ zr11rXIR}#sB^Pl{kZkx zhuxC+!84r)?Xi}}zn1X?ZS3WIf-6TKX>k8R#-Ja3io6ghqgU+J#oX6;WA2AjPoJL1 zW~4w^-(=`n`1W<;1aY`JzmsWagsSml#c5PAN-I9-XSi@k%#b}=f1hmEOWDQ&v zU1NLfy)!j1F_9)AVf2J>eC;#Maz!uor=b$ZD%a)C47CQ)<%R9Fj#jG0HQ67Y8@=Q? zaejDvscFhT7CR#cjSsux^(}^aj-Im|HX7cU*X^M{mD>_D$%<4UjqFp@vTS=i8Tg#S z>D$NBsHmT|qayU8rDL9As(cS|=o6>q!{X0f`z~F2=T7zOcjMlBX|Bt*;kpu{WOh}H z(|NV%jk=@Uc*!HB&;^!+YRv7Xm*;5DoXOBHHpkp4tS6J?l<@JkYI##^)D;S(TKfbloaFV4}7(j5_~A%_-)a1b5L}?-{+2Qxp{XQew@hY=46WEA&y%73$=FQ)_&}_MSZ|JW{NSm1%>~01B>`vdC?KWs5)WApLMb_h> zm`y@`DWfN?h+es${?sm`ekSkN?)+s@Ckq+kbY&an9rmEZ^>hw7&YE>~>)}!^`IfS= zN8QnU<1cDdFOjRZQmS&S?nJAwh?UftHrKg*rL+5lJtY5Aj77=o>s#XD;saE^m()Zl zXM%osE|#RbiQZ3sGCeF^0iZuMSvT)gt{=RhaSap?MeFTRCbJLG;3sVpT_pU9^-KHdn zWaQ7tn{&OWf;E_h+-Wiix1A4!9xsGVO&!h+>b059B<-BEdye)wbgu{wrtu*j7SqzU z?Pyo_UiqBl=chzKRvTc(-s8FbtK@y%;jWBeQ7g)xLtA!SL`H$?b>SRU|43G40t(==GN|_$ciwF7Dh*AD^DQtT(k>XFSO) z6j=M-?ee(S)=93T!#(EOx2c7fgk>?I4NM7a@!OoYUhBSnI62#Wsf-VMjtaf|i)F;K zBjvVd;y#{vcHL)JgU-d}ZMgNX^>v{~>vP??StyOIrP0N*@!D@4czST(hD)qU9L#w2 zEWdqpt9&!lvBlOybm~-c)Qcci4NsC=1a&*p(>=`E{h{yz9>6Mgaw1&sW038 z5uCv4l)M&Q6Z*N~mr@M%n_a=a>T62L>5hA&u1!JWOT&@Q9pdDNhqc9ANafe3JtlcN z;0|&=GY2>%6Yq(Uoz-?cXT*Zk`0J>JL*{~-^^5;XTaE!!PUR~I zaUF9H2^vV)H4zSNx$h8;W(rWSN-H*(cKAej^YE46_u>T-BpyxuG>W=AQQ9kA#iv=! zc*b(5n8u*Y_FjBm89*jSXagAy_5`c4qN_P2CcoMw>%RP6aB%W;0BoVDe~)sBkbPgV*8V#6tcptTux(YeY)bjP+>S{p}V| zZOkJD>@7+(@0VBQ`_FIpg*anl#2&PmW}FkcjEy}qqTgjVean41dD2eFQ1Q zIyM}EU$~_lXPsrva@&dCydz%RW~g|3+ih3a=g?DxcDT$g9Zy|fKWjuB;dFxwd1Sx4 zuwv(0SZrxK7rE>ewK+|gX;=MXQg*f|d?(dB+qn8g!mEu4pTDL}UIkB=DG4tqXYO=bXM8qX! zng{XxoC{XA6FJG}t0Apn-u=S$!Ol?Wh7Kj7R7L}{u9ij-BhgKH$p)c}uN1AsGZr^x zHQZ;ch8Q4KLUyw$C76qp5fc%4J9BB*?Z@-BmL#$j&3>+WRa;6E9G}xxdCmHO3g36J zQ?rOTY^hHs8h`FFycb62qGLXKw)8Lvx86kxBgJ#q2r=;8@e=<;v=Jld zFK3;DD4Vp01c9i;LlGblqc})BgzwVTWb{h8d;)d^_zgFCh2wAk=U{*TjD2JzRplxV z;^E5-^S!lh70HT<3d3qQH9b{h-6u5McT}l$r4CBpT5l{Ab+n_At)Ax)s&;qraaH4@c-Kznuk)0yJZp(UQ0%ooELn)DQy4! z6U3{%**ZK%v=kJ4ZolqUYO>sfKf!c@ucz|vRIJwf;iaK4YKQPu&->``p!v~Edz`4t zg08IW(+afD(Sd6aIy100-NyX@P+l=sNG;T^0&QYqq7o;frKWc6{-?lragU&36jE8$ ze0i)Um|pmL>)zI~r04e4@wphg+LOe@1_k%=snjIQ*bntKhDxkv4Q{s;oX&N}srLn& z(C4wdw)aMx^+ejUzFY&x+3<*n%BrfFnVF(qm^UFIq498`|0_B{bCcf9bVaq$garGe=}t_Rz29=tudmVz4D_eSmSVMw8{!y zjXONoKqg5-MmAn*L)SV|^nlDvJ&>!ft+_d=tu>P8MRY_Wk_U12bjA+th@MlN@@A6N zK7?H_s6x`q)t@sfq21iwukD$dnaSO_K|nyD&g{7RP9f64(b3W`S*>&jCNF0g#B3JT zpKgaMFJbop_OZBr`Tc2#)-Us=B=bBgWHZjTFD()?9;tR;n?BlZl5|URPBA~;2q{dz zYPt1?$M}=u-@o72Pn-90BqZ0I`}Qp)BBJr|-kSd@x~RiRwW10%)kY+@VHPcN&2xLkLMvY`2X}4{>V$kZTC=M=aM@cd3ucszueQ$e4v8L5G9Ll^+VCjRvobLUb zsP6}}qaDO*G_|$0@7}#DzN4)zfa=MZn7At`S+}`3%)-oEDAJv-lKv%x_wuDn>gwv{ zXsp3~535VMc)gOPVRvtj5Fh{5IX?eq-}GT-b%5f4GfF0jNu{V>)aqoj&;Ck4GO4lF zOX(7Ojg5I$&j(=at7C>wFVG@{;c*I``!i_=k&kY0Z3OBM{d{k?OcxAqaREk( z**Z|RwVylX@vdqkG5qT`_qD*rYD7KU?r54f>tVbH%WOtrVXq@+5E&m5QQGD>RZ?n> z5k7TJTO1-rLl=0v>x?%scs}xXJ|>%EaXjPPz{#a4GYK!g>PJ3DUt~Ym{QdG14m%6} z+Esu5v&^9UeO(6TF4A(9)es!m_VWsj|;Q)FZ_N4=Vpm8gjQ zU>*Et&z?z3L&CNFN)&PD1l(e{fL24rvjF#OQnj8tywNLw<{H{rmpjC_Mu3TV-ObG&P-;Yqr=0()Yu|DTn$xI(RNzxclYJ#hbzB#l3d< zk#ba?+0mk|?@`DxhwIlr!aN#ua1hF;dOTmP&2$7jeJZxzDP!!tV`guEt%Ig_q|9zS zNhUxY6B-(dMOoDASFgmK=P2byF5h^Wcqt1KnR_iP3~On(=2JC^I)WKQsmaMd^j14e zwcrtwcH$!G7#PGKPDshsk@TWomRR+N=IyW}>NCNDn1W+g*VYoeHU|Z4hTc$ktp6;q z8 zN}iaW9?p;1*Bl%iGFGQrBcHJ=`CZ8*7-SeD4G*yhiMW+9q<`r;!G{ z0L~{e^eBkD>G&<~p6R)B=gtDNEG9ZS+5w3@gQrh_d0_O__>l9zQFE+gIR74A>v(}l zz(9^RQd`8N@ul~{Zh;xg=a1lf{sPF|9zQy~`B>Cp@-MCg39m<=0?EZ6-N|G_m#HL3 zGz5_Gz#lHIS3yCcfO!XvE1#ZIxYL++02aDrqrV$#FHU@TQk-v&q1D*1dgui?^bS5g zesR5R)@l7--C2kfmKX8_tT4m~f)l9PM({<9Uh7|}x%9WUw@(ldNbLV=2q0zP?bYTJ z4q0z8D!6s~HZT_How~`>;vSpD)`OvS{W-gj38!E-ao?=~0s{jB!LH&Z&D)}UQPY?h zmQ0uJU*8fFFYJoks}W+;U&HmqfwRrsC}-p1g1|>H+RC57TWh&uCO&2Pq}h9k{C!4M zEZRQAd6_XycaeuDc=R4CE9=jZinW0Pleb9Ub2>*h^Oq^6j!LNz^?vC28_LSc!=*NZ z48i5)LWh<3Z)9AI`nH#xw$2XT8hS5G?<|7_*(>+EU7Yw6ycHe8o)Eo=2@ z2Yj@f_iwaYcy2}BBb%%#i;}}c(IAAdNZHn^Af6KONY;$Ma(;NPiDLHB>D+i?OGvN)TN4}K(sJ9ZZ=*98XZ~9LqXmgh zNyw9M+vCMWAFcoF>{K;29{(NK@jiQ(mR4%j|6VQS1`UsqnB%k}Sh=dIs+3gymypXH z36dS9O7ilvok>p;5*Vk*&hbwE{F(ypX>)Tk4daD{7H+NXp*vTuT`MqajVQKpbK8D- zj!&^f=@CS!5Ge0$Z#&KPSZ*#1hO4C9r{d+~vm`0L2gyJDZPhO_y-TD~q&1eqR7Dm` z3F#k>>HD!7kFY%pcZduF$QaFSX$7ox_4M@I-G!%`K7ArNFBYXOuMYknfWTz<{qK0P zSXEv9`VIgPe6B83wB~}CF_LrZ%W9FOED|ENFF9--|iWPQbEuD(4W(ab(^efA^ z9_0O+1ipgPXKfl2A0O}O_`0uNQ^&o?dDLq{_D>#TfgzTU=66~e5x%DJjZ>Q?(f@b+ zXISeQ9aLCYcPr3Y%ep zuSwIkX1Rv0ceApmG5SK@6SCNh2tMHjdhpbV*B_OdUNfZhCMRDic%8}Iqokleb5D&J-|6 zU%^X9pa>pG^YR3Xt^V~aTT%RbjrkJu{mw+`f`S4T<=Dijl$*g=S4B;FUhGzHUyhzk zE9?apyHF;AueTt?8ox8ynIsbqhb;fq0-{F?i;T=oL0P~@e=R zq%Q?hS=}JRr&R4WvO-!F-rcXS@J0I>y=iNW=6i7C6)hD#y&}yagQ!dDysM)ils#5N z7Kh7J6ctT(Ac`Y9c`~W(H>9Nrv@r6SlT_^|2-?M)I+DtL3JD2;>}YIkY_!r@W2Ez{ z7F|TGU%VDg8l^u zMnPX+UspGZmj>|ya1kdTAAHxs4Oxg2AUJ^S&CbE`{R{;o-GJCwdY74tSFc{>=2l+0 zeG=h}`*)&S^Jb~9p+H{v0)jq(ym#StaLk>PP3^=zBu>xBdtZjBCJ4D0(GQCvh!V{c zY3N0HUN15r@KyObiC@RjG;Nal;%1!tiQm-z!o-76$`1w@Xi}0Q{$~IZU%;VGDgXpp zc9n33*t+EE?K$Hw#oT{h#l2m&Ft%&DznbU~>{F$oXU+s4!vX({Sryw=Ztv}?1asM( zj{Y{7Z81e{c4{6nbHjVGNYPDuiq7k?G}O!wb~g+LU2ge5pk#`9G7cKcB77We6|Ve5T}iz+W3f z$Jd>gw-|qjlc<7S?I(A((ZapqJ>wP&r+)Qd)`4?pPXk#HjI;54r~20gk(2%ZAHCqz&SYPblUGe-jLzF0j+fnFQ3imUJhpSiONL#`_HRd~>Mrk+Y2gktNWa-~cLy$T> zIXO8!eYiWHCnqOoW@gs%s8O@%o^e-J0y>h%*n2*=OaOBF(t~dSjNzX}d`IhIpeR;Y zfBzG;Z8eg(!1?Cj;2>D7>!>r0L5Z{LrO?_da`f!)L{!99o)*>*l?&g(_#Cb!Sh@&U z59E98xfD8DX0|YD6mNZ5NA%?EKfP@UMS#7lug;6_MjCsoQO_e3+FgY26Q^YB)p^$x zsvG8;-iqkS6l@A+tXnJ{&hXkbHxm|ERQ8)ZtItb9yRRfTD%)GTGfig}t!3}{1`WB` zCX+%$j&8x@1;qkB{Pvbdszpf|323&}cM88{%W#*Gg4I^uz%x!9M#>vQC}7Dy4T^nv_9=)2KQMMs|egL0a0c z@O+Oo>s*q#%O!g^iMSoO(N=d(adl?h59>GbcvYFA-n#^`=*^dB$*HNQXJ-c?JuWXV zmkA&(NQRizWpStk0>V?Y{J*xg(&twI>sC%l9__C|TClUT^VL6BzeGR(?uTadd1If0 z4GD;|A!h_g104Py!}_MU-t!{2LSu7Q&Hid@4?0;x`-(%!@=*SzK5_FHr`1eRbtSF8%l+Gfn}~ z968UWYhQ7dK7oy74B?T9No4~gb;0Uec3y3=78zOa!QK|}DTA=L96GzZyX-1w=o;)S z-k>G-7K&cJBq550>FSt?n_zFuTSj%8pM~LZ3Sh1&W)Z?xeVi>9odKn{LvEhGSC+&U z_O@`B(rVnXL%c(5tqr#5JyJ)>-hKs$(%a4=u1ktkw>}knHZi#FAMPz{qS5HvBHnYE z=~xs#cTQdokaT_UT{4iHg^g@S5D?Cf*BwnZhq(t&Hi7$vRp<9n#_>*avG#B>vSjz= z-;-HWrXqaBD2QL30-ZIOV^i`7NHRj+ypbZ#g`&}RNQu-9bELe{{noz6 z)r!VqQ18oKy;5*Zl}|P4JW4`ukKZJ}s)N!1TU*jHP;*0+HzjEjKyXh}QQ(&(n_*yHP% zRr^A-TxIvmVomhg0fRyBAFi#g>ei+jXUX_+dC>q>wAIAuld4#m&&tl8HTsKcV9U(G zf%kY_y2RF7Y`SSv_CFfGu5OL{@KC=E4nd31$s440|^O=+>FX!VVi2hbxi zsH8_RonxvkRyZuT>#>3JK5O61yXpP4gdp zn8mnMWjP}ArYupl$TQYjFI%G&^&EXpBTez!&Z_t9?a#T7&afz~K?Q`Y)psb-b zTWs0w_Ie<^w@Ra4|8CXtw{Oy|;3iqJ?Q8?(PE_Sei~Xv{yH6~JGOBdSjPm&*)Y;0M zaW3ON7qTcLn!ZIlT9Hbv=+o^5|I2X&M+eJ3?#!Rfu4K;OP#~C_x6pG#MqNwO_?rvJN z^Dfgx@WodEf*8oZ$9&XhcZy7m0dL%+TBe^~CB&=Ul zLN;AFZlDwpC%ZQpQKcl^aMeeqfqitP&+T=fZi}r=kq2hMOTq{!+za|?!K(WDv|J}n zoLCsEi7BZc>kNGTnmVRNntsWQqKA_ZE_H$)+xLo3N!g42q2R(g7EX_`*Ksr-lwOt2=Bh&sZ}lB+?h!T)AI82vXtrF;%MW_oT%gq zYz?(<5oPLs<3CVR9mR90G~eRuC2jY{m*-gC9mMCjlPFX}!hQeArTrSHnC}Kn=@y7~ ziMM{OB*YD0^MCrDOzR+3Q}Gf z=|3=b*50>~igSZtqty@nHsQ{MSoPjg6(j_y?w;#kZ@x7XkSY<~{Y#B zwe@54loY0gjOXF`k*wum(z93VD?6Hs^BrBaZ|WEcNMjsG-kT#{KFQ0=`~C3Q{cDL# zJ{)})&$<}-+C$VyB%;HI;3n%K(ey>*Jmdrz!9fw!7eK6y4>VO1F0%FN!Vl)S?7{ps z+?RMgoDCB^9kbdP|2P^h@Gw*J^9E{aa39UF6`bAEM&%Es_3n}+nxFI6LiDsJVZI$O6DP_esE6+D+D~mTy+?K-d&gCK=G`aVBLX!10 zEesT)z2Q%;!*SSb&M?9e6%0 z{UK@k88iJ(2zwmz2Q1vk?bpv$lIt1ib_@YAT%ZcM(GWod&;%$fJ>Rd+TBFjpv0P`cmHpa@l91 z-jBItK*`FHboh#Zz-CXT1{D<*a9uwKi&!|+MvTYLzUb6#xs%e!ptYC0t71`ya~+hD~Y? zW66{dE_0smTL|td=&f3+R5)G{)}o52A*4#b2MaQ2Gkw4rw`g5bT--A{DlRFRko(@K zMnp(xTYr&Oz;@(TQbcpx?Fj33fqPj@0>3Go!w?( zVJRYt1(ttx`%h}>`DWorV1=9YDJ26+;tIaLM{Z?HOm2697DJus_qL{--WSP-5nKiq zB^eokc`btdbYR>c^Q!$Rkvdn`nU8(YLDCybDb%z^ysXCQ?X~g>=A6kaA%sN{7M`~sg`3*Ta zs4g{MS&xc|;W+PSGhABmzQq5Ne$^7}AzF}QP#R3bfY-iX{H6W|*GQ~ZH`(tAwLRwX ze$}QLTYxopipOlEFQ&V?x&}Bb=(3Io#NwKPY0o!xB zY_f(>E#t~R>MQ`>PCxC%eU}qI70tJ9eEzBB>`4Cna&ak>3XshjCz{sRS6g|yR|Cl} zzb|Ed__OokP*!1KC~nQur%!>i(O5jcw>~R_14SX>=n_a0}NHn;}w*a1LXfeex^E?bY62UKn6kSNWE53d^XxGoHYyf?yZ zsA+3A&KCn0d+k~XmiZIEa^>ltV#jv0n0eb-mlaXFF|p=p`sbIgV1d$C46ZP@T&N`^#yCVNIX&OunY1Tj!4nt6#VL6 zszoFX@lREYPC(*Kf(sy@&yrM>U9l=V2sN6P7kP)|3iA%5V>spZ*oBCnjMKrqGjOr< z;`Mef!hyO?CTG!^&@r5RY8a?V!q(veK)_D5YE*1OcxmO2{CB4wne$g?KWr8ZBGNvA zB5(n=h5hFp%q336cA`L|{`rcxxdhxcr`;bOMz-A6#{c^XV~m)hk@NhUS6=bPW5tr^ zx*^*JB?kBJrB}EqDOV0A&=)>XC;bay1+@|?r}yT`$?H9vYRhK3TMDjqS*?nBRgr`@ zhL>g;5t}Da5%&(un=im8f3Myz-5*B!xL>M~Rw;D6kz$n3_wW>UaWzi86==-Nl>X_( zK9gu2yMc2;qOdAQ{Ie(0HTVJ^|Fm0jW6x&0|6pPSYm-C|ZDMsWBr%5q7Z9+l2zx{P za`txcxv>lxdIab3*ooKu?#CIcvc!i8g3|F`M!y{J`)`9Z&zde*1bY{P)l2zy@)gIykWly#YE5V4b{7T z|NedOST!{@4Gj%Y`~1M$5i3kVLqh{P*wd%4I!u0nY6~!0=($dbU2Q(sr8@=W(;uD@ z)0End4gsepa8KEECo?k>;y|mOjIY2jMRCuxMXyEH?LV{g{@IyCa0;Yfl${RXw1C3T zAfJ2myU3%pIOO@X8tI=g4Nz5T*=-ICn?7B8j9?yAt~`e!1cKywf7$2ghp^NUcCsv5 ze1mWNYrGF0>2q^%06j|98vOkEIWf1ZQ*XW`r=&E!qI(e9n3VIgpy|A*>k@r)IRDmM zcHZj#y#{|Z4REQ|&p=-T{zphqFw*9*&%kbDc|3s&D*K^rw}_dTn0hv#vhOZ>zd!3T z<+m755aQgscMo!=yE|EQUF`Bcdz*_n?Vzn#syp%?qkM=Zu^xwIRg{-UeUu5h$HqGq z4XFTd7m#UNJ8#VOe)#Z#87b+Yt7c?$-atu3g@tZ)AF|vRy}HEUa2X`M#ke%JwA6AS zk2u;x8Q{_h+NQbw-7yi9ZwkOg;09*J??sdRcS(A?H$x>K)* z4dj%w%r9|pyzpO~nE?SsiDfr)%V3R%;G)8RxVTm4Ud4-i20$fnoO?5~ru3;7Y1Q|J z`e^$HS4TTWE}vgtdLQhA_#kASoIrP%CG$=f}*vo=#y zR3sxKV|llK2)7*ppurv-6H6YKy;HfSuBJx*n|#%ma9KFJsGz16lH3kt{UmByZLRB_ zLi2MmV=c^;5pd6;feD_g^3?CqsO0V9V*bs?3~~CJN1(wDdS7yb^;JbVUz?S6yY?QKGw`CIKhJn1fft8|Z0%I{ZT*poYKJ@V^5y#0 z))v$mqC!T2j+LaQJN8`p2?@qvLlhJgdWTWhOa^j&LEEr$LlJr2)XrjicUS#F+nDE@ z>}X{=7w#38z}fC}sB)^-k&}~8-cBu7h$q_Z;0f5v!EBv3-8H4yeiY$&m^Llv))9V6UH99j2RI@u%?D~;;W=EkG05RlE?qA-Ue?%7rQ9P=}3G{{Mox|*=QDKg2NNkXX{3Nwk+_nf4zj3#9LxD+x7-#~^hDlof8`nu#1`QIwXptycE>Fm-__slI#{Q!B zR`!i(kA@yQV|EUXw|b=4t@ep&xSg^k%)X?qhegnE>C5gMEgv07ioDD+C|LUmrGDc= z)8-&5jxW`q)tcapxQ>~FwEi(_E@>L5+^%N^kde~zTTCo3`}7;S0&^QgRiMJ?atX03vR%s*Q=-#8@*_N-*jtYJ}i6B@1RiGFslQf-< z4z@J5Z%A710I1%R%d!dTw6_?q6=(D1ZU*t59`o>1E~{4&Trp(UP`U-7E2tO4$II{kjB+08dtrY`GnR*$yI!Tt7I<6@ zz`P5hbM`GIBD$D4c0;0kEmAh$evmG_3yYv086DO0Vq|307dJeWoidaYWil4{7k;&- z)Bd)-ep*7YqHuLjwNgl6;9s;yKqDQ3?O|0UWz#kTExaKDM9s!Hsw>f;V{ zmaTMIoYQucF&VG*vZWsH>%USlnwJ`MjW+ z#4)cwY!Lj&W6;#)@$+n_cMIAFmwNVh)@V3&r9y__VJhcOX5L=8<#KnkLWmh~XtQ?w zgIefaxUOTofsI`_d~#L|W%KzgYzry z!-mF6Y)8=!phUV-MtfGDa1gqa1o?{}^$+3O>piVZ+4oqg2v8hKS7Fpodp_~69T49+ zo1MKAVPKo2G*oJ%FyjKz!XS&576vYVvKG-emWDMrFj{*BQ6hE`K==Sgpt{pu0TXJd z0m_LhF2M>0``>?9?nI2vmf&M&uKgC zv?$1hFXGZP&Y{w>ukXJ3njS%a@R`OqUnMIG-QCsorFD`f!Z`aa-4B8q>n!09w-ppX z#gWIEec5SBVTLZwnx}UAQprxI3>ll%&yzE>i_Z7fGTKT`t*a%Gy+j57=lCu{f=gx^ z7`8?2H4YZRk5H*#D^0dbtgo+!f<<*|s;r)jicr5r-is_&o2N{54$17s?wNswhL?St zd;x(WY|vie{gA-PAoNg5kHD9fknH>fWeT3ZpiG!>px z?=Gnp`8_;`Y-p7#`+~jHe&|zAnNTi70J-1H5>+Nw-$n*OS@q)?q}AIY9B{x7nT}t> ziYQ1jI5Tb-p$JJJ2YmacRmX_^zATcC8apfkp>X^H$kYD%zxnx*2?VyF2nEY%d0hDm znDejP##nw#! ziB5qM2Qr?mm6h3^%x<6?N3)K&@U!1UM-L{+5Dw*oGI81`(NLX*iRl^d!TvrjF0Mbx zIfwP1Y7u$JGrc#?LOm+YvutiW{NjL*88;hSlKH{G0T5mSpt{x?x7^UGs}UH#yynNI zhyf<{pEQ(w3g|*+ZfuX_Zd&^snL1WQmfs~XKLQ;B* z(ATmh;QrX3V(mZ-x=Qc^2Qs(R=wit&JfyQQ;*B{e6ETxGn@aK_)vWN zby^>=iA@4H$CjIDj(0IKueyiS0;BMc7-xX-$%BW_Ro8D4Lr%%*GgxSrtVBditak{4 zHt1a2nI=(~i^@9cXOW9!p1n2siZ#5cF*Y)?NxQqdyW!)<_r33LboHNFDv=+!tVLs$ ziIpaC)*Z95aa*;=I-t9yB!8dwrq4(!#qX_Yt_JP)*ZQCL#n{;aUX+-nFG2r7vD7z~ z>|>b@^K?X}<*I%fiec{BKQhFgJcA8Mw8@VC`rUtr;T@IcY956c=b+IR2OuD1p=14`%rCZwhIz_eaE2dKw5zK9Gz;DC9td zAC)DbhD_6r18rs(?jBhx$bc5Us2)1sydIdYPE3qIN1EE*G=x4(!m$<9< z9;+S@^)F^Cd*qhf4D$QxPl*qG0B>>UfK#?YDv;3fvjS}oI?1#-JKD86Ry*C}g8-1Y zj$R>;8_oYl0FbUu z+J{+PJ~rMS52Mx9)d419!+6)F5qCJ77TWiL*S7SJj!_YWMI}aE4(`WR=J%*WL0Or! zihzuS{(U@)zQClFpZV`Af~{k5U~kL6k0CHGj+4?JhGST| zq3Kw|C@3t7{k=tBlNq2FwU4iVAbGF#@kUodTANSwU*k^sq5ZV}3vRJBvYf}qsQ>o{ zxI=zO_4k!IP7;4#nPZjGpJzdx17zwkwoVMV+e1!k_Fu!gOaNWtp1d%zPc`*f=Lnte zTw%M6(EYCuD!~iIDuz7PWRAZ4lhx!83JDxa9rXVs<& z#VstH!xzQ-Z5*EhfonIE!=AK@6fX~FU^`KDKWPUZPC5C1-64kZrRsro5aIuigC_mo z7|?&%89>omlM(^gi}vnEVjfoFg=fb`2nEK7FEl_M=q8(ghiE)aYxlVsr%A>8)|HhN z+o9EGK*&X8M0>R(5F1ckaVCJGYM&G)u^l+v0osg;!#`v!kh||s^xX0Jm+SrgV>SZv zbOStOKVt)XeM#>=)_ZUB7*$)yOSD!4%^RhsgLUEyLnW>K{bk9?*Yyw}Lc8N*tBL~} zyPPl8(%Jf5flSWyk8KI37o;#7SY?*e`)RsZ8ZCUQDTDv;;Kx+3?>SYz3_jp~o`Nz3 zlt|k!Uh%Z-d0q!9xP0dew4gRN6Lc${>HPjRp+aTApd`qvC3oTc# zZE(+Gce6d)JOtZ;_30)t<-u29tYAjMt0h{gc-5UajdlYHvW2~nCwe@36hw8lIuA;l zAbk>coK~%FR^YOfO%nS&(DkqHrYULA|I*R%UTUyuNP@G(R#JEu7``^{>&V z8Nz&gVa{2|v$^8xB-T(wF1{^&&A_k(^%hkGCDLQjZz)k5$m}~#)uv8W12?F@<^W<0 z+5u>twTR{(7!-=eu{4Inf51?Oh$Jtjk5EnDjPkCZp`L&j1_v-REoQJ0)XK}qB{ z%X}3%Q0uh^`k&uZx}fH3^`ja^pr88goxJUH#NKawL7-etVLkP$z{igtLl-0T!)WW_ zfsD1{&_dkS!Tp#eR4@d#9h5AOt~xnr-(83HX7<(`Xvxkw#*psJ=#P{8sl{(?&MUhvhkz*9^oL3d zZ65%)HqgV)jn8}9K2DmA)fD0GhVer?n$2%+m??33_Xp^bNo8kHY3|?d@t%Wm*5@Q` z?hzhy$JqBx2EtTlQ0A9h?n0H)xAJ($SYve5)}>{9a9a zucx8)?yxW7_m(^Wea|lT92(CcyF1(vQeOXfUIl1DE4MG6Bu2I#)4`J{pjVf4WFT*x z?RJE&L)V|3-#Rvvjeq90a3W&Epvb7NRr@zt4E0-KVPP;nXi(R)zG-0;u7?ofb0YZa zF8e?O#5tlxKpCqR71qBT8M(9j39FiwFi03AM$?#4Bb15bME^VrwW5C>CE3lv{wF>X z{OfTE_jrB}$>p@vc}{kQJWlD|JDu_Er)V>vMsfLmD#S~0uiL2f-=|x^$GqSrMx;M$ zXApVvQ!KeCGbiU|oE}W{i6nvbP_gCsy#3aw>$r2jA;23n3B4?)*&21K`tRQX(ZE7m zauFG5n|Hr&2qBz+>_&b2)&B#5@agj|QHC&-mDF9F6gmlg{_`hQuM*|sD8xjHJfQkK z%Y#`H9*!c!Lp4p~TndPCPwlP72Fe%(hdqg$@wLZ3jU8#-GI1HTs|)4O{VcNMg_g5A zjeQ-wIddWIIW}cqqd*bt;{5K)d4&Di1g(N5Qh&D!LL+(mfB(l#SVcGl;2h$%z>`B` zP2QYGh;jf2^ic_7qq*+LVys3>P_PWRXjbCb6NInF)1dRo5eL~HfbF!=KDD75lZxE? zQNJ?2U5%LFmBK`PjD7q6A-&oM{|;$MCfW`O#Gh8w>)fu=pgySC-N=UM0NPa&sViNV zYoRfF=)m~mZ#|XfMKLi?(B7pDF^fA)-hxcj%hOZDd5+&>)4JetZ-j=rIxDtEr16iG ztIuOU>t>31PlS3Mbf7W$p}C$lTl}kzf+8BWMO&YVL+*EnwvI-*qJm8|vXqvN&I1}P zf`Wp`FW*=P&LM3E`aURC}IzR*nhpH%+1uf_}8R}=gI^7#8VS0kQd1BGhquCu?KoAR9b=l-8o&O98- zwU6U-+Pue_Y?b6>4dKKLvM)ImW6M@aC|QO$YV5nyQBs(h7-Of5Wh`au7)gbqqAU|~ zknCG!31fYKGjux3^=3St6R6k4~35NjT`;$lI8r&0D#LN_VRR=~wOdz@c_Wf?dtDn-B*=do?UcR{wzL zKk=Dniz~##WNB8HTLvWL&LXq2H03VKzu$!I~E!M>NWcZlwF14$~8dk0E8%$J*l%f9lo%C-6yBy zRo-z!LuetJ5048rK1A3f8Jh}X852aYp~*?~XeiVwj(5H*yuoU$1Nnd|ZZlKUH;`F` zw?*1*RB#p^`g^-=n+~DR4Rzv#S(;4D<24>h{=0@Cd$(AK^ zPVt=i5;o|6K0chAR6PEhju=4!f(f|gUYnJ^2Lf*ts>Y%&s>$c*H6hyVkD>_M4`SBD z`}^$5gMKENq|fbDb6%SBn$HO}=1@c-9y_8@(YHOis{P+k_T2hJ-lnz{fsJUPy?U>M zr3S{{jI6eIMlkw=Zcidc3&<_^6aj|RZj#Prq+<}B9E_b@^4$0EWXAvkL@Z-|3x15b zE%2kie`Or<+-Par#(%yY2n00N8TJf_ss3TB!{pIp&7BRDrm*z6fwpz$v@wOT*f!_- zBjwwbmQp%B&Q1QzkkCP%T=@Lj^JrWOva6p<@kq}|9p{FBZrBd*blctF*k$UR;Rp6* zmxd@!Ngp){DCF{?+E}Rh7y*1+_65PVsXGTGVZfVV+;{|lZutw|@`1NQ zaC+@>NNaq|48h4XmTDBA6LRm%;qMs6kRn22Bp6djsj1aaOb48c5M^LkIBT7g`f*KR zd3X053=Q@@#?a{|0iKO`aYjO)$1LTzPL@jjmuF)i<%$2oRJ42>?PQ;y@AQXyN0A#m zW73Z@GPF}Z1Pp9y?7Bx8!yU4mbxyA?b!lmzAd4P*o8%eom|lluXz5%>H0xpbQKF$$x1lT zJ?zv0(SfZmX)7zUhvlnz-O^qYgpTGN=i}4j7bva@^3U+ZeqF}rN4HT#pyjySx+&~S2cn=jhWui+J`_=ZM_V!FsE(JqFA(_?&K z0Oj{t!!Bq#&E}F$fvwtuzy;1*dr=R;j-<8z@NA2w3DTI_Za>AouTv7`z1P#ZB`YtK zZWxHkbTwa>Oj{K6^(wqzb_H1`Ewg$55G=X=t#_S&y6G1uDI;}Ka&8WipVy7TqGips3mZn9*T8OLp#u78A<2m54AK3b5DFF#&h11Q$|;C-DCc*` z%}lWSID)Q)IuO$>l_4izquMjfP)CMkW5`!tft9let<-0Q_U-G5&ysv((CrvB&j_^ooHhH-GD3IGbk(>Y4W8tIF{?L`Ry1I@er7r>;9%%LmSUAQh0W49H46kNFer0b|voh=L&MwQ7B~RMg21Z*bCJ*xS zje4ql9{?H_sm0|;pHz46DB`P3GY+Jp$!z{1IruH-sFT?Z#Q#xCxBcZ;E#2A7(klK0 z3aPd8e(mUZnol@ZZ{V(BpcQGJ7!ja=vh8T|-ocTmtc!A`x0&>jFVqL%;Hp^iPk#~7 z11nv*n?R%YPnJ!Y{$9Zp7SgIgkt+V@3HcDt8Wxn`KW1`3pct?xur#; zVePyqka;3}D3_myR=pBL^A1Tc#EQ5BHIMqE?5d6zDdt}lH8GdpumvVlPNT6>s=-T! z!*o@XHga_*)thZLs9OA5Zlys0F4H^sj`viScdD7?vLSBL#Llm>Qu>!X&>tGwTcZfX z`j?hH5~?FoKHW81%WpC*dGX6j0qYZX15K1MS&+`*quC251PdAWEAm6Y&^Hkt(_1IG zlIrUtCJKeb-+j891~1lgxCKib%LDaa2oBlln7KbxD8cW7hC(ZvhfNT9hnraxCLyxFo{tE_VpSZPy76~jO7@l%lDX> z&4T22wD#5@HcEmMQ$!^X?`af%l`_9HxOiB;X4Xj^3=V93DA6O{5lO}QM?#(zoGvAU z4#t0GP`NLN(EiY|5f2!8N=gb)D4UKrIxOA**Cu3t)62mshp0Jp+?c?h{m!__*h{~@ zcz8c}zF3nPHO%?^)C9I^oaGzrT7)Ll;Yxkv=&KTfZFe z777cSXhDM6@03qv4l0hu}}=YE>)M&mK;&c{@&uv?JlG)Z*eXo|6`&_ckF-q zD;xiz#A3x^E4>S#Me2fJ)$tdMAsG96+b!D8F8L(Q=h*Y+89F2VJ2nTvCdf$JoYg?s z3nOXh9OSRN=fa_Ny~wA+hrFnCYiFo89$vSD*VT|T_AvhD%la25C@jqI-}ueavu0gO s<&terminal> ./zbox sign-data" as cli -collections gosdk - -cli -> gosdk: ReadPoolUnlock() -cli -> gosdk: Sign(data) -gosdk -> gosdk: create signature -group for each client key (key, idx) -gosdk -> gosdk: create new signature scheme -gosdk -> gosdk: set private key -alt length of signature equal 0 -gosdk -> gosdk: sign hash to signature -else -gosdk -> gosdk: add hash to signature -end -end - -gosdk --> cli: return signature -@enduml diff --git a/mobilesdk/zcn/readpool.go b/mobilesdk/zcn/readpool.go deleted file mode 100644 index 1b71d24ae..000000000 --- a/mobilesdk/zcn/readpool.go +++ /dev/null @@ -1,41 +0,0 @@ -//go:build mobile -// +build mobile - -package zcn - -import ( - "github.com/0chain/gosdk/core/util" - "github.com/0chain/gosdk/zboxcore/sdk" -) - -// ReadPoolLock locks given number of tokes for given duration in read pool. -// ## Inputs -// - tokens: sas tokens -// - fee: sas tokens -func ReadPoolLock(tokens, fee string) (string, error) { - t, err := util.ParseCoinStr(tokens) - if err != nil { - return "", err - } - - f, err := util.ParseCoinStr(fee) - if err != nil { - return "", err - } - - hash, _, err := sdk.ReadPoolLock(t, f) - return hash, err -} - -// ReadPoolUnLock unlocks all the tokens in the readpool associated with the current wallet. -// ## Inputs -// - fee: sas tokens -func ReadPoolUnLock(fee string) (string, error) { - f, err := util.ParseCoinStr(fee) - if err != nil { - return "", err - } - - hash, _, err := sdk.ReadPoolUnlock(f) - return hash, err -} diff --git a/wasmsdk/allocation.go b/wasmsdk/allocation.go index 472b7255f..b54ff7f76 100644 --- a/wasmsdk/allocation.go +++ b/wasmsdk/allocation.go @@ -387,21 +387,6 @@ func lockStakePool(providerType, tokens, fee uint64, providerID string) (string, return hash, err } -// unlockWritePool unlocks the read pool -// - tokens: amount of tokens to lock (in SAS) -// - fee: transaction fees (in SAS) -func lockReadPool(tokens, fee uint64) (string, error) { - hash, _, err := sdk.ReadPoolLock(tokens, fee) - return hash, err -} - -// unLockWritePool unlocks the write pool -// - fee: transaction fees (in SAS) -func unLockReadPool(fee uint64) (string, error) { - hash, _, err := sdk.ReadPoolUnlock(fee) - return hash, err -} - // unlockWritePool unlocks the write pool // - providerType: provider type (1: miner, 2:sharder, 3:blobber, 4:validator, 5:authorizer) // - fee: transaction fees (in SAS) diff --git a/zboxcore/sdk/blobber_operations.go b/zboxcore/sdk/blobber_operations.go new file mode 100644 index 000000000..c2714795b --- /dev/null +++ b/zboxcore/sdk/blobber_operations.go @@ -0,0 +1,306 @@ +package sdk + +import ( + "encoding/json" + "github.com/0chain/errors" + "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/transaction" + "math" +) + +// CreateAllocationForOwner creates a new allocation with the given options (txn: `storagesc.new_allocation_request`). +// +// - owner is the client id of the owner of the allocation. +// - ownerpublickey is the public key of the owner of the allocation. +// - datashards is the number of data shards for the allocation. +// - parityshards is the number of parity shards for the allocation. +// - size is the size of the allocation. +// - readPrice is the read price range for the allocation (Reads in Züs are free!). +// - writePrice is the write price range for the allocation. +// - lock is the lock value for the transaction (how much tokens to lock to the allocation, in SAS). +// - preferredBlobberIds is a list of preferred blobber ids for the allocation. +// - thirdPartyExtendable is a flag indicating whether the allocation can be extended by a third party. +// - fileOptionsParams is the file options parameters for the allocation, which control the usage permissions of the files in the allocation. +// +// returns the hash of the transaction, the nonce of the transaction, the transaction object and an error if any. +func CreateAllocationForOwner( + owner, ownerpublickey string, + datashards, parityshards int, size int64, + readPrice, writePrice PriceRange, + lock uint64, preferredBlobberIds, blobberAuthTickets []string, thirdPartyExtendable, IsEnterprise, force bool, fileOptionsParams *FileOptionsParameters, +) (hash string, nonce int64, txn *transaction.Transaction, err error) { + + if lock > math.MaxInt64 { + return "", 0, nil, errors.New("invalid_lock", "int64 overflow on lock value") + } + + if datashards < 1 || parityshards < 1 { + return "", 0, nil, errors.New("allocation_validation_failed", "atleast 1 data and 1 parity shards are required") + } + + allocationRequest, err := getNewAllocationBlobbers( + datashards, parityshards, size, readPrice, writePrice, preferredBlobberIds, blobberAuthTickets, force) + if err != nil { + return "", 0, nil, errors.New("failed_get_allocation_blobbers", "failed to get blobbers for allocation: "+err.Error()) + } + + if !sdkInitialized { + return "", 0, nil, sdkNotInitialized + } + + allocationRequest["owner_id"] = owner + allocationRequest["owner_public_key"] = ownerpublickey + allocationRequest["third_party_extendable"] = thirdPartyExtendable + allocationRequest["file_options_changed"], allocationRequest["file_options"] = calculateAllocationFileOptions(63 /*0011 1111*/, fileOptionsParams) + allocationRequest["is_enterprise"] = IsEnterprise + + var sn = transaction.SmartContractTxnData{ + Name: transaction.NEW_ALLOCATION_REQUEST, + InputArgs: allocationRequest, + } + hash, _, nonce, txn, err = storageSmartContractTxnValue(sn, lock) + return +} + +// CreateFreeAllocation creates a new free allocation (txn: `storagesc.free_allocation_request`). +// - marker is the marker for the free allocation. +// - value is the value of the free allocation. +// +// returns the hash of the transaction, the nonce of the transaction and an error if any. +func CreateFreeAllocation(marker string, value uint64) (string, int64, error) { + if !sdkInitialized { + return "", 0, sdkNotInitialized + } + + recipientPublicKey := client.PublicKey() + + var input = map[string]interface{}{ + "recipient_public_key": recipientPublicKey, + "marker": marker, + } + + blobbers, err := GetFreeAllocationBlobbers(input) + if err != nil { + return "", 0, err + } + + input["blobbers"] = blobbers + + var sn = transaction.SmartContractTxnData{ + Name: transaction.NEW_FREE_ALLOCATION, + InputArgs: input, + } + hash, _, n, _, err := storageSmartContractTxnValue(sn, value) + return hash, n, err +} + +// UpdateAllocation sends an update request for an allocation (txn: `storagesc.update_allocation_request`) +// +// - size is the size of the allocation. +// - extend is a flag indicating whether to extend the allocation. +// - allocationID is the id of the allocation. +// - lock is the lock value for the transaction (how much tokens to lock to the allocation, in SAS). +// - addBlobberId is the id of the blobber to add to the allocation. +// - addBlobberAuthTicket is the auth ticket of the blobber to add to the allocation, in case the blobber is restricted. +// - removeBlobberId is the id of the blobber to remove from the allocation. +// - setThirdPartyExtendable is a flag indicating whether the allocation can be extended by a third party. +// - fileOptionsParams is the file options parameters for the allocation, which control the usage permissions of the files in the allocation. +// +// returns the hash of the transaction, the nonce of the transaction and an error if any. +func UpdateAllocation( + size int64, + extend bool, + allocationID string, + lock uint64, + addBlobberId, addBlobberAuthTicket, removeBlobberId string, + setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, +) (hash string, nonce int64, err error) { + + if lock > math.MaxInt64 { + return "", 0, errors.New("invalid_lock", "int64 overflow on lock value") + } + + if !sdkInitialized { + return "", 0, sdkNotInitialized + } + + alloc, err := GetAllocation(allocationID) + if err != nil { + return "", 0, allocationNotFound + } + + updateAllocationRequest := make(map[string]interface{}) + updateAllocationRequest["owner_id"] = client.ClientID() + updateAllocationRequest["owner_public_key"] = "" + updateAllocationRequest["id"] = allocationID + updateAllocationRequest["size"] = size + updateAllocationRequest["extend"] = extend + updateAllocationRequest["add_blobber_id"] = addBlobberId + updateAllocationRequest["add_blobber_auth_ticket"] = addBlobberAuthTicket + updateAllocationRequest["remove_blobber_id"] = removeBlobberId + updateAllocationRequest["set_third_party_extendable"] = setThirdPartyExtendable + updateAllocationRequest["file_options_changed"], updateAllocationRequest["file_options"] = calculateAllocationFileOptions(alloc.FileOptions, fileOptionsParams) + + sn := transaction.SmartContractTxnData{ + Name: transaction.STORAGESC_UPDATE_ALLOCATION, + InputArgs: updateAllocationRequest, + } + hash, _, nonce, _, err = storageSmartContractTxnValue(sn, lock) + return +} + +// StakePoolLock locks tokens in a stake pool. +// This function is the entry point for the staking operation. +// Provided the provider type and provider ID, the value is locked in the stake pool between the SDK client and the provider. +// Based on the locked amount, the client will get rewards as share of the provider's rewards. +// - providerType: provider type +// - providerID: provider ID +// - value: value to lock +// - fee: transaction fee +func StakePoolLock(providerType ProviderType, providerID string, value, fee uint64) (hash string, nonce int64, err error) { + if !sdkInitialized { + return "", 0, sdkNotInitialized + } + + if providerType == 0 { + return "", 0, errors.New("stake_pool_lock", "provider is required") + } + + if providerID == "" { + return "", 0, errors.New("stake_pool_lock", "provider_id is required") + } + + spr := stakePoolRequest{ + ProviderType: providerType, + ProviderID: providerID, + } + + var sn = transaction.SmartContractTxnData{ + InputArgs: &spr, + } + + var scAddress string + switch providerType { + case ProviderBlobber, ProviderValidator: + scAddress = STORAGE_SCADDRESS + sn.Name = transaction.STORAGESC_STAKE_POOL_LOCK + case ProviderMiner, ProviderSharder: + scAddress = MINERSC_SCADDRESS + sn.Name = transaction.MINERSC_LOCK + case ProviderAuthorizer: + scAddress = ZCNSC_SCADDRESS + sn.Name = transaction.ZCNSC_LOCK + default: + return "", 0, errors.Newf("stake_pool_lock", "unsupported provider type: %v", providerType) + } + + hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, value, fee) + return +} + +// StakePoolUnlock unlocks a stake pool tokens. If tokens can't be unlocked due +// to opened offers, then it returns time where the tokens can be unlocked, +// marking the pool as 'want to unlock' to avoid its usage in offers in the +// future. The time is maximal time that can be lesser in some cases. To +// unlock tokens can't be unlocked now, wait the time and unlock them (call +// this function again). +// - providerType: provider type +// - providerID: provider ID +// - fee: transaction fee +func StakePoolUnlock(providerType ProviderType, providerID string, fee uint64) (unstake int64, nonce int64, err error) { + if !sdkInitialized { + return 0, 0, sdkNotInitialized + } + + if providerType == 0 { + return 0, 0, errors.New("stake_pool_lock", "provider is required") + } + + if providerID == "" { + return 0, 0, errors.New("stake_pool_lock", "provider_id is required") + } + + spr := stakePoolRequest{ + ProviderType: providerType, + ProviderID: providerID, + } + + var sn = transaction.SmartContractTxnData{ + InputArgs: &spr, + } + + var scAddress string + switch providerType { + case ProviderBlobber, ProviderValidator: + scAddress = STORAGE_SCADDRESS + sn.Name = transaction.STORAGESC_STAKE_POOL_UNLOCK + case ProviderMiner, ProviderSharder: + scAddress = MINERSC_SCADDRESS + sn.Name = transaction.MINERSC_UNLOCK + case ProviderAuthorizer: + scAddress = ZCNSC_SCADDRESS + sn.Name = transaction.ZCNSC_UNLOCK + default: + return 0, 0, errors.Newf("stake_pool_unlock", "unsupported provider type: %v", providerType) + } + + var out string + if _, out, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, 0, fee); err != nil { + return // an error + } + + var spuu stakePoolLock + if err = json.Unmarshal([]byte(out), &spuu); err != nil { + return + } + + return spuu.Amount, nonce, nil +} + +// WritePoolLock locks given number of tokes for given duration in read pool. +// - allocID: allocation ID +// - tokens: number of tokens to lock +// - fee: transaction fee +func WritePoolLock(allocID string, tokens, fee uint64) (hash string, nonce int64, err error) { + if !sdkInitialized { + return "", 0, sdkNotInitialized + } + + type lockRequest struct { + AllocationID string `json:"allocation_id"` + } + + var req lockRequest + req.AllocationID = allocID + + var sn = transaction.SmartContractTxnData{ + Name: transaction.STORAGESC_WRITE_POOL_LOCK, + InputArgs: &req, + } + + hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, tokens, fee) + return +} + +// WritePoolUnlock unlocks ALL tokens of a write pool. Needs to be cancelled first. +// - allocID: allocation ID +// - fee: transaction fee +func WritePoolUnlock(allocID string, fee uint64) (hash string, nonce int64, err error) { + if !sdkInitialized { + return "", 0, sdkNotInitialized + } + + type unlockRequest struct { + AllocationID string `json:"allocation_id"` + } + + var req unlockRequest + req.AllocationID = allocID + + var sn = transaction.SmartContractTxnData{ + Name: transaction.STORAGESC_WRITE_POOL_UNLOCK, + InputArgs: &req, + } + hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee) + return +} diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index bac657b7e..3edf5c1d1 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -200,37 +200,6 @@ func GetReadPoolInfo(clientID string) (info *ReadPool, err error) { return } -// ReadPoolLock locks given number of tokes for given duration in read pool. -// - tokens: number of tokens to lock -// - fee: transaction fee -func ReadPoolLock(tokens, fee uint64) (hash string, nonce int64, err error) { - if !sdkInitialized { - return "", 0, sdkNotInitialized - } - - var sn = transaction.SmartContractTxnData{ - Name: transaction.STORAGESC_READ_POOL_LOCK, - InputArgs: nil, - } - hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, tokens, fee) - return -} - -// ReadPoolUnlock unlocks tokens in expired read pool -// - fee: transaction fee -func ReadPoolUnlock(fee uint64) (hash string, nonce int64, err error) { - if !sdkInitialized { - return "", 0, sdkNotInitialized - } - - var sn = transaction.SmartContractTxnData{ - Name: transaction.STORAGESC_READ_POOL_UNLOCK, - InputArgs: nil, - } - hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee) - return -} - // // stake pool // @@ -352,55 +321,6 @@ type stakePoolRequest struct { ProviderID string `json:"provider_id,omitempty"` } -// StakePoolLock locks tokens in a stake pool. -// This function is the entry point for the staking operation. -// Provided the provider type and provider ID, the value is locked in the stake pool between the SDK client and the provider. -// Based on the locked amount, the client will get rewards as share of the provider's rewards. -// - providerType: provider type -// - providerID: provider ID -// - value: value to lock -// - fee: transaction fee -func StakePoolLock(providerType ProviderType, providerID string, value, fee uint64) (hash string, nonce int64, err error) { - if !sdkInitialized { - return "", 0, sdkNotInitialized - } - - if providerType == 0 { - return "", 0, errors.New("stake_pool_lock", "provider is required") - } - - if providerID == "" { - return "", 0, errors.New("stake_pool_lock", "provider_id is required") - } - - spr := stakePoolRequest{ - ProviderType: providerType, - ProviderID: providerID, - } - - var sn = transaction.SmartContractTxnData{ - InputArgs: &spr, - } - - var scAddress string - switch providerType { - case ProviderBlobber, ProviderValidator: - scAddress = STORAGE_SCADDRESS - sn.Name = transaction.STORAGESC_STAKE_POOL_LOCK - case ProviderMiner, ProviderSharder: - scAddress = MINERSC_SCADDRESS - sn.Name = transaction.MINERSC_LOCK - case ProviderAuthorizer: - scAddress = ZCNSC_SCADDRESS - sn.Name = transaction.ZCNSC_LOCK - default: - return "", 0, errors.Newf("stake_pool_lock", "unsupported provider type: %v", providerType) - } - - hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, value, fee) - return -} - // stakePoolLock is stake pool unlock response in case where tokens // can't be unlocked due to opened offers. type stakePoolLock struct { @@ -410,117 +330,6 @@ type stakePoolLock struct { Amount int64 `json:"amount"` } -// StakePoolUnlock unlocks a stake pool tokens. If tokens can't be unlocked due -// to opened offers, then it returns time where the tokens can be unlocked, -// marking the pool as 'want to unlock' to avoid its usage in offers in the -// future. The time is maximal time that can be lesser in some cases. To -// unlock tokens can't be unlocked now, wait the time and unlock them (call -// this function again). -// - providerType: provider type -// - providerID: provider ID -// - fee: transaction fee -func StakePoolUnlock(providerType ProviderType, providerID string, fee uint64) (unstake int64, nonce int64, err error) { - if !sdkInitialized { - return 0, 0, sdkNotInitialized - } - - if providerType == 0 { - return 0, 0, errors.New("stake_pool_lock", "provider is required") - } - - if providerID == "" { - return 0, 0, errors.New("stake_pool_lock", "provider_id is required") - } - - spr := stakePoolRequest{ - ProviderType: providerType, - ProviderID: providerID, - } - - var sn = transaction.SmartContractTxnData{ - InputArgs: &spr, - } - - var scAddress string - switch providerType { - case ProviderBlobber, ProviderValidator: - scAddress = STORAGE_SCADDRESS - sn.Name = transaction.STORAGESC_STAKE_POOL_UNLOCK - case ProviderMiner, ProviderSharder: - scAddress = MINERSC_SCADDRESS - sn.Name = transaction.MINERSC_UNLOCK - case ProviderAuthorizer: - scAddress = ZCNSC_SCADDRESS - sn.Name = transaction.ZCNSC_UNLOCK - default: - return 0, 0, errors.Newf("stake_pool_unlock", "unsupported provider type: %v", providerType) - } - - var out string - if _, out, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, 0, fee); err != nil { - return // an error - } - - var spuu stakePoolLock - if err = json.Unmarshal([]byte(out), &spuu); err != nil { - return - } - - return spuu.Amount, nonce, nil -} - -// -// write pool -// - -// WritePoolLock locks given number of tokes for given duration in read pool. -// - allocID: allocation ID -// - tokens: number of tokens to lock -// - fee: transaction fee -func WritePoolLock(allocID string, tokens, fee uint64) (hash string, nonce int64, err error) { - if !sdkInitialized { - return "", 0, sdkNotInitialized - } - - type lockRequest struct { - AllocationID string `json:"allocation_id"` - } - - var req lockRequest - req.AllocationID = allocID - - var sn = transaction.SmartContractTxnData{ - Name: transaction.STORAGESC_WRITE_POOL_LOCK, - InputArgs: &req, - } - - hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, tokens, fee) - return -} - -// WritePoolUnlock unlocks ALL tokens of a write pool. Needs to be cancelled first. -// - allocID: allocation ID -// - fee: transaction fee -func WritePoolUnlock(allocID string, fee uint64) (hash string, nonce int64, err error) { - if !sdkInitialized { - return "", 0, sdkNotInitialized - } - - type unlockRequest struct { - AllocationID string `json:"allocation_id"` - } - - var req unlockRequest - req.AllocationID = allocID - - var sn = transaction.SmartContractTxnData{ - Name: transaction.STORAGESC_WRITE_POOL_UNLOCK, - InputArgs: &req, - } - hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee) - return -} - // // challenge pool // @@ -1075,60 +884,6 @@ func CreateAllocationWith(options CreateAllocationOptions) ( options.BlobberIds, options.BlobberAuthTickets, options.ThirdPartyExtendable, options.IsEnterprise, options.Force, options.FileOptionsParams) } -// CreateAllocationForOwner creates a new allocation with the given options (txn: `storagesc.new_allocation_request`). -// -// - owner is the client id of the owner of the allocation. -// - ownerpublickey is the public key of the owner of the allocation. -// - datashards is the number of data shards for the allocation. -// - parityshards is the number of parity shards for the allocation. -// - size is the size of the allocation. -// - readPrice is the read price range for the allocation (Reads in Züs are free!). -// - writePrice is the write price range for the allocation. -// - lock is the lock value for the transaction (how much tokens to lock to the allocation, in SAS). -// - preferredBlobberIds is a list of preferred blobber ids for the allocation. -// - thirdPartyExtendable is a flag indicating whether the allocation can be extended by a third party. -// - fileOptionsParams is the file options parameters for the allocation, which control the usage permissions of the files in the allocation. -// -// returns the hash of the transaction, the nonce of the transaction, the transaction object and an error if any. -func CreateAllocationForOwner( - owner, ownerpublickey string, - datashards, parityshards int, size int64, - readPrice, writePrice PriceRange, - lock uint64, preferredBlobberIds, blobberAuthTickets []string, thirdPartyExtendable, IsEnterprise, force bool, fileOptionsParams *FileOptionsParameters, -) (hash string, nonce int64, txn *transaction.Transaction, err error) { - - if lock > math.MaxInt64 { - return "", 0, nil, errors.New("invalid_lock", "int64 overflow on lock value") - } - - if datashards < 1 || parityshards < 1 { - return "", 0, nil, errors.New("allocation_validation_failed", "atleast 1 data and 1 parity shards are required") - } - - allocationRequest, err := getNewAllocationBlobbers( - datashards, parityshards, size, readPrice, writePrice, preferredBlobberIds, blobberAuthTickets, force) - if err != nil { - return "", 0, nil, errors.New("failed_get_allocation_blobbers", "failed to get blobbers for allocation: "+err.Error()) - } - - if !sdkInitialized { - return "", 0, nil, sdkNotInitialized - } - - allocationRequest["owner_id"] = owner - allocationRequest["owner_public_key"] = ownerpublickey - allocationRequest["third_party_extendable"] = thirdPartyExtendable - allocationRequest["file_options_changed"], allocationRequest["file_options"] = calculateAllocationFileOptions(63 /*0011 1111*/, fileOptionsParams) - allocationRequest["is_enterprise"] = IsEnterprise - - var sn = transaction.SmartContractTxnData{ - Name: transaction.NEW_ALLOCATION_REQUEST, - InputArgs: allocationRequest, - } - hash, _, nonce, txn, err = storageSmartContractTxnValue(sn, lock) - return -} - // GetAllocationBlobbers returns a list of blobber ids that can be used for a new allocation. // // - datashards is the number of data shards for the allocation. @@ -1317,93 +1072,6 @@ func AddFreeStorageAssigner(name, publicKey string, individualLimit, totalLimit return hash, n, err } -// CreateFreeAllocation creates a new free allocation (txn: `storagesc.free_allocation_request`). -// - marker is the marker for the free allocation. -// - value is the value of the free allocation. -// -// returns the hash of the transaction, the nonce of the transaction and an error if any. -func CreateFreeAllocation(marker string, value uint64) (string, int64, error) { - if !sdkInitialized { - return "", 0, sdkNotInitialized - } - - recipientPublicKey := client.PublicKey() - - var input = map[string]interface{}{ - "recipient_public_key": recipientPublicKey, - "marker": marker, - } - - blobbers, err := GetFreeAllocationBlobbers(input) - if err != nil { - return "", 0, err - } - - input["blobbers"] = blobbers - - var sn = transaction.SmartContractTxnData{ - Name: transaction.NEW_FREE_ALLOCATION, - InputArgs: input, - } - hash, _, n, _, err := storageSmartContractTxnValue(sn, value) - return hash, n, err -} - -// UpdateAllocation sends an update request for an allocation (txn: `storagesc.update_allocation_request`) -// -// - size is the size of the allocation. -// - extend is a flag indicating whether to extend the allocation. -// - allocationID is the id of the allocation. -// - lock is the lock value for the transaction (how much tokens to lock to the allocation, in SAS). -// - addBlobberId is the id of the blobber to add to the allocation. -// - addBlobberAuthTicket is the auth ticket of the blobber to add to the allocation, in case the blobber is restricted. -// - removeBlobberId is the id of the blobber to remove from the allocation. -// - setThirdPartyExtendable is a flag indicating whether the allocation can be extended by a third party. -// - fileOptionsParams is the file options parameters for the allocation, which control the usage permissions of the files in the allocation. -// -// returns the hash of the transaction, the nonce of the transaction and an error if any. -func UpdateAllocation( - size int64, - extend bool, - allocationID string, - lock uint64, - addBlobberId, addBlobberAuthTicket, removeBlobberId string, - setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, -) (hash string, nonce int64, err error) { - - if lock > math.MaxInt64 { - return "", 0, errors.New("invalid_lock", "int64 overflow on lock value") - } - - if !sdkInitialized { - return "", 0, sdkNotInitialized - } - - alloc, err := GetAllocation(allocationID) - if err != nil { - return "", 0, allocationNotFound - } - - updateAllocationRequest := make(map[string]interface{}) - updateAllocationRequest["owner_id"] = client.ClientID() - updateAllocationRequest["owner_public_key"] = "" - updateAllocationRequest["id"] = allocationID - updateAllocationRequest["size"] = size - updateAllocationRequest["extend"] = extend - updateAllocationRequest["add_blobber_id"] = addBlobberId - updateAllocationRequest["add_blobber_auth_ticket"] = addBlobberAuthTicket - updateAllocationRequest["remove_blobber_id"] = removeBlobberId - updateAllocationRequest["set_third_party_extendable"] = setThirdPartyExtendable - updateAllocationRequest["file_options_changed"], updateAllocationRequest["file_options"] = calculateAllocationFileOptions(alloc.FileOptions, fileOptionsParams) - - sn := transaction.SmartContractTxnData{ - Name: transaction.STORAGESC_UPDATE_ALLOCATION, - InputArgs: updateAllocationRequest, - } - hash, _, nonce, _, err = storageSmartContractTxnValue(sn, lock) - return -} - // FinalizeAllocation sends a finalize request for an allocation (txn: `storagesc.finalize_allocation`) // // - allocID is the id of the allocation. From 502c0e63e72f81467029b7d8c5e388174e33e6d5 Mon Sep 17 00:00:00 2001 From: Jayash Satolia <73050737+Jayashsatolia403@users.noreply.github.com> Date: Mon, 23 Sep 2024 00:34:40 +0530 Subject: [PATCH 121/319] Remove readpool --- docs/uml/create read pool.png | Bin 11007 -> 0 bytes docs/uml/create read pool.puml | 8 ---- docs/uml/get read pool info.png | Bin 21522 -> 0 bytes docs/uml/get read pool info.puml | 16 -------- mobilesdk/sdk/sdk.go | 15 ------- wasmsdk/allocation.go | 11 ----- wasmsdk/proxy.go | 6 --- wasmsdk/zcn.go | 7 ---- zboxcore/sdk/blobber_operations_mobile.go | 31 -------------- zboxcore/sdk/sdk.go | 47 ---------------------- 10 files changed, 141 deletions(-) delete mode 100644 docs/uml/create read pool.png delete mode 100644 docs/uml/create read pool.puml delete mode 100644 docs/uml/get read pool info.png delete mode 100644 docs/uml/get read pool info.puml diff --git a/docs/uml/create read pool.png b/docs/uml/create read pool.png deleted file mode 100644 index ebde51726279df89352b8dbfd461ef0ec5bcd983..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 11007 zcmbt)Wmr}1+O47>-ALC$xacSy37dnF#sOqep13WWcJA9z6~QelH@z0e{nk=w1T{ zdUr`}cQYqvAA1W+_eatejux&a?iS`0ralzb?(WX~tgO!VCXVhN4)!c&P7YXXd?Y|C zGB)bk?th z{PRAwD&6bZjFkD56oKW$8D032!zal0buuOq2oK1B{pNBLutyEJl@B) zFIK-3Ck~5TvAU`H<)3AX5hx_0z%^{X84w!5Hzi(c+&O-kp64NqD=+mbU0!Oe-1_C} z4e~cC`S=rh#TEJ;FOviOCi?lAE+un?XKiSGo7-vw{?Er6sx4oqi?z(PN2XTT*hPx#G!A+rjr*e4L0; zHaDtXlEw<%z@z=vudUq=50B}cWOQnEWU9It}vdMD6WtKp*{4=eU2DTNV z5yBZ9P7_5MzkYm{t`*Ip9A56B8V^qBo#@XlR+Q$1oCYgLj4os~nsy<(qi_*DHZw8F zzBoiYrJ#0w>9$!kHiBBQqbhQ-U0k(8&5m^1z>;Y#tEgyL?`ol``TQwSHtg@4%^2Cn za8angT>pONq+^`5MMgJ?O<<;2Z~5X=)N|%9ep#5FZJN}sUS3|6Z%==%~?ERdb!o~606*C9?nLG!-|L= zi?feqqRLdVJ^qqLt+;&iKCRBU4X*JV$pqi_BN&HPYRU9?9SqM7qE?-kiAYAeGC5o{ zTkqc-4m_L1F*P;aS)hd&6u!mw@vaNJyKIt`{QFKQ_Er=T9ToxSI<-LDf5f10@llFW}M&z8KZI02t&VJC9i$5Ak=CboOiLT zEmKCxl#IWqW3uH#wO&uRlaVo&qTsEWPXEMlw(OWDa)NkuYVFB7V1ZI=&&9 zcd=Ozsb#8~sU!OzM2cwIrW^y;8L#o-ycKY{}E7*)sr=8$_hztXB!YztFG zG2@D+ugP_7Cqv*K9iw-yV#TZ@^cI@;SqBG&wkP(pl~4yZ-c7hXGd0o6?Lf-!X*{&B zCrM1~@Ca88I;#?nP32>s)d~iid{&0FHl@ss%NBe#h8{l_X1ZVQ3JiYXqNN`pD|dW# zhPE|b`K#K_XS7>A9(xN7F98wmZ5NF!M354DiIsYdBYMXxlO4HBAozp7@9w06`mU)H%`m#v3t2hmqu-J1SL#R$R0x{4oTm5pAZ_5-nY5FMK8$7JBitGYoyKmfY z=@x)^436a3HG25LEsYMOU58kNNXh21rk8?g75zubO>Tev*uO1ipX1@km38~qw3hAn^Xjz%);Zi3A30in;pJkLmq zVzOaO8yogYPfccRA&NbLV*Y+C;30pT*HD*%@uVMwKAaZhP9cna60T~WSQ&OUi zv>_?;)pplfhJ={-=Z=+@3pJbYN)@^p<3;lsE<@oum`(m?)A}}pLVk&jNRz}*X9m7Z zwcNPUm$4cRuBG7J$S43arB{A(Vx1YZ%k2kRJ6sM;M!f9nJFl2eab}`XAQ}`1J7-&e z_k{p}J`}wV)<4Rqlb=qcI^VK&FrxcWb4N!;RsNbht#e+wqqn#Cz!nnX(dHRSZcypY zy);wv+IuYjDbo*XIeG2K8v*<#@h5Qp2P|F8FOZdzvMEY+;b9GpUKnTum@~y%JT^Zm zMx$(t-xEz@Cq90jIe}xBZS{W3VYQQ=3CKb^1j#9U2XAw*gSJ_Ll2fRE&VYY~n2-xv zo`jc+mTN$6ek8VrW`_2&XV+A?;#nUhf`axkv?a0`)_PFfsMuYOD$ITzfK^nU=+&Sa z(^{DIw2%;NMpcMvQHXGtt5O%0U<(*6=3icF5~;nC&E>Jzfa#_=ZySe%g?eHyc)X`z zs($UK8cRmX?oy$=vO;^PTI&=TtOcTBxzd#~ta~AL)ZWo(w$*^- zXj?Tlm`Z-aazg$ znqMh z9o~-m7W;P3i9?4m6ijk{2tpUTVvT^&-<6M?JU|VuoagI0!fYxqH7`VFa{`#CF1_GeDGMl zLK?dnLfP&e$~?rN90oRLF-F$_krG}}va`Dkrt{IfN4i&d^-9c%gxC2Isj8|fC_qqL zh)fwa`=iWg#v@$EQFSKNhrB@Po^JQH-n^mm!#8?mP~$!r!fEcOZ#&r!pQV@*wq5@7 zrET9>e0+R4eVRyMfVeoEJK+eXIpSj#8=FrLS_sgK5aX{0XQFgb(@pWilmxtCdteIZ zIKFYXZ+bQM)1!HENlZHNsi~~5tx%cSOj2tHTfFO1dlQ5J2&m(FPcyUM`j}~j@O=x#AfjXd@hjDYkPsIu`=&{5w zw_d2rfxnch9M_Dei8MxXU%kJ(y$-y;Jztiu4q}9sM*^XNPQrVz`tAMl95;nOY`^82 zg_pMu1`C=8#Lc_dSd80BbI5KScEq=V0f)Id7hqNOWVF@a(<&|0IHa**7`fJ{0UXkU0cYX%c0Vvy7&jji z0+$Om<@em-_1e>SDY96~%gd{Fn5_x8z0w}Fu&_`@Zo0wJK_wS%YHCVflUG!9Tx=0q zMctAJLvMCog5&BM8+$JediCm6uVr7uf9e|W1~PL3k!>si<6U6XK?t_`k%TA8vzWAj8EX;Ja?wrI5+_Xe08!#B~OV6 z53H(d4@|p3+n_)(1ADQYsR@{U!U}-dd7#)>SnTG#nEnt^NLW}bF;C3eT$RljhRBU^ zneJB^axQC3R+e`WCC~9K8(r7F+kI|6I~h}A#AojtN4z@QMS-t(S?TVI!j}*i&y{t* zJhJEn*L5A`Fbyq!QlotujJ!2BG+k-sLU}d)Zg;j;;&})?H;9J|VrQmI9e-VPaj?{( zt^VV)5kAl$u%{=t-u*KSvZ}ng!o=}$ z^g!5k%M+|th!Z&B_*ijLm2`*7T8s026NzykvFhr06Uj(PqX`~IP*PHU`}R#AUn-VtfLk-m|0qJ=#fCX6 z@QyFF?e60@TdWWw${b9rwf#Ye=zM12oeyFKki)utfu#Mm4Z}k~tGYGz-&*ZD0?_zA z8#G$F=s!sa3w722VjVJ%b!Tv@=hR&7yd=IfpSB^Vm{;<-PCW3Jj5e_Scg9ZgB-Py* z+wa?;NmwO)zDR`%fZLKFg;X)IaGFZNb?C>9m;nPtO6)dDYEb(3=H5b`#r}ru6te7; z)2!*!YcD!5s6SK##Q$6ai$+T4G;`4T7kZ!V!8~gxLmjk^ zvnDnjcSZ0Yh~9A7Z>&T(ZxO&j5CZ`a`v+1plqME&P8V?wTaJ<}xVXAIKgxo639pFl z#0medbWk9k0z*I)e+|rvJ5$jEOg}u^!Z67MJOD`uAAbJ793L9$fu-h2N#qqk%7&xI9sgF?aj{dn}CcWlsQ$=8Vlhi@bglEVKTOl@9*;@n67=v^++wG$K z%MTB4_9*ZBUJ->OrC>0@)ye@7$r5UrQLC`+c;TBjkL=C)-8bX1vW7GVQ(xN6)H>~N zZA}At2S9g>1q&l1F}o=wHdYB>{V)u2Lqo%|9SE}suG3h4^k&kGH$wfk_7#9U(C{cJ zjTh9^NJ6xa$_9 zF91q~g@px~zL6F&;o$@v<}x-m^pnos-WPQ)D+w9Km$xhA_lU^Iy1GMBr0^;4qCkE!3-+VF_K7D0Ei~B7_@^-`rxzZ|T)Ko*o}_GV)(uti)n*>@2n#dCMig{S{Kh&nH$<#JbK9 zAqD~UQw;drT)0#bURfjtlGKxv)Lzs*yRJBrjCzg~h^SU(Ty3z#3q;U~MujDmn5g&J zbEJi(c5pExIb)=SWosQuDwoY@xk*Q(*FFO;5-RFy>lL%OBCEH@cfzTjKREh?9i1px zh-egm5-P2OqqV+R;VZgD!f!(8HHqy#)3r|Ib{N;aViV1N=Tdpu+1d5=^)gV#-y>E9 ziR<;Ep`ln^tOuERteHug^t_FN!9XUKl`x#Ah_Ql4y#QfBY9WNVzLXlU;Aa@JoIj~h z9{-;zM|NcvB)VWI{8ltvY)ytKw5>~s(=>0vplK9RE!0H(dYy^LCA$;HnX06;JDI9iDn5kBn1*>5^lw6LJ= z>n|_D7c`)w+fiNz7y}jrFj#w6jl*nQ_<#WrG+_9U%rG?KvoMjH$5`+4?)-X%&@-t) zAQ&J9>govC@dNz_x5f&fgxjG63^X(}6cnP_zo+~`oMp3d0s4VTI_Bi~xI-!j5DZh5 zy|04nx7`!19DkG<=lo(^DKOAs8-i|LM;w5C9Da%T79ns*%}!cxx2e7 z%<^x01%8o#+bIzqZ_J;0VERFD-__` zo^&+-J)FJaLcK;P01Z_k4H7wyyHWssfN3XFCFip#sZ~*c#>BKk5@eWxo0qj@BF2ID z4HM#N$tvZQwEDDw{X59wus^{AQj=Kr2z~$d_@sKEgLRkxMT-?JaCbYvMSB%=*>Bvf zF`wcw$uHh)4!`3U`m!BrICof07fkxsI}~^aKqN%ZpO&6&*DnM<8r9^&dL5ht5i9;5JO(e$4MeP%I-o(aNY%&F)`wOYEXaIzVu>`^2 z3&(_t7sg&lu{@|}V(swN;Z|DEpFyMF0!aQa4?yJ_n3fD}yXQniW~R#2FsH-cYWiUk z;Q>|spGQDtk5m*|X8)n0APO#Ey8cLaf~0>J-xTTS^jIePz!pz)9}wY8Ehe-(n?`LP z+DmnTuEWe!2A2D?RU{)A7#MaML-Xh^q<5EJNaE_#&lSM~9CuZuT}g+kps%mrm&hz6 zBorJRY-VPrW2&Pw9gaoi@9&?=?ZEGUad33xT4@0-(W%UTeSOxu_!qvfu`)1h|M;Ah zl%&-YLyCigQ_29X04IXTi)3VFu{F{DA(%NykITzT873B%P#!=h8yPKDnDuaoRa_+l znS*nQKM}K0JtJc?;p5}us_t3 zk(%8$6!nu56Uq2oA$ihZFe)L(Q~J2vgM$O0V=2sXTpS!n+Y`mLj`NX`k-poX3bgN9 zUSFH;&IA0g+!Y0=9#!fux>#-(-x)tYyvjM;ViMR9Y8k!$5a_?2Vx@hkkj8y`(1Eee zuq*0^fJW3A)p~Q(=loE%T(gE70Cs){-B7VM*UN-_ODx$+4 zRp#+da?2%^nG6D3U*}(du5SmcRC01~1O)~4$7>$;R26}nJh7Ufn#G#dMH70!P92JG zTD!AjZ^Hd|r%bFT%M7C4ko!dr4-e1H>E{|x8z=HqzrlCy2t@{o=L}~$zse6+b6-h4Wt=tew$i%kfrxaQ^osyE$o-8xN04xD_ z{ogC#{p@6a@je9K(0T;c1r!BzVER-(^q~*gPg$V&1yMKBl)#HWi3`!d{CQMoQONMfl+tw3?fbhgGprZXT{>$4$~ zfqFWrB&m>Z!fV{OAKpoE_^fS!foxkF0AT(>y7wQIVqfB0ytm7p)}26B_^<%ztw|gu z!MBCBKqgFm1myochny`fHRwb^Ozbh_$#L3ituAhb^&*M2zw~?3$RGV)xx6C+C?n14 zb}uVUMpb9JD*{r|-TnR8ZU_YOf|j<@UB!jX_r$s;=LX;(Z%|H2$vY91Fm%$XHZ<@V zu&I|W2CzXnF@zJj6#FV^(y&m;f+#(m)g@RkuK!td40B6cHo46{4ot zm=->JsGqQL2_nv5o{%u*YzIDb3;KN_OzcD9jG2l!^BG!NTdp)S4o|o=J|_*SFeZbxAAeMMx75d?5E~Vaec0x79=-%@|ceXK(exNpuV-$F@6AP9vy) z@gQ@JDMx24mjF5f-Vm->@=~r^Vr96sg64rn!A4N)2fhK8z#8R$IRZRXbf%r4{BRBU z4eT-0{;bo#6^np9{sW7M1DlwK5}a0Fq#Fid8&Hvw!!h{gXw=9;q9gtD3`U*AzG@YS%Cx6URDzJ90FBJh>fCuS}ssqFV(HKy}dfs z_6~=`ghPDRFuA#BU#R^NjK2L(V`q1Bc#N9HOO(i86LZVUl^@>zmm5nm4tSI08WFB; zYHqHoMb+`~`oB$2J&%-p?UDun0W2*nD4%|{1V21v5^6obWFg&7bk^Ua31CKI$PT}jiBRWJ1z83KXy2SC z^KtT@WZj-Yl9LJkc5pp@`Q{c#KkSSk2x!~a*F5|4*Isj({zobT3@VC>$V~^ui*Fo8>aOb&Bf`DXKoVO<~i8&&3=WFC?X|1Tg;Aw>af|`Rw&I(m+eMMf{b}P zdc|wgxZf5rlz9pUzCJIwyx_7ZR@Yw`h+Z?n*~_|@M@QRzy6bnb^s%$^+uW1y;yC2K zbmfN44ZAbdiV7KRqft?n!9o5C`L)wM9bud*+?;MLg^&9$6Pd}wAc9E$ZwJ<_FY)o; z7VPoST4liXtS7K@wVUj}p3u7Q&D&_Zt6QW*4gkF3&UgR6zUk!lKfWo2_IvI(eEV~k zhjVk|?d{CTliw#^?KYnS`f0&@q6lLsecJQqSF2%MzM;6T9&)EA4*Y(e#0ltcSI0j& zed%Y^8eFJ9-amNLPbwhHZ9he>bCvqC!rAra!grFBNu@2e7jO<9Jg9$nJL|n&Xs>%| zNG2JE-gE)G5n_G)<_2P2s)i7a?;8j=vCV-63n8}G%r*4zWv2~^H2<@w6G|C;=PLj%6L ze0h9i%WEz}UKJ-2qK}Fqr)x={H#Slgls|{MflXkz zd|B4Swcm^8^XMwmH$J!C;gW}R9q!1OTqP&fG6^;b1T_NV(dOrh5x`=Aej$efHcyV! zEh~4?CeKNKJazt8QKLItSN5H)v9w>~pE#WscspYend^gEj|Z~=Yfk&FE9xHM)pA)K zcJ}9g`ON413bwi6tM+NxlM!hg1awrKe9<@MRTNU~V8Cm3TcWRF9BI98M&DO$vOLs% zeuc}2FLi-Dp`rPSwpNxnA>7YW0mzz^;U_zT8Nu3G3k%Vq7~~L{f{(@>k$B{FLUmIR z+1#JfGYA?F?y~o@S&hu2yKAZ`HM-7nW%5-%e_plKqJUf9_8XwQlUnE&LuN2rI*Wst zB~K)uzFE6>|A zv5)giQM-MtNy}G9Yex4*rtl)FYLXGjnzDH$O1TH?Us;%5tbWT^AjTj*>*?>m+(9*D zIGNMP%L}U*Ar`DlI?{UoFngNRMb##zOYptf3>g9kZw ziGwuyvo#n_GYM}lqpiv1pjoW%2#>L0=h4Lb^Wv}rcS7XRA`Ob7M!)mnH;=QMeLdbI zpu5Vx(8?J9_*z(4VQeg?G*cTzC{5mpiq;*67!%A+*WO~?K_xLqt3VV(iVXLvDutDs zNnBrIYuI4!Lpu4VPlaHsp}QimUX8zgDPW&A^RdMc46hHQp%RW3jKaX0lBJ`g9aBg! zo|ao~gDC{)Lk5O1LY-Yq|H~Mq--L=KRc%lBRZ-L@?n7D{a$aA>O&+%U zz+&qquq_IC>AImx9bc?ytISj9Fh)aQ8)WH!&}PjyR1O5++~9o}-J-GycY16%LiWc%RIBIaT2ieguOdh>uOzhW+IhU*JM>qN$z8#-rpAuqA&Cjr_f{0a;DQg{H3kCSUSjGiD90&T+BP*4 zQ<9F*K_c?Z`p%MCn~*;op4cDExefKcjxy5mU}2#JNb{=wlWeXI74;XaS*zZ3J~Q1~ zM@j{+)Q1&#_|mI70r4$TP||x^o=o9pLKUSPsZ8>Kix{r7u?JXx5GBCkwq6@NLsSNW zEmmj}nd8On2lY+n?zOq8??^OxX0+XOe=-7jy@Nq$F&@B%v!SD^>%kFM)a@P@$5D*yJ&; zj=-Y^xjl9(#NJvq=)y~(9|5Xwmi<6c5g@1>iKFA=sL069Sn0x^C$jF>GpBNm=30-hKG1w%R&Q`6;iJ~w$9oV&F6csN3S=bw*q>vMC# zH{K9m!H;0^gVC?aOYlK|8Lf@2_aXa5yDpz}IPv}YEyy4kD26#Tg3R>vNNHa#k2h#v zyr2*VJBfx4QvxR3!-sGZnh|We-X6weD|xAso0zES5{vhU)ynWE=xsl~02N>sg9h_& z@#Ev;xY1B`_?a5t)9t1vL2FFkCmaB2FdtH3ZBrJ53E29Ln?%``z95kXy>;8XHeyw& zq%xIg-vhkbbrwTXg8KIMb{q(L+gHAm+dy$zmC775iQi7H7+p?M;q*ua<%8`St_%C; npMU=-@jt3+zbp6~_m8{8T&cF4iO+yfQ6IgMR0Nla8wdR#RwopV diff --git a/docs/uml/create read pool.puml b/docs/uml/create read pool.puml deleted file mode 100644 index 7487acddd..000000000 --- a/docs/uml/create read pool.puml +++ /dev/null @@ -1,8 +0,0 @@ -@startuml -participant "<&terminal> ./zbox rp-create" as cli -collections gosdk - -cli -> gosdk: CreateReadPool() -gosdk -> gosdk: check initialized sdk -gosdk -> gosdk: send smart contract txn -@enduml diff --git a/docs/uml/get read pool info.png b/docs/uml/get read pool info.png deleted file mode 100644 index c1d7e3aa2192c4ddff46ffd05d52622808d163d6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 21522 zcmb@uby$_#+b%i*K`Bw`RwP8EyF@`iq$H$6x*McJg^7~V-AH#g2-4l%-QBgv1XujN z{e63%bFQ=gU~$QuZ;W@0=YH;Z$M^RV!ssXjC=dt)UF7W>X$S=VH3S0p`yM>_m;8ML zH1I!aYe5BTEi-d_V;x;Z9jcVX9?$F- z&SEnLSz4#eAHW-s5crPZ>@!>u0)sQs=8s0@y3CwT0 z5Qy|0kvFep?bJ3W(N$zWT=AJdkpB28kV>4!M)WDuwc!(`faLPQTqkm?u#k?nPS(yg zR(Vwl*L0)L&nWvccw0>2y`=RGGrz4umoQh%ig&&XBHFjy!Saw@x(>lCZFlCd($Ed( z9_~K3QLo;LcHAmI?&7t2X{C!~#fSiba8ThIfEnQy^a=_GaVzBi^YioT3#a@Hmh*T_ zT0VJ|Ks5vii&PStRfSaD4l0y$b7l3N&FcCa(b2B2eMS&BO2c3c33e1bmtPF2#Ou|3 zFZ^gaB?eFT1e}OW>D7v*XrLVlj143&Aze z<1H0Ea6OBuJy~K7t>y5G<(i}`#;wIR-5NRx;pF5jFE4NQC#8S>JR>VBSd9dt@CB`Z z^^1lCYU=qxRCU=%r|S7uVn;_uR#ukt`BCpBEj@kMzduHGb`a&vq4K;f&$UA#Pq#DD z#|N#V_JGY}D(Njf2IR$S=&CiidJoJ)g--&F_@NhJr*Y0l_><}mQT&CQU%tSvF2cD{ zv@pcbfeSTHBpKhC-MMx-H?bVb7;zszb7Enoq^34FTpKhR&iQ%#jHNsDud~-ftBS%o z+8EHR45QPU*(Q1-)LkOVp3GKzsA15?FjWc-UMBeSNarnx0}g(PJBrKO>@{chX=3oSH!-AqLM z6vWi#HIvs+xDf$NCZDRSGt?rNvx5|gm@fpL!}P`>8r+|<;)mMWKDDr~YV^7|kUAd> z)2VV5m|-p|bD}bdp-T7{uh(A%~IY#Yhi#m>hnXZPv)%HO^K2rNv|VF{0LYT zR5UinFpBg>jB*7fA|6rDS=9@+hP2q`a=W4)$5Md&o2tyoQ&F7UTzN*sBmq#Yig`=GF%&Y zcDy~BBEDX^y_ILZYMp>MW;>K6zg1OuX;NBlyFF#U*v6SvRm74haQ8Yqe0Zdxs)}Wd zop#f?6*;e|_H0eSOT+L9GxKLxj#Dy{l998a?C;KJcS=p2#~Uy_ovtsD7YT*j4k;)+K0To zOY;{e%5xhU9Q*6rM$62%t8JZ67Xz=aZ1=se3#pY}UY)3x*v$RLg+~~tHXSRV2oU;d zR(qLM;UM|8#Z1k?AnoFg!hE&EWLQjfLHrC2f%A!H5d9vS<3hl7SWw~FNIv@I886<$ zU53oiSQ6nVmy;)7RketZJ1B0a#ZR4j_ho~oS}-*oO60S>45p{1e?GArF#WySAElzI ziu)p5j96>B##I_|Xn@e{;^ngz|H12(q+024WO3w}gaXUsy3Yyzd{e23nxz-o;Ncz3 zxS=KKC;U>Ce)~(PkaTvEh7eaS*F`dIr;A(b>(|Pjwnp#DXFtiKwko%mC`-g59t{>7 zQN7CrrLr(%ZF(;08zC-~D&ftpxtBLjLqWk+7u(O2n(eKvCfx{QOv2SQmgsW3+sT4O zeqfxdxX+%jK<|p*Y!M+%SL(s;9!<_ct@|^-Sox6&aaVYA-}7&$q%W)uRy!V% zXPRhY7}^iSJ60`}TK8N+%9c#zf_PTlZ!b3#?&zhqBHORphbS1(otkzt(7> zszRCIo^k)?%)~v1{f7i+Zn?R~;f&Wop+{H6Mt9fN_%7wL=)3Z`w~hI%S0G)snKz4a z743ZHD|?(Xn~Dm5ui)q_MoK#E`7ch8zCev>*V&91Fk$tx_- zzE#3p#$oE1;@}PC9oOr+aFB#e^iSL;?1?3x)OB_1*K=!oa#Y7>8{*Q8S{Uq)A51SI zW5you@AFze6Lh*ihlr2#5fn!af8HUZMz7I&fykN(XIsw*X7vDBt$BXSJ)Q*ZvxBe^ zPsa^rvf}PbeaKor)roDb*`f>F8P zX|-qdb`9Oepqf(T;?~xutBYJ$YQX>;lBuZ6)8L&3&kCqTf^umkyQx_Gac!ntqn%w5 zjth^_cEHy!7b~4iCW-vJ^iQ8TZdGLR5^pv6ewE}Mbggsq%u%mBwfJH85Q$ji``GM) zcNwCH(MEzMbkL$(rQoJ8k5rsZTU&47z3r!WDB-)bHub#E?%MzB<nj6 z^C+Mx@^qi-yZ4HRAW`8o6T8f2zZ*R|Tt9(cDrTy^FSpTkc|XEW*>O5i^eh+9PRL+xnqS$ zv|jhs-`_j>Q`vjP+O_vC#;2!#$Qwo6&ZeBtJB`z-N>=r2GZJU?DJxrNW96)mlDmNxTBB0$A>|Z+PX(5;7lQhIU4N z2h#;vJ6r-*p(O^iT+0)zG%-^;BaV$(z~LzWIh;vLUYR=vPN$=uZpUx-=_q1Y{eAS$ z@$1o53$$A&%B}ReqB$LpjN9^9jfS*!b(a`M$o|b(3HolaS1tAHNB%+|B^YdU2 zQWq$MPboxC>);t45bzpd#n7C{;cC|+2q$?dS)w4Cu=lUzj78DQaU7aS9JqoMV>!Th)2&Fq22cE!sm1{o!7;woymyeETb%xHOmjuw0*2&4(r5(q@k z<6l2@7SI|>cR%vN4#I2+ot5vs{r!JCAV4pk6+wuD?tgxPGwJgDxU#HFrSg+_MMAm4 zSNUAE5YzXJu2)Wce0)Kz!7(wklazaHG`TMn3*OY+hx7!R_M)}7w-d4&%D#X90DTA! z4o(sj5?$StqQGzD$Rh$W)g>n5_epq8_^wYS=XoPVS{QbpI5|6mCU_K+O5&}N(c|dy zPzD-mY6}~iOMsNggaq7{3vZkKh>eVlMjPtu`Mr2Awu#{47bm^&k2=|>Pfku=C>5>L zd!QcVWn?_(<-IH(Rtp-!lx|w1Bpk(<7~jH_2skO zB0}#^m52#6)qjAG&*^ls)7JLREAjPLB=K0D(T*6;UhS<$Ty>{~0O1qbAHIa_u{`#D zDskwtFST19$h|b5dFAER9!4J)9PAE-wlQxsle#9`FbPV;qu7RX)#>T!ZEb8iySw$2+0CYi9z6IMioUh+Wb7cbEXXleL>~Lbpg@e0D?z>G`fq*MPOGVZ4#D z@DaH=@T*Rgr{`VtPA_~RjPONIIACu;)#^u($Z2N zN~g`pmgCWeh8`Jb;w!Z{vLKn9P>nz%jaqjsGijq8xhPNSzfRMFfWhLdS?!tabj zQI4hb)L5pqxNqr)U{n#F>ftSqp9px~?_CkwM@C^j+h*?O*#{FZPdp?fWWL!SWeZ z{KpM5p>lh<)dr@QGOFjM8%!@;El^T)AYB07OS!!AZLY*s6$Y-hN-n;B^l%*nu1 zcOt|fKxPzbS2`Ac*5Ph|@9@wq7N4_W2iq=UWv;YzO-xEL>d z98%KhbQvnHQgEK8#>Ra9Mq=8R*kEMcbQ2T!cz7;0MhlBv=6-)?*6Ye+WPkpAZLZN9 z3`%crFQ|K3Ay1z^^~4}mQ&bd5x{iPSl~OXc!^|CNE4TKl0<0K(e0=n~7arsW3!XAQ zWEPy+W)o$*3#~y#(f}uxnL(NE^6LO+216PaX1hOqB3~K(8R2dpwrY)2#3L3K7TH)0 zL_|c{GUxM8N~x0=q%S*0d*@`}J}#gc^_A{IyapVk6czE&bB$l&<}sG$<^ABD0Eeoq zsmme&94e5dbnoJSv4c~PI z=TP<{`7U-4Vwfr76fZ6HnS>pQxj*@YN6#cDJ*Hkz86wujPYrlmbV8T?dqA`1a9 zwUeoz7pp{{IFm3NvV=az-`EkKAO7LUq^HD083V(gkloarz+k%Oa@?$zmJTZ~)Hrypxg&C@Y8Pf}*CE^TR!Zdys<`mNS{9Xe8I(WdoUS1XEvtP9die%4<80kcIOe`;j5~v1^D>^bsFGrRJlG!Ot1tE@cjIIF$+It@l>^A z?ZsX@%N#pV2{OKr2aBs_+zG9EjfzT*`D}e%-77AA@Sn8wBdP}d#)bxg6N9;n)BVo1 zFsq}DQ7|7ZUBbfS;v^p~f&;Fuii9=26qOzSGY@6X*+^b-|EX3$3C;&422!W^@1LMv z?wIf>_&Z2th^jZ!^;yBYA%VJ6b@I#M{~^K4T0Dfs05r+VQHwZ3-|Kv%=Ed-n1uPW_Vu+hFlF zm%sTrVmLlH1FYNjCe5N+1s@nYoERuJAyFPb>n$!loHjpyRf!a?_z52J7I;kg)^gtN*C_X+_vm(~SYFwVOxX_(>T z-c@cs45cA-x~qG_I|{$g0GVO1gi^Z@V)T#)s>vR8CJ;-CRYhp$T=rFy2>ZZsga$C9 z)k(M;jCM{_IE)?Jeuvb3Mnlkl85z3i)2W|F9_g^?BH`_|{1rB54tnp^OpX7lK7I(RyDk`jJ7{0=5P1q$l? zP<)>Nb@`JIMR1QB>f{vE*8uiNNJyBPZtc!DdwY33r=j_ISeK4>YU6>%42jaiJD1i5 zi>D9$6BNygVr;eb%Kn6j8)2j5b5xa;5n0^Fms+Us+^jK^l~hd*a#X1ZUV`)K;; zYJtualx^5yWVyZIX4r|5daBUID{PE%)vCups|=_DDQTQAsk1d`zkn1trl6m1lP{D# z($(ZkC{7u4F!n+tm7FIlCnuIkr;XTlVs>lGrJMKo1#HdwQj%I%+fLappB=7WpEUFJ z&2rhSy^*KQVb*Fwd+xgP>#k%p#}hi-_pN{l8F>7pMBgnU!N+fXj%sLVxZ*cqGpq_q z`RC7{<6O?t6`e_%_%=a9c7Apy{gjZ+_(2$|LPB_1G+nY@cdWG(5C#GU!nWz0Lf_(- zI2^8lj=4{E1Js<`F}mmG!b0B=7!*WZs^+xY2%ej#hldnuY-}ux(&Ye01EpAa9f{DJ zH)3g2CM`J6>hyi4%B@nt?FIPM&o}udc{X@rs8`uBE}~@1s2~rjTY>(3{Th|uwZ(8GYg_1zs7bJW4M_I7}Tlv%@rgS}pvnVDtj zpi2RKOlfy-?@^>ql)I-prQy`n6fT`gJacLUCM(6%JNfo{#3TUo@;Ff@b*z z+ZQxu@X*~W9yQxXZ@%9%V4fMvPFNdMZo0up1SI9z~H;c>%A)ZsN)u) zlf-a-Cl_!68o4Zma!ZO%-4QC6Il~K^*}~sIW`l-A5i&tzKhr9gjO)!ji`GC2qoFKD8e+d}_QRsicd6u*IX>Nwet`-wZH@P8pJ{`=_S9GO zvh+20`s-8G+=N|N%o{)zNbx)dQX1*?i7=rHsjf5ZV$8+GJALq(E38-N0P~xl$Hc)o z>UO=7k@l^<8m_(iuorcI@7G<@le4pm0gNsF*)NEwY1#)Xz5L%tKq0xlSmv9yUyj*e zC&oh7{n?Ozm=ar}xEOqAuFd+1H75&U6q`w8^21gEW~PpK(8ivaRKtO(3>K%505*Cg zmTsKu6({WKUQkeY!22|5%1xz(EER~jMGy+j0-Rl2Gh8j9)D#yH{jdSrw~lZ|**Ll6 z@n|?+)q~_#dVGA{w@Q`8%%Bv3yYBdGjQw#5I%ARFIL*<1*ZO{j2qxt^#@e%sS_>gi z28T>HeST;l&8Xn(Hb9=%`#+Y5<)NNLT9b(3qTsQav(yfM@}%L_aP+eVW5SOgt-$7$lEmGcYX~y!$-)P4 zT(=7A8BMC-ICvU3s#id7jd1t=0T|5Qq-L;MJ$h?1G7N#fGbJgVp}!mVl=h zPrb%p+=?}e@)|t1!}j^+Nv_)33p=w7G&D5SmPqIXEcSca zg+CR0zdd|f-_Ssf)p>X=nFT1oGhMvY#eeH1{jK*Fy@avM67f=!)N7o|lat9=Sg`Ps zV5G7lWgLwg2u3jqE-o(}%SM4N61V9qd{J>&oH=W>U#Xx;+2EMJmfHn_Ag0A{me5jf> z-oNHp$Ds@(=yox`=>A!N;u!c#3M~x(tVbwXA@-l8{X^sS4}qY+n{Fa>YhdnxwYj=9 zf7W{A&7$-?Ci9GQgY_l1YuoAs84u~teYG(B>rYEq_;QX3djnoRG>waW=q2%scV3}_ zQ-M~T=nC}W-Lv>j+k!wD0TM(P()BMLZj5)OqxYCPX0m0l3N}c#!{%S5*e3DE? zwPC2(zQYAhR|qNnHk-#?L5{QX{vB0F9Ebw?!sWf=MjgPUp_m zr|Rvxfiu^pJXlwZR!IEkmJfbtG%8yv=yMtd#6Dm_1xE!F>;geUQCyx3vih_!Z#&#)Tzd1@Lsf<{ z)=IRioToUVzw+S(md8yMJ&4eNj|?@zxK4t#%Qtlp_Kain@U(_qQxX2h!U!v^rnQ$x zaFC${HFq>-5?D*@b_RSH@DP7J2KD*Tqeo-}2Wx}gNbvxe`8YUE;M{cWSAT~6&=C59 zhL9Q%6ciK@Q4T^R-HThR{4%0wF#DEVW70i90>>~l*D{2`j1%gZbI5-r**fLW{^tdsAs#V}pt%yQ7fy;4l;;Sf-CFPi9(bWLy| zEKL}+!}Mg)giL7BNYy$iuRW)@mlqbai?P-O`^d~K2VB!|rPLEiJ}4?uZ|cp+{?M3V z(JxD|?PS>kei4xw^?-M>;LJkW^?Ulnnm4=|pyH2p*QEnSb)z2vhXMe)+u{Ffw}SgH z@s9;j?eL*ucF26C=SfhjcIUJ%vg8cyl^8sr_01C1Es+82R8-3W!X&!~2gc(iCZKq+ z8;?9T<}#nD%hPDMxV!{Qk;`_A$Dog#@JjjJP;_DElLj|nQSOYSi-4u%bA2x)fzLlCrU3Y-YCY?tbTJn+mXSiMW@}K-KU)!cEW44#BZtJ&)c5xP|sd zT}ZH+tgrz>70asqN`j{!5$7JzJwcE0RrhVRqBURGA-<%Ob@>MVZv2osZ#y`!=NSYEx!KiH(s zHv$L0EdMR7B?|OlMny%nn*k0B03H;lA(?eSlCjAl01bh=fR4}fW+X;v>K_p+%kmZ;4afw0`9r`*C(P^A-#6VsLV9^T zVHF$()FFO*DXE97Fo8o#6>*~ssM09{D&>}qS(d-S6wfFD3YOI+pNz;a>W$1$oalBU zRzoatQffzsqYX}*HQ5A}DJ(%*JG&D=MGP)~f5!wqCZm z3AOrpC50>0Vi%+{)<%v3$m6CP+o9L9gs%Ab9@zg6LZ+Dk3pH2Tr7= zsR-D8s)vl1LD)Asbm$kmEEY^8z6W|x6CgVr#y95` z)6?ZRknZBHx&?XT5r*AmPnHjfVu=QzM6FGf?*r);c+JASQ+@qx!`IpZF264>F7omi z-n}^@6g^TTi{P*zJ0GwBP7OaQ+A`DV_M_F{CLjC;m*Z!A=+HiSj))&W9vn^LrRr$o zflhe91sgq{*dc)R0mDbLTQSB6G|5x9?(NVk=;!Lc1ba|$Q;tk{+&&F>KWieYeA|r- zApl2!0D%GtrU3P@{ud=o6?nqtux>l*eqL;Y<3mu;8bHy;<%8*GEwfaRcsN2$}TDqby3wi67xar^fbokAzzZ{EOy z=DZlUV}LZOy}``AXrzwXc72llVWfi1KV)l!K3jwPR%pX>>&*WyU(t=Jj#!)5wV!W8 za)i#n46X3kfUl1NBS~P5E(M2ly5pNOB4fVg(_4&Giha`qZgPhA3X+$kMs`t3S#a zX}#MamSb;jKhto{Y6{#pMu;#BG0YtvMunQJUuXQSpYbR;1gKue+=tA-gjSeW9|7u6 z?J8s)X5_$rhd_J)p@KlVQ2zC^?MCVPha?%~&i83!z{1HsoL*GLWV(X#HpyBxFim&kRl7!U63i%<-?LJS2`sRHO|FqrrvptYP)ctEDA#`J5VZn5=qS$ou43srPU$8Q4*9N|K zB?BsEVPR2U1FRIV4hl<4*+1Y2T>!WMRtxa!Pj39~?}%v2UJJfagY+^VFI!QmI!L5Z zjejO5yFc&uN>>Bi&HOzB1E`zKz28VlNL-FK6qWca=6**o>y3OQz{a-OookGYj9gh+ z0gNoaEg&EOkPy)FCqHrQiGLkLCAq}T|MYNJ-E}fSz|+Ua2a0g@Fo2fW|EcAc(n+MF78@;?%RBe zV&;d1R||toX#07}m`rH#oS-=9mvywaW8xMt7X3f6RIW#EEfsh~634V5J3y_XqQ-&$ z3WON)bAb6iykg?wKf{x;+n5_Ii)o~dRW`+bXYtu<<9!3`JM!xnPpD{Q@i){#Bt^N= z($@e&czJkCC(3>qB!f&c2uYTdsXXTNQkpL^0HzaATY=65x~T51E}SE5zcRgU60ZkJ zt(~18UdUIH6-v)XGe1;t-P;R#4V`QQJ+=55kmv)H=rv`Y0dY;pe;hF(*EUQZEHW&4Gb|-@ku@XBs8wTPSW|V1N~>JIdqJ*0|4biknrgyTg9{gj`W_@nOLCwjnszZCwdok$-;L`E5>ouWSm6W`E^K<6CQK;rT%=oxLgrVOzh=6?V zN5UJKq5_Z#C=m|p*`*3kfqnp{0uV}d7lD)-L@HWF*4Q-I^`;{-D79KYhs}0t0$_7L zrrfFQn3Tn}n1!_vgdN zw8O2*^TjZA99J?bf4Igb&{=@gij$KQ4=-=}!`)_!4cqUOK?Xnps^5lC^Fh%Gg$^$- zE+{3U-(o&K&36aSdm_t{QgbMpr?sOac{M#O4A(@p-0~-RLmGSx=edF<%-MfZR|hNm zNb7=o_u$M8VYT_|AsK)PA5hvh$L%1SXhYsVG(Ix?t;d55J`Jo&rqekSSH6|J2O$96 zIZWk&StHCNrMoi^fW7jk0;^qoaaF0;`D*HWmfTuEI^CrUxPq1D7*zbaDr}Wb;D73fWFYN#t2vMLvvzZ&*F~c)O zB_4-|aZfmNn3ptncU_A9`BdQ3H(>(*HdTsopYP+QI@wI8(R1jW0S|fRO{URB&Luem*X8e?gI)#`+i!6c5213ud18!wt~Na1EXk?H@*za+0aMG!e=qa_iP-zTtgLbfV4grat%1Ldj7fP1bW&m|KYsj}m{4J3prE3%1#upm^&!xz zX8!MxFj^xbBJ%6kFHm4)UuypTw!hphFDq;3;Lx{qPiJ zL31@=jip*?Ve59sil4`F*?0}ro^MqGL8=d#o#g`c)IRb9Q&v$?@bka1LgbtIX<_n6 z*2NwzTUYrfxXePs!3^M@_Rp@^KhPA&7caIfZu#!-zdewSNnCvkKYN)BMwJToGJy!6 z32tD-4(q?b$Xj-h$O847Mn31g{Vio#eL~Up$Cy8wEVGzz4WjBid`cODfzR|ii{*XB zCf&!%H-dtyRd)F)DU@7X73#Gvusf3N?Ci`2)qg0Ra&dGTq}Y|>*v&||?;>B>dr5|u z^kKqQZD3$vEUzQrkTJ!NT|-~6wSOua;aDC+%<4*$O43(H!59PZnQk1U8D+S*r!0U1 z)G+fKfa>0#ZScf4GAvhUOnHS0=1z_8-Szat#DW4Q0&5_r{q`{am>3Yr`{M5~6~=9t zYUdB>v7Oc(Elbg{EkSK3FEARWv$eHlww>G9{g3P*YuiPO^t;y02W04$J^k+Z%K zoZ6$%U||$lZj3O>F|)E31M^xpL{Ef&>IF^dNZDAicRtsi^Wc~)O&Ap>H62|F`yD`w zlk7v@rzyz9f6Dg~|BM4ui=Pu@r4D)PtE4b~o67b^r9i;UoF9(|MVy3$1ju3x!>%bn z%^S*5<$eyj?-WB22pg`cd}T3F`)QLvZyKLj_u>jrgE?>dSeG*cf&x$|LH{z4A|3@8 zqnEPo$ue~okaWG`e1ZA@{!8SCSc<}*x{Ac!zBEH&PDE`ei{}}|>2f)tV%ATvuC0-8 zVNfnBeNhuT3Z52LLJh0Gbuy60oxVj%0W8QtK~|PLN6XZ%KL$pXIABx>X&S(W{-~;$ z3DNR~sHYJKC;(HbM!%QLVFX+Ta3XTSjO%k)Tc+-2Xg#2yr4{GF2;vbn?0iQztvPQv z+(qJr+IZeq9Hp~Qn*gS&{ld3ik6X;_uUL4_qL%e)0!tsX`#?S|h7 z{r>_#Qc^&cglJ&w%22t)IN|V?U*%c&I#ZLyVo-0+cU*y7NM5%4uI* z$=}cK>Zjnt;{9MfybpLB<{yD?39<$Nrh&N##<7VYfbXv>jW)u~gUPS`174fwL&|3P z;$9{hA?@2QIGxW}cr%NOi&=j8-lPhc*qsU~R6LrzKNzDF{g2^-g{P_@m-{{y@a)~) z-7KfsdJo_u%KiyBj75bwtc#$KV?XF0bfnP+q;4E>k`ir^Jgo zHdPZUC3c^XtpjwJ-QE5<{3qkupdJimmG3Sp8%JAh!ppU|gy}Oy0QU6sqC^H(4^Cq! zKq3g_Mr4DgFb}pSl}{k=EisE#vXo>#2K)t*J>Jognn!K{Fm5!1c&A_o zOby6P*6mN2-vj(@;p%6DKXbKkZ;W}c2>-u;B+OV}{s$xRV7U|oe580#jA1b0R%}Qw z8><43#=+vv6&bzCZJ0YFY$=QeNR ze$)jw5(Y4c-X$+K8MpVhQTtb^^z1XK(=g)L)_W(}s=%-0hJ!qSWr6e54%UBX&Q3F2 zti=Ytp#IL5RAjbIa*GBYfkNeSj-zv1wJb4n84&}(U#&UWV%BdZAws=s8fCH zh7*2^dAGFc7CJd=wy499;Sv;=o&#uPN&u%GQ~m~6*_vj-2j1& z!bjT5D{kT&^aV*~Sf0X+tL|@hWVZglv!nk&N|?$B8iBvQ0MgJ};O^WWZcNusH#RPU zyg(m16M!i2{s(h}*y!kJpGhYLk?OetO9P+?D@8_zhRK9PzjEX=h(Vg(&w7um7F%B| z08|BFC~Vi9OH};gBJiJ*EivgVDT<3~iX_Ms0X7%APWmlefYo%yaJG| zR#%?}BA%SQ{4JbHv&vNe{{5ykGnhBonIe00vNDV$B{lUf&=FCc&ki_2Kze@Ohzm}R z`Q^(Rz+46-Kwf%&ZqD6VK3i!Jr2Ik7E#pPBw1L4Yu&6*U3bY(5@yOpo=5p%0P7A)6 zfYksBVA%acypX)R-28oDK)@4Ylj)l32*>KVeoXej;NUmi2)t(28AaPbwee6T$4?pI zCcOX^$b+z%F&fSgm0w{Quk0CZ)}SSB>Kkq zCxu?D_FJtE)Xto&tSZYzl#n+_|G(-f^q1k zKA?Zx6hp8(%Q@jOD%a69!Oa{ACW3W@AX_#}i2iCr!`{9;Gt=nC!n5CBa&vmYaN!WjXXTDqE!+a~(F?^Yoh-jb03h$~ z0}hfj;{%+1t*pDc|lV$TG2D{1FZzx`4f^hp=?cF7`|bDiwXp~Rk|}F zMtlqyz*;vDbcLx8v-OBpm+dZqd@n(}5By1Z+t%JYN9uLNaEDFV#By3er&J5%bLV=J zbjSY;rsk}k*>_Qp zdk$GvYr+};@qaY{Z~f?|+W{F^c8Ru34MnG1nNt)`|1P!#mY z!9H$YzE(nH-c&z8yzlNFz;uwpovbW9DI`8>X9;R$(e1#SwRH<7ZGfUDodH+gdTAr3 zVC?i6o2eWpCL_3ytXa-ro@LEI0i$xY?;fbQNJvP1_E0Dkm?9##w_$a;&F0~aj*P4^ z?MuyOWa>l{c?rk6mkHAr9_RrcTFE6Rh|Q&RQCHN z#Hj!1>!hhA9W@de60}fEKJm7%v2vOj3&*Vmcjn zxsZ&0^|k#INw8_*!z*OS#2mOK93R8}mU4gV$?D682HB<(jeQemCOgiFe^FumFMtSN z`(3mv`R~}MAprOGNfU$!sIfiT^+m7{IxmR;V8$pw4m&lKXpfE47_P-YfMq6cSaE-! z_r}?_HRR^r`Bva>MA5%rW`f-?OaT2`^aR>`sM$Q+$^U2;THObZhpmN0xx-;b9$B^q z>c6t*ejkTv5FuJXe;C~!KM5sO+YHwDH(rnA;=;Kdf`(4+>Ujl>2@k~MCp}4J1yq0t z#*ryCbm-wBmHbfFdT9B7$eSNNd;q=|P;>SV4}tj)j5!-YL66i_st?SAqGDpfXnx?W z6&eE*;2In-@bdC|;oWe)q8YiPTd`~v zdV-V-i1UCReq(m2GYW*3-3`42YIokZF#J~_l`n+7A4Cxz!sIjeSq>J>H%v)M`LxpW zSq`xKG#+|-$q7IADn&Wdd<$AIk}by{jAXJ^V#Sidt2dpUCLkrslKTh++X&?bEX#F4 zK3Shps4Gv`iI5(D=^64$!oTt|0wycCd|*2XP1d9Lc|dyxnEq*Vtalbi&&uDel;Rb5JLyl%cEz{ugXHi;ol%?54qMWd98ZjnXLq2SilDZ4+MN9 zFqoN{D?e?nEiMA*zQl6zoyg8Jwj4Z&_AL*!E(7`pSe7C(b3)H#Jkbf4fPuceEWG^m z!o>2!hY<5F;N)*qGB)aV`qKZ_oL8dzj9oBI5h<1eBKYq){vyYSt=-+Jdf|o+4i4a* zu^X1jT?Av9keN58t7r|^{8J=Yg?Ax!B%tbmKvcW!vh)7N_+RoT(h#Ep)WYE*gd-Z1 z=&MZqNIT-F>s4IhSP`4e$}VOiI9&{+b_RP;l`hM`@}pE6t#y8yeV7E^OB%N4DM^@o zdz8S(Vny?SuP|;3eTfF?LD+ekjC;E5FpMVIru$fyKDS(Ah`H& z+B4f^|CuHlqz+;lf~NH+S3Uz`<&dt;p*99M@%B%=?AWfPNRXKU@G7X*4y^2Ru>N5@ zf0<#mUP%8}47icF{`$gy>z-vuK`(DNkI@L-Ry$L-Pq2b*BJ*ha@l2k~cCk8reJ5g` zS`u2)F!p(zu|{v}7uP@q?$q^a*fuCK;O%Cd<`CUfO^XfK{xQ&UN{hrPZ?I_z4GO+c zZhl3DfC5ov@W$IlhGBAv0Y?ifIFR&7>GwdlQl6IwuStVWn1s)z3WWO#3Z$KCUtJI@ zT+KywgC86YYu1$mY8NR*ZyM^~d>ce8;@4Z``fS*>`N5_Xiia-4rk6FCLNEZC7nzzAACz#PFd>J^Djh|_x9 z*ynS*!NHn|L0JNG?FEyCK)oYsHHi|J&Ju92%&p+Ez!oOn-|WhYSiG-j(CHp(hi8M= z<-vX;nWaBxx%2SBJ2oPIzV_X@W)tiEp)VA_-0l;jU}v{28dg*7#OaBt1+s0le{V}; z`;jruIW%wWGCaJp9{0*%Em6?>&v~ALooITv8b^4KCq_A{Tt0(5;O!<9hb7-kSA<>t zW7w&*cN}x8@d*iGj~47l171wo{)}Lx@BHga>%q?FyIT*hBee$7ezA4r5p&DEH`3AJ zUWhu#)90)9e|csUMn|k%Y$$m6_hvGK;~ zaSOFf;=*EmA}={n!N7o%yh_z3*nVM8d~R`LG&@+WJ*=C@cFQP(KDSO0y!CJHkfn+{ z#OHdpN~kfZsSeu#0Jg~u&!Mbq$(-~xpB^jg3pU5o}gacDuDsB?xRnV)T`uipdlWM_b}Y`};brPbO<;5!l)2 zF4omWfC6gVpK>^9YpYOUUC{%-$!#=5V>>l^RWd#SOf100iR8e>6N3p~UBx<#HZ;`K z2sWcak7^dcZY$s&X$`Qw%>ciyF8sCmo*<8L{$aK<=J^3KaDYoZN$;Otoug+dC81{4 zvJ^9i!M0K0;2p{VTOGWjBKn~LcEcbdzI>rfPdC*R3U<-CkFhO|N~1SlUwu4vxELmS zbw1rmBiE6le|j=+J390oQ~e&AacY@Cd*~Z4&;LL7e#y?fZaMB`PlAUxJ>J{HdwKD_ zn@@eF?*UIVJEn+8A=o&hI!Zayj~D@$HnI&R{R-@1l$HKOfJ9OvmWqX93r#+E%h!i! zpwiY{r1ev4D`>kne!<}j_40qMO#bo3SC_AzCe9Z%Q@)l;)^0f_|JN^7{l&RPRhF%{ zY?HRro#Mt3OcWVL!xF)2AP%MXVX(GQ_#wN*Ycgsnp6EHU?!2t5+CuU4^zF*Z%6!F? zlSzjdC^PQuXoiM@4NO4l#7#UZw2vG5Ayad#^cdIFb1dpR*oLCKqJmC=A!2G`WBgb$ zA*gD%X&dYbqHqc3GL1(>PjC>XVmpw z6paLnY3)58yPYL}lF|AmL;`yEM+D!~+^3eeMuW0SzVjwyqd@=X9bvHK>nna};B>20 z-A+lAM-I(kC#-4rz}gEPqDo>g$id38RP^-XeX{FArQg@DPrK<+Y(pm(lZ1!)4tg$G z1S>5CXHAD-ZdKAx=8dlyQ%*;t<`&3;xH<@p;blL3Q9V4A-5+h@gKZ!PK{9J%Ldp5+ z6y72#u&Gh~qxpS#`MAn@GPBoxksPHzG>)29^EXCAr)4ULqX}PQuJrWWb#kt5_Bo9! zDmu_FysKKX-*p6hj+u*>;Dxb|7)s0=->LYYg58qJHl{AWJE`yPjc8TN zWrfr7B(JuE1r*M?k1fK=Mn_K7jrlG`()GJ@S@V>7rt_A(a%JxSn*aO<_y4|I`QwNE z{kq-#6?Z0r4ly};SW^$U!Q$UcZ%CPVy6M%KneoPNRqvBt%)*TbCY}TzxTV>b60PjJ}qtM?%i|k|G$p+3S83K{ynYU zPW8`+h3gVyUR-w)PvCibe1vM>^|3*!lp0C?|>+(b3 zxZ{s2!GGC#Dqj5hIy-;q(e9|Wm9l>OfSVj%?MT;y6j(aH4;ZMXE(|bAK6d&|Sw@X9 z(!n-$-Nw1MB2LCG<8#PAY5_UfW_S9GijL(uFYfPOUa`8nr(!*@v^adI>Vkv+#w{r) zO%m77ufOO2*mb#|;~V$XLr3(it5s8<^tJ7`S?t2YlVe{0PvxUv>oMTE@-mB`m)`IM zTNnQX74Y|yGxM%&OjhvS|3dcg^7-f9UU>K{-|9yA)U1597rNIkExp_?d%NJzkLedL zp1(9Vss5y0=A{?k-u`{5%XjwP3cIh`Cu7rO8QB!E>K@ZhnFJc}8G>sKzA z&no$B3DyH23p+M~uL>3b3jMoihhIlhyY9y!S=|Xw6jk5ip-qBYf@vmk9y83f90qVZc?I!EI|JH>&|#A5Fy>9%v# z64ll?0POp?zxM;~%UU{}onhLFG>N^2OTxwXdM;rsF$JCOQn7m7v7dI9KhRE?d1-n# zjLTrxW?%;ze9BFTaZg~R)K1`# z_FT_3?$5UYS4RO)ItZ@1n7fo=HE@DHPKY655%B1kOTeRJ?x|e5FoCtt`MmwTRoM@9 z=F21`@g_L0l5{()y4QBe^$5_>G2ruS!eYB0`+YjesCEr}?hM1Zqmg!&2aZUAT9*j| r=b$pcWfy42zaSq417Uyt&nU1keCf|AQ@#RE=Vb77^>bP0l+XkKj10p9 diff --git a/docs/uml/get read pool info.puml b/docs/uml/get read pool info.puml deleted file mode 100644 index df023e799..000000000 --- a/docs/uml/get read pool info.puml +++ /dev/null @@ -1,16 +0,0 @@ -@startuml -participant "<&terminal> ./zbox rp-info" as cli -collections gosdk - -cli -> gosdk: GetReadPoolInfo() - -gosdk -> gosdk: check initialized sdk -alt empty client id -gosdk -> gosdk: get client id -end -gosdk -> gosdk: make SC rest api call -gosdk -> gosdk: check response result -gosdk -> gosdk: create new allocation pool stats -gosdk -> gosdk: return response result for allocation pool stats -gosdk --> cli: return allocation pool stats -@enduml diff --git a/mobilesdk/sdk/sdk.go b/mobilesdk/sdk/sdk.go index c20a97a1d..7548a8212 100644 --- a/mobilesdk/sdk/sdk.go +++ b/mobilesdk/sdk/sdk.go @@ -325,21 +325,6 @@ func (s *StorageSDK) CancelAllocation(allocationID string) (string, error) { return hash, err } -// GetReadPoolInfo is to get information about the read pool for the allocation -// - clientID: client ID -func (s *StorageSDK) GetReadPoolInfo(clientID string) (string, error) { - readPool, err := sdk.GetReadPoolInfo(clientID) - if err != nil { - return "", err - } - - retBytes, err := json.Marshal(readPool) - if err != nil { - return "", err - } - return string(retBytes), nil -} - // WRITE POOL METHODS // WritePoolLock lock write pool with given number of tokens // - durInSeconds: duration in seconds diff --git a/wasmsdk/allocation.go b/wasmsdk/allocation.go index b54ff7f76..1ff2d2ea2 100644 --- a/wasmsdk/allocation.go +++ b/wasmsdk/allocation.go @@ -414,17 +414,6 @@ func getSkatePoolInfo(providerType int, providerID string) (*sdk.StakePoolInfo, return info, err } -// getReadPoolInfo is to get information about the read pool for the allocation -// - clientID: client id -func getReadPoolInfo(clientID string) (*sdk.ReadPool, error) { - readPool, err := sdk.GetReadPoolInfo(clientID) - if err != nil { - return nil, err - } - - return readPool, nil -} - // getAllocationWith retrieves the information of a free or a shared allocation object given the auth ticket. // A free allocation is an allocation that is created to the user using Vult app for the first time with no fees. // A shared allocation is an allocation that has some shared files. The user who needs diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index 2071941fa..8f444628a 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -268,12 +268,6 @@ func main() { "getAllocationWith": getAllocationWith, "createfreeallocation": createfreeallocation, - // readpool - "getReadPoolInfo": getReadPoolInfo, - "lockReadPool": lockReadPool, - "unLockReadPool": unLockReadPool, - "createReadPool": createReadPool, - // claim rewards "collectRewards": collectRewards, diff --git a/wasmsdk/zcn.go b/wasmsdk/zcn.go index 6a663bfe9..bb3a53a3d 100644 --- a/wasmsdk/zcn.go +++ b/wasmsdk/zcn.go @@ -5,7 +5,6 @@ package main import ( "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/zboxcore/sdk" "github.com/0chain/gosdk/zcncore" ) @@ -38,9 +37,3 @@ func getWalletBalance(clientId string) (*Balance, error) { Nonce: bal.Nonce, }, nil } - -// createReadPool creates a read pool for the client where they should lock tokens to be able to read data. -func createReadPool() (string, error) { - hash, _, err := sdk.CreateReadPool() - return hash, err -} diff --git a/zboxcore/sdk/blobber_operations_mobile.go b/zboxcore/sdk/blobber_operations_mobile.go index 2d87f4fb9..a590b4455 100644 --- a/zboxcore/sdk/blobber_operations_mobile.go +++ b/zboxcore/sdk/blobber_operations_mobile.go @@ -263,37 +263,6 @@ func StakePoolUnlock(providerType ProviderType, providerID string, fee string) ( return spuu.Amount, nonce, nil } -// ReadPoolLock locks given number of tokes for given duration in read pool. -// - tokens: number of tokens to lock -// - fee: transaction fee -func ReadPoolLock(tokens, fee string) (hash string, nonce int64, err error) { - if !sdkInitialized { - return "", 0, sdkNotInitialized - } - - var sn = transaction.SmartContractTxnData{ - Name: transaction.STORAGESC_READ_POOL_LOCK, - InputArgs: nil, - } - hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, tokens, fee) - return -} - -// ReadPoolUnlock unlocks tokens in expired read pool -// - fee: transaction fee -func ReadPoolUnlock(fee string) (hash string, nonce int64, err error) { - if !sdkInitialized { - return "", 0, sdkNotInitialized - } - - var sn = transaction.SmartContractTxnData{ - Name: transaction.STORAGESC_READ_POOL_UNLOCK, - InputArgs: nil, - } - hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, "0", fee) - return -} - // // write pool // diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index 3edf5c1d1..7a5286a47 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -148,58 +148,11 @@ func InitStorageSDK(walletJSON string, return nil } -func CreateReadPool() (hash string, nonce int64, err error) { - if !sdkInitialized { - return "", 0, sdkNotInitialized - } - hash, _, nonce, _, err = storageSmartContractTxn(transaction.SmartContractTxnData{ - Name: transaction.STORAGESC_CREATE_READ_POOL, - }) - return -} - type BackPool struct { ID string `json:"id"` Balance common.Balance `json:"balance"` } -// -// read pool -// - -type ReadPool struct { - Balance common.Balance `json:"balance"` -} - -// GetReadPoolInfo for given client, or, if the given clientID is empty, -// for current client of the sdk. -func GetReadPoolInfo(clientID string) (info *ReadPool, err error) { - if !sdkInitialized { - return nil, sdkNotInitialized - } - - if clientID == "" { - clientID = client.ClientID() - } - - var b []byte - b, err = client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/getReadPoolStat", - map[string]string{"client_id": clientID}) - if err != nil { - return nil, errors.Wrap(err, "error requesting read pool info") - } - if len(b) == 0 { - return nil, errors.New("", "empty response") - } - - info = new(ReadPool) - if err = json.Unmarshal(b, info); err != nil { - return nil, errors.Wrap(err, "2 error decoding response:") - } - - return -} - // // stake pool // From bdbd749b25b12ca127a48591e8d093be894d1a90 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Mon, 23 Sep 2024 02:30:48 +0530 Subject: [PATCH 122/319] Fix --- zboxcore/sdk/blobber_operations_mobile.go | 371 ---------------------- zboxcore/sdk/transaction_mobile.go | 130 -------- 2 files changed, 501 deletions(-) delete mode 100644 zboxcore/sdk/blobber_operations_mobile.go delete mode 100644 zboxcore/sdk/transaction_mobile.go diff --git a/zboxcore/sdk/blobber_operations_mobile.go b/zboxcore/sdk/blobber_operations_mobile.go deleted file mode 100644 index a590b4455..000000000 --- a/zboxcore/sdk/blobber_operations_mobile.go +++ /dev/null @@ -1,371 +0,0 @@ -//go:build mobile -// +build mobile - -package sdk - -import ( - "encoding/json" - "math" - "strconv" - "strings" - - "github.com/0chain/errors" - "github.com/0chain/gosdk/core/transaction" - "github.com/0chain/gosdk/zboxcore/client" -) - -// CreateAllocationForOwner creates a new allocation with the given options (txn: `storagesc.new_allocation_request`). -// -// - owner is the client id of the owner of the allocation. -// - ownerpublickey is the public key of the owner of the allocation. -// - datashards is the number of data shards for the allocation. -// - parityshards is the number of parity shards for the allocation. -// - size is the size of the allocation. -// - readPrice is the read price range for the allocation (Reads in Züs are free!). -// - writePrice is the write price range for the allocation. -// - lock is the lock value for the transaction (how much tokens to lock to the allocation, in SAS). -// - preferredBlobberIds is a list of preferred blobber ids for the allocation. -// - thirdPartyExtendable is a flag indicating whether the allocation can be extended by a third party. -// - fileOptionsParams is the file options parameters for the allocation, which control the usage permissions of the files in the allocation. -// -// returns the hash of the transaction, the nonce of the transaction, the transaction object and an error if any. -func CreateAllocationForOwner( - owner, ownerpublickey string, - datashards, parityshards int, size int64, - readPrice, writePrice PriceRange, - lock uint64, preferredBlobberIds, blobberAuthTickets []string, thirdPartyExtendable, IsEnterprise, force bool, fileOptionsParams *FileOptionsParameters, -) (hash string, nonce int64, txn *transaction.Transaction, err error) { - - if lock > math.MaxInt64 { - return "", 0, nil, errors.New("invalid_lock", "int64 overflow on lock value") - } - - if datashards < 1 || parityshards < 1 { - return "", 0, nil, errors.New("allocation_validation_failed", "atleast 1 data and 1 parity shards are required") - } - - allocationRequest, err := getNewAllocationBlobbers( - datashards, parityshards, size, readPrice, writePrice, preferredBlobberIds, blobberAuthTickets, force) - if err != nil { - return "", 0, nil, errors.New("failed_get_allocation_blobbers", "failed to get blobbers for allocation: "+err.Error()) - } - - if !sdkInitialized { - return "", 0, nil, sdkNotInitialized - } - - allocationRequest["owner_id"] = owner - allocationRequest["owner_public_key"] = ownerpublickey - allocationRequest["third_party_extendable"] = thirdPartyExtendable - allocationRequest["file_options_changed"], allocationRequest["file_options"] = calculateAllocationFileOptions(63 /*0011 1111*/, fileOptionsParams) - allocationRequest["is_enterprise"] = IsEnterprise - - var sn = transaction.SmartContractTxnData{ - Name: transaction.NEW_ALLOCATION_REQUEST, - InputArgs: allocationRequest, - } - hash, _, nonce, txn, err = storageSmartContractTxnValue(sn, strconv.FormatUint(lock, 10)) - return -} - -// CreateFreeAllocation creates a new free allocation (txn: `storagesc.free_allocation_request`). -// - marker is the marker for the free allocation. -// - value is the value of the free allocation. -// -// returns the hash of the transaction, the nonce of the transaction and an error if any. -func CreateFreeAllocation(marker string, value string) (string, int64, error) { - if !sdkInitialized { - return "", 0, sdkNotInitialized - } - - recipientPublicKey := client.GetClientPublicKey() - - var input = map[string]interface{}{ - "recipient_public_key": recipientPublicKey, - "marker": marker, - } - - blobbers, err := GetFreeAllocationBlobbers(input) - if err != nil { - return "", 0, err - } - - input["blobbers"] = blobbers - - var sn = transaction.SmartContractTxnData{ - Name: transaction.NEW_FREE_ALLOCATION, - InputArgs: input, - } - hash, _, n, _, err := storageSmartContractTxnValue(sn, value) - return hash, n, err -} - -// UpdateAllocation sends an update request for an allocation (txn: `storagesc.update_allocation_request`) -// -// - size is the size of the allocation. -// - extend is a flag indicating whether to extend the allocation. -// - allocationID is the id of the allocation. -// - lock is the lock value for the transaction (how much tokens to lock to the allocation, in SAS). -// - addBlobberId is the id of the blobber to add to the allocation. -// - addBlobberAuthTicket is the auth ticket of the blobber to add to the allocation, in case the blobber is restricted. -// - removeBlobberId is the id of the blobber to remove from the allocation. -// - setThirdPartyExtendable is a flag indicating whether the allocation can be extended by a third party. -// - fileOptionsParams is the file options parameters for the allocation, which control the usage permissions of the files in the allocation. -// -// returns the hash of the transaction, the nonce of the transaction and an error if any. -func UpdateAllocation( - size int64, - extend bool, - allocationID string, - lock uint64, - addBlobberId, addBlobberAuthTicket, removeBlobberId string, - setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, -) (hash string, nonce int64, err error) { - - if lock > math.MaxInt64 { - return "", 0, errors.New("invalid_lock", "int64 overflow on lock value") - } - - if !sdkInitialized { - return "", 0, sdkNotInitialized - } - - alloc, err := GetAllocation(allocationID) - if err != nil { - return "", 0, allocationNotFound - } - - updateAllocationRequest := make(map[string]interface{}) - updateAllocationRequest["owner_id"] = client.GetClientID() - updateAllocationRequest["owner_public_key"] = "" - updateAllocationRequest["id"] = allocationID - updateAllocationRequest["size"] = size - updateAllocationRequest["extend"] = extend - updateAllocationRequest["add_blobber_id"] = addBlobberId - updateAllocationRequest["add_blobber_auth_ticket"] = addBlobberAuthTicket - updateAllocationRequest["remove_blobber_id"] = removeBlobberId - updateAllocationRequest["set_third_party_extendable"] = setThirdPartyExtendable - updateAllocationRequest["file_options_changed"], updateAllocationRequest["file_options"] = calculateAllocationFileOptions(alloc.FileOptions, fileOptionsParams) - - sn := transaction.SmartContractTxnData{ - Name: transaction.STORAGESC_UPDATE_ALLOCATION, - InputArgs: updateAllocationRequest, - } - hash, _, nonce, _, err = storageSmartContractTxnValue(sn, strconv.FormatUint(lock, 10)) - return -} - -// StakePoolLock locks tokens in a stake pool. -// This function is the entry point for the staking operation. -// Provided the provider type and provider ID, the value is locked in the stake pool between the SDK client and the provider. -// Based on the locked amount, the client will get rewards as share of the provider's rewards. -// - providerType: provider type -// - providerID: provider ID -// - value: value to lock -// - fee: transaction fee -func StakePoolLock(providerType ProviderType, providerID string, value, fee string) (hash string, nonce int64, err error) { - if !sdkInitialized { - return "", 0, sdkNotInitialized - } - - if providerType == 0 { - return "", 0, errors.New("stake_pool_lock", "provider is required") - } - - if providerID == "" { - return "", 0, errors.New("stake_pool_lock", "provider_id is required") - } - - spr := stakePoolRequest{ - ProviderType: providerType, - ProviderID: providerID, - } - - var sn = transaction.SmartContractTxnData{ - InputArgs: &spr, - } - - var scAddress string - switch providerType { - case ProviderBlobber, ProviderValidator: - scAddress = STORAGE_SCADDRESS - sn.Name = transaction.STORAGESC_STAKE_POOL_LOCK - case ProviderMiner, ProviderSharder: - scAddress = MINERSC_SCADDRESS - sn.Name = transaction.MINERSC_LOCK - case ProviderAuthorizer: - scAddress = ZCNSC_SCADDRESS - sn.Name = transaction.ZCNSC_LOCK - default: - return "", 0, errors.Newf("stake_pool_lock", "unsupported provider type: %v", providerType) - } - - hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(scAddress, sn, value, fee) - return -} - -// StakePoolUnlock unlocks a stake pool tokens. If tokens can't be unlocked due -// to opened offers, then it returns time where the tokens can be unlocked, -// marking the pool as 'want to unlock' to avoid its usage in offers in the -// future. The time is maximal time that can be lesser in some cases. To -// unlock tokens can't be unlocked now, wait the time and unlock them (call -// this function again). -// - providerType: provider type -// - providerID: provider ID -// - fee: transaction fee -func StakePoolUnlock(providerType ProviderType, providerID string, fee string) (unstake int64, nonce int64, err error) { - if !sdkInitialized { - return 0, 0, sdkNotInitialized - } - - if providerType == 0 { - return 0, 0, errors.New("stake_pool_lock", "provider is required") - } - - if providerID == "" { - return 0, 0, errors.New("stake_pool_lock", "provider_id is required") - } - - spr := stakePoolRequest{ - ProviderType: providerType, - ProviderID: providerID, - } - - var sn = transaction.SmartContractTxnData{ - InputArgs: &spr, - } - - var scAddress string - switch providerType { - case ProviderBlobber, ProviderValidator: - scAddress = STORAGE_SCADDRESS - sn.Name = transaction.STORAGESC_STAKE_POOL_UNLOCK - case ProviderMiner, ProviderSharder: - scAddress = MINERSC_SCADDRESS - sn.Name = transaction.MINERSC_UNLOCK - case ProviderAuthorizer: - scAddress = ZCNSC_SCADDRESS - sn.Name = transaction.ZCNSC_UNLOCK - default: - return 0, 0, errors.Newf("stake_pool_unlock", "unsupported provider type: %v", providerType) - } - - var out string - if _, out, nonce, _, err = smartContractTxnValueFeeWithRetry(scAddress, sn, "0", fee); err != nil { - return // an error - } - - var spuu stakePoolLock - if err = json.Unmarshal([]byte(out), &spuu); err != nil { - return - } - - return spuu.Amount, nonce, nil -} - -// -// write pool -// - -// WritePoolLock locks given number of tokes for given duration in read pool. -// - allocID: allocation ID -// - tokens: number of tokens to lock -// - fee: transaction fee -func WritePoolLock(allocID string, tokens, fee string) (hash string, nonce int64, err error) { - if !sdkInitialized { - return "", 0, sdkNotInitialized - } - - type lockRequest struct { - AllocationID string `json:"allocation_id"` - } - - var req lockRequest - req.AllocationID = allocID - - var sn = transaction.SmartContractTxnData{ - Name: transaction.STORAGESC_WRITE_POOL_LOCK, - InputArgs: &req, - } - - hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, tokens, fee) - return -} - -// WritePoolUnlock unlocks ALL tokens of a write pool. Needs to be cancelled first. -// - allocID: allocation ID -// - fee: transaction fee -func WritePoolUnlock(allocID string, fee string) (hash string, nonce int64, err error) { - if !sdkInitialized { - return "", 0, sdkNotInitialized - } - - type unlockRequest struct { - AllocationID string `json:"allocation_id"` - } - - var req unlockRequest - req.AllocationID = allocID - - var sn = transaction.SmartContractTxnData{ - Name: transaction.STORAGESC_WRITE_POOL_UNLOCK, - InputArgs: &req, - } - hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, "0", fee) - return -} - -func smartContractTxn(scAddress string, sn transaction.SmartContractTxnData) ( - hash, out string, nonce int64, txn *transaction.Transaction, err error) { - return smartContractTxnValue(scAddress, sn, "0") -} - -func StorageSmartContractTxn(sn transaction.SmartContractTxnData) ( - hash, out string, nonce int64, txn *transaction.Transaction, err error) { - - return storageSmartContractTxnValue(sn, "0") -} - -func storageSmartContractTxn(sn transaction.SmartContractTxnData) ( - hash, out string, nonce int64, txn *transaction.Transaction, err error) { - - return storageSmartContractTxnValue(sn, "0") -} - -func smartContractTxnValue(scAddress string, sn transaction.SmartContractTxnData, value string) ( - hash, out string, nonce int64, txn *transaction.Transaction, err error) { - - return smartContractTxnValueFeeWithRetry(scAddress, sn, value, strconv.FormatUint(client.TxnFee(), 10)) -} - -func storageSmartContractTxnValue(sn transaction.SmartContractTxnData, value string) ( - hash, out string, nonce int64, txn *transaction.Transaction, err error) { - - // Fee is set during sdk initialization. - return smartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, value, strconv.FormatUint(client.TxnFee(), 10)) -} - -func smartContractTxnValueFeeWithRetry(scAddress string, sn transaction.SmartContractTxnData, - value, fee string) (hash, out string, nonce int64, t *transaction.Transaction, err error) { - hash, out, nonce, t, err = smartContractTxnValueFee(scAddress, sn, value, fee) - - if err != nil && strings.Contains(err.Error(), "invalid transaction nonce") { - return smartContractTxnValueFee(scAddress, sn, value, fee) - } - - return -} - -func smartContractTxnValueFee(scAddress string, sn transaction.SmartContractTxnData, - value, fee string) (hash, out string, nonce int64, t *transaction.Transaction, err error) { - t, err = ExecuteSmartContract(scAddress, sn, value, fee) - if err != nil { - if t != nil { - return "", "", t.TransactionNonce, nil, err - } - - return "", "", 0, nil, err - } - - return t.Hash, t.TransactionOutput, t.TransactionNonce, t, nil -} diff --git a/zboxcore/sdk/transaction_mobile.go b/zboxcore/sdk/transaction_mobile.go deleted file mode 100644 index d9d29e0eb..000000000 --- a/zboxcore/sdk/transaction_mobile.go +++ /dev/null @@ -1,130 +0,0 @@ -//go:build mobile -// +build mobile - -package sdk - -import ( - "fmt" - "sync" - - "errors" - - "github.com/0chain/gosdk/core/transaction" - l "github.com/0chain/gosdk/zboxcore/logger" - "github.com/0chain/gosdk/zcncore" -) - -type transactionCallback struct { - wg *sync.WaitGroup - success bool - errMsg string - - txn *zcncore.Transaction -} - -func (cb *transactionCallback) OnTransactionComplete(t *zcncore.Transaction, status int) { - defer cb.wg.Done() - cb.txn = t - if status == zcncore.StatusSuccess { - cb.success = true - } else { - cb.errMsg = t.GetTransactionError() - } -} - -func (cb *transactionCallback) OnVerifyComplete(t *zcncore.Transaction, status int) { - defer cb.wg.Done() - cb.txn = t - if status == zcncore.StatusSuccess { - cb.success = true - } else { - cb.errMsg = t.GetVerifyError() - } -} - -func (cb *transactionCallback) OnAuthComplete(t *zcncore.Transaction, status int) { - cb.txn = t - fmt.Println("Authorization complete on zauth.", status) -} - -// ExecuteSmartContract executes the smart contract -func ExecuteSmartContract(address string, sn transaction.SmartContractTxnData, value, fee string) (*transaction.Transaction, error) { - wg := &sync.WaitGroup{} - cb := &transactionCallback{wg: wg} - txn, err := zcncore.NewTransaction(cb, fee, 0) - if err != nil { - return nil, err - } - - wg.Add(1) - t, err := txn.ExecuteSmartContract(address, sn.Name, sn.InputArgs, value) - if err != nil { - return nil, err - } - - msg := fmt.Sprintf("Executing transaction '%s' with hash %s ", sn.Name, t.Hash) - l.Logger.Info(msg) - l.Logger.Info("estimated txn fee: ", t.TransactionFee) - - wg.Wait() - - if !cb.success { - return nil, fmt.Errorf("smartcontract: %s", cb.errMsg) - } - - cb.success = false - wg.Add(1) - err = txn.Verify() - if err != nil { - return nil, err - } - - wg.Wait() - - if !cb.success { - return nil, fmt.Errorf("smartcontract: %s", cb.errMsg) - } - - switch txn.GetVerifyConfirmationStatus() { - case zcncore.ChargeableError: - return t, errors.New(txn.GetVerifyOutput()) - case zcncore.Success: - return t, nil - } - - return nil, fmt.Errorf("smartcontract: %v", txn.GetVerifyConfirmationStatus()) -} - -// ExecuteSmartContractSend create send transaction to transfer tokens from the caller to target address -func ExecuteSmartContractSend(to, tokens, fee, desc string) (string, error) { - wg := &sync.WaitGroup{} - cb := &transactionCallback{wg: wg} - txn, err := zcncore.NewTransaction(cb, fee, 0) - if err != nil { - return "", err - } - - wg.Add(1) - err = txn.Send(to, tokens, desc) - if err == nil { - wg.Wait() - } else { - return "", err - } - - if cb.success { - cb.success = false - wg.Add(1) - err := txn.Verify() - if err == nil { - wg.Wait() - } else { - return "", err - } - if cb.success { - return txn.GetVerifyOutput(), nil - } - } - - return "", errors.New(cb.errMsg) -} From 9c293005477ba509b4bf8d9ecd67901d30031686 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Mon, 23 Sep 2024 22:37:34 +0530 Subject: [PATCH 123/319] fix copy and move --- zboxcore/sdk/copyworker.go | 8 +++++++- zboxcore/sdk/moveworker.go | 12 ++++++++++-- zboxcore/sdk/multi_operation_worker.go | 2 +- zboxcore/sdk/repairworker.go | 4 ++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/zboxcore/sdk/copyworker.go b/zboxcore/sdk/copyworker.go index 9330ab610..336ec5af2 100644 --- a/zboxcore/sdk/copyworker.go +++ b/zboxcore/sdk/copyworker.go @@ -46,6 +46,7 @@ type CopyRequest struct { connectionID string timestamp int64 dirOnly bool + destLookupHash string Consensus } @@ -242,6 +243,7 @@ func (req *CopyRequest) ProcessWithBlobbersV2() ([]fileref.RefEntity, error) { l.Logger.Debug(err.Error()) return } + refEntity.Path = path.Join(req.destPath, path.Base(refEntity.Path)) objectTreeRefs[blobberIdx] = refEntity req.maskMU.Lock() versionMap[refEntity.AllocationRoot] += 1 @@ -288,6 +290,7 @@ func (req *CopyRequest) ProcessWithBlobbersV2() ([]fileref.RefEntity, error) { if err != nil && strings.Contains(err.Error(), objAlreadyExists) && consensusRef.Type == fileref.DIRECTORY { return nil, errNoChange } + req.destLookupHash = fileref.GetReferenceLookup(req.allocationID, consensusRef.Path) return objectTreeRefs, err } @@ -441,6 +444,9 @@ func (co *CopyOperation) Process(allocObj *Allocation, connectionID string) ([]f if !cR.isConsensusOk() { l.Logger.Error("copy failed: ", cR.remotefilepath, cR.destPath) if err != nil { + if err == errNoChange { + return nil, cR.copyMask, err + } return nil, cR.copyMask, errors.New("copy_failed", fmt.Sprintf("Copy failed. %s", err.Error())) } @@ -448,7 +454,7 @@ func (co *CopyOperation) Process(allocObj *Allocation, connectionID string) ([]f fmt.Sprintf("Copy failed. Required consensus %d, got %d", cR.Consensus.consensusThresh, cR.Consensus.consensus)) } - co.destLookupHash = fileref.GetReferenceLookup(cR.allocationID, co.destPath) + co.destLookupHash = cR.destLookupHash return co.objectTreeRefs, cR.copyMask, nil } diff --git a/zboxcore/sdk/moveworker.go b/zboxcore/sdk/moveworker.go index 94ba599f8..5fb24e124 100644 --- a/zboxcore/sdk/moveworker.go +++ b/zboxcore/sdk/moveworker.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "mime/multipart" "net/http" + "path" "strings" "sync" "time" @@ -44,6 +45,7 @@ type MoveRequest struct { maskMU *sync.Mutex connectionID string timestamp int64 + destLookupHash string Consensus } @@ -231,6 +233,7 @@ func (req *MoveRequest) ProcessWithBlobbersV2() ([]fileref.RefEntity, error) { l.Logger.Debug(err.Error()) return } + refEntity.Path = path.Join(req.destPath, path.Base(refEntity.Path)) objectTreeRefs[blobberIdx] = refEntity req.maskMU.Lock() versionMap[refEntity.AllocationRoot] += 1 @@ -291,7 +294,12 @@ func (req *MoveRequest) ProcessWithBlobbersV2() ([]fileref.RefEntity, error) { }(int(pos)) } wg.Wait() - return objectTreeRefs, zboxutil.MajorError(blobberErrors) + err := zboxutil.MajorError(blobberErrors) + if err != nil && strings.Contains(err.Error(), objAlreadyExists) && consensusRef.Type == fileref.DIRECTORY { + return nil, errNoChange + } + req.destLookupHash = fileref.GetReferenceLookup(req.allocationID, consensusRef.Path) + return objectTreeRefs, err } func (req *MoveRequest) ProcessMove() error { @@ -446,7 +454,7 @@ func (mo *MoveOperation) Process(allocObj *Allocation, connectionID string) ([]f fmt.Sprintf("Move failed. Required consensus %d, got %d", mR.Consensus.consensusThresh, mR.Consensus.consensus)) } - mo.destLookupHash = fileref.GetReferenceLookup(mR.allocationID, mR.destPath) + mo.destLookupHash = mR.destLookupHash mo.srcLookupHash = fileref.GetReferenceLookup(mR.allocationID, mR.remotefilepath) return mo.objectTreeRefs, mR.moveMask, nil } diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 72965c642..8a9d12be0 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -198,8 +198,8 @@ func (mo *MultiOperation) Process() error { l.Logger.Error(err) errsSlice[idx] = errors.New("", err.Error()) ctxCncl(err) - return } + return } mo.maskMU.Lock() if mo.allocationObj.StorageVersion == StorageV2 { diff --git a/zboxcore/sdk/repairworker.go b/zboxcore/sdk/repairworker.go index 093bba6f6..eee6b9101 100644 --- a/zboxcore/sdk/repairworker.go +++ b/zboxcore/sdk/repairworker.go @@ -399,10 +399,12 @@ func (r *RepairRequest) iterateDirV2(ctx context.Context) { if srcEOF && diff.tgtEOF { continue } + // if target is at EOF, upload the src file if diff.tgtEOF { uploadMask = uploadMask.Or(diff.mask) continue } + // if source is at EOF, delete the target file if srcEOF { delMask := diff.mask op := OperationRequest{ @@ -422,7 +424,9 @@ func (r *RepairRequest) iterateDirV2(ctx context.Context) { } continue } + // if both source and target are at same path if diff.tgtRef.Path == srcRef.Path { + // if both source and target are at same path and hash is different if diff.tgtRef.ActualFileHash != srcRef.ActualFileHash { deleteMask = deleteMask.Or(diff.mask) uploadMask = uploadMask.Or(diff.mask) From af418a75c10969f8b417647427d1cbc41f625d42 Mon Sep 17 00:00:00 2001 From: Yash Verma Date: Tue, 24 Sep 2024 01:09:38 +0530 Subject: [PATCH 124/319] Fix mobilesdk build for refactor zboxcore (#1621) * fix: GetWalletBalance in mobilesdk. fix: include mobile build flags in execute_transactions.go fix: remove ignore package tag from transaction.go * fix: add GetUserLockedTotal. fix: InitSDK method while initializing storage SDK. fix: Conversion methods from ZCN to SAS. * fix: add proper conversions to WritePoolLock for tokens and fee. * fix: wasm build. * set toolcahin to go1.21.0 in znft/example. * Revert "set toolcahin to go1.21.0 in znft/example." This reverts commit 6ceea1ae4a1b44e2a8ccc5b332c7682ad04a43ff. * refactor: setup go 1.22 in build-sdks.yml for wasm builds. * refactor: setup go 1.22 in tests.yml for wasm tests. * nit fix build-sdks.yml wasm build. * nit fix build-sdks.yml wasm build. * nit fix build-sdks.yml wasm build. --------- Co-authored-by: Jayash Satolia <73050737+Jayashsatolia403@users.noreply.github.com> --- .github/workflows/build-sdks.yml | 8 ++++---- .github/workflows/tests.yml | 6 +++--- core/version/version.go | 2 +- go.mod | 14 ++++++++------ go.sum | 20 ++++++++++---------- mobilesdk/sdk/sdk.go | 27 +++++++++++++++++++++------ mobilesdk/zcn/writepool.go | 19 ++++++++++++++++++- zcncore/get_data.go | 30 ++++++++++++++++++++++++++++++ zcncore/transaction.go | 3 --- zcncore/wallet_mobile.go | 5 +++-- znft/example/go.mod | 8 ++++---- znft/example/go.sum | 2 ++ 12 files changed, 104 insertions(+), 40 deletions(-) diff --git a/.github/workflows/build-sdks.yml b/.github/workflows/build-sdks.yml index d2850771d..b024d1fec 100644 --- a/.github/workflows/build-sdks.yml +++ b/.github/workflows/build-sdks.yml @@ -335,13 +335,13 @@ jobs: name: Build-wasm runs-on: [self-hosted, arc-runner] steps: - - name: Set up Go 1.x + - name: Set up Go 1.22 uses: actions/setup-go@v3 with: - go-version: 1.21.5 + go-version: 1.22 - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Install dependencies run: | @@ -349,7 +349,7 @@ jobs: sudo apt-get -y install build-essential nghttp2 libnghttp2-dev libssl-dev wget - name: Build - run: docker run --rm -v $PWD:/gosdk -w /gosdk golang:1.21.5 make wasm-build + run: docker run --rm -v $PWD:/gosdk -w /gosdk golang:1.22 make wasm-build - name: 'Upload Artifact' uses: actions/upload-artifact@v3 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 989856a3c..75aad9557 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -167,10 +167,10 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Set up Go 1.x - uses: actions/setup-go@v3 + - name: Set up Go 1.22 + uses: actions/setup-go@v2 with: - go-version: 1.21.5 + go-version: 1.22 - uses: actions/setup-node@v2 with: diff --git a/core/version/version.go b/core/version/version.go index 087550579..656a0a4db 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.6-70-g3bd80b5b" +const VERSIONSTR = "v1.17.9-114-g6fc99ddf" diff --git a/go.mod b/go.mod index 08444d4c5..d7713db2f 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module github.com/0chain/gosdk -go 1.21 +go 1.22.0 + +toolchain go1.22.1 require ( github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565 @@ -29,8 +31,8 @@ require ( github.com/uptrace/bunrouter v1.0.20 go.dedis.ch/kyber/v3 v3.1.0 go.uber.org/zap v1.24.0 - golang.org/x/crypto v0.26.0 - golang.org/x/image v0.19.0 + golang.org/x/crypto v0.27.0 + golang.org/x/image v0.20.0 golang.org/x/sync v0.8.0 gopkg.in/cheggaaa/pb.v1 v1.0.28 gopkg.in/natefinch/lumberjack.v2 v2.2.1 @@ -106,8 +108,8 @@ require ( github.com/valyala/fasthttp v1.51.0 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect go.dedis.ch/fixbuf v1.0.3 // indirect - golang.org/x/sys v0.23.0 // indirect - golang.org/x/text v0.17.0 // indirect + golang.org/x/sys v0.25.0 // indirect + golang.org/x/text v0.18.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect @@ -122,7 +124,7 @@ require ( github.com/yusufpapurcu/wmi v1.2.2 // indirect go.uber.org/atomic v1.10.0 // indirect go.uber.org/multierr v1.9.0 // indirect - golang.org/x/net v0.28.0 // indirect + golang.org/x/net v0.29.0 // indirect golang.org/x/time v0.3.0 // indirect ) diff --git a/go.sum b/go.sum index 583cab899..8d97d9986 100644 --- a/go.sum +++ b/go.sum @@ -543,8 +543,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= -golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= +golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= +golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -561,8 +561,8 @@ golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMk golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= -golang.org/x/image v0.19.0 h1:D9FX4QWkLfkeqaC62SonffIIuYdOk/UE2XKUBgRIBIQ= -golang.org/x/image v0.19.0/go.mod h1:y0zrRqlQRWQ5PXaYCOMLTW2fpsxZ8Qh9I/ohnInJEys= +golang.org/x/image v0.20.0 h1:7cVCUjQwfL18gyBJOmYvptfSHS8Fb3YUDtfLIZ7Nbpw= +golang.org/x/image v0.20.0/go.mod h1:0a88To4CYVBAHp5FXJm8o7QbUl37Vd85ply1vyD8auM= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= @@ -621,8 +621,8 @@ golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= -golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= +golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= +golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -696,8 +696,8 @@ golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= -golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -705,8 +705,8 @@ golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3 golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= -golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= +golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/mobilesdk/sdk/sdk.go b/mobilesdk/sdk/sdk.go index 7548a8212..c7a19f672 100644 --- a/mobilesdk/sdk/sdk.go +++ b/mobilesdk/sdk/sdk.go @@ -114,7 +114,7 @@ func InitStorageSDK(clientJson string, configJson string) (*StorageSDK, error) { l.Logger.Error(err) return nil, err } - err = zcncore.InitZCNSDK(configObj.BlockWorker, configObj.SignatureScheme) + err = Init(configObj.BlockWorker) if err != nil { l.Logger.Error(err) return nil, err @@ -332,10 +332,25 @@ func (s *StorageSDK) CancelAllocation(allocationID string) (string, error) { // - fee: fee of the transaction // - allocID: allocation ID func (s *StorageSDK) WritePoolLock(durInSeconds int64, tokens, fee float64, allocID string) error { - _, _, err := sdk.WritePoolLock( + formattedWpLock := strconv.FormatUint(zcncore.ConvertToValue(tokens), 10) + formattedFee := strconv.FormatUint(zcncore.ConvertToValue(fee), 10) + + wpLockUint, err := strconv.ParseUint(formattedWpLock, 10, 64) + if err != nil { + return errors.Errorf("Error parsing write pool lock: %v", err) + } + + feeUint, err := strconv.ParseUint(formattedFee, 10, 64) + + if err != nil { + return errors.Errorf("Error parsing fee: %v", err) + } + + _, _, err = sdk.WritePoolLock( allocID, - strconv.FormatUint(zcncore.ConvertTokenToSAS(tokens), 10), - strconv.FormatUint(zcncore.ConvertTokenToSAS(fee), 10)) + wpLockUint, + feeUint, + ) return err } @@ -397,7 +412,7 @@ func (s *StorageSDK) RedeemFreeStorage(ticket string) (string, error) { return "", fmt.Errorf("invalid_free_marker: free marker is not assigned to your wallet") } - hash, _, err := sdk.CreateFreeAllocation(marker, strconv.FormatUint(lock, 10)) + hash, _, err := sdk.CreateFreeAllocation(marker, lock) return hash, err } @@ -428,7 +443,7 @@ func decodeTicket(ticket string) (string, string, uint64, error) { markerStr, _ := json.Marshal(markerInput) s, _ := strconv.ParseFloat(string(fmt.Sprintf("%v", lock)), 64) - return string(recipientPublicKey), string(markerStr), zcncore.ConvertTokenToSAS(s), nil + return string(recipientPublicKey), string(markerStr), zcncore.ConvertToValue(s), nil } // RegisterAuthorizer Client can extend interface and FaSS implementation to this register like this: diff --git a/mobilesdk/zcn/writepool.go b/mobilesdk/zcn/writepool.go index e21418db5..e69513a65 100644 --- a/mobilesdk/zcn/writepool.go +++ b/mobilesdk/zcn/writepool.go @@ -5,6 +5,7 @@ package zcn import ( "github.com/0chain/gosdk/zboxcore/sdk" + "strconv" ) // WritePoolLock locks given number of tokes for given duration in read pool. @@ -13,7 +14,23 @@ import ( // - tokens: sas tokens // - fee: sas tokens func WritePoolLock(allocID string, tokens, fee string) (string, error) { - hash, _, err := sdk.WritePoolLock(allocID, tokens, fee) + tokensUint, err := strconv.ParseUint(tokens, 10, 64) + + if err != nil { + return "", err + } + + feeUint, err := strconv.ParseUint(fee, 10, 64) + + if err != nil { + return "", err + } + + hash, _, err := sdk.WritePoolLock( + allocID, + tokensUint, + feeUint, + ) return hash, err } diff --git a/zcncore/get_data.go b/zcncore/get_data.go index 09d5d1df8..cae2c7f69 100644 --- a/zcncore/get_data.go +++ b/zcncore/get_data.go @@ -289,3 +289,33 @@ func GetNotProcessedZCNBurnTickets(ethereumAddress, startNonce string) ([]byte, "nonce": startNonce, }) } + +// GetUserLockedTotal get total token user locked +// # Inputs +// - clientID wallet id +func GetUserLockedTotal(clientID string) (int64, error) { + var result map[string]int64 + + err := CheckConfig() + if err != nil { + return 0, err + } + + const GET_USER_LOCKED_TOTAL = `/v1/getUserLockedTotal` + + info, err := client.MakeSCRestAPICall(ZCNSCSmartContractAddress, GET_USER_LOCKED_TOTAL, Params{ + "client_id": clientID, + }) + + err = json.Unmarshal([]byte(info), &result) + if err != nil { + return 0, errors.New("invalid json format: " + err.Error()) + } + + total, ok := result["total"] + if ok { + return total, nil + } else { + return 0, err + } +} diff --git a/zcncore/transaction.go b/zcncore/transaction.go index 68b1cc458..b0d1e7870 100644 --- a/zcncore/transaction.go +++ b/zcncore/transaction.go @@ -1,6 +1,3 @@ -//go:build !mobile -// +build !mobile - package zcncore import ( diff --git a/zcncore/wallet_mobile.go b/zcncore/wallet_mobile.go index 93c3c8beb..7fd9b0564 100644 --- a/zcncore/wallet_mobile.go +++ b/zcncore/wallet_mobile.go @@ -4,6 +4,7 @@ package zcncore import ( + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/zcncrypto" ) @@ -30,9 +31,9 @@ func (w *wallet) Sign(hash string) (string, error) { // GetWalletBalance retrieve wallet balance from sharders // - id: client id func GetWalletBalance(id string) (int64, error) { - balance, _, err := getWalletBalance(id) + response, err := client.GetBalance(id) if err != nil { return 0, err } - return int64(balance), nil + return int64(response.Balance), nil } diff --git a/znft/example/go.mod b/znft/example/go.mod index 7c20400ac..8f6566b01 100644 --- a/znft/example/go.mod +++ b/znft/example/go.mod @@ -1,8 +1,8 @@ module example -go 1.21 +go 1.22.0 -toolchain go1.21.0 +toolchain go1.22.1 require github.com/0chain/gosdk v1.8.9 @@ -25,8 +25,8 @@ require ( github.com/tklauser/numcpus v0.6.0 // indirect github.com/tyler-smith/go-bip39 v1.1.0 // indirect github.com/yusufpapurcu/wmi v1.2.2 // indirect - golang.org/x/crypto v0.26.0 // indirect - golang.org/x/sys v0.23.0 // indirect + golang.org/x/crypto v0.27.0 // indirect + golang.org/x/sys v0.25.0 // indirect gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/znft/example/go.sum b/znft/example/go.sum index 99ba4bdc5..bb384aa82 100644 --- a/znft/example/go.sum +++ b/znft/example/go.sum @@ -137,6 +137,7 @@ golang.org/x/crypto v0.0.0-20221012134737-56aed061732a/go.mod h1:IxCIyHEi3zRg3s0 golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58= golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= +golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -163,6 +164,7 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= From b2d955f34e9ed34e32d5322176e77aab3718c558 Mon Sep 17 00:00:00 2001 From: Yash Verma Date: Tue, 24 Sep 2024 03:50:08 +0530 Subject: [PATCH 125/319] Fix/refactor zboxcore lint. (#1627) * fix: GetWalletBalance in mobilesdk. fix: include mobile build flags in execute_transactions.go fix: remove ignore package tag from transaction.go * fix: add GetUserLockedTotal. fix: InitSDK method while initializing storage SDK. fix: Conversion methods from ZCN to SAS. * fix: add proper conversions to WritePoolLock for tokens and fee. * fix: wasm build. * set toolcahin to go1.21.0 in znft/example. * Revert "set toolcahin to go1.21.0 in znft/example." This reverts commit 6ceea1ae4a1b44e2a8ccc5b332c7682ad04a43ff. * refactor: setup go 1.22 in build-sdks.yml for wasm builds. * refactor: setup go 1.22 in tests.yml for wasm tests. * nit fix build-sdks.yml wasm build. * nit fix build-sdks.yml wasm build. * nit fix build-sdks.yml wasm build. * lint fixes. * fix: proofBurnTicket.go test --------- Co-authored-by: Jayash Satolia <73050737+Jayashsatolia403@users.noreply.github.com> --- core/client/http.go | 16 +++------- core/conf/vars.go | 2 +- core/logger/logger.go | 30 +++++++++++++++--- core/resty/resty.go | 3 +- core/resty/resty_test.go | 4 +-- core/tokenrate/tokenrate_test.go | 7 +++-- core/util/httpnet.go | 4 +-- mobilesdk/zbox/m3u8.go | 25 ++++++++++++--- wasmsdk/bridge.go | 10 +++--- wasmsdk/demo/main.go | 7 +++-- wasmsdk/tokenrate.go | 2 +- wasmsdk/updateImage.go | 3 +- zboxapi/sdk_test.go | 3 +- zboxcore/encryption/pre_test.go | 8 +++-- zboxcore/sdk/allocation.go | 7 ++--- zboxcore/sdk/allocation_file_test.go | 29 +++++++++-------- zboxcore/sdk/allocation_test.go | 41 ++++++++++++------------- zboxcore/sdk/commitworker.go | 3 +- zboxcore/sdk/common.go | 6 ++-- zboxcore/sdk/copyworker.go | 4 +-- zboxcore/sdk/copyworker_test.go | 21 ++++++------- zboxcore/sdk/deleteworker.go | 3 +- zboxcore/sdk/deleteworker_test.go | 13 ++++---- zboxcore/sdk/dirworker.go | 4 +-- zboxcore/sdk/downloadworker.go | 3 +- zboxcore/sdk/filemetaworker_test.go | 11 +++---- zboxcore/sdk/filerefsworker.go | 6 ++-- zboxcore/sdk/filestatsworker.go | 4 +-- zboxcore/sdk/filestatsworker_test.go | 11 +++---- zboxcore/sdk/listworker.go | 4 +-- zboxcore/sdk/listworker_test.go | 11 +++---- zboxcore/sdk/moveworker.go | 4 +-- zboxcore/sdk/multi_operation_worker.go | 4 +-- zboxcore/sdk/renameworker.go | 4 +-- zboxcore/sdk/renameworker_test.go | 23 +++++++------- zboxcore/sdk/rollback.go | 20 +++++++++--- zboxcore/sdk/sdk.go | 6 ++-- zboxcore/sdk/sync.go | 5 ++- zboxcore/sdk/writemarker_mutex_test.go | 5 ++- zcnbridge/authorizer/proofBurnTicket.go | 12 +++++--- zcnbridge/authorizers_query.go | 4 +-- zcnbridge/bridge_test.go | 14 ++++----- zcncore/get_data.go | 6 +++- zcncore/wallet_base.go | 12 ++++---- zcncore/wallet_callback.go | 4 +-- 45 files changed, 235 insertions(+), 193 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index 8f2bcb1d2..1f0a74e5a 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -11,9 +11,7 @@ import ( "github.com/hashicorp/go-retryablehttp" "github.com/shopspring/decimal" "io" - "io/ioutil" "log" - "math" "net" "net/http" "net/url" @@ -130,7 +128,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] } defer response.Body.Close() - entityBytes, _ := ioutil.ReadAll(response.Body) + entityBytes, _ := io.ReadAll(response.Body) mu.Lock() if response.StatusCode > http.StatusBadRequest { nodeClient.sharders.Fail(sharder) @@ -196,10 +194,10 @@ func isCurrentDominantStatus(respStatus int, currentTotalPerStatus map[int]int, // hashAndBytesOfReader computes hash of readers data and returns hash encoded to hex and bytes of reader data. // If error occurs while reading data from reader, it returns non nil error. -func hashAndBytesOfReader(r io.Reader) (hash string, reader []byte, err error) { +func hashAndBytesOfReader(r io.Reader) (hash string, reader []byte, err error) { //nolint:unused h := sha1.New() teeReader := io.TeeReader(r, h) - readerBytes, err := ioutil.ReadAll(teeReader) + readerBytes, err := io.ReadAll(teeReader) if err != nil { return "", nil, err } @@ -208,7 +206,7 @@ func hashAndBytesOfReader(r io.Reader) (hash string, reader []byte, err error) { } // extractSharders returns string slice of randomly ordered sharders existing in the current network. -func extractSharders() []string { +func extractSharders() []string { //nolint:unused sharders := nodeClient.Network().Sharders return util.GetRandom(sharders, len(sharders)) } @@ -219,7 +217,7 @@ const ( ) // makeScURL creates url.URL to make smart contract request to sharder. -func makeScURL(params map[string]string, sharder, restApiUrl, scAddress, relativePath string) *url.URL { +func makeScURL(params map[string]string, sharder, restApiUrl, scAddress, relativePath string) *url.URL { //nolint:unused uString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath) u, _ := url.Parse(uString) @@ -332,10 +330,6 @@ type GetBalanceResponse struct { // ToToken converts Balance to ZCN tokens. func (b GetBalanceResponse) ToToken() (float64, error) { - if b.Balance > math.MaxInt64 { - return 0.0, errors.New("to_token failed", "value is too large") - } - f, _ := decimal.New(b.Balance, -10).Float64() return f, nil } diff --git a/core/conf/vars.go b/core/conf/vars.go index 3d7ac63e7..c1e7be234 100644 --- a/core/conf/vars.go +++ b/core/conf/vars.go @@ -51,7 +51,7 @@ func InitClientConfig(c *Config) { }) } -//Deprecated: Use client.Init() function. To normalize urls, use network.NormalizeURLs() method +// Deprecated: Use client.Init() function. To normalize urls, use network.NormalizeURLs() method // // InitChainNetwork set global chain network func InitChainNetwork(n *Network) { if n == nil { diff --git a/core/logger/logger.go b/core/logger/logger.go index a875dae85..fbe517017 100644 --- a/core/logger/logger.go +++ b/core/logger/logger.go @@ -95,30 +95,50 @@ func (l *Logger) SetLogFile(logFile io.Writer, verbose bool) { func (l *Logger) Debug(v ...interface{}) { if l.lvl >= DEBUG { - l.logDebug.Output(2, fmt.Sprint(v...)) + err := l.logDebug.Output(2, fmt.Sprint(v...)) + if err != nil { + fmt.Printf("Error logging debug message: %v", err) + return + } } } func (l *Logger) Info(v ...interface{}) { if l.lvl >= INFO { - l.logInfo.Output(2, fmt.Sprint(v...)) + err := l.logInfo.Output(2, fmt.Sprint(v...)) + if err != nil { + fmt.Printf("Error logging info message: %v", err) + return + } } } func (l *Logger) Error(v ...interface{}) { if l.lvl >= ERROR { - l.logError.Output(2, fmt.Sprint(v...)+cReset) + err := l.logError.Output(2, fmt.Sprint(v...)+cReset) + if err != nil { + fmt.Printf("Error logging error message: %v", err) + return + } } } func (l *Logger) Fatal(v ...interface{}) { if l.lvl >= FATAL { - l.logFatal.Output(2, fmt.Sprint(v...)+cReset) + err := l.logFatal.Output(2, fmt.Sprint(v...)+cReset) + if err != nil { + fmt.Printf("Error logging fatal message: %v", err) + return + } } } func (l *Logger) Close() { if c, ok := l.fWriter.(io.Closer); ok && c != nil { - c.Close() + err := c.Close() + if err != nil { + fmt.Printf("Error closing log file: %v", err) + return + } } } diff --git a/core/resty/resty.go b/core/resty/resty.go index 49ea9d695..404797755 100644 --- a/core/resty/resty.go +++ b/core/resty/resty.go @@ -3,7 +3,6 @@ package resty import ( "context" "io" - "io/ioutil" "net" "net/http" "sync" @@ -195,7 +194,7 @@ func (r *Resty) httpDo(req *http.Request) { result := Result{Request: request, Response: resp, Err: err} if resp != nil { // read and close body to reuse http connection - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { result.Err = err } else { diff --git a/core/resty/resty_test.go b/core/resty/resty_test.go index c424d9925..fabb4fecf 100644 --- a/core/resty/resty_test.go +++ b/core/resty/resty_test.go @@ -3,7 +3,7 @@ package resty import ( "context" "fmt" - "io/ioutil" + "io" "net/http" "strings" "testing" @@ -158,7 +158,7 @@ func setupMockClient(mck *mocks.Client, urls []string, statusCode int, name stri return r.URL.String() == u })).Return(&http.Response{ StatusCode: statusCode, - Body: ioutil.NopCloser(strings.NewReader(name)), + Body: io.NopCloser(strings.NewReader(name)), }, nil) }(url) } diff --git a/core/tokenrate/tokenrate_test.go b/core/tokenrate/tokenrate_test.go index f0ae824e2..f2e30abde 100644 --- a/core/tokenrate/tokenrate_test.go +++ b/core/tokenrate/tokenrate_test.go @@ -6,9 +6,10 @@ import ( "encoding/json" "fmt" "github.com/stretchr/testify/mock" - "io/ioutil" + "io" "log" "net/http" + "os" "strings" "testing" "time" @@ -125,7 +126,7 @@ func setupMockHttpResponse( })).Return( &http.Response{ StatusCode: statusCode, - Body: ioutil.NopCloser(bytes.NewReader(body)), + Body: io.NopCloser(bytes.NewReader(body)), }, nil).Once() } @@ -234,7 +235,7 @@ func getCoinGeckoResponse() func(testCaseName, mockProviderURL, providerName, sy } func getProviderJsonResponse(t *testing.T, provider string) []byte { - data, err := ioutil.ReadFile("mockresponses/" + provider + ".json") + data, err := os.ReadFile("mockresponses/" + provider + ".json") if err != nil { t.Fatal(err) } diff --git a/core/util/httpnet.go b/core/util/httpnet.go index 0acf86135..de44210ba 100644 --- a/core/util/httpnet.go +++ b/core/util/httpnet.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "encoding/json" - "io/ioutil" + "io" "net/http" "net/url" "os" @@ -164,7 +164,7 @@ func (r *PostRequest) Post() (*PostResponse, error) { defer resp.Body.Close() } - rspBy, err := ioutil.ReadAll(resp.Body) + rspBy, err := io.ReadAll(resp.Body) if err != nil { return err } diff --git a/mobilesdk/zbox/m3u8.go b/mobilesdk/zbox/m3u8.go index d9aa8c174..11a05a9d9 100644 --- a/mobilesdk/zbox/m3u8.go +++ b/mobilesdk/zbox/m3u8.go @@ -2,6 +2,7 @@ package zbox import ( "bytes" + "github.com/0chain/gosdk/zboxcore/logger" "io" "sort" "strconv" @@ -86,10 +87,26 @@ func (m *MediaPlaylist) flush() { return } - m.Writer.Truncate(0) - m.Writer.Seek(0, 0) - m.Writer.Write(m.Encode()) - m.Writer.Sync() + err := m.Writer.Truncate(0) + if err != nil { + logger.Logger.Error("flush m3u8 error: ", err) + return + } + _, err = m.Writer.Seek(0, 0) + if err != nil { + logger.Logger.Error("flush m3u8 error: ", err) + return + } + _, err = m.Writer.Write(m.Encode()) + if err != nil { + logger.Logger.Error("flush m3u8 error: ", err) + return + } + err = m.Writer.Sync() + if err != nil { + logger.Logger.Error("flush m3u8 error: ", err) + return + } } // Encode encode m3u8 diff --git a/wasmsdk/bridge.go b/wasmsdk/bridge.go index d027decc6..d7c43bd9e 100644 --- a/wasmsdk/bridge.go +++ b/wasmsdk/bridge.go @@ -14,7 +14,7 @@ import ( "strconv" ) -var bridge *zcnbridge.BridgeClient +var bridge *zcnbridge.BridgeClient //nolint:unused // initBridge initializes the bridge client // - ethereumAddress: ethereum address of the wallet owner @@ -25,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, @@ -100,7 +100,7 @@ func mintZCN(burnTrxHash string, timeout int) string { //nolint // getMintWZCNPayload returns the mint payload for the given burn transaction hash // - burnTrxHash: hash of the burn transaction -func getMintWZCNPayload(burnTrxHash string) string { +func getMintWZCNPayload(burnTrxHash string) string { //nolint:unused mintPayload, err := bridge.QueryEthereumMintPayload(burnTrxHash) if err != nil { return errors.Wrap("getMintWZCNPayload", "failed to query ethereum mint payload", err).Error() @@ -115,7 +115,7 @@ func getMintWZCNPayload(burnTrxHash string) string { } // 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 @@ -145,7 +145,7 @@ func getNotProcessedWZCNBurnEvents() string { } // getNotProcessedZCNBurnTickets Returns all not processed ZCN burn tickets burned for a certain ethereum address -func getNotProcessedZCNBurnTickets() string { +func getNotProcessedZCNBurnTickets() string { //nolint:unused userNonce, err := bridge.GetUserNonceMinted(context.Background(), bridge.EthereumAddress) if err != nil { return errors.Wrap("getNotProcessedZCNBurnTickets", "failed to retreive user nonce", err).Error() diff --git a/wasmsdk/demo/main.go b/wasmsdk/demo/main.go index 9e08e1f1d..64934403a 100644 --- a/wasmsdk/demo/main.go +++ b/wasmsdk/demo/main.go @@ -34,7 +34,10 @@ func main() { } w.WriteHeader(http.StatusOK) - w.Write([]byte(wallet)) + _, err = w.Write([]byte(wallet)) + if err != nil { + return err + } return nil }) @@ -53,7 +56,7 @@ func main() { } -type statusBar struct { +type statusBar struct { //nolint:unused walletString string wg *sync.WaitGroup success bool diff --git a/wasmsdk/tokenrate.go b/wasmsdk/tokenrate.go index 66cc05f0d..dd5897657 100644 --- a/wasmsdk/tokenrate.go +++ b/wasmsdk/tokenrate.go @@ -7,6 +7,6 @@ import ( ) // getUSDRate gets the USD rate for the given crypto symbol -func getUSDRate(symbol string) (float64, error) { +func getUSDRate(symbol string) (float64, error) { //nolint:unused return tokenrate.GetUSD(context.TODO(), symbol) } diff --git a/wasmsdk/updateImage.go b/wasmsdk/updateImage.go index c3ffce2ad..d2bf6fff3 100644 --- a/wasmsdk/updateImage.go +++ b/wasmsdk/updateImage.go @@ -8,7 +8,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "strings" ) @@ -302,7 +301,7 @@ func doHTTPRequest(method, url, authToken string, body io.Reader) ([]byte, int, return nil, 0, err } defer resp.Body.Close() - respBody, err := ioutil.ReadAll(resp.Body) + respBody, err := io.ReadAll(resp.Body) return respBody, resp.StatusCode, err } diff --git a/zboxapi/sdk_test.go b/zboxapi/sdk_test.go index 3dfded2bb..c9f344884 100644 --- a/zboxapi/sdk_test.go +++ b/zboxapi/sdk_test.go @@ -196,7 +196,8 @@ func TestDeleteSharedInfo(t *testing.T) { items := make(map[string]any) - json.Unmarshal(buf, &items) + err = json.Unmarshal(buf, &items) + require.Nil(t, err, "Error unmarshalling buffer items.") lookupHash := items["file_path_hash"].(string) diff --git a/zboxcore/encryption/pre_test.go b/zboxcore/encryption/pre_test.go index d33779308..6681b420f 100644 --- a/zboxcore/encryption/pre_test.go +++ b/zboxcore/encryption/pre_test.go @@ -1,8 +1,8 @@ package encryption import ( + "crypto/rand" "encoding/base64" - "math/rand" "testing" "github.com/0chain/gosdk/zboxcore/fileref" @@ -170,8 +170,10 @@ func BenchmarkEncrypt(t *testing.B) { encscheme.InitForEncryption("filetype:audio") for i := 0; i < 10000; i++ { dataToEncrypt := make([]byte, fileref.CHUNK_SIZE) - rand.Read(dataToEncrypt) - _, err := encscheme.Encrypt(dataToEncrypt) + read, err := rand.Read(dataToEncrypt) + require.Nil(t, err, "Error in reading random data", read) + + _, err = encscheme.Encrypt(dataToEncrypt) require.Nil(t, err) require.Equal(t, len(dataToEncrypt), fileref.CHUNK_SIZE) } diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 20de41bc9..c948a9a10 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -8,7 +8,6 @@ import ( "fmt" "github.com/0chain/gosdk/core/transaction" "io" - "io/ioutil" "math" "mime/multipart" "net/http" @@ -911,7 +910,7 @@ func (a *Allocation) GetCurrentVersion() (bool, error) { } if prevVersion > latestVersion { - prevVersion, latestVersion = latestVersion, prevVersion + prevVersion, latestVersion = latestVersion, prevVersion //nolint:ineffassign,staticcheck } // TODO: Check if allocation can be repaired @@ -2194,7 +2193,7 @@ func (a *Allocation) RevokeShare(path string, refereeClientID string) error { } defer resp.Body.Close() - respbody, err := ioutil.ReadAll(resp.Body) + respbody, err := io.ReadAll(resp.Body) if err != nil { l.Logger.Error("Error: Resp ", err) return err @@ -2358,7 +2357,7 @@ func (a *Allocation) UploadAuthTicketToBlobber(authTicket string, clientEncPubKe } defer resp.Body.Close() - respbody, err := ioutil.ReadAll(resp.Body) + respbody, err := io.ReadAll(resp.Body) if err != nil { l.Logger.Error("Error: Resp ", err) return err diff --git a/zboxcore/sdk/allocation_file_test.go b/zboxcore/sdk/allocation_file_test.go index ed3920133..d923ccd43 100644 --- a/zboxcore/sdk/allocation_file_test.go +++ b/zboxcore/sdk/allocation_file_test.go @@ -6,7 +6,6 @@ import ( "encoding/json" "github.com/0chain/gosdk/zboxcore/mocks" "io" - "io/ioutil" "net/http" "os" "strconv" @@ -132,7 +131,7 @@ func setupHttpResponses( } return http.StatusBadRequest }(), - Body: ioutil.NopCloser(bytes.NewReader([]byte(`{"status":2}`))), + Body: io.NopCloser(bytes.NewReader([]byte(`{"status":2}`))), }, nil) mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { @@ -147,7 +146,7 @@ func setupHttpResponses( }(), Body: func() io.ReadCloser { s := `{"meta_data":{"chunk_size":0,"created_at":0,"hash":"","lookup_hash":"","name":"/","num_of_blocks":0,"path":"/","path_hash":"","size":0,"type":"d","updated_at":0},"Ref":{"ID":0,"Type":"d","AllocationID":"` + allocID + `","LookupHash":"","Name":"/","Path":"/","Hash":"","NumBlocks":0,"PathHash":"","ParentPath":"","PathLevel":1,"CustomMeta":"","ContentHash":"","Size":0,"MerkleRoot":"","ActualFileSize":0,"ActualFileHash":"","MimeType":"","WriteMarker":"","ThumbnailSize":0,"ThumbnailHash":"","ActualThumbnailSize":0,"ActualThumbnailHash":"","EncryptedKey":"","Children":null,"OnCloud":false,"CreatedAt":0,"UpdatedAt":0,"ChunkSize":0},"list":[{"meta_data":{"chunk_size":0,"created_at":0,"hash":"","lookup_hash":"","name":"1.txt","num_of_blocks":0,"path":"/1.txt","path_hash":"","size":0,"type":"f","updated_at":0},"Ref":{"ID":0,"Type":"f","AllocationID":"` + allocID + `","LookupHash":"","Name":"1.txt","Path":"/1.txt","Hash":"","NumBlocks":0,"PathHash":"","ParentPath":"/","PathLevel":1,"CustomMeta":"","ContentHash":"","Size":0,"MerkleRoot":"","ActualFileSize":0,"ActualFileHash":"","MimeType":"","WriteMarker":"","ThumbnailSize":0,"ThumbnailHash":"","ActualThumbnailSize":0,"ActualThumbnailHash":"","EncryptedKey":"","Children":null,"OnCloud":false,"CreatedAt":0,"UpdatedAt":0,"ChunkSize":0}}],"latest_write_marker":null}` - return ioutil.NopCloser(bytes.NewReader([]byte(s))) + return io.NopCloser(bytes.NewReader([]byte(s))) }(), }, nil) @@ -163,7 +162,7 @@ func setupHttpResponses( }(), Body: func() io.ReadCloser { s := `{"latest_write_marker":null,"prev_write_marker":null}` - return ioutil.NopCloser(bytes.NewReader([]byte(s))) + return io.NopCloser(bytes.NewReader([]byte(s))) }(), }, nil) @@ -177,7 +176,7 @@ func setupHttpResponses( } return http.StatusBadRequest }(), - Body: ioutil.NopCloser(bytes.NewReader(nil)), + Body: io.NopCloser(bytes.NewReader(nil)), }, nil) mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { @@ -190,7 +189,7 @@ func setupHttpResponses( } return http.StatusBadRequest }(), - Body: ioutil.NopCloser(bytes.NewReader(nil)), + Body: io.NopCloser(bytes.NewReader(nil)), }, nil) mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { @@ -709,7 +708,7 @@ func TestAllocation_RepairFile(t *testing.T) { // }, // }) // require.NoError(t, err) - // return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + // return io.NopCloser(bytes.NewReader([]byte(jsonFR))) // }(frName, hash), // }, nil) // } @@ -736,7 +735,7 @@ func TestAllocation_RepairFile(t *testing.T) { }, }) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(frName, hash), } @@ -756,7 +755,7 @@ func TestAllocation_RepairFile(t *testing.T) { Hash: mockChunkHash, }) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(frName, hash), }, nil) @@ -767,7 +766,7 @@ func TestAllocation_RepairFile(t *testing.T) { StatusCode: http.StatusOK, Body: func() io.ReadCloser { s := `{"latest_write_marker":null,"prev_write_marker":null}` - return ioutil.NopCloser(bytes.NewReader([]byte(s))) + return io.NopCloser(bytes.NewReader([]byte(s))) }(), }, nil) @@ -776,7 +775,7 @@ func TestAllocation_RepairFile(t *testing.T) { return strings.HasPrefix(req.URL.String(), urlRollback) })).Return(&http.Response{ StatusCode: http.StatusOK, - Body: ioutil.NopCloser(bytes.NewReader(nil)), + Body: io.NopCloser(bytes.NewReader(nil)), }, nil) urlFilePath := "http://TestAllocation_RepairFile" + testName + mockBlobberUrl + strconv.Itoa(i) + "/v1/file/referencepath" @@ -794,7 +793,7 @@ func TestAllocation_RepairFile(t *testing.T) { LatestWM: nil, }) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(frName, hash), }, nil) @@ -806,7 +805,7 @@ func TestAllocation_RepairFile(t *testing.T) { Body: func(fileRefName, hash string) io.ReadCloser { jsonFR, err := json.Marshal(&ReferencePathResult{}) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(frName, hash), }, nil) @@ -821,7 +820,7 @@ func TestAllocation_RepairFile(t *testing.T) { Status: WMLockStatusOK, } respBuf, _ := json.Marshal(resp) - return ioutil.NopCloser(bytes.NewReader(respBuf)) + return io.NopCloser(bytes.NewReader(respBuf)) }(), }, nil) @@ -833,7 +832,7 @@ func TestAllocation_RepairFile(t *testing.T) { StatusCode: http.StatusOK, Body: func() io.ReadCloser { respBuf, _ := json.Marshal("connection_id") - return ioutil.NopCloser(bytes.NewReader(respBuf)) + return io.NopCloser(bytes.NewReader(respBuf)) }(), }, nil) } diff --git a/zboxcore/sdk/allocation_test.go b/zboxcore/sdk/allocation_test.go index f73a5a830..c1cfc4338 100644 --- a/zboxcore/sdk/allocation_test.go +++ b/zboxcore/sdk/allocation_test.go @@ -8,7 +8,6 @@ import ( "github.com/0chain/gosdk/zboxcore/mocks" "io" "io/fs" - "io/ioutil" "log" "net/http" "os" @@ -64,7 +63,7 @@ func setupMockGetFileMetaResponse( strings.HasPrefix(req.URL.String(), url) })).Return(&http.Response{ StatusCode: statusCode, - Body: ioutil.NopCloser(bytes.NewReader(body)), + Body: io.NopCloser(bytes.NewReader(body)), }, nil).Once() } } @@ -141,7 +140,7 @@ func setupMockWriteLockRequest(a *Allocation, mockClient *mocks.HttpClient) { func setupMockFile(t *testing.T, path string) (teardown func(t *testing.T)) { _, err := os.Create(path) require.Nil(t, err) - err = ioutil.WriteFile(path, []byte("mockActualHash"), os.ModePerm) + err = os.WriteFile(path, []byte("mockActualHash"), os.ModePerm) require.Nil(t, err) return func(t *testing.T) { os.Remove(path) @@ -159,7 +158,7 @@ func setupMockRollback(a *Allocation, mockClient *mocks.HttpClient) { StatusCode: http.StatusOK, Body: func() io.ReadCloser { s := `{"latest_write_marker":null,"prev_write_marker":null}` - return ioutil.NopCloser(bytes.NewReader([]byte(s))) + return io.NopCloser(bytes.NewReader([]byte(s))) }(), }, nil) @@ -169,13 +168,13 @@ func setupMockRollback(a *Allocation, mockClient *mocks.HttpClient) { return strings.Contains(req.URL.String(), newUrl) })).Return(&http.Response{ StatusCode: http.StatusOK, - Body: ioutil.NopCloser(bytes.NewReader(nil)), + Body: io.NopCloser(bytes.NewReader(nil)), }, nil) } } -func setupMockFileAndReferencePathResult(t *testing.T, allocationID, name string) (teardown func(t *testing.T)) { +func setupMockFileAndReferencePathResult(t *testing.T, allocationID, name string) (teardown func(t *testing.T)) { //nolint:unused var buf = []byte("mockActualHash") h := sha3.New256() f, _ := os.Create(name) @@ -422,7 +421,7 @@ func TestAllocation_GetBlobberStats(t *testing.T) { Tx: mockAllocationTxId, }) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), StatusCode: http.StatusOK, }, nil) @@ -525,7 +524,7 @@ func TestAllocation_isInitialized(t *testing.T) { // return strings.HasPrefix(req.URL.Path, "TestAllocation_CreateDir") // })).Return(&http.Response{ // StatusCode: http.StatusOK, -// Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), +// Body: io.NopCloser(bytes.NewReader([]byte(""))), // }, nil) // for i := 0; i < numBlobbers; i++ { @@ -574,7 +573,7 @@ func TestAllocation_RepairRequired(t *testing.T) { StatusCode: http.StatusOK, Body: func() io.ReadCloser { respString := `{"file_meta_hash":"` + mockActualHash + `"}` - return ioutil.NopCloser(bytes.NewReader([]byte(respString))) + return io.NopCloser(bytes.NewReader([]byte(respString))) }(), }, nil) } @@ -615,7 +614,7 @@ func TestAllocation_RepairRequired(t *testing.T) { StatusCode: http.StatusOK, Body: func(hash string) io.ReadCloser { respString := `{"file_meta_hash":"` + hash + `"}` - return ioutil.NopCloser(bytes.NewReader([]byte(respString))) + return io.NopCloser(bytes.NewReader([]byte(respString))) }(hash), }, nil) } @@ -638,7 +637,7 @@ func TestAllocation_RepairRequired(t *testing.T) { return strings.HasPrefix(req.URL.Path, url) })).Return(&http.Response{ StatusCode: http.StatusBadRequest, - Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + Body: io.NopCloser(bytes.NewReader([]byte(""))), }, nil) } return nil @@ -729,7 +728,7 @@ func TestAllocation_DownloadFileToFileHandler(t *testing.T) { ActualFileHash: mockActualHash, }) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), }, nil) } @@ -803,7 +802,7 @@ func TestAllocation_DownloadFile(t *testing.T) { ActualFileHash: mockActualHash, }) require.NoError(err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), }, nil) } @@ -953,7 +952,7 @@ func TestAllocation_downloadFile(t *testing.T) { ActualFileHash: mockActualHash, }) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), }, nil) } @@ -1167,7 +1166,7 @@ func TestAllocation_GetAuthTicketForShare(t *testing.T) { ValidationRoot: mockValidationRoot, }) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), } zboxutil.Client = &mockClient @@ -1221,7 +1220,7 @@ func TestAllocation_GetAuthTicket(t *testing.T) { ValidationRoot: "mock validation root", }) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), } for i := 0; i < numBlobbers; i++ { @@ -1253,7 +1252,7 @@ func TestAllocation_GetAuthTicket(t *testing.T) { ValidationRoot: "mock validation root", }) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), } for i := 0; i < numBlobbers; i++ { @@ -1305,7 +1304,7 @@ func TestAllocation_GetAuthTicket(t *testing.T) { ValidationRoot: "mock validation root", }) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), } for i := 0; i < numBlobbers; i++ { @@ -1346,7 +1345,7 @@ func TestAllocation_GetAuthTicket(t *testing.T) { ValidationRoot: "mock validation root", }) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), } for i := 0; i < numBlobbers; i++ { @@ -2369,7 +2368,7 @@ func setupMockGetFileInfoResponse(t *testing.T, mockClient *mocks.HttpClient) { ValidationRoot: "mock validation root", }) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), } for i := 0; i < numBlobbers; i++ { @@ -2414,7 +2413,7 @@ func getMockAuthTicket(t *testing.T) string { httpResponse := &http.Response{ StatusCode: http.StatusOK, Body: func() io.ReadCloser { - return ioutil.NopCloser(bytes.NewReader(jsonFR)) + return io.NopCloser(bytes.NewReader(jsonFR)) }(), } diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 5b57a12ba..6a0d46852 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -7,7 +7,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "mime/multipart" "net/http" "strconv" @@ -367,7 +366,7 @@ func (commitreq *CommitRequest) calculateHashRequest(ctx context.Context, paths if resp.StatusCode != http.StatusOK { l.Logger.Error("Calculate hash response : ", resp.StatusCode) } - resp_body, err := ioutil.ReadAll(resp.Body) + resp_body, err := io.ReadAll(resp.Body) if err != nil { l.Logger.Error("Calculate hash: Resp", err) return err diff --git a/zboxcore/sdk/common.go b/zboxcore/sdk/common.go index 61eb45a00..3c2c7bd84 100644 --- a/zboxcore/sdk/common.go +++ b/zboxcore/sdk/common.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "path" "strconv" @@ -36,7 +36,7 @@ func getObjectTreeFromBlobber(ctx context.Context, allocationID, allocationTx, s if resp.StatusCode != http.StatusOK { l.Logger.Error("Object tree response : ", resp.StatusCode) } - resp_body, err := ioutil.ReadAll(resp.Body) + resp_body, err := io.ReadAll(resp.Body) if err != nil { l.Logger.Error("Object tree: Resp", err) return err @@ -81,7 +81,7 @@ func getAllocationDataFromBlobber(blobber *blockchain.StorageNode, allocationId if resp.StatusCode != http.StatusOK { l.Logger.Error("Get allocation response : ", resp.StatusCode) } - resp_body, err := ioutil.ReadAll(resp.Body) + resp_body, err := io.ReadAll(resp.Body) if err != nil { l.Logger.Error("Get allocation: Resp", err) return err diff --git a/zboxcore/sdk/copyworker.go b/zboxcore/sdk/copyworker.go index 0b5e21b0b..2942740ad 100644 --- a/zboxcore/sdk/copyworker.go +++ b/zboxcore/sdk/copyworker.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "fmt" - "io/ioutil" + "io" "mime/multipart" "net/http" "strings" @@ -119,7 +119,7 @@ func (req *CopyRequest) copyBlobberObject( if resp.Body != nil { defer resp.Body.Close() } - respBody, err = ioutil.ReadAll(resp.Body) + respBody, err = io.ReadAll(resp.Body) if err != nil { logger.Logger.Error("Error: Resp ", err) return diff --git a/zboxcore/sdk/copyworker_test.go b/zboxcore/sdk/copyworker_test.go index 3baa183e1..5037b179c 100644 --- a/zboxcore/sdk/copyworker_test.go +++ b/zboxcore/sdk/copyworker_test.go @@ -6,7 +6,6 @@ import ( "encoding/json" "github.com/0chain/gosdk/zboxcore/mocks" "io" - "io/ioutil" "mime" "mime/multipart" "net/http" @@ -75,7 +74,7 @@ func TestCopyRequest_copyBlobberObject(t *testing.T) { return strings.HasPrefix(req.URL.Path, testName) })).Return(&http.Response{ StatusCode: http.StatusBadRequest, - Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + Body: io.NopCloser(bytes.NewReader([]byte(""))), }, nil) }, wantErr: true, @@ -101,7 +100,7 @@ func TestCopyRequest_copyBlobberObject(t *testing.T) { Body: func() io.ReadCloser { jsonFR, err := json.Marshal(p.referencePathToRetrieve) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), }, nil) @@ -112,7 +111,7 @@ func TestCopyRequest_copyBlobberObject(t *testing.T) { req.Header.Get("X-App-Client-Key") == mockClientKey })).Return(&http.Response{ StatusCode: http.StatusBadRequest, - Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + Body: io.NopCloser(bytes.NewReader([]byte(""))), }, nil) }, wantErr: true, @@ -148,7 +147,7 @@ func TestCopyRequest_copyBlobberObject(t *testing.T) { Body: func() io.ReadCloser { jsonFR, err := json.Marshal(p.referencePathToRetrieve) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), }, nil) @@ -167,7 +166,7 @@ func TestCopyRequest_copyBlobberObject(t *testing.T) { } expected, ok := p.requestFields[part.FormName()] require.True(t, ok) - actual, err := ioutil.ReadAll(part) + actual, err := io.ReadAll(part) require.NoError(t, err) require.EqualValues(t, expected, string(actual)) } @@ -183,7 +182,7 @@ func TestCopyRequest_copyBlobberObject(t *testing.T) { Body: func() io.ReadCloser { jsonFR, err := json.Marshal(p.referencePathToRetrieve) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), }, nil) }, @@ -275,7 +274,7 @@ func TestCopyRequest_ProcessCopy(t *testing.T) { }, }) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), }, nil) @@ -290,7 +289,7 @@ func TestCopyRequest_ProcessCopy(t *testing.T) { } return http.StatusBadRequest }(), - Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + Body: io.NopCloser(bytes.NewReader([]byte(""))), }, nil) mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { @@ -304,7 +303,7 @@ func TestCopyRequest_ProcessCopy(t *testing.T) { } return http.StatusBadRequest }(), - Body: ioutil.NopCloser(bytes.NewReader([]byte(`{"status":2}`))), + Body: io.NopCloser(bytes.NewReader([]byte(`{"status":2}`))), }, nil) mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { @@ -318,7 +317,7 @@ func TestCopyRequest_ProcessCopy(t *testing.T) { } return http.StatusBadRequest }(), - Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + Body: io.NopCloser(bytes.NewReader([]byte(""))), }, nil) mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index 601ec2137..162cd73a2 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "net/http" "net/url" "sync" @@ -117,7 +116,7 @@ func (req *DeleteRequest) deleteBlobberFile( return } - respBody, err = ioutil.ReadAll(resp.Body) + respBody, err = io.ReadAll(resp.Body) if err != nil { l.Logger.Error(blobber.Baseurl, "Response: ", string(respBody)) return diff --git a/zboxcore/sdk/deleteworker_test.go b/zboxcore/sdk/deleteworker_test.go index 7b0de8e5a..a0b517df1 100644 --- a/zboxcore/sdk/deleteworker_test.go +++ b/zboxcore/sdk/deleteworker_test.go @@ -6,7 +6,6 @@ import ( "encoding/json" "github.com/0chain/gosdk/zboxcore/mocks" "io" - "io/ioutil" "net/http" "strconv" "strings" @@ -81,7 +80,7 @@ func TestDeleteRequest_deleteBlobberFile(t *testing.T) { Body: func() io.ReadCloser { jsonFR, err := json.Marshal(p.referencePathToRetrieve) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), }, nil} } @@ -96,7 +95,7 @@ func TestDeleteRequest_deleteBlobberFile(t *testing.T) { for _, c := range mockClient.ExpectedCalls { c.ReturnArguments = mock.Arguments{&http.Response{ StatusCode: http.StatusBadRequest, - Body: ioutil.NopCloser(strings.NewReader("")), + Body: io.NopCloser(strings.NewReader("")), }, nil} } }) @@ -131,7 +130,7 @@ func TestDeleteRequest_deleteBlobberFile(t *testing.T) { Body: func() io.ReadCloser { jsonFR, err := json.Marshal(p.referencePathToRetrieve) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), }, nil) @@ -151,7 +150,7 @@ func TestDeleteRequest_deleteBlobberFile(t *testing.T) { Body: func() io.ReadCloser { jsonFR, err := json.Marshal(p.referencePathToRetrieve) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), }, nil) }, @@ -244,7 +243,7 @@ func TestDeleteRequest_ProcessDelete(t *testing.T) { }, }) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), }, nil) mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { @@ -257,7 +256,7 @@ func TestDeleteRequest_ProcessDelete(t *testing.T) { } return http.StatusBadRequest }(), - Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + Body: io.NopCloser(bytes.NewReader([]byte(""))), }, nil) } diff --git a/zboxcore/sdk/dirworker.go b/zboxcore/sdk/dirworker.go index 80aea731a..ac58eee3f 100644 --- a/zboxcore/sdk/dirworker.go +++ b/zboxcore/sdk/dirworker.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "fmt" - "io/ioutil" + "io" "mime/multipart" "net/http" "path" @@ -236,7 +236,7 @@ func (req *DirRequest) createDirInBlobber(blobber *blockchain.StorageNode, pos u return } - respBody, err = ioutil.ReadAll(resp.Body) + respBody, err = io.ReadAll(resp.Body) if err != nil { l.Logger.Error(err) return diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index b5f97e181..720e74396 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -9,7 +9,6 @@ import ( "fmt" "hash" "io" - "io/ioutil" "net/http" "os" "path/filepath" @@ -894,7 +893,7 @@ func (req *DownloadRequest) attemptSubmitReadMarker(blobber *blockchain.StorageN } func (req *DownloadRequest) handleReadMarkerError(resp *http.Response, blobber *blockchain.StorageNode, rm *marker.ReadMarker) error { - respBody, err := ioutil.ReadAll(resp.Body) + respBody, err := io.ReadAll(resp.Body) if err != nil { return err } diff --git a/zboxcore/sdk/filemetaworker_test.go b/zboxcore/sdk/filemetaworker_test.go index 395abb6c4..4695aebe4 100644 --- a/zboxcore/sdk/filemetaworker_test.go +++ b/zboxcore/sdk/filemetaworker_test.go @@ -6,7 +6,6 @@ import ( "encoding/json" "github.com/0chain/gosdk/zboxcore/mocks" "io" - "io/ioutil" "mime" "mime/multipart" "net/http" @@ -70,7 +69,7 @@ func TestListRequest_getFileMetaInfoFromBlobber(t *testing.T) { mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { return strings.HasPrefix(req.URL.Path, "Test_Http_Error") })).Return(&http.Response{ - Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + Body: io.NopCloser(bytes.NewReader([]byte(""))), StatusCode: p.respStatusCode, }, errors.New("", mockErrorMessage)) }, @@ -86,7 +85,7 @@ func TestListRequest_getFileMetaInfoFromBlobber(t *testing.T) { mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { return strings.HasPrefix(req.URL.Path, "Test_Badly_Formatted") })).Return(&http.Response{ - Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + Body: io.NopCloser(bytes.NewReader([]byte(""))), StatusCode: p.respStatusCode, }, nil) }, @@ -130,7 +129,7 @@ func TestListRequest_getFileMetaInfoFromBlobber(t *testing.T) { } expected, ok := p.requestFields[part.FormName()] require.True(t, ok) - actual, err := ioutil.ReadAll(part) + actual, err := io.ReadAll(part) require.NoError(t, err) require.EqualValues(t, expected, string(actual)) } @@ -147,7 +146,7 @@ func TestListRequest_getFileMetaInfoFromBlobber(t *testing.T) { Body: func(p parameters) io.ReadCloser { jsonFR, err := json.Marshal(p.fileRefToRetrieve) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(p), }, nil) }, @@ -229,7 +228,7 @@ func TestListRequest_getFileConsensusFromBlobbers(t *testing.T) { }, }) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(frName, hash), }, nil) } diff --git a/zboxcore/sdk/filerefsworker.go b/zboxcore/sdk/filerefsworker.go index 09d8a1b98..33d98f497 100644 --- a/zboxcore/sdk/filerefsworker.go +++ b/zboxcore/sdk/filerefsworker.go @@ -5,7 +5,7 @@ import ( "encoding/hex" "encoding/json" "fmt" - "io/ioutil" + "io" "math" "net/http" "sync" @@ -173,7 +173,7 @@ func (o *ObjectTreeRequest) getFileRefs(bUrl string, respChan chan *oTreeRespons return err } defer resp.Body.Close() - respBody, err := ioutil.ReadAll(resp.Body) + respBody, err := io.ReadAll(resp.Body) if err != nil { l.Logger.Error(err) return err @@ -328,7 +328,7 @@ func (r *RecentlyAddedRefRequest) getRecentlyAddedRefs(resp *RecentlyAddedRefRes return err } defer hResp.Body.Close() - body, err := ioutil.ReadAll(hResp.Body) + body, err := io.ReadAll(hResp.Body) if err != nil { l.Logger.Error(err) return err diff --git a/zboxcore/sdk/filestatsworker.go b/zboxcore/sdk/filestatsworker.go index 568d1cf49..4e86e6f40 100644 --- a/zboxcore/sdk/filestatsworker.go +++ b/zboxcore/sdk/filestatsworker.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "encoding/json" - "io/ioutil" + "io" "mime/multipart" "net/http" "strings" @@ -84,7 +84,7 @@ func (req *ListRequest) getFileStatsInfoFromBlobber(blobber *blockchain.StorageN return err } defer resp.Body.Close() - resp_body, err := ioutil.ReadAll(resp.Body) + resp_body, err := io.ReadAll(resp.Body) if err != nil { return errors.Wrap(err, "Error: Resp") } diff --git a/zboxcore/sdk/filestatsworker_test.go b/zboxcore/sdk/filestatsworker_test.go index a5981f389..9126d4626 100644 --- a/zboxcore/sdk/filestatsworker_test.go +++ b/zboxcore/sdk/filestatsworker_test.go @@ -7,7 +7,6 @@ import ( "github.com/0chain/gosdk/zboxcore/mocks" "io" - "io/ioutil" "mime" "mime/multipart" "net/http" @@ -71,7 +70,7 @@ func TestListRequest_getFileStatsInfoFromBlobber(t *testing.T) { mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { return strings.HasPrefix(req.URL.Path, "Test_Http_Error") })).Return(&http.Response{ - Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + Body: io.NopCloser(bytes.NewReader([]byte(""))), StatusCode: p.respStatusCode, }, errors.New("", mockErrorMessage)) }, @@ -87,7 +86,7 @@ func TestListRequest_getFileStatsInfoFromBlobber(t *testing.T) { mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { return strings.HasPrefix(req.URL.Path, "Test_Badly_Formatted") })).Return(&http.Response{ - Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + Body: io.NopCloser(bytes.NewReader([]byte(""))), StatusCode: p.respStatusCode, }, nil) }, @@ -122,7 +121,7 @@ func TestListRequest_getFileStatsInfoFromBlobber(t *testing.T) { require.NoError(t, err) expected, ok := p.requestFields[part.FormName()] require.True(t, ok) - actual, err := ioutil.ReadAll(part) + actual, err := io.ReadAll(part) require.NoError(t, err) require.EqualValues(t, expected, string(actual)) @@ -138,7 +137,7 @@ func TestListRequest_getFileStatsInfoFromBlobber(t *testing.T) { Body: func(p parameters) io.ReadCloser { jsonFR, err := json.Marshal(p.fileStatsHttpResp) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(p), }, nil).Once() }, @@ -202,7 +201,7 @@ func TestListRequest_getFileStatsFromBlobbers(t *testing.T) { Name: fileStatsName, }) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(frName), }, nil) } diff --git a/zboxcore/sdk/listworker.go b/zboxcore/sdk/listworker.go index 220974a7e..5bf6e6554 100644 --- a/zboxcore/sdk/listworker.go +++ b/zboxcore/sdk/listworker.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "math/rand" "net/http" "strings" @@ -139,7 +139,7 @@ func (req *ListRequest) getListInfoFromBlobber(blobber *blockchain.StorageNode, return err } defer resp.Body.Close() - resp_body, err := ioutil.ReadAll(resp.Body) + resp_body, err := io.ReadAll(resp.Body) if err != nil { return errors.Wrap(err, "Error: Resp") } diff --git a/zboxcore/sdk/listworker_test.go b/zboxcore/sdk/listworker_test.go index 0145304f1..01df9c576 100644 --- a/zboxcore/sdk/listworker_test.go +++ b/zboxcore/sdk/listworker_test.go @@ -7,7 +7,6 @@ import ( "fmt" "github.com/0chain/gosdk/zboxcore/mocks" "io" - "io/ioutil" "net/http" "strconv" "strings" @@ -79,7 +78,7 @@ func TestListRequest_getListInfoFromBlobber(t *testing.T) { mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { return strings.HasPrefix(req.URL.Path, name) })).Return(&http.Response{ - Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + Body: io.NopCloser(bytes.NewReader([]byte(""))), StatusCode: p.respStatusCode, }, errors.New("", mockErrorMessage)) }, @@ -96,7 +95,7 @@ func TestListRequest_getListInfoFromBlobber(t *testing.T) { mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { return strings.HasPrefix(req.URL.Path, name) })).Return(&http.Response{ - Body: ioutil.NopCloser(bytes.NewReader([]byte(mockErrorMessage))), + Body: io.NopCloser(bytes.NewReader([]byte(mockErrorMessage))), StatusCode: p.respStatusCode, }, nil) }, @@ -113,7 +112,7 @@ func TestListRequest_getListInfoFromBlobber(t *testing.T) { mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { return strings.HasPrefix(req.URL.Path, name) })).Return(&http.Response{ - Body: ioutil.NopCloser(bytes.NewReader([]byte("this is not json format"))), + Body: io.NopCloser(bytes.NewReader([]byte("this is not json format"))), StatusCode: p.respStatusCode, }, nil) }, @@ -163,7 +162,7 @@ func TestListRequest_getListInfoFromBlobber(t *testing.T) { Body: func(p parameters) io.ReadCloser { jsonFR, err := json.Marshal(p.ListResult) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(p), }, nil).Once() }, @@ -236,7 +235,7 @@ func TestListRequest_GetListFromBlobbers(t *testing.T) { }) fmt.Println("returned", string(jsonFR)) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), }, nil) } diff --git a/zboxcore/sdk/moveworker.go b/zboxcore/sdk/moveworker.go index a70c814ce..8eba03258 100644 --- a/zboxcore/sdk/moveworker.go +++ b/zboxcore/sdk/moveworker.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "fmt" - "io/ioutil" + "io" "mime/multipart" "net/http" "strings" @@ -116,7 +116,7 @@ func (req *MoveRequest) moveBlobberObject( if resp.Body != nil { defer resp.Body.Close() } - respBody, err = ioutil.ReadAll(resp.Body) + respBody, err = io.ReadAll(resp.Body) if err != nil { logger.Logger.Error("Error: Resp ", err) return diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 115cb8482..25d3534af 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "fmt" - "io/ioutil" + "io" "mime/multipart" "net/http" "sync" @@ -113,7 +113,7 @@ func (mo *MultiOperation) createConnectionObj(blobberIdx int) (err error) { defer resp.Body.Close() } var respBody []byte - respBody, err = ioutil.ReadAll(resp.Body) + respBody, err = io.ReadAll(resp.Body) if err != nil { logger.Logger.Error("Error: Resp ", err) return diff --git a/zboxcore/sdk/renameworker.go b/zboxcore/sdk/renameworker.go index 3260104fa..c23fc1ffc 100644 --- a/zboxcore/sdk/renameworker.go +++ b/zboxcore/sdk/renameworker.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "fmt" - "io/ioutil" + "io" "mime/multipart" "net/http" "path/filepath" @@ -113,7 +113,7 @@ func (req *RenameRequest) renameBlobberObject( defer resp.Body.Close() } var respBody []byte - respBody, err = ioutil.ReadAll(resp.Body) + respBody, err = io.ReadAll(resp.Body) if err != nil { logger.Logger.Error("Error: Resp ", err) return diff --git a/zboxcore/sdk/renameworker_test.go b/zboxcore/sdk/renameworker_test.go index b14eaab10..e40a9aacf 100644 --- a/zboxcore/sdk/renameworker_test.go +++ b/zboxcore/sdk/renameworker_test.go @@ -6,7 +6,6 @@ import ( "encoding/json" "github.com/0chain/gosdk/zboxcore/mocks" "io" - "io/ioutil" "mime" "mime/multipart" "net/http" @@ -80,7 +79,7 @@ func TestRenameRequest_renameBlobberObject(t *testing.T) { return strings.HasPrefix(req.URL.Path, testName) })).Return(&http.Response{ StatusCode: http.StatusBadRequest, - Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + Body: io.NopCloser(bytes.NewReader([]byte(""))), }, nil) }, wantErr: true, @@ -104,7 +103,7 @@ func TestRenameRequest_renameBlobberObject(t *testing.T) { StatusCode: http.StatusOK, Body: func() io.ReadCloser { jsonFR := `{"latest_write_marker":null,"prev_write_marker":null}` - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), }, nil) @@ -118,7 +117,7 @@ func TestRenameRequest_renameBlobberObject(t *testing.T) { Body: func() io.ReadCloser { jsonFR, err := json.Marshal(p.referencePathToRetrieve) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), }, nil) @@ -129,7 +128,7 @@ func TestRenameRequest_renameBlobberObject(t *testing.T) { req.Header.Get("X-App-Client-Key") == mockClientKey })).Return(&http.Response{ StatusCode: http.StatusBadRequest, - Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + Body: io.NopCloser(bytes.NewReader([]byte(""))), }, nil) }, wantErr: true, @@ -176,7 +175,7 @@ func TestRenameRequest_renameBlobberObject(t *testing.T) { Body: func() io.ReadCloser { jsonFR, err := json.Marshal(p.referencePathToRetrieve) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), }, nil) @@ -195,7 +194,7 @@ func TestRenameRequest_renameBlobberObject(t *testing.T) { } expected, ok := p.requestFields[part.FormName()] require.True(t, ok) - actual, err := ioutil.ReadAll(part) + actual, err := io.ReadAll(part) require.NoError(t, err) require.EqualValues(t, expected, string(actual)) } @@ -211,7 +210,7 @@ func TestRenameRequest_renameBlobberObject(t *testing.T) { Body: func() io.ReadCloser { jsonFR, err := json.Marshal(p.referencePathToRetrieve) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), }, nil) }, @@ -300,7 +299,7 @@ func TestRenameRequest_ProcessRename(t *testing.T) { }, }) require.NoError(t, err) - return ioutil.NopCloser(bytes.NewReader([]byte(jsonFR))) + return io.NopCloser(bytes.NewReader([]byte(jsonFR))) }(), }, nil) @@ -315,7 +314,7 @@ func TestRenameRequest_ProcessRename(t *testing.T) { } return http.StatusBadRequest }(), - Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + Body: io.NopCloser(bytes.NewReader([]byte(""))), }, nil) if i < numCorrect { @@ -330,7 +329,7 @@ func TestRenameRequest_ProcessRename(t *testing.T) { } return http.StatusBadRequest }(), - Body: ioutil.NopCloser(bytes.NewReader([]byte(`{"status":2}`))), + Body: io.NopCloser(bytes.NewReader([]byte(`{"status":2}`))), }, nil) mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { @@ -344,7 +343,7 @@ func TestRenameRequest_ProcessRename(t *testing.T) { } return http.StatusBadRequest }(), - Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + Body: io.NopCloser(bytes.NewReader([]byte(""))), }, nil) } diff --git a/zboxcore/sdk/rollback.go b/zboxcore/sdk/rollback.go index b5812a7ea..00d009371 100644 --- a/zboxcore/sdk/rollback.go +++ b/zboxcore/sdk/rollback.go @@ -160,9 +160,21 @@ func (rb *RollbackBlobber) processRollback(ctx context.Context, tx string) error return err } connID := zboxutil.NewConnectionId() - formWriter.WriteField("write_marker", string(wmData)) - formWriter.WriteField("connection_id", connID) - formWriter.Close() + + err = formWriter.WriteField("write_marker", string(wmData)) + if err != nil { + return err + } + + err = formWriter.WriteField("connection_id", connID) + if err != nil { + return err + } + + err = formWriter.Close() + if err != nil { + return err + } req, err := zboxutil.NewRollbackRequest(rb.blobber.Baseurl, wm.AllocationID, tx, body) if err != nil { @@ -248,7 +260,7 @@ func (rb *RollbackBlobber) processRollback(ctx context.Context, tx string) error } - return thrown.New("rolback_error", fmt.Sprint("Rollback failed")) + return thrown.New("rolback_error", "Rollback failed") } // CheckAllocStatus checks the status of the allocation diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index 7a5286a47..b3a44bca3 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -11,7 +11,7 @@ import ( "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/core/zcncrypto" "gopkg.in/natefinch/lumberjack.v2" - "io/ioutil" + "io" "math" "net/http" "strconv" @@ -53,7 +53,7 @@ type StatusCallback interface { var ( numBlockDownloads = 100 sdkInitialized = false - networkWorkerTimerInHours = 1 + networkWorkerTimerInHours = 1 //nolint:unused singleClientMode = false shouldVerifyHash = true ) @@ -1322,7 +1322,7 @@ func CommitToFabric(metaTxnData, fabricConfigJSON string) (string, error) { return err } defer resp.Body.Close() - respBody, err := ioutil.ReadAll(resp.Body) + respBody, err := io.ReadAll(resp.Body) if err != nil { return errors.Wrap(err, "Error reading response :") } diff --git a/zboxcore/sdk/sync.go b/zboxcore/sdk/sync.go index 0e40ba150..375f7f074 100644 --- a/zboxcore/sdk/sync.go +++ b/zboxcore/sdk/sync.go @@ -5,7 +5,6 @@ import ( "encoding/hex" "encoding/json" "io" - "io/ioutil" "log" "os" "path/filepath" @@ -323,7 +322,7 @@ func (a *Allocation) GetAllocationDiff(lastSyncCachePath string, localRootPath s if fileInfo.IsDir() { return lFdiff, errors.Wrap(err, "invalid file cache.") } - content, err := ioutil.ReadFile(lastSyncCachePath) + content, err := os.ReadFile(lastSyncCachePath) if err != nil { return lFdiff, errors.New("", "can't read cache file.") } @@ -389,7 +388,7 @@ func (a *Allocation) SaveRemoteSnapshot(pathToSave string, remoteExcludePath []s if err != nil { return errors.Wrap(err, "failed to convert JSON.") } - err = ioutil.WriteFile(pathToSave, by, 0644) + err = os.WriteFile(pathToSave, by, 0644) if err != nil { return errors.Wrap(err, "error saving file.") } diff --git a/zboxcore/sdk/writemarker_mutex_test.go b/zboxcore/sdk/writemarker_mutex_test.go index 8645810e4..c8a30fe61 100644 --- a/zboxcore/sdk/writemarker_mutex_test.go +++ b/zboxcore/sdk/writemarker_mutex_test.go @@ -5,7 +5,6 @@ import ( "context" "github.com/0chain/gosdk/zboxcore/mocks" "io" - "io/ioutil" "net/http" "strconv" "strings" @@ -50,7 +49,7 @@ func TestWriteMarkerMutext_Should_Lock(t *testing.T) { } return http.StatusBadRequest }(), - Body: ioutil.NopCloser(bytes.NewReader([]byte(`{"status":2}`))), + Body: io.NopCloser(bytes.NewReader([]byte(`{"status":2}`))), }, nil) mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { @@ -187,7 +186,7 @@ func TestWriteMarkerMutext_Too_Less_Blobbers_Response_Should_Not_Lock(t *testing } return http.StatusBadRequest }(), - Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), + Body: io.NopCloser(bytes.NewReader([]byte(""))), }, nil) mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool { diff --git a/zcnbridge/authorizer/proofBurnTicket.go b/zcnbridge/authorizer/proofBurnTicket.go index ca2c3ec9e..706687d64 100644 --- a/zcnbridge/authorizer/proofBurnTicket.go +++ b/zcnbridge/authorizer/proofBurnTicket.go @@ -4,9 +4,7 @@ import ( "encoding/json" "fmt" "github.com/0chain/gosdk/core/client" - - "github.com/0chain/gosdk/zcncore" - + "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/zcnbridge" @@ -74,7 +72,13 @@ func (pb *ProofOfBurn) Sign() (err error) { // SignWith0Chain can sign with the provided walletString func (pb *ProofOfBurn) SignWith0Chain(w *zcncrypto.Wallet) (err error) { hash := zcncrypto.Sha3Sum256(pb.UnsignedMessage()) - sig, err := zcncore.SignWith0Wallet(hash, w) + config, err := conf.GetClientConfig() + + if err != nil { + return errors.Wrap("signature_0chain", "failed to get client config", err) + } + + sig, err := w.Sign(hash, config.SignatureScheme) if err != nil { return errors.Wrap("signature_0chain", "failed to sign proof-of-burn ticket using walletString ID "+w.ClientID, err) } diff --git a/zcnbridge/authorizers_query.go b/zcnbridge/authorizers_query.go index ea8aeacaf..c1cd99943 100644 --- a/zcnbridge/authorizers_query.go +++ b/zcnbridge/authorizers_query.go @@ -3,7 +3,7 @@ package zcnbridge import ( "encoding/json" "fmt" - "io/ioutil" + "io" "math" "net/http" "strings" @@ -331,7 +331,7 @@ func readResponse(response *http.Response, err error) (res *authorizerResponse, Logger.Error("request response status", zap.Error(err)) } - body, er := ioutil.ReadAll(response.Body) + body, er := io.ReadAll(response.Body) log.Logger.Debug("response", zap.String("response", string(body))) defer response.Body.Close() diff --git a/zcnbridge/bridge_test.go b/zcnbridge/bridge_test.go index ee114fa22..ca2055597 100644 --- a/zcnbridge/bridge_test.go +++ b/zcnbridge/bridge_test.go @@ -54,7 +54,7 @@ const ( txnFee = 1 nonce = 1 - ethereumTxnID = "0x3b59971c2aa294739cd73912f0c5a7996aafb796238cf44408b0eb4af0fbac82" + ethereumTxnID = "0x3b59971c2aa294739cd73912f0c5a7996aafb796238cf44408b0eb4af0fbac82" //nolint:unused clientId = "d6e9b3222434faa043c683d1a939d6a0fa2818c4d56e794974d64a32005330d3" ) @@ -75,7 +75,7 @@ var ( }, } - zcnScSignatures = []*zcnsc.AuthorizerSignature{ + zcnScSignatures = []*zcnsc.AuthorizerSignature{ //nolint:unused { ID: "0x2ec8F26ccC678c9faF0Df20208aEE3AF776160CD", Signature: "0xEAe8229c0E457efBA1A1769e7F8c20110fF68E61", @@ -91,19 +91,19 @@ func (ecm *ethereumClientMock) Cleanup(callback func()) { callback() } -type transactionMock struct { +type transactionMock struct { //nolint:unused mock.TestingT } -func (tem *transactionMock) Cleanup(callback func()) { +func (tem *transactionMock) Cleanup(callback func()) { //nolint:unused callback() } -type transactionProviderMock struct { +type transactionProviderMock struct { //nolint:unused mock.TestingT } -func (tem *transactionProviderMock) Cleanup(callback func()) { +func (tem *transactionProviderMock) Cleanup(callback func()) { //nolint:unused callback() } @@ -270,7 +270,7 @@ func prepareEthereumClientGeneralMockCalls(ethereumClient *mock.Mock) { ethereumClient.On("SendTransaction", mock.Anything, mock.Anything).Return(nil) } -func prepareTransactionGeneralMockCalls(transaction *mock.Mock) { +func prepareTransactionGeneralMockCalls(transaction *mock.Mock) { //nolint:unused transaction.On("ExecuteSmartContract", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(zcnTxnID, nil) transaction.On("Verify", mock.Anything).Return(nil) } diff --git a/zcncore/get_data.go b/zcncore/get_data.go index cae2c7f69..b520a7de8 100644 --- a/zcncore/get_data.go +++ b/zcncore/get_data.go @@ -145,7 +145,7 @@ func (p Params) Query() string { return "?" + params.Encode() } -func withParams(uri string, params Params) string { +func withParams(uri string, params Params) string { //nolint:unused return uri + params.Query() } @@ -307,6 +307,10 @@ func GetUserLockedTotal(clientID string) (int64, error) { "client_id": clientID, }) + if err != nil { + return 0, errors.New("error while making rest api call: " + err.Error()) + } + err = json.Unmarshal([]byte(info), &result) if err != nil { return 0, errors.New("invalid json format: " + err.Error()) diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 8740e9c85..0d2d041b6 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -51,14 +51,14 @@ const ( ) // In percentage -const consensusThresh = 25 +const consensusThresh = 25 //nolint:unused const ( - defaultMinSubmit = int(10) - defaultMinConfirmation = int(10) - defaultConfirmationChainLength = int(3) - defaultTxnExpirationSeconds = 60 - defaultWaitSeconds = 3 * time.Second + defaultMinSubmit = int(10) //nolint:unused + defaultMinConfirmation = int(10) //nolint:unused + defaultConfirmationChainLength = int(3) //nolint:unused + defaultTxnExpirationSeconds = 60 //nolint:unused + defaultWaitSeconds = 3 * time.Second //nolint:unused ) const ( diff --git a/zcncore/wallet_callback.go b/zcncore/wallet_callback.go index 74447ecc8..054068fba 100644 --- a/zcncore/wallet_callback.go +++ b/zcncore/wallet_callback.go @@ -6,7 +6,7 @@ import ( "github.com/0chain/gosdk/core/common" ) -type walletCallback struct { +type walletCallback struct { //nolint:unused sync.WaitGroup success bool @@ -15,7 +15,7 @@ type walletCallback struct { err error } -func (cb *walletCallback) OnBalanceAvailable(status int, value int64, info string) { +func (cb *walletCallback) OnBalanceAvailable(status int, value int64, info string) { //nolint:unused defer cb.Done() if status == StatusSuccess { From 7d71307b393713c23bf9d54299dde64f4cafffd9 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Tue, 24 Sep 2024 11:01:12 +0530 Subject: [PATCH 126/319] fix no changes in connection --- go.mod | 2 +- go.sum | 4 ++-- zboxcore/sdk/copyworker.go | 2 +- zboxcore/sdk/moveworker.go | 2 +- zboxcore/sdk/renameworker.go | 3 ++- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 3aaf12743..4860ac31f 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/0chain/gosdk go 1.21 require ( - github.com/0chain/common v1.13.1-0.20240915144523-0e4d37ab39a1 + github.com/0chain/common v1.13.1-0.20240924052546-7261e0567e47 github.com/0chain/errors v1.0.3 github.com/Luzifer/go-openssl/v3 v3.1.0 github.com/btcsuite/btcd v0.23.4 diff --git a/go.sum b/go.sum index 3ea776362..37b298217 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/0chain/common v1.13.1-0.20240915144523-0e4d37ab39a1 h1:jHfgxejNdfybNzNvYc1K+lZQ6yDT2BGNXQvQBhB3ChA= -github.com/0chain/common v1.13.1-0.20240915144523-0e4d37ab39a1/go.mod h1:vHK9Q0/nz28PxmKsLSMLzEs5iK6QVgl1LxdDQdR7y6s= +github.com/0chain/common v1.13.1-0.20240924052546-7261e0567e47 h1:nAXvrfLrBlN+H65CcF6qroWAYu75Weu/TCLrQ93lbGA= +github.com/0chain/common v1.13.1-0.20240924052546-7261e0567e47/go.mod h1:vHK9Q0/nz28PxmKsLSMLzEs5iK6QVgl1LxdDQdR7y6s= github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM= github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= diff --git a/zboxcore/sdk/copyworker.go b/zboxcore/sdk/copyworker.go index 336ec5af2..5309c9085 100644 --- a/zboxcore/sdk/copyworker.go +++ b/zboxcore/sdk/copyworker.go @@ -455,7 +455,7 @@ func (co *CopyOperation) Process(allocObj *Allocation, connectionID string) ([]f cR.Consensus.consensusThresh, cR.Consensus.consensus)) } co.destLookupHash = cR.destLookupHash - return co.objectTreeRefs, cR.copyMask, nil + return co.objectTreeRefs, cR.copyMask, err } diff --git a/zboxcore/sdk/moveworker.go b/zboxcore/sdk/moveworker.go index 5fb24e124..29cb1e251 100644 --- a/zboxcore/sdk/moveworker.go +++ b/zboxcore/sdk/moveworker.go @@ -456,7 +456,7 @@ func (mo *MoveOperation) Process(allocObj *Allocation, connectionID string) ([]f } mo.destLookupHash = mR.destLookupHash mo.srcLookupHash = fileref.GetReferenceLookup(mR.allocationID, mR.remotefilepath) - return mo.objectTreeRefs, mR.moveMask, nil + return mo.objectTreeRefs, mR.moveMask, err } func (mo *MoveOperation) buildChange(refs []fileref.RefEntity, uid uuid.UUID) []allocationchange.AllocationChange { diff --git a/zboxcore/sdk/renameworker.go b/zboxcore/sdk/renameworker.go index bc1d6cc33..b2747cc62 100644 --- a/zboxcore/sdk/renameworker.go +++ b/zboxcore/sdk/renameworker.go @@ -229,6 +229,7 @@ func (req *RenameRequest) ProcessWithBlobbersV2() ([]fileref.RefEntity, error) { l.Logger.Error(err.Error()) return } + refEntity.Path = path.Join(path.Dir(req.remotefilepath), req.newName) objectTreeRefs[blobberIdx] = refEntity req.maskMU.Lock() versionMap[refEntity.AllocationRoot] += 1 @@ -459,7 +460,7 @@ func (ro *RenameOperation) Process(allocObj *Allocation, connectionID string) ([ ro.srcLookupHash = fileref.GetReferenceLookup(rR.allocationID, rR.remotefilepath) destPath := path.Join(path.Dir(rR.remotefilepath), rR.newName) ro.destLookupHash = fileref.GetReferenceLookup(rR.allocationID, destPath) - return ro.objectTreeRefs, rR.renameMask, nil + return ro.objectTreeRefs, rR.renameMask, err } func (ro *RenameOperation) buildChange(refs []fileref.RefEntity, uid uuid.UUID) []allocationchange.AllocationChange { From 0a915db58bce2aa49562ee2bfc08e3872bd1f1e7 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Tue, 24 Sep 2024 14:13:02 +0530 Subject: [PATCH 127/319] fix repair --- zboxcore/sdk/allocation.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 12b516df9..7181baacd 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -461,7 +461,7 @@ func (a *Allocation) dispatchWork(ctx context.Context) { }() case repairReq := <-a.repairChan: - l.Logger.Info(fmt.Sprintf("received a repair request for %v\n", repairReq.listDir.Path)) + l.Logger.Info(fmt.Sprintf("received a repair request for %v\n", repairReq.repairPath)) go repairReq.processRepair(ctx, a) } } From 616c4d241de9747ceaeb5cb988f0013b8860aae8 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Tue, 24 Sep 2024 16:44:03 +0530 Subject: [PATCH 128/319] fix eof check --- zboxcore/sdk/deleteworker.go | 2 +- zboxcore/sdk/repairworker.go | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index ebb75e1ba..72de623bd 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -485,7 +485,7 @@ func (deleteReq *DeleteRequest) processDeleteV2() ([]fileref.RefEntity, zboxutil } } if deleteReq.remotefilepath == "/" { - return objectTreeRefs, deleteReq.deleteMask, nil + return objectTreeRefs, deleteReq.deleteMask, errNoChange } pos = 0 deleteReq.consensus.Reset() diff --git a/zboxcore/sdk/repairworker.go b/zboxcore/sdk/repairworker.go index eee6b9101..daf616611 100644 --- a/zboxcore/sdk/repairworker.go +++ b/zboxcore/sdk/repairworker.go @@ -373,7 +373,7 @@ func (r *RepairRequest) iterateDirV2(ctx context.Context) { return } if toNextRef { - if srcEOF { + if !srcEOF { break } srcRef, srcEOF = <-srcChan @@ -396,16 +396,16 @@ func (r *RepairRequest) iterateDirV2(ctx context.Context) { } // check if both target and source are at EOF - if srcEOF && diff.tgtEOF { + if !srcEOF && !diff.tgtEOF { continue } // if target is at EOF, upload the src file - if diff.tgtEOF { + if !diff.tgtEOF { uploadMask = uploadMask.Or(diff.mask) continue } // if source is at EOF, delete the target file - if srcEOF { + if !srcEOF { delMask := diff.mask op := OperationRequest{ OperationType: constants.FileOperationDelete, @@ -447,6 +447,7 @@ func (r *RepairRequest) iterateDirV2(ctx context.Context) { ops = append(ops, op) } if uploadMask.CountOnes() > 0 { + l.Logger.Debug("Repair by upload: ", srcRef.Path) op := r.uploadFileOp(srcRef, uploadMask) ops = append(ops, op) } From 5244fe273be49e60c8a39103fc67cdf9cba19be7 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Tue, 24 Sep 2024 18:55:42 +0530 Subject: [PATCH 129/319] fix repair eof --- zboxcore/sdk/allocation.go | 12 ++++++++---- zboxcore/sdk/commitworker.go | 5 +++++ zboxcore/sdk/repairworker.go | 4 ++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 7181baacd..ba1c6bbe4 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -522,7 +522,9 @@ func (a *Allocation) RepairFile(file sys.File, remotepath string, statusCallback if Workdir != "" { idr = Workdir } - mask = mask.Not().And(zboxutil.NewUint128(1).Lsh(uint64(len(a.Blobbers))).Sub64(1)) + if a.StorageVersion == 0 { + mask = mask.Not().And(zboxutil.NewUint128(1).Lsh(uint64(len(a.Blobbers))).Sub64(1)) + } fileMeta := FileMeta{ ActualSize: ref.ActualFileSize, MimeType: ref.MimeType, @@ -1809,9 +1811,11 @@ func (a *Allocation) ListObjects(ctx context.Context, path, offsetPath, updatedD for { oRefs, err := a.GetRefs(path, continuationPath, updatedDate, offsetDate, fileType, refType, level, pageLimit, opts...) if err != nil { - sendObjectRef(ORef{ - Err: err, - }) + if !strings.Contains(err.Error(), "invalid_path") { + sendObjectRef(ORef{ + Err: err, + }) + } return } for _, ref := range oRefs.Refs { diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index e72eeffba..e2645faa4 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -415,6 +415,11 @@ func (commitReq *CommitRequestV2) processCommit() { } } + if commitReq.isRepair && len(paths) == 0 { + commitReq.result = SuccessCommitResult() + return + } + var ( pos uint64 mu = &sync.Mutex{} diff --git a/zboxcore/sdk/repairworker.go b/zboxcore/sdk/repairworker.go index daf616611..0c355261e 100644 --- a/zboxcore/sdk/repairworker.go +++ b/zboxcore/sdk/repairworker.go @@ -365,7 +365,7 @@ func (r *RepairRequest) iterateDirV2(ctx context.Context) { var ( toNextRef = true srcRef ORef - srcEOF bool + srcEOF = true ops []OperationRequest ) for { @@ -385,6 +385,7 @@ func (r *RepairRequest) iterateDirV2(ctx context.Context) { return } } + l.Logger.Debug("Checking file for the path :", srcRef.Path) toNextRef = true var ( uploadMask zboxutil.Uint128 @@ -447,7 +448,6 @@ func (r *RepairRequest) iterateDirV2(ctx context.Context) { ops = append(ops, op) } if uploadMask.CountOnes() > 0 { - l.Logger.Debug("Repair by upload: ", srcRef.Path) op := r.uploadFileOp(srcRef, uploadMask) ops = append(ops, op) } From 4edbc833e3829c3ab20a6f48a243c240c13ed005 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 25 Sep 2024 01:40:18 +0530 Subject: [PATCH 130/319] Empty commit From 3fcf0055db5cf0a98c8ba3fb3f9ebe7136b85563 Mon Sep 17 00:00:00 2001 From: Jayash Satolia <73050737+Jayashsatolia403@users.noreply.github.com> Date: Wed, 25 Sep 2024 01:49:02 +0530 Subject: [PATCH 131/319] Fix --- core/version/version.go | 2 +- zboxcore/sdk/deleteworker.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/version/version.go b/core/version/version.go index 656a0a4db..c6b1dd42b 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-114-g6fc99ddf" +const VERSIONSTR = "v1.17.9-114-g255d39c1" diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index dc9b61c54..1e64f0b67 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -102,8 +102,8 @@ func (req *DeleteRequest) deleteBlobberFile( return } if resp.StatusCode == http.StatusBadRequest { - body, err := ioutil.ReadAll(resp.Body) - if err!= nil { + body, err := io.ReadAll(resp.Body) + if err != nil { logger.Logger.Error("Failed to read response body", err) } From 66cf176c9d49bb176ea23aa2bdff3d93d4c2823a Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Wed, 25 Sep 2024 10:24:02 +0530 Subject: [PATCH 132/319] set path hash --- zboxcore/sdk/filestatsworker.go | 1 + 1 file changed, 1 insertion(+) diff --git a/zboxcore/sdk/filestatsworker.go b/zboxcore/sdk/filestatsworker.go index 568d1cf49..8c10d21f6 100644 --- a/zboxcore/sdk/filestatsworker.go +++ b/zboxcore/sdk/filestatsworker.go @@ -101,6 +101,7 @@ func (req *ListRequest) getFileStatsInfoFromBlobber(blobber *blockchain.StorageN } fileStats.BlobberID = blobber.ID fileStats.BlobberURL = blobber.Baseurl + fileStats.PathHash = req.remotefilepathhash return nil } return errors.New(resp.Status, s.String()) From 743428f750823065d83257fc0806f76a7d5a12c4 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Wed, 25 Sep 2024 12:52:34 +0530 Subject: [PATCH 133/319] fix update key --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 4860ac31f..0f13c511f 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/0chain/gosdk go 1.21 require ( - github.com/0chain/common v1.13.1-0.20240924052546-7261e0567e47 + github.com/0chain/common v1.13.1-0.20240925072115-9dbb21f7f83c github.com/0chain/errors v1.0.3 github.com/Luzifer/go-openssl/v3 v3.1.0 github.com/btcsuite/btcd v0.23.4 diff --git a/go.sum b/go.sum index 37b298217..464827c69 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/0chain/common v1.13.1-0.20240924052546-7261e0567e47 h1:nAXvrfLrBlN+H65CcF6qroWAYu75Weu/TCLrQ93lbGA= -github.com/0chain/common v1.13.1-0.20240924052546-7261e0567e47/go.mod h1:vHK9Q0/nz28PxmKsLSMLzEs5iK6QVgl1LxdDQdR7y6s= +github.com/0chain/common v1.13.1-0.20240925072115-9dbb21f7f83c h1:dZwBgPZLJxpkQZetGO5HF5h7vV0LbtpxzHhfIfMRYr8= +github.com/0chain/common v1.13.1-0.20240925072115-9dbb21f7f83c/go.mod h1:vHK9Q0/nz28PxmKsLSMLzEs5iK6QVgl1LxdDQdR7y6s= github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM= github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= From dab42761fbb3479a355289a555a6e0908fd03c4e Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Wed, 25 Sep 2024 19:16:04 +0530 Subject: [PATCH 134/319] add prev weight --- zboxcore/sdk/commitworker.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index e2645faa4..258ad5316 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -473,7 +473,7 @@ func (commitReq *CommitRequestV2) processCommit() { return } elapsedGetRefPath := time.Since(now) - + prevWeight := trie.Weight() for _, change := range commitReq.changes { if change == nil { continue @@ -499,7 +499,7 @@ func (commitReq *CommitRequestV2) processCommit() { wg.Add(1) go func(ind int) { defer wg.Done() - err = commitReq.commitBlobber(rootHash, rootWeight, blobberPos, blobber) + err = commitReq.commitBlobber(rootHash, rootWeight, prevWeight, blobberPos, blobber) if err != nil { l.Logger.Error("Error committing to blobber", err) errSlice[ind] = err @@ -528,9 +528,8 @@ func (commitReq *CommitRequestV2) processCommit() { commitReq.result = SuccessCommitResult() } -func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, changeIndex uint64, blobber *blockchain.StorageNode) (err error) { +func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, prevWeight, changeIndex uint64, blobber *blockchain.StorageNode) (err error) { hasher := sha256.New() - var prevChainSize int64 if blobber.LatestWM != nil { prevChainHash, err := hex.DecodeString(blobber.LatestWM.ChainHash) if err != nil { @@ -538,14 +537,13 @@ func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, changeInd return err } hasher.Write(prevChainHash) //nolint:errcheck - prevChainSize = numBlocks(blobber.LatestWM.ChainSize) } hasher.Write(rootHash) //nolint:errcheck chainHash := hex.EncodeToString(hasher.Sum(nil)) allocationRoot := hex.EncodeToString(rootHash) wm := &marker.WriteMarker{} wm.AllocationRoot = allocationRoot - wm.Size = (int64(rootWeight) - prevChainSize) * CHUNK_SIZE + wm.Size = (int64(rootWeight) - int64(prevWeight)) * CHUNK_SIZE wm.ChainHash = chainHash wm.ChainSize = int64(rootWeight) * CHUNK_SIZE if blobber.LatestWM != nil { From 1c3af845e1c35e16ff84c97691d695f8c3a0c62e Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Wed, 25 Sep 2024 19:17:51 +0530 Subject: [PATCH 135/319] add debug log for commit size --- zboxcore/sdk/commitworker.go | 1 + 1 file changed, 1 insertion(+) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 258ad5316..6cff05704 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -544,6 +544,7 @@ func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, prevWeigh wm := &marker.WriteMarker{} wm.AllocationRoot = allocationRoot wm.Size = (int64(rootWeight) - int64(prevWeight)) * CHUNK_SIZE + l.Logger.Debug("Committing to blobber."+blobber.Baseurl, " rootWeight ", rootWeight, " prevWeight ", prevWeight, " size ", wm.Size) wm.ChainHash = chainHash wm.ChainSize = int64(rootWeight) * CHUNK_SIZE if blobber.LatestWM != nil { From 7034f12ae444de0f306e2ac3ecc9a4851bd7d764 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 26 Sep 2024 02:58:02 +0530 Subject: [PATCH 136/319] Fix wasm initialisation --- core/client/set.go | 60 ++++++++++++- core/version/version.go | 2 +- wasmsdk/proxy.go | 44 +++++++++- wasmsdk/sdk.go | 49 ++++++----- wasmsdk/wallet.go | 67 -------------- zboxcore/sdk/allocation.go | 3 +- zboxcore/sdk/allocation_file_delete_test.go | 4 +- zboxcore/sdk/allocation_file_test.go | 2 +- zboxcore/sdk/allocation_test.go | 34 ++++---- zboxcore/sdk/blobber_operations.go | 14 +-- zboxcore/sdk/sdk.go | 97 +++++---------------- 11 files changed, 181 insertions(+), 195 deletions(-) delete mode 100644 wasmsdk/wallet.go diff --git a/core/client/set.go b/core/client/set.go index 1bea43d0e..e4676be6b 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -1,7 +1,10 @@ package client import ( + "context" + "encoding/json" "errors" + "github.com/0chain/gosdk/core/conf" "strings" "github.com/0chain/gosdk/constants" @@ -10,7 +13,8 @@ import ( ) var ( - client Client + client Client + sdkInitialized bool ) type SignFunc func(hash string) (string, error) @@ -189,3 +193,57 @@ func GetWallet() *zcncrypto.Wallet { func GetClient() *zcncrypto.Wallet { return client.wallet } + +// InitSDK Initialize the storage SDK +// +// - walletJSON: Client's wallet JSON +// - blockWorker: Block worker URL (block worker refers to 0DNS) +// - chainID: ID of the blokcchain network +// - signatureScheme: Signature scheme that will be used for signing transactions +// - preferredBlobbers: List of preferred blobbers to use when creating an allocation. This is usually configured by the client in the configuration files +// - nonce: Initial nonce value for the transactions +// - fee: Preferred value for the transaction fee, just the first value is taken +func InitSDK(walletJSON string, + blockWorker, chainID, signatureScheme string, + preferredBlobbers []string, + nonce int64, isSplitWallet bool, + fee ...uint64) error { + + wallet := zcncrypto.Wallet{} + err := json.Unmarshal([]byte(walletJSON), &wallet) + if err != nil { + return err + } + + SetWallet(wallet) + SetSignatureScheme(signatureScheme) + SetNonce(nonce) + if len(fee) > 0 { + SetTxnFee(fee[0]) + } + + err = Init(context.Background(), conf.Config{ + BlockWorker: blockWorker, + SignatureScheme: signatureScheme, + ChainID: chainID, + PreferredBlobbers: preferredBlobbers, + MaxTxnQuery: 5, + QuerySleepTime: 5, + MinSubmit: 10, + MinConfirmation: 10, + IsSplitWallet: isSplitWallet, + }) + if err != nil { + return err + } + SetSdkInitialized(true) + return nil +} + +func IsSDKInitialized() bool { + return sdkInitialized +} + +func SetSdkInitialized(val bool) { + sdkInitialized = val +} diff --git a/core/version/version.go b/core/version/version.go index c6b1dd42b..44c720322 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-114-g255d39c1" +const VERSIONSTR = "v1.17.9-115-g3fcf0055" diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index 579c9dc3d..f7dd1c4ac 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -7,6 +7,8 @@ import ( "encoding/json" "errors" "fmt" + "github.com/0chain/gosdk/core/zcncrypto" + "log" "os" "runtime/debug" "strconv" @@ -203,7 +205,6 @@ func main() { jsbridge.BindAsyncFuncs(sdk, map[string]interface{}{ //sdk "init": initSDKs, - "setWallet": setWallet, "getPublicEncryptionKey": zcncore.GetPublicEncryptionKey, "hideLogs": hideLogs, "showLogs": showLogs, @@ -463,7 +464,26 @@ func main() { registerZauthServer(zauthServer) } - setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemonic, isSplit) + wallet := zcncrypto.Wallet{ + ClientID: clientID, + ClientKey: clientKey, + PeerPublicKey: peerPublicKey, + Keys: []zcncrypto.KeyPair{ + { + PublicKey: publicKey, + PrivateKey: privateKey, + }, + }, + Mnemonic: mnemonic, + IsSplit: isSplit, + } + + // Convert the wallet struct to JSON + _, err = json.MarshalIndent(wallet, "", " ") + if err != nil { + log.Fatalf("Error converting wallet to JSON: %v", err) + } + hideLogs() debug.SetGCPercent(75) debug.SetMemoryLimit(1 * 1024 * 1024 * 1024) //1GB @@ -527,6 +547,24 @@ func UpdateWalletWithEventData(data *safejs.Value) error { } fmt.Println("update wallet with event data") - setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemonic, isSplit) + wallet := zcncrypto.Wallet{ + ClientID: clientID, + ClientKey: clientKey, + PeerPublicKey: peerPublicKey, + Keys: []zcncrypto.KeyPair{ + { + PublicKey: publicKey, + PrivateKey: privateKey, + }, + }, + Mnemonic: mnemonic, + IsSplit: isSplit, + } + + // Convert the wallet struct to JSON + _, err = json.MarshalIndent(wallet, "", " ") + if err != nil { + log.Fatalf("Error converting wallet to JSON: %v", err) + } return nil } diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index 55ec66641..bdeaca504 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -4,7 +4,6 @@ package main import ( - "context" "encoding/hex" "encoding/json" "fmt" @@ -13,10 +12,14 @@ import ( "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/imageutil" "github.com/0chain/gosdk/core/logger" + "github.com/0chain/gosdk/core/zcncrypto" + "github.com/0chain/gosdk/wasmsdk/jsbridge" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/0chain/gosdk/zcncore" + "io" "os" + "strconv" ) var CreateObjectURL func(buf []byte, mimeType string) string @@ -31,43 +34,45 @@ var CreateObjectURL func(buf []byte, mimeType string) string // - zboxHost is the url of the 0box service // - zboxAppType is the application type of the 0box service // - sharderconsensous is the number of sharders to reach consensus -func initSDKs(chainID, blockWorker, signatureScheme string, +func initSDKs(walletJson, chainID, blockWorker, signatureScheme string, minConfirmation, minSubmit, confirmationChainLength int, zboxHost, zboxAppType string, sharderconsensous int, isSplit bool) error { zboxApiClient.SetRequest(zboxHost, zboxAppType) - err := sdk.InitStorageSDK("{}", blockWorker, chainID, signatureScheme, nil, 0) + clientConf, err := conf.GetClientConfig() + if err != nil { + return err + } + err = client.InitSDK(walletJson, blockWorker, chainID, signatureScheme, nil, 0, !isSplit && clientConf.IsSplitWallet) if err != nil { fmt.Println("wasm: InitStorageSDK ", err) return err } - clientConf, err := conf.GetClientConfig() + wallet := zcncrypto.Wallet{} + err = json.Unmarshal([]byte(walletJson), &wallet) if err != nil { return err } - if !isSplit && clientConf.IsSplitWallet { - // split wallet should not be reset back, use the existing - isSplit = true + mode := os.Getenv("MODE") + zboxApiClient.SetWallet(wallet.ClientID, wallet.Keys[0].PrivateKey, wallet.ClientKey) + if mode == "" { // main thread, need to notify the web worker to update wallet + // notify the web worker to update wallet + if err := jsbridge.PostMessageToAllWorkers(jsbridge.MsgTypeUpdateWallet, map[string]string{ + "client_id": wallet.ClientID, + "client_key": wallet.ClientKey, + "peer_public_key": wallet.PeerPublicKey, + "public_key": wallet.ClientKey, + "private_key": wallet.Keys[0].PrivateKey, + "mnemonic": wallet.Mnemonic, + "is_split": strconv.FormatBool(wallet.IsSplit), + }); err != nil { + return err + } } - err = client.Init(context.Background(), conf.Config{ - BlockWorker: blockWorker, - SignatureScheme: signatureScheme, - ChainID: chainID, - MinConfirmation: minConfirmation, - MinSubmit: minSubmit, - ConfirmationChainLength: confirmationChainLength, - SharderConsensous: sharderconsensous, - IsSplitWallet: isSplit, - }) - - if err != nil { - fmt.Println("wasm: InitZCNSDK ", err) - return err - } sdk.SetWasm() return nil } diff --git a/wasmsdk/wallet.go b/wasmsdk/wallet.go deleted file mode 100644 index 33bc332d3..000000000 --- a/wasmsdk/wallet.go +++ /dev/null @@ -1,67 +0,0 @@ -//go:build js && wasm -// +build js,wasm - -package main - -import ( - "errors" - - "fmt" - "os" - "strconv" - - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/zcncrypto" - "github.com/0chain/gosdk/wasmsdk/jsbridge" -) - -func setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemonic string, isSplit bool) error { - if mnemonic == "" && !isSplit { - return errors.New("mnemonic is required") - } - mode := os.Getenv("MODE") - fmt.Println("gosdk setWallet, mode:", mode, "is split:", isSplit) - keys := []zcncrypto.KeyPair{ - { - PrivateKey: privateKey, - PublicKey: publicKey, - }, - } - - c := client.GetClient() - c.Mnemonic = mnemonic - c.ClientID = clientID - c.ClientKey = clientKey - c.PeerPublicKey = peerPublicKey - c.Keys = keys - c.IsSplit = isSplit - - w := &zcncrypto.Wallet{ - ClientID: clientID, - ClientKey: clientKey, - PeerPublicKey: peerPublicKey, - Mnemonic: mnemonic, - Keys: keys, - IsSplit: isSplit, - } - fmt.Println("set Wallet, is split:", isSplit) - client.SetWallet(*w) - - zboxApiClient.SetWallet(clientID, privateKey, publicKey) - if mode == "" { // main thread, need to notify the web worker to update wallet - // notify the web worker to update wallet - if err := jsbridge.PostMessageToAllWorkers(jsbridge.MsgTypeUpdateWallet, map[string]string{ - "client_id": clientID, - "client_key": clientKey, - "peer_public_key": peerPublicKey, - "public_key": publicKey, - "private_key": privateKey, - "mnemonic": mnemonic, - "is_split": strconv.FormatBool(isSplit), - }); err != nil { - return err - } - } - - return nil -} diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index c948a9a10..b32bd7602 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -6,6 +6,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/transaction" "io" "math" @@ -431,7 +432,7 @@ func (a *Allocation) InitAllocation() { } func (a *Allocation) isInitialized() bool { - return a.initialized && sdkInitialized + return a.initialized && client.IsSDKInitialized() } func (a *Allocation) startWorker(ctx context.Context) { diff --git a/zboxcore/sdk/allocation_file_delete_test.go b/zboxcore/sdk/allocation_file_delete_test.go index e9be2c349..b94b41fdb 100644 --- a/zboxcore/sdk/allocation_file_delete_test.go +++ b/zboxcore/sdk/allocation_file_delete_test.go @@ -52,7 +52,7 @@ func TestAllocation_DeleteFile(t *testing.T) { FileOptions: 63, } a.InitAllocation() - sdkInitialized = true + client.SetSdkInitialized(true) for i := 0; i < numBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ @@ -168,7 +168,7 @@ func TestAllocation_deleteFile(t *testing.T) { FileOptions: 63, } a.InitAllocation() - sdkInitialized = true + client.SetSdkInitialized(true) for i := 0; i < numBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ ID: tt.name + mockBlobberId + strconv.Itoa(i), diff --git a/zboxcore/sdk/allocation_file_test.go b/zboxcore/sdk/allocation_file_test.go index d923ccd43..14f55b5ec 100644 --- a/zboxcore/sdk/allocation_file_test.go +++ b/zboxcore/sdk/allocation_file_test.go @@ -896,7 +896,7 @@ func TestAllocation_RepairFile(t *testing.T) { a.downloadProgressMap = make(map[string]*DownloadRequest) a.mutex = &sync.Mutex{} a.initialized = true - sdkInitialized = true + client.SetSdkInitialized(true) for i := 0; i < tt.numBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ ID: mockBlobberId + strconv.Itoa(i), diff --git a/zboxcore/sdk/allocation_test.go b/zboxcore/sdk/allocation_test.go index c1cfc4338..cebf13f7e 100644 --- a/zboxcore/sdk/allocation_test.go +++ b/zboxcore/sdk/allocation_test.go @@ -216,7 +216,7 @@ func TestGetMinMaxWriteReadSuccess(t *testing.T) { ssc.ParityShards = 4 ssc.initialized = true - sdkInitialized = true + client.SetSdkInitialized(true) require.NotNil(t, ssc.BlobberDetails) t.Run("Success minR, minW", func(t *testing.T) { @@ -264,7 +264,7 @@ func TestGetMaxMinStorageCostSuccess(t *testing.T) { ssc.ParityShards = 2 ssc.initialized = true - sdkInitialized = true + client.SetSdkInitialized(true) t.Run("Storage cost", func(t *testing.T) { cost, err := ssc.GetMaxStorageCost(100 * GB) @@ -483,9 +483,9 @@ func TestAllocation_isInitialized(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - originalSDKInitialized := sdkInitialized - defer func() { sdkInitialized = originalSDKInitialized }() - sdkInitialized = tt.sdkInitialized + originalSDKInitialized := client.IsSDKInitialized() + defer func() { client.SetSdkInitialized(originalSDKInitialized) }() + client.SetSdkInitialized(tt.sdkInitialized) a := &Allocation{initialized: tt.allocationInitialized} got := a.isInitialized() require := require.New(t) @@ -658,7 +658,7 @@ func TestAllocation_RepairRequired(t *testing.T) { FileOptions: 63, } a.InitAllocation() - sdkInitialized = true + client.SetSdkInitialized(true) if tt.setup != nil { if teardown := tt.setup(t, tt.name, a); teardown != nil { defer teardown(t) @@ -972,7 +972,7 @@ func TestAllocation_downloadFile(t *testing.T) { a.downloadProgressMap = make(map[string]*DownloadRequest) a.mutex = &sync.Mutex{} a.initialized = true - sdkInitialized = true + client.SetSdkInitialized(true) setupMockAllocation(t, a) for i := 0; i < numBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ @@ -1025,7 +1025,7 @@ func TestAllocation_GetRefs(t *testing.T) { } testCaseName := "Test_Get_Refs_Returns_Slice_Of_Length_0_When_File_Not_Present" a.InitAllocation() - sdkInitialized = true + client.SetSdkInitialized(true) for i := 0; i < numBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ ID: testCaseName + mockBlobberId + strconv.Itoa(i), @@ -1124,7 +1124,7 @@ func TestAllocation_GetFileMeta(t *testing.T) { FileOptions: 63, } a.InitAllocation() - sdkInitialized = true + client.SetSdkInitialized(true) for i := 0; i < numBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ ID: tt.name + mockBlobberId + strconv.Itoa(i), @@ -1184,7 +1184,7 @@ func TestAllocation_GetAuthTicketForShare(t *testing.T) { for i := 0; i < numberBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{}) } - sdkInitialized = true + client.SetSdkInitialized(true) at, err := a.GetAuthTicketForShare("/1.txt", "1.txt", fileref.FILE, "") require.NotEmptyf(at, "unexpected empty auth ticket") require.NoErrorf(err, "unexpected error: %v", err) @@ -1391,7 +1391,7 @@ func TestAllocation_GetAuthTicket(t *testing.T) { FileOptions: 63, } a.InitAllocation() - sdkInitialized = true + client.SetSdkInitialized(true) for i := 0; i < numBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ @@ -1454,7 +1454,7 @@ func TestAllocation_CancelDownload(t *testing.T) { require := require.New(t) a := &Allocation{FileOptions: 63} a.InitAllocation() - sdkInitialized = true + client.SetSdkInitialized(true) if tt.setup != nil { if teardown := tt.setup(t, a); teardown != nil { defer teardown(t) @@ -1610,7 +1610,7 @@ func TestAllocation_ListDirFromAuthTicket(t *testing.T) { setupMockGetFileInfoResponse(t, &mockClient) a.InitAllocation() - sdkInitialized = true + client.SetSdkInitialized(true) if len(a.Blobbers) == 0 { for i := 0; i < numBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{}) @@ -1883,7 +1883,7 @@ func TestAllocation_listDir(t *testing.T) { tt.parameters.expectedResult.deleteMask = zboxutil.NewUint128(1).Lsh(uint64(a.DataShards + a.ParityShards)).Sub64(1) } a.InitAllocation() - sdkInitialized = true + client.SetSdkInitialized(true) for i := 0; i < numBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ ID: tt.name + mockBlobberId + strconv.Itoa(i), @@ -2012,7 +2012,7 @@ func TestAllocation_GetFileMetaFromAuthTicket(t *testing.T) { FileOptions: 63, } a.InitAllocation() - sdkInitialized = true + client.SetSdkInitialized(true) a.initialized = true require := require.New(t) @@ -2324,7 +2324,7 @@ func setupMockAllocation(t *testing.T, a *Allocation) { if a.DataShards != 0 { a.fullconsensus, a.consensusThreshold = a.getConsensuses() } - sdkInitialized = true + client.SetSdkInitialized(true) go func() { for { @@ -2393,7 +2393,7 @@ func getMockAuthTicket(t *testing.T) string { } a.InitAllocation() - sdkInitialized = true + client.SetSdkInitialized(true) for i := 0; i < numBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{ ID: strconv.Itoa(i), diff --git a/zboxcore/sdk/blobber_operations.go b/zboxcore/sdk/blobber_operations.go index c2714795b..31a5a05dc 100644 --- a/zboxcore/sdk/blobber_operations.go +++ b/zboxcore/sdk/blobber_operations.go @@ -44,7 +44,7 @@ func CreateAllocationForOwner( return "", 0, nil, errors.New("failed_get_allocation_blobbers", "failed to get blobbers for allocation: "+err.Error()) } - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", 0, nil, sdkNotInitialized } @@ -68,7 +68,7 @@ func CreateAllocationForOwner( // // returns the hash of the transaction, the nonce of the transaction and an error if any. func CreateFreeAllocation(marker string, value uint64) (string, int64, error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -120,7 +120,7 @@ func UpdateAllocation( return "", 0, errors.New("invalid_lock", "int64 overflow on lock value") } - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -158,7 +158,7 @@ func UpdateAllocation( // - value: value to lock // - fee: transaction fee func StakePoolLock(providerType ProviderType, providerID string, value, fee uint64) (hash string, nonce int64, err error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -208,7 +208,7 @@ func StakePoolLock(providerType ProviderType, providerID string, value, fee uint // - providerID: provider ID // - fee: transaction fee func StakePoolUnlock(providerType ProviderType, providerID string, fee uint64) (unstake int64, nonce int64, err error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return 0, 0, sdkNotInitialized } @@ -262,7 +262,7 @@ func StakePoolUnlock(providerType ProviderType, providerID string, fee uint64) ( // - tokens: number of tokens to lock // - fee: transaction fee func WritePoolLock(allocID string, tokens, fee uint64) (hash string, nonce int64, err error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -286,7 +286,7 @@ func WritePoolLock(allocID string, tokens, fee uint64) (hash string, nonce int64 // - allocID: allocation ID // - fee: transaction fee func WritePoolUnlock(allocID string, fee uint64) (hash string, nonce int64, err error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index b3a44bca3..b08927e52 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -1,15 +1,12 @@ package sdk import ( - "context" "encoding/base64" "encoding/json" "fmt" "github.com/0chain/common/core/currency" "github.com/0chain/errors" - "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/logger" - "github.com/0chain/gosdk/core/zcncrypto" "gopkg.in/natefinch/lumberjack.v2" "io" "math" @@ -52,7 +49,6 @@ type StatusCallback interface { var ( numBlockDownloads = 100 - sdkInitialized = false networkWorkerTimerInHours = 1 //nolint:unused singleClientMode = false shouldVerifyHash = true @@ -103,51 +99,6 @@ func GetLogger() *logger.Logger { return &l.Logger } -// InitStorageSDK Initialize the storage SDK -// -// - walletJSON: Client's wallet JSON -// - blockWorker: Block worker URL (block worker refers to 0DNS) -// - chainID: ID of the blokcchain network -// - signatureScheme: Signature scheme that will be used for signing transactions -// - preferredBlobbers: List of preferred blobbers to use when creating an allocation. This is usually configured by the client in the configuration files -// - nonce: Initial nonce value for the transactions -// - fee: Preferred value for the transaction fee, just the first value is taken -func InitStorageSDK(walletJSON string, - blockWorker, chainID, signatureScheme string, - preferredBlobbers []string, - nonce int64, - fee ...uint64) error { - - wallet := zcncrypto.Wallet{} - err := json.Unmarshal([]byte(walletJSON), &wallet) - if err != nil { - return err - } - - client.SetWallet(wallet) - client.SetSignatureScheme(signatureScheme) - client.SetNonce(nonce) - if len(fee) > 0 { - client.SetTxnFee(fee[0]) - } - - err = client.Init(context.Background(), conf.Config{ - BlockWorker: blockWorker, - SignatureScheme: signatureScheme, - ChainID: chainID, - PreferredBlobbers: preferredBlobbers, - MaxTxnQuery: 5, - QuerySleepTime: 5, - MinSubmit: 10, - MinConfirmation: 10, - }) - if err != nil { - return err - } - sdkInitialized = true - return nil -} - type BackPool struct { ID string `json:"id"` Balance common.Balance `json:"balance"` @@ -206,7 +157,7 @@ type StakePoolInfo struct { // - providerType: provider type // - providerID: provider ID func GetStakePoolInfo(providerType ProviderType, providerID string) (info *StakePoolInfo, err error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return nil, sdkNotInitialized } @@ -239,7 +190,7 @@ type StakePoolUserInfo struct { // - offset: offset // - limit: limit func GetStakePoolUserInfo(clientID string, offset, limit int) (info *StakePoolUserInfo, err error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return nil, sdkNotInitialized } if clientID == "" { @@ -299,7 +250,7 @@ type ChallengePoolInfo struct { // GetChallengePoolInfo retrieve challenge pool info for given allocation. // - allocID: allocation ID func GetChallengePoolInfo(allocID string) (info *ChallengePoolInfo, err error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return nil, sdkNotInitialized } @@ -323,7 +274,7 @@ func GetChallengePoolInfo(allocID string) (info *ChallengePoolInfo, err error) { // GetMptData retrieves mpt key data. func GetMptData(key string) ([]byte, error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return nil, sdkNotInitialized } @@ -518,7 +469,7 @@ func getBlobbersInternal(active, stakable bool, limit, offset int) (bs []*Blobbe // - active: if true then only active blobbers are returned // - stakable: if true then only stakable blobbers are returned func GetBlobbers(active, stakable bool) (bs []*Blobber, err error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return nil, sdkNotInitialized } @@ -552,7 +503,7 @@ func GetBlobbers(active, stakable bool) (bs []*Blobber, err error) { // GetBlobber retrieve blobber by id. // - blobberID: the id of blobber func GetBlobber(blobberID string) (blob *Blobber, err error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return nil, sdkNotInitialized } var b []byte @@ -577,7 +528,7 @@ func GetBlobber(blobberID string) (blob *Blobber, err error) { // GetValidator retrieve validator instance by id. // - validatorID: the id of validator func GetValidator(validatorID string) (validator *Validator, err error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return nil, sdkNotInitialized } var b []byte @@ -602,7 +553,7 @@ func GetValidator(validatorID string) (validator *Validator, err error) { // GetValidators returns list of validators. // - stakable: if true then only stakable validators are returned func GetValidators(stakable bool) (validators []*Validator, err error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return nil, sdkNotInitialized } var b []byte @@ -627,7 +578,7 @@ func GetValidators(stakable bool) (validators []*Validator, err error) { // GetClientEncryptedPublicKey - get the client's public key func GetClientEncryptedPublicKey() (string, error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", sdkNotInitialized } encScheme := encryption.NewEncryptionScheme() @@ -644,7 +595,7 @@ func GetClientEncryptedPublicKey() (string, error) { // // returns the allocation instance and error if any func GetAllocationFromAuthTicket(authTicket string) (*Allocation, error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return nil, sdkNotInitialized } sEnc, err := base64.StdEncoding.DecodeString(authTicket) @@ -665,7 +616,7 @@ func GetAllocationFromAuthTicket(authTicket string) (*Allocation, error) { // // returns the allocation instance and error if any func GetAllocation(allocationID string) (*Allocation, error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return nil, sdkNotInitialized } params := make(map[string]string) @@ -762,7 +713,7 @@ func getAllocationsInternal(clientID string, limit, offset int) ([]*Allocation, // // returns the list of allocations and error if any func GetAllocationsForClient(clientID string) ([]*Allocation, error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return nil, sdkNotInitialized } limit, offset := 20, 0 @@ -1005,7 +956,7 @@ func GetFreeAllocationBlobbers(request map[string]interface{}) ([]string, error) // // returns the hash of the transaction, the nonce of the transaction and an error if any. func AddFreeStorageAssigner(name, publicKey string, individualLimit, totalLimit float64) (string, int64, error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1031,7 +982,7 @@ func AddFreeStorageAssigner(name, publicKey string, individualLimit, totalLimit // // returns the hash of the transaction, the nonce of the transaction and an error if any. func FinalizeAllocation(allocID string) (hash string, nonce int64, err error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } var sn = transaction.SmartContractTxnData{ @@ -1048,7 +999,7 @@ func FinalizeAllocation(allocID string) (hash string, nonce int64, err error) { // // returns the hash of the transaction, the nonce of the transaction and an error if any. func CancelAllocation(allocID string) (hash string, nonce int64, err error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } var sn = transaction.SmartContractTxnData{ @@ -1074,7 +1025,7 @@ const ( // - providerId is the id of the provider. // - providerType` is the type of the provider, either 3 for `ProviderBlobber` or 4 for `ProviderValidator. func KillProvider(providerId string, providerType ProviderType) (string, int64, error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1100,7 +1051,7 @@ func KillProvider(providerId string, providerType ProviderType) (string, int64, // - providerId is the id of the provider. // - providerType` is the type of the provider, either 3 for `ProviderBlobber` or 4 for `ProviderValidator. func ShutdownProvider(providerType ProviderType, providerID string) (string, int64, error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1127,7 +1078,7 @@ func ShutdownProvider(providerType ProviderType, providerID string) (string, int // - providerId is the id of the provider. // - providerType is the type of the provider. func CollectRewards(providerId string, providerType ProviderType) (string, int64, error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1167,7 +1118,7 @@ func CollectRewards(providerId string, providerType ProviderType) (string, int64 // // returns the hash of the transaction, the nonce of the transaction and an error if any. func TransferAllocation(allocationId, newOwner, newOwnerPublicKey string) (string, int64, error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1200,7 +1151,7 @@ func TransferAllocation(allocationId, newOwner, newOwnerPublicKey string) (strin // UpdateBlobberSettings updates the settings of a blobber (txn: `storagesc.update_blobber_settings`) // - blob is the update blobber request inputs. func UpdateBlobberSettings(blob *UpdateBlobber) (resp string, nonce int64, err error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } var sn = transaction.SmartContractTxnData{ @@ -1214,7 +1165,7 @@ func UpdateBlobberSettings(blob *UpdateBlobber) (resp string, nonce int64, err e // UpdateValidatorSettings updates the settings of a validator (txn: `storagesc.update_validator_settings`) // - v is the update validator request inputs. func UpdateValidatorSettings(v *UpdateValidator) (resp string, nonce int64, err error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1229,7 +1180,7 @@ func UpdateValidatorSettings(v *UpdateValidator) (resp string, nonce int64, err // ResetBlobberStats resets the stats of a blobber (txn: `storagesc.reset_blobber_stats`) // - rbs is the reset blobber stats dto, contains the blobber id and its stats. func ResetBlobberStats(rbs *ResetBlobberStatsDto) (string, int64, error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1242,7 +1193,7 @@ func ResetBlobberStats(rbs *ResetBlobberStatsDto) (string, int64, error) { } func ResetAllocationStats(allocationId string) (string, int64, error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1274,7 +1225,7 @@ func storageSmartContractTxnValue(sn transaction.SmartContractTxnData, value uin } func CommitToFabric(metaTxnData, fabricConfigJSON string) (string, error) { - if !sdkInitialized { + if !client.IsSDKInitialized() { return "", sdkNotInitialized } var fabricConfig struct { From 0277b7ba84130b7c259ca634cd92815b2cecfa61 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 26 Sep 2024 03:03:37 +0530 Subject: [PATCH 137/319] Fix --- mobilesdk/sdk/sdk.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mobilesdk/sdk/sdk.go b/mobilesdk/sdk/sdk.go index c7a19f672..d0feff104 100644 --- a/mobilesdk/sdk/sdk.go +++ b/mobilesdk/sdk/sdk.go @@ -124,12 +124,12 @@ func InitStorageSDK(clientJson string, configJson string) (*StorageSDK, error) { l.Logger.Info(configObj.ChainID) l.Logger.Info(configObj.SignatureScheme) l.Logger.Info(configObj.PreferredBlobbers) - if err = sdk.InitStorageSDK(clientJson, + if err = client.InitSDK(clientJson, configObj.BlockWorker, configObj.ChainID, configObj.SignatureScheme, configObj.PreferredBlobbers, - 0); err != nil { + 0, false); err != nil { l.Logger.Error(err) return nil, err } From 163d4f7ee4811ede82ff6854ec100d4af223d0c5 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Thu, 26 Sep 2024 14:14:07 +0530 Subject: [PATCH 138/319] fix update key weight --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 0f13c511f..547e38ab5 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/0chain/gosdk go 1.21 require ( - github.com/0chain/common v1.13.1-0.20240925072115-9dbb21f7f83c + github.com/0chain/common v1.13.1-0.20240926083613-87cdd2286565 github.com/0chain/errors v1.0.3 github.com/Luzifer/go-openssl/v3 v3.1.0 github.com/btcsuite/btcd v0.23.4 diff --git a/go.sum b/go.sum index 464827c69..1d0132b9a 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/0chain/common v1.13.1-0.20240925072115-9dbb21f7f83c h1:dZwBgPZLJxpkQZetGO5HF5h7vV0LbtpxzHhfIfMRYr8= -github.com/0chain/common v1.13.1-0.20240925072115-9dbb21f7f83c/go.mod h1:vHK9Q0/nz28PxmKsLSMLzEs5iK6QVgl1LxdDQdR7y6s= +github.com/0chain/common v1.13.1-0.20240926083613-87cdd2286565 h1:9K3v8dtjX6g+DtGJ/XX9+ZoeuwV46rB3vzfjXoMR64g= +github.com/0chain/common v1.13.1-0.20240926083613-87cdd2286565/go.mod h1:vHK9Q0/nz28PxmKsLSMLzEs5iK6QVgl1LxdDQdR7y6s= github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM= github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= From 671aa1c9d255464024d914eb3ac8aa11e7fa22cd Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Thu, 26 Sep 2024 15:23:47 +0530 Subject: [PATCH 139/319] fix alloc root --- zboxcore/sdk/commitworker.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 6cff05704..35f10e7b5 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -19,6 +19,7 @@ import ( "github.com/0chain/common/core/util/wmpt" "github.com/0chain/errors" thrown "github.com/0chain/errors" + "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk/zboxcore/blockchain" "github.com/0chain/gosdk/zboxcore/client" @@ -540,9 +541,9 @@ func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, prevWeigh } hasher.Write(rootHash) //nolint:errcheck chainHash := hex.EncodeToString(hasher.Sum(nil)) - allocationRoot := hex.EncodeToString(rootHash) + fileMetaRoot := hex.EncodeToString(rootHash) wm := &marker.WriteMarker{} - wm.AllocationRoot = allocationRoot + wm.AllocationRoot = encryption.Hash(fileMetaRoot + req.allocationObj.ID) wm.Size = (int64(rootWeight) - int64(prevWeight)) * CHUNK_SIZE l.Logger.Debug("Committing to blobber."+blobber.Baseurl, " rootWeight ", rootWeight, " prevWeight ", prevWeight, " size ", wm.Size) wm.ChainHash = chainHash @@ -553,7 +554,7 @@ func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, prevWeigh wm.BlobberID = blobber.ID wm.Timestamp = req.timestamp wm.AllocationID = req.allocationObj.ID - wm.FileMetaRoot = allocationRoot + wm.FileMetaRoot = fileMetaRoot wm.ClientID = client.GetClientID() err = wm.Sign() if err != nil { @@ -572,7 +573,7 @@ func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, prevWeigh return err } blobber.LatestWM = wm - blobber.AllocationRoot = allocationRoot + blobber.AllocationRoot = wm.AllocationRoot return } From 9fac357bd76195e96c96f4600e1d4853cbeef677 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Thu, 26 Sep 2024 16:04:21 +0530 Subject: [PATCH 140/319] fix chain hash --- zboxcore/sdk/commitworker.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 35f10e7b5..d1028a715 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -539,13 +539,13 @@ func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, prevWeigh } hasher.Write(prevChainHash) //nolint:errcheck } - hasher.Write(rootHash) //nolint:errcheck - chainHash := hex.EncodeToString(hasher.Sum(nil)) fileMetaRoot := hex.EncodeToString(rootHash) wm := &marker.WriteMarker{} wm.AllocationRoot = encryption.Hash(fileMetaRoot + req.allocationObj.ID) wm.Size = (int64(rootWeight) - int64(prevWeight)) * CHUNK_SIZE - l.Logger.Debug("Committing to blobber."+blobber.Baseurl, " rootWeight ", rootWeight, " prevWeight ", prevWeight, " size ", wm.Size) + decodedAllocationRoot, _ := hex.DecodeString(wm.AllocationRoot) + hasher.Write(decodedAllocationRoot) //nolint:errcheck + chainHash := hex.EncodeToString(hasher.Sum(nil)) wm.ChainHash = chainHash wm.ChainSize = int64(rootWeight) * CHUNK_SIZE if blobber.LatestWM != nil { From a65b29b0d83688e65600395d20569987cf0f23f2 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Thu, 26 Sep 2024 16:39:16 +0530 Subject: [PATCH 141/319] add fix for verifying trie root --- zboxcore/sdk/commitworker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index d1028a715..86dc63eae 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -666,7 +666,7 @@ func getReferencePathV2(blobber *blockchain.StorageNode, allocationID, allocatio if trie.Weight() != uint64(chainBlocks) { return nil, errors.New("chain_length_mismatch", fmt.Sprintf("Expected chain length %d, got %d", chainBlocks, trie.Weight())) } - if hex.EncodeToString(trie.Root()) != lR.LatestWM.AllocationRoot { + if hex.EncodeToString(trie.Root()) != lR.LatestWM.FileMetaRoot { return nil, errors.New("allocation_root_mismatch", fmt.Sprintf("Expected allocation root %s, got %s", lR.LatestWM.AllocationRoot, hex.EncodeToString(trie.Root()))) } } From cdfa4556290ec484611def3aa4abf81a018c9bbd Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Thu, 26 Sep 2024 20:43:08 +0530 Subject: [PATCH 142/319] fix delete directory --- go.mod | 2 +- go.sum | 4 ++-- zboxcore/sdk/commitworker.go | 6 +++--- zboxcore/sdk/deleteworker.go | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 547e38ab5..4c6429a00 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/0chain/gosdk go 1.21 require ( - github.com/0chain/common v1.13.1-0.20240926083613-87cdd2286565 + github.com/0chain/common v1.13.1-0.20240926135437-61b88d39bbbd github.com/0chain/errors v1.0.3 github.com/Luzifer/go-openssl/v3 v3.1.0 github.com/btcsuite/btcd v0.23.4 diff --git a/go.sum b/go.sum index 1d0132b9a..309be727d 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/0chain/common v1.13.1-0.20240926083613-87cdd2286565 h1:9K3v8dtjX6g+DtGJ/XX9+ZoeuwV46rB3vzfjXoMR64g= -github.com/0chain/common v1.13.1-0.20240926083613-87cdd2286565/go.mod h1:vHK9Q0/nz28PxmKsLSMLzEs5iK6QVgl1LxdDQdR7y6s= +github.com/0chain/common v1.13.1-0.20240926135437-61b88d39bbbd h1:PDnzyJJA4rF2LBW+0OQJ5y9vIwsPYYjbbFwImvgiDdY= +github.com/0chain/common v1.13.1-0.20240926135437-61b88d39bbbd/go.mod h1:vHK9Q0/nz28PxmKsLSMLzEs5iK6QVgl1LxdDQdR7y6s= github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM= github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 86dc63eae..f16a37b6a 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -569,7 +569,7 @@ func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, prevWeigh err = submitWriteMarker(wmData, nil, blobber, req.connectionID, req.allocationObj.ID, req.allocationObj.Tx, req.allocationObj.StorageVersion) if err != nil { - l.Logger.Error("Error submitting writemarker", err) + l.Logger.Error("Error submitting writemarker ", err) return err } blobber.LatestWM = wm @@ -601,8 +601,8 @@ func getFormWritter(connectionID string, wmData, fileIDMetaData []byte, body *by func getReferencePathV2(blobber *blockchain.StorageNode, allocationID, allocationTx, sig string, paths []string, success *bool, mu *sync.Mutex) (*wmpt.WeightedMerkleTrie, error) { if len(paths) == 0 { var node wmpt.Node - if blobber.LatestWM != nil { - decodedRoot, _ := hex.DecodeString(blobber.LatestWM.AllocationRoot) + if blobber.LatestWM != nil && len(blobber.LatestWM.FileMetaRoot) > 0 { + decodedRoot, _ := hex.DecodeString(blobber.LatestWM.FileMetaRoot) node = wmpt.NewHashNode(decodedRoot, uint64(numBlocks(blobber.LatestWM.ChainSize))) } trie := wmpt.New(node, nil) diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index 72de623bd..6f9231023 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -661,7 +661,7 @@ func (dop *DeleteOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, chang } decodedKey, _ := hex.DecodeString(dop.lookupHash) err := trie.Update(decodedKey, nil, 0) - if err != nil { + if err != nil && err != wmpt.ErrNotFound { logger.Logger.Error("Error updating trie", err) return err } From 8afe385e68fdaa57fcaa3d2d68240e4099c27119 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Thu, 26 Sep 2024 22:42:54 +0530 Subject: [PATCH 143/319] fix mask pos --- zboxcore/sdk/commitworker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index f16a37b6a..9692d99c3 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -505,7 +505,7 @@ func (commitReq *CommitRequestV2) processCommit() { l.Logger.Error("Error committing to blobber", err) errSlice[ind] = err mu.Lock() - commitReq.commitMask = commitReq.commitMask.And(zboxutil.NewUint128(1).Lsh(pos).Not()) + commitReq.commitMask = commitReq.commitMask.And(zboxutil.NewUint128(1).Lsh(blobberPos).Not()) mu.Unlock() return } From 2480cab2daedc644dbde5a245ad93d935131639f Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Fri, 27 Sep 2024 08:30:20 +0200 Subject: [PATCH 144/319] fix: fixed bugs --- wasmsdk/auth_txn.go | 6 +++--- wasmsdk/proxy.go | 4 ++-- zcncore/zauth.go | 18 ++++++++++++------ 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/wasmsdk/auth_txn.go b/wasmsdk/auth_txn.go index 184b52018..e9c8d71c0 100644 --- a/wasmsdk/auth_txn.go +++ b/wasmsdk/auth_txn.go @@ -43,13 +43,13 @@ func registerZauthServer(serverAddr string) { } // zvaultNewWallet generates new split wallet -func zvaultNewWallet(serverAddr, token string) (string, error) { - return zcncore.CallZvaultNewWalletString(serverAddr, token, "") +func zvaultNewWallet(serverAddr, token string, roles []string) (string, error) { + return zcncore.CallZvaultNewWalletString(serverAddr, token, "", nil) } // zvaultNewSplit generates new split wallet from existing clientID func zvaultNewSplit(clientID, serverAddr, token string, roles []string) (string, error) { - return zcncore.CallZvaultNewWalletString(serverAddr, token, clientID) + return zcncore.CallZvaultNewWalletString(serverAddr, token, clientID, roles) } func zvaultStoreKey(serverAddr, token, privateKey string) (string, error) { diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index ab21d090a..1927d8d04 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -82,7 +82,7 @@ func main() { return "", fmt.Errorf("failed to sign with split key: %v", err) } - data, err := json.Marshal(zauth.AuthMessage{ + data, err := json.Marshal(zcncore.AuthMessage{ Hash: hash, Signature: sig, ClientID: client.GetClient().ClientID, @@ -379,7 +379,7 @@ func main() { return "", fmt.Errorf("failed to sign with split key: %v", err) } - data, err := json.Marshal(zauth.AuthMessage{ + data, err := json.Marshal(zcncore.AuthMessage{ Hash: hash, Signature: sig, ClientID: client.GetClient().ClientID, diff --git a/zcncore/zauth.go b/zcncore/zauth.go index b10cd7bc7..fdb240a80 100644 --- a/zcncore/zauth.go +++ b/zcncore/zauth.go @@ -164,14 +164,20 @@ func CallZvaultNewWalletString(serverAddr, token, clientID string, roles []strin endpoint = endpoint + "/" + clientID } - data, err := json.Marshal(newWalletRequest{ - Roles: roles, - }) - if err != nil { - return "", errors.Wrap(err, "failed to serialize request") + var body io.Reader + + if roles != nil { + data, err := json.Marshal(newWalletRequest{ + Roles: roles, + }) + if err != nil { + return "", errors.Wrap(err, "failed to serialize request") + } + + body = bytes.NewReader(data) } - req, err := http.NewRequest("POST", endpoint, bytes.NewReader(data)) + req, err := http.NewRequest("POST", endpoint, body) if err != nil { return "", errors.Wrap(err, "failed to create HTTP request") } From 91ab7c69bb5d68a2b60d44856fc91ec73029d664 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Fri, 27 Sep 2024 14:16:46 +0530 Subject: [PATCH 145/319] fix create existing dir --- zboxcore/sdk/dirworker.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/zboxcore/sdk/dirworker.go b/zboxcore/sdk/dirworker.go index 9c712bfa3..e05707f8f 100644 --- a/zboxcore/sdk/dirworker.go +++ b/zboxcore/sdk/dirworker.go @@ -310,13 +310,17 @@ func (dirOp *DirOperation) Process(allocObj *Allocation, connectionID string) ([ fullconsensus: dR.fullconsensus, } - _ = dR.ProcessWithBlobbers(allocObj) + existCount := dR.ProcessWithBlobbers(allocObj) dirOp.alreadyExists = dR.alreadyExists if !dR.isConsensusOk() { return nil, dR.dirMask, errors.New("consensus_not_met", "directory creation failed due to consensus not met") } - return refs, dR.dirMask, nil + var err error + if allocObj.StorageVersion == StorageV2 && existCount >= dR.consensusThresh { + err = errNoChange + } + return refs, dR.dirMask, err } From 70b692960bb4029beeabc8eba9b17f57c12c05cb Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Fri, 27 Sep 2024 16:11:49 +0530 Subject: [PATCH 146/319] fix new dir consensus --- zboxcore/sdk/dirworker.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zboxcore/sdk/dirworker.go b/zboxcore/sdk/dirworker.go index e05707f8f..6deee05ff 100644 --- a/zboxcore/sdk/dirworker.go +++ b/zboxcore/sdk/dirworker.go @@ -306,8 +306,8 @@ func (dirOp *DirOperation) Process(allocObj *Allocation, connectionID string) ([ } dR.Consensus = Consensus{ RWMutex: &sync.RWMutex{}, - consensusThresh: dR.consensusThresh, - fullconsensus: dR.fullconsensus, + consensusThresh: dirOp.consensusThresh, + fullconsensus: dirOp.fullconsensus, } existCount := dR.ProcessWithBlobbers(allocObj) From b513b977f5db0ed9088161ef081a7e18aaf04284 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 28 Sep 2024 00:02:10 +0530 Subject: [PATCH 147/319] Fix --- core/transaction/transaction_mobile.go | 82 -------------------------- core/version/version.go | 2 +- zcncore/zauth.go | 73 +++++++++-------------- 3 files changed, 28 insertions(+), 129 deletions(-) delete mode 100644 core/transaction/transaction_mobile.go diff --git a/core/transaction/transaction_mobile.go b/core/transaction/transaction_mobile.go deleted file mode 100644 index 3119429e5..000000000 --- a/core/transaction/transaction_mobile.go +++ /dev/null @@ -1,82 +0,0 @@ -//go:build mobile -// +build mobile - -package transaction - -import ( - "encoding/json" - "strconv" -) - -// Transaction represents entity that encapsulates the transaction related data and metadata. -type Transaction struct { - Hash string `json:"hash,omitempty"` - Version string `json:"version,omitempty"` - ClientID string `json:"client_id,omitempty"` - PublicKey string `json:"public_key,omitempty"` - ToClientID string `json:"to_client_id,omitempty"` - ChainID string `json:"chain_id,omitempty"` - TransactionData string `json:"transaction_data"` - Value string `json:"transaction_value"` - Signature string `json:"signature,omitempty"` - CreationDate int64 `json:"creation_date,omitempty"` - TransactionType int `json:"transaction_type"` - TransactionOutput string `json:"transaction_output,omitempty"` - TransactionFee string `json:"transaction_fee"` - TransactionNonce int64 `json:"transaction_nonce"` - OutputHash string `json:"txn_output_hash"` - Status int `json:"transaction_status"` -} - -// TransactionWrapper represents wrapper for mobile transaction entity. -type TransactionWrapper struct { - Hash string `json:"hash,omitempty"` - Version string `json:"version,omitempty"` - ClientID string `json:"client_id,omitempty"` - PublicKey string `json:"public_key,omitempty"` - ToClientID string `json:"to_client_id,omitempty"` - ChainID string `json:"chain_id,omitempty"` - TransactionData string `json:"transaction_data"` - Value uint64 `json:"transaction_value"` - Signature string `json:"signature,omitempty"` - CreationDate int64 `json:"creation_date,omitempty"` - TransactionType int `json:"transaction_type"` - TransactionOutput string `json:"transaction_output,omitempty"` - TransactionFee uint64 `json:"transaction_fee"` - TransactionNonce int64 `json:"transaction_nonce"` - OutputHash string `json:"txn_output_hash"` - Status int `json:"transaction_status"` -} - -func (t *Transaction) MarshalJSON() ([]byte, error) { - valueRaw, err := strconv.ParseUint(t.Value, 0, 64) - if err != nil { - return nil, err - } - - transactionFeeRaw, err := strconv.ParseUint(t.TransactionFee, 0, 64) - if err != nil { - return nil, err - } - - wrapper := TransactionWrapper{ - Hash: t.Hash, - Version: t.Version, - ClientID: t.ClientID, - PublicKey: t.PublicKey, - ToClientID: t.ToClientID, - ChainID: t.ChainID, - TransactionData: t.TransactionData, - Value: valueRaw, - Signature: t.Signature, - CreationDate: t.CreationDate, - TransactionType: t.TransactionType, - TransactionOutput: t.TransactionOutput, - TransactionFee: transactionFeeRaw, - TransactionNonce: t.TransactionNonce, - OutputHash: t.OutputHash, - Status: t.Status, - } - - return json.Marshal(wrapper) -} diff --git a/core/version/version.go b/core/version/version.go index 44c720322..65b4e7073 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-115-g3fcf0055" +const VERSIONSTR = "v1.17.9-172-gf945a840" diff --git a/zcncore/zauth.go b/zcncore/zauth.go index c88f83ea6..3aef3119f 100644 --- a/zcncore/zauth.go +++ b/zcncore/zauth.go @@ -15,13 +15,14 @@ import ( // SplitWallet represents wallet info for split wallet // The client id and client key are the same as the primary wallet client id and client key type SplitWallet struct { - ClientID string `json:"client_id"` - ClientKey string `json:"client_key"` - PublicKey string `json:"public_key"` - PrivateKey string `json:"private_key"` - PeerPublicKey string `json:"peer_public_key"` - IsRevoked bool `json:"is_revoked"` - ExpiredAt int64 `json:"expired_at"` + ClientID string `json:"client_id"` + ClientKey string `json:"client_key"` + PublicKey string `json:"public_key"` + PrivateKey string `json:"private_key"` + PeerPublicKey string `json:"peer_public_key"` + Roles []string `json:"roles"` + IsRevoked bool `json:"is_revoked"` + ExpiredAt int64 `json:"expired_at"` } // CallZauthSetup calls the zauth setup endpoint @@ -152,14 +153,31 @@ func CallZauthDelete(serverAddr, token, clientID string) error { return nil } -func CallZvaultNewWalletString(serverAddr, token, clientID string) (string, error) { +type newWalletRequest struct { + Roles []string `json:"roles"` +} + +func CallZvaultNewWalletString(serverAddr, token, clientID string, roles []string) (string, error) { // Add your code here endpoint := serverAddr + "/generate" if clientID != "" { endpoint = endpoint + "/" + clientID } - req, err := http.NewRequest("POST", endpoint, nil) + var body io.Reader + + if roles != nil { + data, err := json.Marshal(newWalletRequest{ + Roles: roles, + }) + if err != nil { + return "", errors.Wrap(err, "failed to serialize request") + } + + body = bytes.NewReader(data) + } + + req, err := http.NewRequest("POST", endpoint, body) if err != nil { return "", errors.Wrap(err, "failed to create HTTP request") } @@ -452,7 +470,6 @@ func ZauthSignTxn(serverAddr string) sys.AuthorizeFunc { func ZauthAuthCommon(serverAddr string) sys.AuthorizeFunc { return func(msg string) (string, error) { - // return func(msg string) (string, error) { req, err := http.NewRequest("POST", serverAddr+"/sign/msg", bytes.NewBuffer([]byte(msg))) if err != nil { return "", errors.Wrap(err, "failed to create HTTP request") @@ -496,39 +513,3 @@ type AuthMessage struct { type AuthResponse struct { Sig string `json:"sig"` } - -func ZauthSignMsg(serverAddr string) sys.SignFunc { - return func(hash string, signatureScheme string, keys []sys.KeyPair) (string, error) { - sig, err := SignWithKey(keys[0].PrivateKey, hash) - if err != nil { - return "", err - } - - data, err := json.Marshal(AuthMessage{ - Hash: hash, - Signature: sig, - ClientID: client.GetClient().ClientID, - }) - if err != nil { - return "", err - } - - // fmt.Println("auth - sys.AuthCommon:", sys.AuthCommon) - if sys.AuthCommon == nil { - return "", errors.New("authCommon is not set") - } - - rsp, err := sys.AuthCommon(string(data)) - if err != nil { - return "", err - } - - var ar AuthResponse - err = json.Unmarshal([]byte(rsp), &ar) - if err != nil { - return "", err - } - - return AddSignature(client.PrivateKey(), ar.Sig, hash) - } -} From 4561413d8135c45ac774d295e86358bc4cdbc445 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 28 Sep 2024 00:03:15 +0530 Subject: [PATCH 148/319] Fix --- core/transaction/transaction.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/core/transaction/transaction.go b/core/transaction/transaction.go index 2040630cf..985e2f37c 100644 --- a/core/transaction/transaction.go +++ b/core/transaction/transaction.go @@ -1,6 +1,3 @@ -//go:build !mobile -// +build !mobile - package transaction // Transaction entity that encapsulates the transaction related data and meta data From fe4aad2aaa64267bbbddbfa2307ec54f93cdc5ed Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 28 Sep 2024 01:37:41 +0530 Subject: [PATCH 149/319] Fix wasm --- core/client/set.go | 26 ++++++++-------- core/version/version.go | 2 +- mobilesdk/sdk/sdk.go | 2 +- wasmsdk/sdk.go | 35 ++------------------- wasmsdk/wallet.go | 67 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 47 deletions(-) create mode 100644 wasmsdk/wallet.go diff --git a/core/client/set.go b/core/client/set.go index e4676be6b..06685ee45 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -206,23 +206,25 @@ func GetClient() *zcncrypto.Wallet { func InitSDK(walletJSON string, blockWorker, chainID, signatureScheme string, preferredBlobbers []string, - nonce int64, isSplitWallet bool, + nonce int64, isSplitWallet, addWallet bool, fee ...uint64) error { - wallet := zcncrypto.Wallet{} - err := json.Unmarshal([]byte(walletJSON), &wallet) - if err != nil { - return err - } + if addWallet { + wallet := zcncrypto.Wallet{} + err := json.Unmarshal([]byte(walletJSON), &wallet) + if err != nil { + return err + } - SetWallet(wallet) - SetSignatureScheme(signatureScheme) - SetNonce(nonce) - if len(fee) > 0 { - SetTxnFee(fee[0]) + SetWallet(wallet) + SetSignatureScheme(signatureScheme) + SetNonce(nonce) + if len(fee) > 0 { + SetTxnFee(fee[0]) + } } - err = Init(context.Background(), conf.Config{ + err := Init(context.Background(), conf.Config{ BlockWorker: blockWorker, SignatureScheme: signatureScheme, ChainID: chainID, diff --git a/core/version/version.go b/core/version/version.go index 65b4e7073..36df616cf 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-172-gf945a840" +const VERSIONSTR = "v1.17.9-174-g4561413d" diff --git a/mobilesdk/sdk/sdk.go b/mobilesdk/sdk/sdk.go index d0feff104..9cd499f37 100644 --- a/mobilesdk/sdk/sdk.go +++ b/mobilesdk/sdk/sdk.go @@ -129,7 +129,7 @@ func InitStorageSDK(clientJson string, configJson string) (*StorageSDK, error) { configObj.ChainID, configObj.SignatureScheme, configObj.PreferredBlobbers, - 0, false); err != nil { + 0, false, true); err != nil { l.Logger.Error(err) return nil, err } diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index bdeaca504..ff91ed8dc 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -8,18 +8,14 @@ import ( "encoding/json" "fmt" "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/imageutil" "github.com/0chain/gosdk/core/logger" - "github.com/0chain/gosdk/core/zcncrypto" - "github.com/0chain/gosdk/wasmsdk/jsbridge" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/0chain/gosdk/zcncore" "io" "os" - "strconv" ) var CreateObjectURL func(buf []byte, mimeType string) string @@ -34,45 +30,18 @@ var CreateObjectURL func(buf []byte, mimeType string) string // - zboxHost is the url of the 0box service // - zboxAppType is the application type of the 0box service // - sharderconsensous is the number of sharders to reach consensus -func initSDKs(walletJson, chainID, blockWorker, signatureScheme string, +func initSDKs(chainID, blockWorker, signatureScheme string, minConfirmation, minSubmit, confirmationChainLength int, zboxHost, zboxAppType string, sharderconsensous int, isSplit bool) error { zboxApiClient.SetRequest(zboxHost, zboxAppType) - clientConf, err := conf.GetClientConfig() - if err != nil { - return err - } - err = client.InitSDK(walletJson, blockWorker, chainID, signatureScheme, nil, 0, !isSplit && clientConf.IsSplitWallet) + err := client.InitSDK("{}", blockWorker, chainID, signatureScheme, nil, 0, false, false) if err != nil { fmt.Println("wasm: InitStorageSDK ", err) return err } - wallet := zcncrypto.Wallet{} - err = json.Unmarshal([]byte(walletJson), &wallet) - if err != nil { - return err - } - - mode := os.Getenv("MODE") - zboxApiClient.SetWallet(wallet.ClientID, wallet.Keys[0].PrivateKey, wallet.ClientKey) - if mode == "" { // main thread, need to notify the web worker to update wallet - // notify the web worker to update wallet - if err := jsbridge.PostMessageToAllWorkers(jsbridge.MsgTypeUpdateWallet, map[string]string{ - "client_id": wallet.ClientID, - "client_key": wallet.ClientKey, - "peer_public_key": wallet.PeerPublicKey, - "public_key": wallet.ClientKey, - "private_key": wallet.Keys[0].PrivateKey, - "mnemonic": wallet.Mnemonic, - "is_split": strconv.FormatBool(wallet.IsSplit), - }); err != nil { - return err - } - } - sdk.SetWasm() return nil } diff --git a/wasmsdk/wallet.go b/wasmsdk/wallet.go new file mode 100644 index 000000000..33bc332d3 --- /dev/null +++ b/wasmsdk/wallet.go @@ -0,0 +1,67 @@ +//go:build js && wasm +// +build js,wasm + +package main + +import ( + "errors" + + "fmt" + "os" + "strconv" + + "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/zcncrypto" + "github.com/0chain/gosdk/wasmsdk/jsbridge" +) + +func setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemonic string, isSplit bool) error { + if mnemonic == "" && !isSplit { + return errors.New("mnemonic is required") + } + mode := os.Getenv("MODE") + fmt.Println("gosdk setWallet, mode:", mode, "is split:", isSplit) + keys := []zcncrypto.KeyPair{ + { + PrivateKey: privateKey, + PublicKey: publicKey, + }, + } + + c := client.GetClient() + c.Mnemonic = mnemonic + c.ClientID = clientID + c.ClientKey = clientKey + c.PeerPublicKey = peerPublicKey + c.Keys = keys + c.IsSplit = isSplit + + w := &zcncrypto.Wallet{ + ClientID: clientID, + ClientKey: clientKey, + PeerPublicKey: peerPublicKey, + Mnemonic: mnemonic, + Keys: keys, + IsSplit: isSplit, + } + fmt.Println("set Wallet, is split:", isSplit) + client.SetWallet(*w) + + zboxApiClient.SetWallet(clientID, privateKey, publicKey) + if mode == "" { // main thread, need to notify the web worker to update wallet + // notify the web worker to update wallet + if err := jsbridge.PostMessageToAllWorkers(jsbridge.MsgTypeUpdateWallet, map[string]string{ + "client_id": clientID, + "client_key": clientKey, + "peer_public_key": peerPublicKey, + "public_key": publicKey, + "private_key": privateKey, + "mnemonic": mnemonic, + "is_split": strconv.FormatBool(isSplit), + }); err != nil { + return err + } + } + + return nil +} From 777f66650a404885d76db057f944e5a6043bf26a Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 28 Sep 2024 20:45:42 +0530 Subject: [PATCH 150/319] Cleanup --- zcnbridge/transaction/functions.go | 59 ------------------------------ 1 file changed, 59 deletions(-) delete mode 100644 zcnbridge/transaction/functions.go diff --git a/zcnbridge/transaction/functions.go b/zcnbridge/transaction/functions.go deleted file mode 100644 index e2716f33d..000000000 --- a/zcnbridge/transaction/functions.go +++ /dev/null @@ -1,59 +0,0 @@ -package transaction - -// ZCNSC smart contract functions wrappers - -//JAYASHTODO : Uncomment if possible or rewrite - -//// AddAuthorizer adds authorizer to the bridge -//// - ctx is the context of the request. -//// - input is the payload of the request. -//func AddAuthorizer(ctx context.Context, input *zcncore.AddAuthorizerPayload) (Transaction, error) { -// t, err := NewTransactionEntity(0) -// if err != nil { -// return nil, err -// } -// -// scheme := t.GetScheme() -// -// err = scheme.ZCNSCAddAuthorizer(input) -// if err != nil { -// return t, err -// } -// -// callBack := t.GetCallback() -// -// err = callBack.WaitCompleteCall(ctx) -// t.SetHash(scheme.Hash()) -// if err != nil { -// return t, err -// } -// -// return t, nil -//} -// -//// AuthorizerHealthCheck performs health check of the authorizer -//// - ctx is the context of the request. -//// - input is the payload of the request. -//func AuthorizerHealthCheck(ctx context.Context, input *zcncore.AuthorizerHealthCheckPayload) (Transaction, error) { -// t, err := NewTransactionEntity(0) -// if err != nil { -// return nil, err -// } -// -// scheme := t.GetScheme() -// -// err = scheme.ZCNSCAuthorizerHealthCheck(input) -// if err != nil { -// return t, err -// } -// -// callBack := t.GetCallback() -// -// err = callBack.WaitCompleteCall(ctx) -// t.SetHash(scheme.Hash()) -// if err != nil { -// return t, err -// } -// -// return t, nil -//} From dc41de7312b4986bf8e42c60ccaa97d9ddbc7c2a Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sat, 28 Sep 2024 21:34:09 +0530 Subject: [PATCH 151/319] take base as dest name --- zboxcore/sdk/renameworker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zboxcore/sdk/renameworker.go b/zboxcore/sdk/renameworker.go index b2747cc62..371f05ff4 100644 --- a/zboxcore/sdk/renameworker.go +++ b/zboxcore/sdk/renameworker.go @@ -522,7 +522,7 @@ func (ro *RenameOperation) Error(allocObj *Allocation, consensus int, err error) func NewRenameOperation(remotePath string, destName string, renameMask zboxutil.Uint128, maskMU *sync.Mutex, consensusTh int, fullConsensus int, ctx context.Context) *RenameOperation { ro := &RenameOperation{} ro.remotefilepath = zboxutil.RemoteClean(remotePath) - ro.newName = destName + ro.newName = path.Base(destName) ro.renameMask = renameMask ro.maskMU = maskMU ro.consensus.consensusThresh = consensusTh From 5ed4b7848146f4231d46691c68a24914abfdfd9f Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 29 Sep 2024 03:52:29 +0530 Subject: [PATCH 152/319] Debug wasm --- core/client/http.go | 3 ++- core/version/version.go | 2 +- wasmsdk/wallet.go | 8 -------- 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index 1f0a74e5a..e8026b5f2 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -16,6 +16,7 @@ import ( "net/http" "net/url" "os" + "strings" "sync" "time" ) @@ -154,7 +155,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] rate := float32(maxCount*100) / float32(cfg.SharderConsensous) if rate < consensusThresh { - err = errors.New("consensus_failed", "consensus failed on sharders") + err = errors.New("consensus_failed", fmt.Sprintf("consensus failed on sharders : %f : ", rate)+strings.Join(sharders, ",")) } if dominant != 200 { diff --git a/core/version/version.go b/core/version/version.go index 36df616cf..f0f79850b 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-174-g4561413d" +const VERSIONSTR = "v1.17.9-176-g777f6665" diff --git a/wasmsdk/wallet.go b/wasmsdk/wallet.go index 33bc332d3..2cbba300c 100644 --- a/wasmsdk/wallet.go +++ b/wasmsdk/wallet.go @@ -28,14 +28,6 @@ func setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemon }, } - c := client.GetClient() - c.Mnemonic = mnemonic - c.ClientID = clientID - c.ClientKey = clientKey - c.PeerPublicKey = peerPublicKey - c.Keys = keys - c.IsSplit = isSplit - w := &zcncrypto.Wallet{ ClientID: clientID, ClientKey: clientKey, From 59de519f2e5f095abc2c319cbcada3deeffda003 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 29 Sep 2024 04:06:35 +0530 Subject: [PATCH 153/319] Debug wasm --- core/client/set.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index 06685ee45..3278490bc 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -180,9 +180,6 @@ func PrivateKey() string { } func ClientID() string { - if client.wallet.ClientID == "" { - panic("Wallet not initialised, client ID empty") - } return client.wallet.ClientID } From da57d8a4c5e17cf6ec73704d6924f6599ebf982b Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 29 Sep 2024 04:29:17 +0530 Subject: [PATCH 154/319] Debug wasm --- core/client/http.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/client/http.go b/core/client/http.go index e8026b5f2..3c578668f 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -111,6 +111,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] go func(sharder string) { defer wg.Done() urlString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath) + fmt.Println(urlString) urlObj, err := url.Parse(urlString) if err != nil { log.Println(err) From 0434428a720292d9891d4b35fa6fd6105c835200 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 29 Sep 2024 04:30:16 +0530 Subject: [PATCH 155/319] Debug wasm --- core/client/http.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index 3c578668f..de7c97b13 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -122,8 +122,8 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] q.Add(k, v) } urlObj.RawQuery = q.Encode() - client := &http.Client{Transport: DefaultTransport} - response, err := client.Get(urlObj.String()) + c := &http.Client{Transport: DefaultTransport} + response, err := c.Get(urlObj.String()) if err != nil { nodeClient.sharders.Fail(sharder) return From b3a20c1369dd5cb93058e67de93dbedce86a2524 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 29 Sep 2024 04:36:21 +0530 Subject: [PATCH 156/319] Debug wasm --- core/client/set.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/client/set.go b/core/client/set.go index 3278490bc..8787037c0 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "github.com/0chain/gosdk/core/conf" "strings" @@ -180,6 +181,9 @@ func PrivateKey() string { } func ClientID() string { + if client.wallet.ClientID == "" { + fmt.Println("ClientID is empty") + } return client.wallet.ClientID } From 657fd73e2770a67ae881fbc0de685399b8ece376 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 29 Sep 2024 05:10:23 +0530 Subject: [PATCH 157/319] Debug wasm --- wasmsdk/wallet.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/wasmsdk/wallet.go b/wasmsdk/wallet.go index 2cbba300c..0ad15d853 100644 --- a/wasmsdk/wallet.go +++ b/wasmsdk/wallet.go @@ -16,9 +16,21 @@ import ( ) func setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemonic string, isSplit bool) error { + fmt.Println("Set Wallet called") + fmt.Println("ClientID : ", clientID) + fmt.Println("ClientKey : ", clientKey) + fmt.Println("PeerPublicKey : ", peerPublicKey) + fmt.Println("PublicKey : ", publicKey) + fmt.Println("PrivateKey : ", privateKey) + fmt.Println("Mnemonic : ", mnemonic) + fmt.Println("IsSplit : ", isSplit) + if mnemonic == "" && !isSplit { return errors.New("mnemonic is required") } + + fmt.Println("Here 1") + mode := os.Getenv("MODE") fmt.Println("gosdk setWallet, mode:", mode, "is split:", isSplit) keys := []zcncrypto.KeyPair{ @@ -38,6 +50,8 @@ func setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemon } fmt.Println("set Wallet, is split:", isSplit) client.SetWallet(*w) + fmt.Println("Here 2") + fmt.Println("Wallet ID", client.ClientID()) zboxApiClient.SetWallet(clientID, privateKey, publicKey) if mode == "" { // main thread, need to notify the web worker to update wallet From 4703a008b8e1900e9fa19a6e1797ba775cd8bfca Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 29 Sep 2024 05:25:46 +0530 Subject: [PATCH 158/319] Debug --- core/version/version.go | 2 +- wasmsdk/wallet.go | 30 ++++++++++++++++-------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/core/version/version.go b/core/version/version.go index f0f79850b..6037a0cf5 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-176-g777f6665" +const VERSIONSTR = "v1.17.9-182-g657fd73e" diff --git a/wasmsdk/wallet.go b/wasmsdk/wallet.go index 0ad15d853..9ed41369e 100644 --- a/wasmsdk/wallet.go +++ b/wasmsdk/wallet.go @@ -5,8 +5,9 @@ package main import ( "errors" + "github.com/0chain/common/core/logging" - "fmt" + "log" "os" "strconv" @@ -16,23 +17,24 @@ import ( ) func setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemonic string, isSplit bool) error { - fmt.Println("Set Wallet called") - fmt.Println("ClientID : ", clientID) - fmt.Println("ClientKey : ", clientKey) - fmt.Println("PeerPublicKey : ", peerPublicKey) - fmt.Println("PublicKey : ", publicKey) - fmt.Println("PrivateKey : ", privateKey) - fmt.Println("Mnemonic : ", mnemonic) - fmt.Println("IsSplit : ", isSplit) + log.Println("Set Wallet called") + logging.Logger.Info("2 Set Wallet called") + log.Println("ClientID : ", clientID) + log.Println("ClientKey : ", clientKey) + log.Println("PeerPublicKey : ", peerPublicKey) + log.Println("PublicKey : ", publicKey) + log.Println("PrivateKey : ", privateKey) + log.Println("Mnemonic : ", mnemonic) + log.Println("IsSplit : ", isSplit) if mnemonic == "" && !isSplit { return errors.New("mnemonic is required") } - fmt.Println("Here 1") + log.Println("Here 1") mode := os.Getenv("MODE") - fmt.Println("gosdk setWallet, mode:", mode, "is split:", isSplit) + log.Println("gosdk setWallet, mode:", mode, "is split:", isSplit) keys := []zcncrypto.KeyPair{ { PrivateKey: privateKey, @@ -48,10 +50,10 @@ func setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemon Keys: keys, IsSplit: isSplit, } - fmt.Println("set Wallet, is split:", isSplit) + log.Println("set Wallet, is split:", isSplit) client.SetWallet(*w) - fmt.Println("Here 2") - fmt.Println("Wallet ID", client.ClientID()) + log.Println("Here 2") + log.Println("Wallet ID", client.ClientID()) zboxApiClient.SetWallet(clientID, privateKey, publicKey) if mode == "" { // main thread, need to notify the web worker to update wallet From eaaa7278f14220d0af4a2074465f8a313e85637b Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sun, 29 Sep 2024 18:05:08 +0200 Subject: [PATCH 159/319] fix: fixed restriction setup --- wasmsdk/auth_txn.go | 20 +++-- wasmsdk/proxy.go | 2 + zcncore/zauth.go | 193 +++++++++++++++++++++++++------------------- 3 files changed, 126 insertions(+), 89 deletions(-) diff --git a/wasmsdk/auth_txn.go b/wasmsdk/auth_txn.go index e9c8d71c0..5aa16cae6 100644 --- a/wasmsdk/auth_txn.go +++ b/wasmsdk/auth_txn.go @@ -42,14 +42,22 @@ func registerZauthServer(serverAddr string) { sys.SetAuthCommon(zcncore.ZauthAuthCommon(serverAddr)) } -// zvaultNewWallet generates new split wallet -func zvaultNewWallet(serverAddr, token string, roles []string) (string, error) { - return zcncore.CallZvaultNewWalletString(serverAddr, token, "", nil) +func zauthRetrieveKey(clientID, peerPublicKey, serverAddr, token string) (string, error) { + return zcncore.CallZauthRetreiveKey(serverAddr, token, clientID, peerPublicKey) } -// zvaultNewSplit generates new split wallet from existing clientID -func zvaultNewSplit(clientID, serverAddr, token string, roles []string) (string, error) { - return zcncore.CallZvaultNewWalletString(serverAddr, token, clientID, roles) +// zvaultNewWallet generates new wallet +func zvaultNewWallet(serverAddr, token string) (string, error) { + return zcncore.CallZvaultNewWallet(serverAddr, token) +} + +// zvaultNewSplit generates new split key for saved wallet +func zvaultNewSplit(clientID, serverAddr, token string, restrictions []string) (string, error) { + return zcncore.CallZvaultNewSplit(serverAddr, token, clientID, restrictions) +} + +func zvaultUpdateRestrictions(clientID, peerPublicKey, serverAddr, token string, restrictions []string) (string, error) { + return zcncore.CallZvaultUpdateRestrictions(serverAddr, token, clientID, peerPublicKey, restrictions) } func zvaultStoreKey(serverAddr, token, privateKey string) (string, error) { diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index 1927d8d04..a3d04c39f 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -323,9 +323,11 @@ func main() { // zauth "registerZauthServer": registerZauthServer, + "zauthRetreiveKey": zauthRetrieveKey, // zvault "zvaultNewWallet": zvaultNewWallet, "zvaultNewSplit": zvaultNewSplit, + "zvaultUpdateRestrictions": zvaultUpdateRestrictions, "zvaultStoreKey": zvaultStoreKey, "zvaultRetrieveKeys": zvaultRetrieveKeys, "zvaultRevokeKey": zvaultRevokeKey, diff --git a/zcncore/zauth.go b/zcncore/zauth.go index fdb240a80..f2480c707 100644 --- a/zcncore/zauth.go +++ b/zcncore/zauth.go @@ -12,69 +12,59 @@ import ( "github.com/pkg/errors" ) -// SplitWallet represents wallet info for split wallet -// The client id and client key are the same as the primary wallet client id and client key -type SplitWallet struct { - ClientID string `json:"client_id"` - ClientKey string `json:"client_key"` - PublicKey string `json:"public_key"` - PrivateKey string `json:"private_key"` - PeerPublicKey string `json:"peer_public_key"` - Roles []string `json:"roles"` - IsRevoked bool `json:"is_revoked"` - ExpiredAt int64 `json:"expired_at"` +type createKeyRequest struct { + Restrictions []string `json:"restrictions"` } -// CallZauthSetup calls the zauth setup endpoint -func CallZauthSetup(serverAddr string, token string, splitWallet SplitWallet) error { - // Add your code here - endpoint := serverAddr + "/setup" - wData, err := json.Marshal(splitWallet) - if err != nil { - return errors.Wrap(err, "failed to marshal split wallet") - } +type updateRestrictionsRequest struct { + Restrictions []string `json:"restrictions"` +} + +type AuthMessage struct { + Hash string `json:"hash"` + Signature string `json:"signature"` + ClientID string `json:"client_id"` +} + +type AuthResponse struct { + Sig string `json:"sig"` +} - req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(wData)) +func CallZauthRetreiveKey(serverAddr, token, clientID, peerPublicKey string) (string, error) { + endpoint := fmt.Sprintf("%s/key/%s", serverAddr, clientID) + + req, err := http.NewRequest("GET", endpoint, nil) if err != nil { - return errors.Wrap(err, "failed to create HTTP request") + return "", errors.Wrap(err, "failed to create HTTP request") } req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Peer-Public-Key", peerPublicKey) req.Header.Set("X-Jwt-Token", token) client := &http.Client{} resp, err := client.Do(req) if err != nil { - return errors.Wrap(err, "failed to send HTTP request") + return "", errors.Wrap(err, "failed to send HTTP request") } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { errMsg, _ := io.ReadAll(resp.Body) - if len(errMsg) > 0 { - return errors.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) - } - - return errors.Errorf("code: %d", resp.StatusCode) - } - - var rsp struct { - Result string `json:"result"` - } - if err := json.NewDecoder(resp.Body).Decode(&rsp); err != nil { - return errors.Wrap(err, "failed to decode response body") + return "", fmt.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) } - if rsp.Result != "success" { - return errors.New("failed to setup zauth server") + d, err := io.ReadAll(resp.Body) + if err != nil { + return "", errors.Wrap(err, "failed to read response body") } - return nil + return string(d), nil } -func CallZauthRevoke(serverAddr, token, clientID, publicKey string) error { +func CallZauthRevoke(serverAddr, token, clientID, peerPublicKey string) error { endpoint := serverAddr + "/revoke/" + clientID - endpoint += "?peer_public_key=" + publicKey + endpoint += "?peer_public_key=" + peerPublicKey req, err := http.NewRequest("POST", endpoint, nil) if err != nil { return errors.Wrap(err, "failed to create HTTP request") @@ -153,40 +143,100 @@ func CallZauthDelete(serverAddr, token, clientID string) error { return nil } -type newWalletRequest struct { - Roles []string `json:"roles"` -} +func CallZvaultNewWallet(serverAddr, token string) (string, error) { + endpoint := serverAddr + "/wallet" -func CallZvaultNewWalletString(serverAddr, token, clientID string, roles []string) (string, error) { - // Add your code here - endpoint := serverAddr + "/generate" - if clientID != "" { - endpoint = endpoint + "/" + clientID + req, err := http.NewRequest("POST", endpoint, nil) + if err != nil { + return "", errors.Wrap(err, "failed to create HTTP request") } - var body io.Reader + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Jwt-Token", token) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return "", errors.Wrap(err, "failed to send HTTP request") + } + defer resp.Body.Close() - if roles != nil { - data, err := json.Marshal(newWalletRequest{ - Roles: roles, - }) - if err != nil { - return "", errors.Wrap(err, "failed to serialize request") + if resp.StatusCode != http.StatusOK { + errMsg, _ := io.ReadAll(resp.Body) + if len(errMsg) > 0 { + return "", errors.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) } - body = bytes.NewReader(data) + return "", errors.Errorf("code: %d", resp.StatusCode) + } + + d, err := io.ReadAll(resp.Body) + if err != nil { + return "", errors.Wrap(err, "failed to read response body") + } + + return string(d), nil +} + +func CallZvaultNewSplit(serverAddr, token, clientID string, restrictions []string) (string, error) { + endpoint := serverAddr + "/key/" + clientID + + data, err := json.Marshal(createKeyRequest{ + Restrictions: restrictions, + }) + if err != nil { + return "", errors.Wrap(err, "failed to serialize request") } - req, err := http.NewRequest("POST", endpoint, body) + req, err := http.NewRequest("POST", endpoint, bytes.NewReader(data)) if err != nil { return "", errors.Wrap(err, "failed to create HTTP request") } - fmt.Println("new wallet endpoint:", endpoint) - fmt.Println("new wallet: serverAddr:", serverAddr) - fmt.Println("new wallet: clientID:", clientID) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Jwt-Token", token) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return "", errors.Wrap(err, "failed to send HTTP request") + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + errMsg, _ := io.ReadAll(resp.Body) + if len(errMsg) > 0 { + return "", errors.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) + } + + return "", errors.Errorf("code: %d", resp.StatusCode) + } + + d, err := io.ReadAll(resp.Body) + if err != nil { + return "", errors.Wrap(err, "failed to read response body") + } + + return string(d), nil +} + +func CallZvaultUpdateRestrictions(serverAddr, token, clientID, peerPublicKey string, restrictions []string) (string, error) { + endpoint := serverAddr + "/restrictions/" + clientID + + data, err := json.Marshal(updateRestrictionsRequest{ + Restrictions: restrictions, + }) + if err != nil { + return "", errors.Wrap(err, "failed to serialize request") + } + + req, err := http.NewRequest("PUT", endpoint, bytes.NewReader(data)) + if err != nil { + return "", errors.Wrap(err, "failed to create HTTP request") + } req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Peer-Public-Key", peerPublicKey) req.Header.Set("X-Jwt-Token", token) client := &http.Client{} @@ -239,19 +289,12 @@ func CallZvaultStoreKeyString(serverAddr, token, privateKey string) (string, err return "", errors.Wrap(err, "failed to create HTTP request") } - fmt.Println("call zvault /store:", endpoint) - req.Header.Set("Content-Type", "application/json") - req.Header.Set("X-Jwt-Token", token) - - fmt.Println(req) - client := &http.Client{} resp, err := client.Do(req) if err != nil { - fmt.Println(err.Error()) - return "", errors.Wrap(err, "failed to send HTTP request") } + defer resp.Body.Close() if resp.StatusCode != http.StatusOK { @@ -279,7 +322,6 @@ func CallZvaultRetrieveKeys(serverAddr, token, clientID string) (string, error) return "", errors.Wrap(err, "failed to create HTTP request") } - fmt.Println("call zvault /keys:", endpoint) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Jwt-Token", token) @@ -311,7 +353,6 @@ func CallZvaultDeletePrimaryKey(serverAddr, token, clientID string) error { return errors.Wrap(err, "failed to create HTTP request") } - fmt.Println("call zvault /delete:", endpoint) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Jwt-Token", token) @@ -343,7 +384,6 @@ func CallZvaultRevokeKey(serverAddr, token, clientID, publicKey string) error { return errors.Wrap(err, "failed to create HTTP request") } - fmt.Println("call zvault /revoke:", endpoint) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Jwt-Token", token) @@ -375,7 +415,6 @@ func CallZvaultRetrieveWallets(serverAddr, token string) (string, error) { return "", errors.Wrap(err, "failed to create HTTP request") } - fmt.Println("call zvault /keys:", endpoint) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Jwt-Token", token) @@ -407,7 +446,6 @@ func CallZvaultRetrieveSharedWallets(serverAddr, token string) (string, error) { return "", errors.Wrap(err, "failed to create HTTP request") } - fmt.Println("call zvault /keys:", endpoint) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Jwt-Token", token) @@ -434,7 +472,6 @@ func CallZvaultRetrieveSharedWallets(serverAddr, token string) (string, error) { // ZauthSignTxn returns a function that sends a txn signing request to the zauth server func ZauthSignTxn(serverAddr string) sys.AuthorizeFunc { return func(msg string) (string, error) { - fmt.Println("zvault sign txn - in sign txn...") req, err := http.NewRequest("POST", serverAddr+"/sign/txn", bytes.NewBuffer([]byte(msg))) if err != nil { return "", errors.Wrap(err, "failed to create HTTP request") @@ -503,13 +540,3 @@ func ZauthAuthCommon(serverAddr string) sys.AuthorizeFunc { return string(d), nil } } - -type AuthMessage struct { - Hash string `json:"hash"` - Signature string `json:"signature"` - ClientID string `json:"client_id"` -} - -type AuthResponse struct { - Sig string `json:"sig"` -} From e1fd3c2e5738fa3b81e2edab1684ab77232d49d2 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Mon, 30 Sep 2024 03:11:22 +0200 Subject: [PATCH 160/319] fix: fixed bugs --- zcncore/zauth.go | 99 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 77 insertions(+), 22 deletions(-) diff --git a/zcncore/zauth.go b/zcncore/zauth.go index f2480c707..709b4ef1b 100644 --- a/zcncore/zauth.go +++ b/zcncore/zauth.go @@ -12,6 +12,83 @@ import ( "github.com/pkg/errors" ) +// AvailableRestrictions represents supported restrictions mapping. +var AvailableRestrictions = map[string][]string{ + "token_transfers": {"transfer"}, + "allocation_file_operations": { + "read_redeem", + "commit_connection", + }, + "allocation_storage_operations": { + "new_allocation_request", + "update_allocation_request", + "finalize_allocation", + "cancel_allocation", + "add_free_storage_assigner", + "free_allocation_request", + }, + "allocation_token_operations": { + "read_pool_lock", + "read_pool_unlock", + "write_pool_lock", + }, + "storage_rewards": { + "collect_reward", + "stake_pool_lock", + "stake_pool_unlock", + }, + "storage_operations": { + "challenge_response", + "add_validator", + "add_blobber", + "blobber_health_check", + "validator_health_check", + }, + "storage_management": { + "kill_blobber", + "kill_validator", + "shutdown_blobber", + "shutdown_validator", + "update_blobber_settings", + "update_validator_settings", + }, + "miner_operations": { + "add_miner", + "add_sharder", + "miner_health_check", + "sharder_health_check", + "contributeMpk", + "shareSignsOrShares", + "wait", + "sharder_keep", + }, + "miner_management_operations": { + "delete_miner", + "delete_sharder", + "update_miner_settings", + "kill_miner", + "kill_sharder", + }, + "miner_financial_operations": { + "addToDelegatePool", + "deleteFromDelegatePool", + "collect_reward", + }, + "token_bridging": { + "mint", + "burn", + }, + "authorizer_management_operations": { + "delete-authorizer", + }, + "authorizer_operations": { + "add-authorizer", + "authorizer-health-check", + "add-to-delegate-pool", + "delete-from-delegate-pool", + }, +} + type createKeyRequest struct { Restrictions []string `json:"restrictions"` } @@ -89,17 +166,6 @@ func CallZauthRevoke(serverAddr, token, clientID, peerPublicKey string) error { return errors.Errorf("code: %d", resp.StatusCode) } - var rsp struct { - Result string `json:"result"` - } - if err := json.NewDecoder(resp.Body).Decode(&rsp); err != nil { - return errors.Wrap(err, "failed to decode response body") - } - - if rsp.Result != "success" { - return errors.New("failed to setup zauth server") - } - return nil } @@ -129,17 +195,6 @@ func CallZauthDelete(serverAddr, token, clientID string) error { return errors.Errorf("code: %d", resp.StatusCode) } - var rsp struct { - Result string `json:"result"` - } - if err := json.NewDecoder(resp.Body).Decode(&rsp); err != nil { - return errors.Wrap(err, "failed to decode response body") - } - - if rsp.Result != "success" { - return errors.New("failed to setup zauth server") - } - return nil } From fe5f167cedff6d2c45492a9bf1aa294f7763ad49 Mon Sep 17 00:00:00 2001 From: Jayash Date: Tue, 1 Oct 2024 22:02:33 +0530 Subject: [PATCH 161/319] Debug --- core/client/set.go | 3 +++ core/version/version.go | 2 +- wasmsdk/sdk.go | 2 ++ wasmsdk/wallet.go | 30 +++++++++++++----------------- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index 8787037c0..8aacba17c 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -95,6 +95,7 @@ func GetClientSysKeys() []sys.KeyPair { // SetWallet should be set before any transaction or client specific APIs func SetWallet(w zcncrypto.Wallet) { + fmt.Println("Setting wallet : ", w.ClientID) client.wallet = &w } @@ -210,6 +211,8 @@ func InitSDK(walletJSON string, nonce int64, isSplitWallet, addWallet bool, fee ...uint64) error { + fmt.Println("2Init Sdk") + if addWallet { wallet := zcncrypto.Wallet{} err := json.Unmarshal([]byte(walletJSON), &wallet) diff --git a/core/version/version.go b/core/version/version.go index 6037a0cf5..0b91cc02e 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-182-g657fd73e" +const VERSIONSTR = "v1.17.9-183-g4703a008" diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index ff91ed8dc..2f3f6cd20 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -34,6 +34,8 @@ func initSDKs(chainID, blockWorker, signatureScheme string, minConfirmation, minSubmit, confirmationChainLength int, zboxHost, zboxAppType string, sharderconsensous int, isSplit bool) error { + fmt.Println("1Init sdk") + zboxApiClient.SetRequest(zboxHost, zboxAppType) err := client.InitSDK("{}", blockWorker, chainID, signatureScheme, nil, 0, false, false) diff --git a/wasmsdk/wallet.go b/wasmsdk/wallet.go index 9ed41369e..2f41e6983 100644 --- a/wasmsdk/wallet.go +++ b/wasmsdk/wallet.go @@ -5,9 +5,6 @@ package main import ( "errors" - "github.com/0chain/common/core/logging" - - "log" "os" "strconv" @@ -17,24 +14,23 @@ import ( ) func setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemonic string, isSplit bool) error { - log.Println("Set Wallet called") - logging.Logger.Info("2 Set Wallet called") - log.Println("ClientID : ", clientID) - log.Println("ClientKey : ", clientKey) - log.Println("PeerPublicKey : ", peerPublicKey) - log.Println("PublicKey : ", publicKey) - log.Println("PrivateKey : ", privateKey) - log.Println("Mnemonic : ", mnemonic) - log.Println("IsSplit : ", isSplit) + fmt.Println("Set Wallet called") + fmt.Println("ClientID : ", clientID) + fmt.Println("ClientKey : ", clientKey) + fmt.Println("PeerPublicKey : ", peerPublicKey) + fmt.Println("PublicKey : ", publicKey) + fmt.Println("PrivateKey : ", privateKey) + fmt.Println("Mnemonic : ", mnemonic) + fmt.Println("IsSplit : ", isSplit) if mnemonic == "" && !isSplit { return errors.New("mnemonic is required") } - log.Println("Here 1") + fmt.Println("Here 1") mode := os.Getenv("MODE") - log.Println("gosdk setWallet, mode:", mode, "is split:", isSplit) + fmt.Println("gosdk setWallet, mode:", mode, "is split:", isSplit) keys := []zcncrypto.KeyPair{ { PrivateKey: privateKey, @@ -50,10 +46,10 @@ func setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemon Keys: keys, IsSplit: isSplit, } - log.Println("set Wallet, is split:", isSplit) + fmt.Println("set Wallet, is split:", isSplit) client.SetWallet(*w) - log.Println("Here 2") - log.Println("Wallet ID", client.ClientID()) + fmt.Println("Here 2") + fmt.Println("Wallet ID", client.ClientID()) zboxApiClient.SetWallet(clientID, privateKey, publicKey) if mode == "" { // main thread, need to notify the web worker to update wallet From ad6b88d8fcd75a575a32afa4bb1eb03dd4ad6755 Mon Sep 17 00:00:00 2001 From: Jayash Date: Tue, 1 Oct 2024 23:50:15 +0530 Subject: [PATCH 162/319] Fix --- core/version/version.go | 2 +- wasmsdk/wallet.go | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/core/version/version.go b/core/version/version.go index 0b91cc02e..d38e3845c 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-183-g4703a008" +const VERSIONSTR = "v1.17.9-184-gfe5f167c" diff --git a/wasmsdk/wallet.go b/wasmsdk/wallet.go index 2f41e6983..0ad15d853 100644 --- a/wasmsdk/wallet.go +++ b/wasmsdk/wallet.go @@ -5,6 +5,8 @@ package main import ( "errors" + + "fmt" "os" "strconv" From 9464c07c29c8dc2fe183e5e786220f910a004356 Mon Sep 17 00:00:00 2001 From: Jayash Date: Wed, 2 Oct 2024 00:44:16 +0530 Subject: [PATCH 163/319] Debug --- wasmsdk/wallet.go | 73 ++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 39 deletions(-) diff --git a/wasmsdk/wallet.go b/wasmsdk/wallet.go index 0ad15d853..51c915742 100644 --- a/wasmsdk/wallet.go +++ b/wasmsdk/wallet.go @@ -8,11 +8,6 @@ import ( "fmt" "os" - "strconv" - - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/zcncrypto" - "github.com/0chain/gosdk/wasmsdk/jsbridge" ) func setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemonic string, isSplit bool) error { @@ -33,41 +28,41 @@ func setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemon mode := os.Getenv("MODE") fmt.Println("gosdk setWallet, mode:", mode, "is split:", isSplit) - keys := []zcncrypto.KeyPair{ - { - PrivateKey: privateKey, - PublicKey: publicKey, - }, - } + //keys := []zcncrypto.KeyPair{ + // { + // PrivateKey: privateKey, + // PublicKey: publicKey, + // }, + //} - w := &zcncrypto.Wallet{ - ClientID: clientID, - ClientKey: clientKey, - PeerPublicKey: peerPublicKey, - Mnemonic: mnemonic, - Keys: keys, - IsSplit: isSplit, - } - fmt.Println("set Wallet, is split:", isSplit) - client.SetWallet(*w) - fmt.Println("Here 2") - fmt.Println("Wallet ID", client.ClientID()) - - zboxApiClient.SetWallet(clientID, privateKey, publicKey) - if mode == "" { // main thread, need to notify the web worker to update wallet - // notify the web worker to update wallet - if err := jsbridge.PostMessageToAllWorkers(jsbridge.MsgTypeUpdateWallet, map[string]string{ - "client_id": clientID, - "client_key": clientKey, - "peer_public_key": peerPublicKey, - "public_key": publicKey, - "private_key": privateKey, - "mnemonic": mnemonic, - "is_split": strconv.FormatBool(isSplit), - }); err != nil { - return err - } - } + //w := &zcncrypto.Wallet{ + // ClientID: clientID, + // ClientKey: clientKey, + // PeerPublicKey: peerPublicKey, + // Mnemonic: mnemonic, + // Keys: keys, + // IsSplit: isSplit, + //} + //fmt.Println("set Wallet, is split:", isSplit) + //client.SetWallet(*w) + //fmt.Println("Here 2") + //fmt.Println("Wallet ID", client.ClientID()) + // + //zboxApiClient.SetWallet(clientID, privateKey, publicKey) + //if mode == "" { // main thread, need to notify the web worker to update wallet + // // notify the web worker to update wallet + // if err := jsbridge.PostMessageToAllWorkers(jsbridge.MsgTypeUpdateWallet, map[string]string{ + // "client_id": clientID, + // "client_key": clientKey, + // "peer_public_key": peerPublicKey, + // "public_key": publicKey, + // "private_key": privateKey, + // "mnemonic": mnemonic, + // "is_split": strconv.FormatBool(isSplit), + // }); err != nil { + // return err + // } + //} return nil } From 534eab7b8fc3fbb8ecff780b2a864b7b3f92564c Mon Sep 17 00:00:00 2001 From: Jayash Date: Wed, 2 Oct 2024 01:01:16 +0530 Subject: [PATCH 164/319] Fix --- core/transaction/entity.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 9b4a22ed3..83e579e2f 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -491,7 +491,7 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, value, fee uint64, toClient ...string) (hash, out string, nonce int64, t *Transaction, err error) { var requestBytes []byte - if requestBytes, err = json.Marshal(sn); err != nil { + if requestBytes, err = json.Marshal(sn.InputArgs); err != nil { return } From 83f0a3a06c3e325082a91d90414726d607e6231c Mon Sep 17 00:00:00 2001 From: Jayash Date: Wed, 2 Oct 2024 01:02:38 +0530 Subject: [PATCH 165/319] Revert "Fix" This reverts commit 534eab7b8fc3fbb8ecff780b2a864b7b3f92564c. --- core/transaction/entity.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 83e579e2f..9b4a22ed3 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -491,7 +491,7 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, value, fee uint64, toClient ...string) (hash, out string, nonce int64, t *Transaction, err error) { var requestBytes []byte - if requestBytes, err = json.Marshal(sn.InputArgs); err != nil { + if requestBytes, err = json.Marshal(sn); err != nil { return } From 856f95f3e84b8d93b22b71f62fc1fd24509e55fa Mon Sep 17 00:00:00 2001 From: Jayash Date: Wed, 2 Oct 2024 01:52:38 +0530 Subject: [PATCH 166/319] Debug --- core/client/http.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/client/http.go b/core/client/http.go index de7c97b13..1d33565a9 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -111,7 +111,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] go func(sharder string) { defer wg.Done() urlString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath) - fmt.Println(urlString) + fmt.Println("Printing url... ", urlString) urlObj, err := url.Parse(urlString) if err != nil { log.Println(err) @@ -119,6 +119,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] } q := urlObj.Query() for k, v := range params { + fmt.Println("Printing keys and values... ", k, v) q.Add(k, v) } urlObj.RawQuery = q.Encode() From 2d5d4960c164f2417e270eefd2b9f9568e116a35 Mon Sep 17 00:00:00 2001 From: Jayash Date: Wed, 2 Oct 2024 01:55:32 +0530 Subject: [PATCH 167/319] Debug --- core/client/http.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/client/http.go b/core/client/http.go index 1d33565a9..2d4aca622 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -123,8 +123,10 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] q.Add(k, v) } urlObj.RawQuery = q.Encode() + debugUrl := urlObj.String() + fmt.Println("Printing debug url... ", debugUrl) c := &http.Client{Transport: DefaultTransport} - response, err := c.Get(urlObj.String()) + response, err := c.Get(debugUrl) if err != nil { nodeClient.sharders.Fail(sharder) return From c5cd4a9a16f6c48b08295a89bb1a1d5278797544 Mon Sep 17 00:00:00 2001 From: Jayash Date: Wed, 2 Oct 2024 01:59:30 +0530 Subject: [PATCH 168/319] Debug --- core/client/http.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/client/http.go b/core/client/http.go index 2d4aca622..f9c149293 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -134,6 +134,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] defer response.Body.Close() entityBytes, _ := io.ReadAll(response.Body) + fmt.Println("Printing entity bytes... ", string(entityBytes)) mu.Lock() if response.StatusCode > http.StatusBadRequest { nodeClient.sharders.Fail(sharder) @@ -148,6 +149,8 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] if isCurrentDominantStatus(response.StatusCode, responses, maxCount) { dominant = response.StatusCode retObj = entityBytes + + fmt.Println("Printing retObj... ", string(retObj)) } entityResult[sharder] = entityBytes From d1955d478bf6399da71a95c09a9563491af0d332 Mon Sep 17 00:00:00 2001 From: Jayash Date: Wed, 2 Oct 2024 02:19:42 +0530 Subject: [PATCH 169/319] Debug --- core/client/http.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index f9c149293..a161b52b7 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -16,7 +16,6 @@ import ( "net/http" "net/url" "os" - "strings" "sync" "time" ) @@ -101,6 +100,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] dominant := 200 wg := sync.WaitGroup{} + var debugUrl string cfg, err := conf.GetClientConfig() if err != nil { return nil, err @@ -123,7 +123,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] q.Add(k, v) } urlObj.RawQuery = q.Encode() - debugUrl := urlObj.String() + debugUrl = urlObj.String() fmt.Println("Printing debug url... ", debugUrl) c := &http.Client{Transport: DefaultTransport} response, err := c.Get(debugUrl) @@ -162,7 +162,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] rate := float32(maxCount*100) / float32(cfg.SharderConsensous) if rate < consensusThresh { - err = errors.New("consensus_failed", fmt.Sprintf("consensus failed on sharders : %f : ", rate)+strings.Join(sharders, ",")) + err = errors.New("consensus_failed", fmt.Sprintf("consensus failed on sharders : %s : %f : ", debugUrl, rate)) } if dominant != 200 { From e43f4364dfd1cfac2658f4983b704956be18d999 Mon Sep 17 00:00:00 2001 From: Jayash Date: Wed, 2 Oct 2024 02:48:59 +0530 Subject: [PATCH 170/319] Debug --- core/client/set.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/client/set.go b/core/client/set.go index 8aacba17c..40a363934 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -184,6 +184,7 @@ func PrivateKey() string { func ClientID() string { if client.wallet.ClientID == "" { fmt.Println("ClientID is empty") + return "f641b0cf2cd7a8a7224a760b3694c52ce617a68dc72b520f4b25c5dcef59d39e" } return client.wallet.ClientID } From 4c6a9077dbdc39ddac1158b81b883d2c306d6f8d Mon Sep 17 00:00:00 2001 From: Jayash Date: Wed, 2 Oct 2024 03:18:12 +0530 Subject: [PATCH 171/319] Debug --- core/client/http.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/core/client/http.go b/core/client/http.go index a161b52b7..b72f7972a 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -128,10 +128,13 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] c := &http.Client{Transport: DefaultTransport} response, err := c.Get(debugUrl) if err != nil { + fmt.Println("Printing error... ", err.Error()) nodeClient.sharders.Fail(sharder) return } + fmt.Println("Printing response... ") + defer response.Body.Close() entityBytes, _ := io.ReadAll(response.Body) fmt.Println("Printing entity bytes... ", string(entityBytes)) @@ -141,11 +144,16 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] } else { nodeClient.sharders.Success(sharder) } + + fmt.Println("Printing response status code... ", response.StatusCode) + responses[response.StatusCode]++ if responses[response.StatusCode] > maxCount { maxCount = responses[response.StatusCode] } + fmt.Println("Printing max count... ", maxCount) + if isCurrentDominantStatus(response.StatusCode, responses, maxCount) { dominant = response.StatusCode retObj = entityBytes @@ -153,6 +161,8 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] fmt.Println("Printing retObj... ", string(retObj)) } + fmt.Println("Printing entity result... ", entityResult) + entityResult[sharder] = entityBytes nodeClient.sharders.Success(sharder) mu.Unlock() @@ -160,27 +170,37 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] } wg.Wait() + fmt.Println("Printing count... ", maxCount, cfg.SharderConsensous) + rate := float32(maxCount*100) / float32(cfg.SharderConsensous) if rate < consensusThresh { - err = errors.New("consensus_failed", fmt.Sprintf("consensus failed on sharders : %s : %f : ", debugUrl, rate)) + err = errors.New("consensus_failed", fmt.Sprintf("consensus failed on sharders : %s : %f : %d", debugUrl, rate, cfg.SharderConsensous)) } + fmt.Println("Printing err... ", err.Error()) + if dominant != 200 { + fmt.Println("bad Printing dominant... ", dominant) var objmap map[string]json.RawMessage err := json.Unmarshal(retObj, &objmap) if err != nil { + fmt.Println("bad Printing error... ", err.Error()) return nil, errors.New("", string(retObj)) } var parsed string err = json.Unmarshal(objmap["error"], &parsed) if err != nil || parsed == "" { + fmt.Println("bad Printing parsed... ", parsed) return nil, errors.New("", string(retObj)) } + fmt.Println("2bad Printing parsed... ", parsed) return nil, errors.New("", parsed) } + fmt.Println("Printing good... ", string(retObj)) + if rate > consensusThresh { return retObj, nil } From f33e0de29e2cff82cf78ff7211dc242ec754d876 Mon Sep 17 00:00:00 2001 From: Jayash Date: Wed, 2 Oct 2024 03:52:37 +0530 Subject: [PATCH 172/319] Debug --- core/client/http.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index b72f7972a..5b0f4ae27 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -123,10 +123,8 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] q.Add(k, v) } urlObj.RawQuery = q.Encode() - debugUrl = urlObj.String() - fmt.Println("Printing debug url... ", debugUrl) c := &http.Client{Transport: DefaultTransport} - response, err := c.Get(debugUrl) + response, err := c.Get(urlObj.String()) if err != nil { fmt.Println("Printing error... ", err.Error()) nodeClient.sharders.Fail(sharder) From 83dc7adae280d1077221c7eab25c77997d999142 Mon Sep 17 00:00:00 2001 From: Jayash Date: Wed, 2 Oct 2024 04:15:16 +0530 Subject: [PATCH 173/319] Debug --- core/client/http.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index 5b0f4ae27..2cb5edd89 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -1,6 +1,7 @@ package client import ( + "context" "crypto/sha1" "encoding/hex" "encoding/json" @@ -20,13 +21,16 @@ import ( "time" ) +var customResolver = &net.Resolver{ + PreferGo: false, // Use the system resolver instead of Go's + Dial: func(ctx context.Context, network, addr string) (net.Conn, error) { + return net.DialTimeout(network, "8.8.8.8:53", 5*time.Second) // Using Google DNS + }, +} + var DefaultTransport = &http.Transport{ - Proxy: EnvProxy.Proxy, - DialContext: (&net.Dialer{ - Timeout: 3 * time.Minute, - KeepAlive: 45 * time.Second, - DualStack: true, - }).DialContext, + Proxy: EnvProxy.Proxy, + DialContext: customResolver.Dial, MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 45 * time.Second, From d651a0eed0c0ae102459019a2a82dd89648cc88d Mon Sep 17 00:00:00 2001 From: Jayash Date: Wed, 2 Oct 2024 05:17:03 +0530 Subject: [PATCH 174/319] Debug --- wasmsdk/wallet.go | 83 +++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 46 deletions(-) diff --git a/wasmsdk/wallet.go b/wasmsdk/wallet.go index 51c915742..2cbba300c 100644 --- a/wasmsdk/wallet.go +++ b/wasmsdk/wallet.go @@ -8,61 +8,52 @@ import ( "fmt" "os" + "strconv" + + "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/zcncrypto" + "github.com/0chain/gosdk/wasmsdk/jsbridge" ) func setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemonic string, isSplit bool) error { - fmt.Println("Set Wallet called") - fmt.Println("ClientID : ", clientID) - fmt.Println("ClientKey : ", clientKey) - fmt.Println("PeerPublicKey : ", peerPublicKey) - fmt.Println("PublicKey : ", publicKey) - fmt.Println("PrivateKey : ", privateKey) - fmt.Println("Mnemonic : ", mnemonic) - fmt.Println("IsSplit : ", isSplit) - if mnemonic == "" && !isSplit { return errors.New("mnemonic is required") } - - fmt.Println("Here 1") - mode := os.Getenv("MODE") fmt.Println("gosdk setWallet, mode:", mode, "is split:", isSplit) - //keys := []zcncrypto.KeyPair{ - // { - // PrivateKey: privateKey, - // PublicKey: publicKey, - // }, - //} + keys := []zcncrypto.KeyPair{ + { + PrivateKey: privateKey, + PublicKey: publicKey, + }, + } - //w := &zcncrypto.Wallet{ - // ClientID: clientID, - // ClientKey: clientKey, - // PeerPublicKey: peerPublicKey, - // Mnemonic: mnemonic, - // Keys: keys, - // IsSplit: isSplit, - //} - //fmt.Println("set Wallet, is split:", isSplit) - //client.SetWallet(*w) - //fmt.Println("Here 2") - //fmt.Println("Wallet ID", client.ClientID()) - // - //zboxApiClient.SetWallet(clientID, privateKey, publicKey) - //if mode == "" { // main thread, need to notify the web worker to update wallet - // // notify the web worker to update wallet - // if err := jsbridge.PostMessageToAllWorkers(jsbridge.MsgTypeUpdateWallet, map[string]string{ - // "client_id": clientID, - // "client_key": clientKey, - // "peer_public_key": peerPublicKey, - // "public_key": publicKey, - // "private_key": privateKey, - // "mnemonic": mnemonic, - // "is_split": strconv.FormatBool(isSplit), - // }); err != nil { - // return err - // } - //} + w := &zcncrypto.Wallet{ + ClientID: clientID, + ClientKey: clientKey, + PeerPublicKey: peerPublicKey, + Mnemonic: mnemonic, + Keys: keys, + IsSplit: isSplit, + } + fmt.Println("set Wallet, is split:", isSplit) + client.SetWallet(*w) + + zboxApiClient.SetWallet(clientID, privateKey, publicKey) + if mode == "" { // main thread, need to notify the web worker to update wallet + // notify the web worker to update wallet + if err := jsbridge.PostMessageToAllWorkers(jsbridge.MsgTypeUpdateWallet, map[string]string{ + "client_id": clientID, + "client_key": clientKey, + "peer_public_key": peerPublicKey, + "public_key": publicKey, + "private_key": privateKey, + "mnemonic": mnemonic, + "is_split": strconv.FormatBool(isSplit), + }); err != nil { + return err + } + } return nil } From 4101873452e3af348fc96c6ce2e586cc8f702f46 Mon Sep 17 00:00:00 2001 From: Jayash Date: Wed, 2 Oct 2024 05:48:22 +0530 Subject: [PATCH 175/319] Debug --- core/client/http.go | 48 +++++++++------------------------------------ core/client/set.go | 1 - 2 files changed, 9 insertions(+), 40 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index 2cb5edd89..1f0a74e5a 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -1,7 +1,6 @@ package client import ( - "context" "crypto/sha1" "encoding/hex" "encoding/json" @@ -21,16 +20,13 @@ import ( "time" ) -var customResolver = &net.Resolver{ - PreferGo: false, // Use the system resolver instead of Go's - Dial: func(ctx context.Context, network, addr string) (net.Conn, error) { - return net.DialTimeout(network, "8.8.8.8:53", 5*time.Second) // Using Google DNS - }, -} - var DefaultTransport = &http.Transport{ - Proxy: EnvProxy.Proxy, - DialContext: customResolver.Dial, + Proxy: EnvProxy.Proxy, + DialContext: (&net.Dialer{ + Timeout: 3 * time.Minute, + KeepAlive: 45 * time.Second, + DualStack: true, + }).DialContext, MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 45 * time.Second, @@ -104,7 +100,6 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] dominant := 200 wg := sync.WaitGroup{} - var debugUrl string cfg, err := conf.GetClientConfig() if err != nil { return nil, err @@ -115,7 +110,6 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] go func(sharder string) { defer wg.Done() urlString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath) - fmt.Println("Printing url... ", urlString) urlObj, err := url.Parse(urlString) if err != nil { log.Println(err) @@ -123,48 +117,34 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] } q := urlObj.Query() for k, v := range params { - fmt.Println("Printing keys and values... ", k, v) q.Add(k, v) } urlObj.RawQuery = q.Encode() - c := &http.Client{Transport: DefaultTransport} - response, err := c.Get(urlObj.String()) + client := &http.Client{Transport: DefaultTransport} + response, err := client.Get(urlObj.String()) if err != nil { - fmt.Println("Printing error... ", err.Error()) nodeClient.sharders.Fail(sharder) return } - fmt.Println("Printing response... ") - defer response.Body.Close() entityBytes, _ := io.ReadAll(response.Body) - fmt.Println("Printing entity bytes... ", string(entityBytes)) mu.Lock() if response.StatusCode > http.StatusBadRequest { nodeClient.sharders.Fail(sharder) } else { nodeClient.sharders.Success(sharder) } - - fmt.Println("Printing response status code... ", response.StatusCode) - responses[response.StatusCode]++ if responses[response.StatusCode] > maxCount { maxCount = responses[response.StatusCode] } - fmt.Println("Printing max count... ", maxCount) - if isCurrentDominantStatus(response.StatusCode, responses, maxCount) { dominant = response.StatusCode retObj = entityBytes - - fmt.Println("Printing retObj... ", string(retObj)) } - fmt.Println("Printing entity result... ", entityResult) - entityResult[sharder] = entityBytes nodeClient.sharders.Success(sharder) mu.Unlock() @@ -172,37 +152,27 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] } wg.Wait() - fmt.Println("Printing count... ", maxCount, cfg.SharderConsensous) - rate := float32(maxCount*100) / float32(cfg.SharderConsensous) if rate < consensusThresh { - err = errors.New("consensus_failed", fmt.Sprintf("consensus failed on sharders : %s : %f : %d", debugUrl, rate, cfg.SharderConsensous)) + err = errors.New("consensus_failed", "consensus failed on sharders") } - fmt.Println("Printing err... ", err.Error()) - if dominant != 200 { - fmt.Println("bad Printing dominant... ", dominant) var objmap map[string]json.RawMessage err := json.Unmarshal(retObj, &objmap) if err != nil { - fmt.Println("bad Printing error... ", err.Error()) return nil, errors.New("", string(retObj)) } var parsed string err = json.Unmarshal(objmap["error"], &parsed) if err != nil || parsed == "" { - fmt.Println("bad Printing parsed... ", parsed) return nil, errors.New("", string(retObj)) } - fmt.Println("2bad Printing parsed... ", parsed) return nil, errors.New("", parsed) } - fmt.Println("Printing good... ", string(retObj)) - if rate > consensusThresh { return retObj, nil } diff --git a/core/client/set.go b/core/client/set.go index 40a363934..8aacba17c 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -184,7 +184,6 @@ func PrivateKey() string { func ClientID() string { if client.wallet.ClientID == "" { fmt.Println("ClientID is empty") - return "f641b0cf2cd7a8a7224a760b3694c52ce617a68dc72b520f4b25c5dcef59d39e" } return client.wallet.ClientID } From e81075c60bdd91c25844617745018d58881a8490 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Wed, 2 Oct 2024 19:39:34 +0200 Subject: [PATCH 176/319] fix: fixed bugs --- wasmsdk/auth_txn.go | 22 ++++++++--- wasmsdk/proxy.go | 3 +- zcncore/zauth.go | 94 ++++++++++++++++++++++++--------------------- 3 files changed, 68 insertions(+), 51 deletions(-) diff --git a/wasmsdk/auth_txn.go b/wasmsdk/auth_txn.go index 5aa16cae6..020e5a92b 100644 --- a/wasmsdk/auth_txn.go +++ b/wasmsdk/auth_txn.go @@ -43,24 +43,34 @@ func registerZauthServer(serverAddr string) { } func zauthRetrieveKey(clientID, peerPublicKey, serverAddr, token string) (string, error) { - return zcncore.CallZauthRetreiveKey(serverAddr, token, clientID, peerPublicKey) + fmt.Println("retrieveing") + + d, err := zcncore.CallZauthRetreiveKey(serverAddr, token, clientID, peerPublicKey) + + fmt.Println(d, err) + + return d, err } // zvaultNewWallet generates new wallet -func zvaultNewWallet(serverAddr, token string) (string, error) { +func zvaultNewWallet(serverAddr, token string) error { return zcncore.CallZvaultNewWallet(serverAddr, token) } // zvaultNewSplit generates new split key for saved wallet -func zvaultNewSplit(clientID, serverAddr, token string, restrictions []string) (string, error) { - return zcncore.CallZvaultNewSplit(serverAddr, token, clientID, restrictions) +func zvaultNewSplit(clientID, serverAddr, token string) error { + return zcncore.CallZvaultNewSplit(serverAddr, token, clientID) +} + +func zvaultRetrieveRestrictions(peerPublicKey, serverAddr, token string) (string, error) { + return zcncore.CallZvaultRetrieveRestrictions(serverAddr, token, peerPublicKey) } -func zvaultUpdateRestrictions(clientID, peerPublicKey, serverAddr, token string, restrictions []string) (string, error) { +func zvaultUpdateRestrictions(clientID, peerPublicKey, serverAddr, token string, restrictions []string) error { return zcncore.CallZvaultUpdateRestrictions(serverAddr, token, clientID, peerPublicKey, restrictions) } -func zvaultStoreKey(serverAddr, token, privateKey string) (string, error) { +func zvaultStoreKey(serverAddr, token, privateKey string) error { return zcncore.CallZvaultStoreKeyString(serverAddr, token, privateKey) } diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index a3d04c39f..226872214 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -323,10 +323,11 @@ func main() { // zauth "registerZauthServer": registerZauthServer, - "zauthRetreiveKey": zauthRetrieveKey, + "zauthRetrieveKey": zauthRetrieveKey, // zvault "zvaultNewWallet": zvaultNewWallet, "zvaultNewSplit": zvaultNewSplit, + "zvaultRetrieveRestrictions": zvaultRetrieveRestrictions, "zvaultUpdateRestrictions": zvaultUpdateRestrictions, "zvaultStoreKey": zvaultStoreKey, "zvaultRetrieveKeys": zvaultRetrieveKeys, diff --git a/zcncore/zauth.go b/zcncore/zauth.go index 709b4ef1b..e011cb32c 100644 --- a/zcncore/zauth.go +++ b/zcncore/zauth.go @@ -194,16 +194,15 @@ func CallZauthDelete(serverAddr, token, clientID string) error { return errors.Errorf("code: %d", resp.StatusCode) } - return nil } -func CallZvaultNewWallet(serverAddr, token string) (string, error) { +func CallZvaultNewWallet(serverAddr, token string) error { endpoint := serverAddr + "/wallet" req, err := http.NewRequest("POST", endpoint, nil) if err != nil { - return "", errors.Wrap(err, "failed to create HTTP request") + return errors.Wrap(err, "failed to create HTTP request") } req.Header.Set("Content-Type", "application/json") @@ -212,43 +211,62 @@ func CallZvaultNewWallet(serverAddr, token string) (string, error) { client := &http.Client{} resp, err := client.Do(req) if err != nil { - return "", errors.Wrap(err, "failed to send HTTP request") + return errors.Wrap(err, "failed to send HTTP request") } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { errMsg, _ := io.ReadAll(resp.Body) if len(errMsg) > 0 { - return "", errors.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) + return errors.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) } - return "", errors.Errorf("code: %d", resp.StatusCode) - } - - d, err := io.ReadAll(resp.Body) - if err != nil { - return "", errors.Wrap(err, "failed to read response body") + return errors.Errorf("code: %d", resp.StatusCode) } - return string(d), nil + return nil } -func CallZvaultNewSplit(serverAddr, token, clientID string, restrictions []string) (string, error) { +func CallZvaultNewSplit(serverAddr, token, clientID string) error { endpoint := serverAddr + "/key/" + clientID - data, err := json.Marshal(createKeyRequest{ - Restrictions: restrictions, - }) + req, err := http.NewRequest("POST", endpoint, nil) if err != nil { - return "", errors.Wrap(err, "failed to serialize request") + return errors.Wrap(err, "failed to create HTTP request") } - req, err := http.NewRequest("POST", endpoint, bytes.NewReader(data)) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Jwt-Token", token) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return errors.Wrap(err, "failed to send HTTP request") + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + errMsg, _ := io.ReadAll(resp.Body) + if len(errMsg) > 0 { + return errors.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) + } + + return errors.Errorf("code: %d", resp.StatusCode) + } + + return nil +} + +func CallZvaultRetrieveRestrictions(serverAddr, token, peerPublicKey string) (string, error) { + endpoint := serverAddr + "/restrictions" + + req, err := http.NewRequest("GET", endpoint, nil) if err != nil { return "", errors.Wrap(err, "failed to create HTTP request") } req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Peer-Public-Key", peerPublicKey) req.Header.Set("X-Jwt-Token", token) client := &http.Client{} @@ -275,19 +293,19 @@ func CallZvaultNewSplit(serverAddr, token, clientID string, restrictions []strin return string(d), nil } -func CallZvaultUpdateRestrictions(serverAddr, token, clientID, peerPublicKey string, restrictions []string) (string, error) { +func CallZvaultUpdateRestrictions(serverAddr, token, clientID, peerPublicKey string, restrictions []string) error { endpoint := serverAddr + "/restrictions/" + clientID data, err := json.Marshal(updateRestrictionsRequest{ Restrictions: restrictions, }) if err != nil { - return "", errors.Wrap(err, "failed to serialize request") + return errors.Wrap(err, "failed to serialize request") } req, err := http.NewRequest("PUT", endpoint, bytes.NewReader(data)) if err != nil { - return "", errors.Wrap(err, "failed to create HTTP request") + return errors.Wrap(err, "failed to create HTTP request") } req.Header.Set("Content-Type", "application/json") @@ -297,29 +315,23 @@ func CallZvaultUpdateRestrictions(serverAddr, token, clientID, peerPublicKey str client := &http.Client{} resp, err := client.Do(req) if err != nil { - return "", errors.Wrap(err, "failed to send HTTP request") + return errors.Wrap(err, "failed to send HTTP request") } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { errMsg, _ := io.ReadAll(resp.Body) if len(errMsg) > 0 { - return "", errors.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) + return errors.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) } - return "", errors.Errorf("code: %d", resp.StatusCode) - } - - d, err := io.ReadAll(resp.Body) - if err != nil { - return "", errors.Wrap(err, "failed to read response body") + return errors.Errorf("code: %d", resp.StatusCode) } - return string(d), nil + return nil } -func CallZvaultStoreKeyString(serverAddr, token, privateKey string) (string, error) { - // Add your code here +func CallZvaultStoreKeyString(serverAddr, token, privateKey string) error { endpoint := serverAddr + "/store" reqData := struct { @@ -334,20 +346,20 @@ func CallZvaultStoreKeyString(serverAddr, token, privateKey string) (string, err err := encoder.Encode(reqData) if err != nil { - return "", errors.Wrap(err, "failed to create HTTP request") + return errors.Wrap(err, "failed to create HTTP request") } var req *http.Request req, err = http.NewRequest("POST", endpoint, &buff) if err != nil { - return "", errors.Wrap(err, "failed to create HTTP request") + return errors.Wrap(err, "failed to create HTTP request") } client := &http.Client{} resp, err := client.Do(req) if err != nil { - return "", errors.Wrap(err, "failed to send HTTP request") + return errors.Wrap(err, "failed to send HTTP request") } defer resp.Body.Close() @@ -355,22 +367,16 @@ func CallZvaultStoreKeyString(serverAddr, token, privateKey string) (string, err if resp.StatusCode != http.StatusOK { errMsg, _ := io.ReadAll(resp.Body) if len(errMsg) > 0 { - return "", errors.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) + return errors.Errorf("code: %d, err: %s", resp.StatusCode, string(errMsg)) } - return "", errors.Errorf("code: %d", resp.StatusCode) - } - - d, err := io.ReadAll(resp.Body) - if err != nil { - return "", errors.Wrap(err, "failed to read response body") + return errors.Errorf("code: %d", resp.StatusCode) } - return string(d), nil + return nil } func CallZvaultRetrieveKeys(serverAddr, token, clientID string) (string, error) { - // Add your code here endpoint := fmt.Sprintf("%s/keys/%s", serverAddr, clientID) req, err := http.NewRequest("GET", endpoint, nil) if err != nil { From adc37444f4673c8e22d4ae4b743f12dbad836db2 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Wed, 2 Oct 2024 20:07:52 +0200 Subject: [PATCH 177/319] fix: fixed bugs --- wasmsdk/auth_txn.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/wasmsdk/auth_txn.go b/wasmsdk/auth_txn.go index 020e5a92b..cd34f925c 100644 --- a/wasmsdk/auth_txn.go +++ b/wasmsdk/auth_txn.go @@ -43,13 +43,7 @@ func registerZauthServer(serverAddr string) { } func zauthRetrieveKey(clientID, peerPublicKey, serverAddr, token string) (string, error) { - fmt.Println("retrieveing") - - d, err := zcncore.CallZauthRetreiveKey(serverAddr, token, clientID, peerPublicKey) - - fmt.Println(d, err) - - return d, err + return zcncore.CallZauthRetreiveKey(serverAddr, token, clientID, peerPublicKey) } // zvaultNewWallet generates new wallet From 09d949acccaff94ab12bf6c3f1a6aeb1d110d215 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 3 Oct 2024 00:14:07 +0530 Subject: [PATCH 178/319] Fix --- core/client/set.go | 46 +++++++++++++++++++++++---------- core/conf/config.go | 3 --- core/transaction/entity_test.go | 1 - core/version/version.go | 2 +- mobilesdk/sdk/sdk.go | 1 - wasmsdk/sdk.go | 4 +-- winsdk/sdk.go | 1 - 7 files changed, 36 insertions(+), 22 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index 8aacba17c..065fb64c4 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -207,9 +207,8 @@ func GetClient() *zcncrypto.Wallet { // - fee: Preferred value for the transaction fee, just the first value is taken func InitSDK(walletJSON string, blockWorker, chainID, signatureScheme string, - preferredBlobbers []string, nonce int64, isSplitWallet, addWallet bool, - fee ...uint64) error { + options ...int) error { fmt.Println("2Init Sdk") @@ -223,21 +222,42 @@ func InitSDK(walletJSON string, SetWallet(wallet) SetSignatureScheme(signatureScheme) SetNonce(nonce) - if len(fee) > 0 { - SetTxnFee(fee[0]) + if len(options) > 0 { + SetTxnFee(uint64(options[0])) } } + var minConfirmation, minSubmit, confirmationChainLength, sharderConsensous int + if len(options) > 1 { + minConfirmation = options[1] + } else { + minConfirmation = 5 + } + if len(options) > 2 { + minSubmit = options[2] + } else { + minSubmit = 5 + } + if len(options) > 3 { + confirmationChainLength = options[3] + } else { + confirmationChainLength = 10 + } + if len(options) > 4 { + sharderConsensous = options[4] + } else { + sharderConsensous = 10 + } + err := Init(context.Background(), conf.Config{ - BlockWorker: blockWorker, - SignatureScheme: signatureScheme, - ChainID: chainID, - PreferredBlobbers: preferredBlobbers, - MaxTxnQuery: 5, - QuerySleepTime: 5, - MinSubmit: 10, - MinConfirmation: 10, - IsSplitWallet: isSplitWallet, + BlockWorker: blockWorker, + SignatureScheme: signatureScheme, + ChainID: chainID, + MinConfirmation: minConfirmation, + MinSubmit: minSubmit, + ConfirmationChainLength: confirmationChainLength, + SharderConsensous: sharderConsensous, + IsSplitWallet: isSplitWallet, }) if err != nil { return err diff --git a/core/conf/config.go b/core/conf/config.go index 799364cc5..4444892e3 100644 --- a/core/conf/config.go +++ b/core/conf/config.go @@ -46,8 +46,6 @@ const ( type Config struct { // BlockWorker the url of 0dns's network api BlockWorker string `json:"block_worker,omitempty"` - // PreferredBlobbers preferred blobbers on new allocation - PreferredBlobbers []string `json:"preferred_blobbers,omitempty"` // MinSubmit mininal submit from blobber MinSubmit int `json:"min_submit,omitempty"` @@ -171,7 +169,6 @@ func LoadConfig(v Reader) (Config, error) { } cfg.BlockWorker = blockWorker - cfg.PreferredBlobbers = v.GetStringSlice("preferred_blobbers") cfg.MinSubmit = minSubmit cfg.MinConfirmation = minCfm cfg.ConfirmationChainLength = CfmChainLength diff --git a/core/transaction/entity_test.go b/core/transaction/entity_test.go index 3076a2ae4..5ccf48576 100644 --- a/core/transaction/entity_test.go +++ b/core/transaction/entity_test.go @@ -11,7 +11,6 @@ func TestOptimisticVerificationLearning(t *testing.T) { t.Skip() conf.InitClientConfig(&conf.Config{ BlockWorker: "", - PreferredBlobbers: nil, MinSubmit: 0, MinConfirmation: 50, ConfirmationChainLength: 3, diff --git a/core/version/version.go b/core/version/version.go index d38e3845c..f96b8b848 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-184-gfe5f167c" +const VERSIONSTR = "v1.17.9-198-g41018734" diff --git a/mobilesdk/sdk/sdk.go b/mobilesdk/sdk/sdk.go index 9cd499f37..e20147348 100644 --- a/mobilesdk/sdk/sdk.go +++ b/mobilesdk/sdk/sdk.go @@ -128,7 +128,6 @@ func InitStorageSDK(clientJson string, configJson string) (*StorageSDK, error) { configObj.BlockWorker, configObj.ChainID, configObj.SignatureScheme, - configObj.PreferredBlobbers, 0, false, true); err != nil { l.Logger.Error(err) return nil, err diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index 2f3f6cd20..4cb7ca648 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -32,13 +32,13 @@ var CreateObjectURL func(buf []byte, mimeType string) string // - sharderconsensous is the number of sharders to reach consensus func initSDKs(chainID, blockWorker, signatureScheme string, minConfirmation, minSubmit, confirmationChainLength int, - zboxHost, zboxAppType string, sharderconsensous int, isSplit bool) error { + zboxHost, zboxAppType string, sharderConsensous int, isSplit bool) error { fmt.Println("1Init sdk") zboxApiClient.SetRequest(zboxHost, zboxAppType) - err := client.InitSDK("{}", blockWorker, chainID, signatureScheme, nil, 0, false, false) + err := client.InitSDK("{}", blockWorker, chainID, signatureScheme, 0, false, false, minConfirmation, minSubmit, confirmationChainLength, sharderConsensous) if err != nil { fmt.Println("wasm: InitStorageSDK ", err) return err diff --git a/winsdk/sdk.go b/winsdk/sdk.go index 86227ae14..76a3747f1 100644 --- a/winsdk/sdk.go +++ b/winsdk/sdk.go @@ -118,7 +118,6 @@ func InitSDKs(configJson *C.char) *C.char { l.Logger.Info(configObj.BlockWorker) l.Logger.Info(configObj.ChainID) l.Logger.Info(configObj.SignatureScheme) - l.Logger.Info(configObj.PreferredBlobbers) if zboxApiClient == nil { zboxApiClient = zboxapi.NewClient() From c05b9fa8155ed0fc5113bc71fcc7e156828985b1 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 3 Oct 2024 01:53:15 +0530 Subject: [PATCH 179/319] Fix --- wasmsdk/proxy.go | 52 +++++++----------------------------------------- 1 file changed, 7 insertions(+), 45 deletions(-) diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index 9157c52a9..531d83d1f 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -7,8 +7,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/0chain/gosdk/core/zcncrypto" - "log" "os" "runtime/debug" "strconv" @@ -201,6 +199,7 @@ func main() { jsbridge.BindAsyncFuncs(sdk, map[string]interface{}{ //sdk "init": initSDKs, + "setWallet": setWallet, "getPublicEncryptionKey": zcncore.GetPublicEncryptionKey, "hideLogs": hideLogs, "showLogs": showLogs, @@ -456,29 +455,10 @@ func main() { registerZauthServer(zauthServer) } - wallet := zcncrypto.Wallet{ - ClientID: clientID, - ClientKey: clientKey, - PeerPublicKey: peerPublicKey, - Keys: []zcncrypto.KeyPair{ - { - PublicKey: publicKey, - PrivateKey: privateKey, - }, - }, - Mnemonic: mnemonic, - IsSplit: isSplit, - } - - // Convert the wallet struct to JSON - _, err = json.MarshalIndent(wallet, "", " ") - if err != nil { - log.Fatalf("Error converting wallet to JSON: %v", err) - } - + setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemonic, isSplit) hideLogs() - debug.SetGCPercent(75) - debug.SetMemoryLimit(1 * 1024 * 1024 * 1024) //1GB + debug.SetGCPercent(40) + debug.SetMemoryLimit(300 * 1024 * 1024) //300MB err = startListener(respChan) if err != nil { fmt.Println("Error starting listener", err) @@ -487,8 +467,8 @@ func main() { } hideLogs() - debug.SetGCPercent(75) - debug.SetMemoryLimit(3.5 * 1024 * 1024 * 1024) //3.5 GB + debug.SetGCPercent(40) + debug.SetMemoryLimit(2.5 * 1024 * 1024 * 1024) //2.5 GB <-make(chan bool) @@ -539,24 +519,6 @@ func UpdateWalletWithEventData(data *safejs.Value) error { } fmt.Println("update wallet with event data") - wallet := zcncrypto.Wallet{ - ClientID: clientID, - ClientKey: clientKey, - PeerPublicKey: peerPublicKey, - Keys: []zcncrypto.KeyPair{ - { - PublicKey: publicKey, - PrivateKey: privateKey, - }, - }, - Mnemonic: mnemonic, - IsSplit: isSplit, - } - - // Convert the wallet struct to JSON - _, err = json.MarshalIndent(wallet, "", " ") - if err != nil { - log.Fatalf("Error converting wallet to JSON: %v", err) - } + setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemonic, isSplit) return nil } From 9e28ad5f31ff01c73a63868a608584aa77990678 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 3 Oct 2024 02:49:30 +0530 Subject: [PATCH 180/319] Remove unnecessary print statements added for debug --- core/client/set.go | 3 --- wasmsdk/sdk.go | 2 -- 2 files changed, 5 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index 065fb64c4..e0840d101 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -95,7 +95,6 @@ func GetClientSysKeys() []sys.KeyPair { // SetWallet should be set before any transaction or client specific APIs func SetWallet(w zcncrypto.Wallet) { - fmt.Println("Setting wallet : ", w.ClientID) client.wallet = &w } @@ -210,8 +209,6 @@ func InitSDK(walletJSON string, nonce int64, isSplitWallet, addWallet bool, options ...int) error { - fmt.Println("2Init Sdk") - if addWallet { wallet := zcncrypto.Wallet{} err := json.Unmarshal([]byte(walletJSON), &wallet) diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index 4cb7ca648..d891d7af7 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -34,8 +34,6 @@ func initSDKs(chainID, blockWorker, signatureScheme string, minConfirmation, minSubmit, confirmationChainLength int, zboxHost, zboxAppType string, sharderConsensous int, isSplit bool) error { - fmt.Println("1Init sdk") - zboxApiClient.SetRequest(zboxHost, zboxAppType) err := client.InitSDK("{}", blockWorker, chainID, signatureScheme, 0, false, false, minConfirmation, minSubmit, confirmationChainLength, sharderConsensous) From 2706fa6f11b0bf4f13075a6bd9173c54898a56b9 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 3 Oct 2024 02:50:06 +0530 Subject: [PATCH 181/319] Update version --- core/version/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/version/version.go b/core/version/version.go index f96b8b848..3d8903f20 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-198-g41018734" +const VERSIONSTR = "v1.17.9-201-g9e28ad5f" From 77ae37bc597889f90f776d9964ac26a4b1866d21 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 3 Oct 2024 03:35:05 +0530 Subject: [PATCH 182/319] Debug init sdk --- wasmsdk/sdk.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index d891d7af7..863c5457c 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -34,6 +34,9 @@ func initSDKs(chainID, blockWorker, signatureScheme string, minConfirmation, minSubmit, confirmationChainLength int, zboxHost, zboxAppType string, sharderConsensous int, isSplit bool) error { + // Print the parameters beautified + fmt.Printf("{ chainID: %s, blockWorker: %s, signatureScheme: %s, minConfirmation: %d, minSubmit: %d, confirmationChainLength: %d, zboxHost: %s, zboxAppType: %s, sharderConsensous: %d, isSplit: %t }\n", chainID, blockWorker, signatureScheme, minConfirmation, minSubmit, confirmationChainLength, zboxHost, zboxAppType, sharderConsensous, isSplit) + zboxApiClient.SetRequest(zboxHost, zboxAppType) err := client.InitSDK("{}", blockWorker, chainID, signatureScheme, 0, false, false, minConfirmation, minSubmit, confirmationChainLength, sharderConsensous) From 4c4ae053428ff2f1b237e6649f3f6c97868d3924 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 3 Oct 2024 04:07:21 +0530 Subject: [PATCH 183/319] Reverted lru cache changes in http.go and added some debug print statements --- core/client/http.go | 6 +++++- wasmsdk/bridge.go | 2 +- wasmsdk/proxy.go | 4 ++++ zboxcore/zboxutil/http.go | 42 +++++++++++++++++++++++++++------------ 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index 1f0a74e5a..ed3051e53 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -121,12 +121,16 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] } urlObj.RawQuery = q.Encode() client := &http.Client{Transport: DefaultTransport} - response, err := client.Get(urlObj.String()) + urlStr := urlObj.String() + response, err := client.Get(urlStr) if err != nil { + fmt.Println("Failing url:", urlStr, "on sharder:", sharder, "Error:", err) nodeClient.sharders.Fail(sharder) return } + fmt.Println("Success url:", urlStr, "on sharder:", sharder) + defer response.Body.Close() entityBytes, _ := io.ReadAll(response.Body) mu.Lock() diff --git a/wasmsdk/bridge.go b/wasmsdk/bridge.go index d7c43bd9e..02f14ae24 100644 --- a/wasmsdk/bridge.go +++ b/wasmsdk/bridge.go @@ -34,7 +34,7 @@ func initBridge( //nolint:unused gasLimit uint64, value int64, consensusThreshold float64) error { - if len(client.Wallet().ClientID) == 0 { + if len(client.ClientID()) == 0 { return errors.New("wallet_error", "wallet is not set") } diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index 531d83d1f..dcbfc3e5e 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -68,6 +68,8 @@ func main() { } //update sign with js sign + zcncrypto.Sign = signFunc + zcncore.SignFn = signFunc sys.Sign = func(hash, signatureScheme string, keys []sys.KeyPair) (string, error) { // js already has signatureScheme and keys return signFunc(hash) @@ -352,6 +354,8 @@ func main() { return result[0].String(), nil } //update sign with js sign + zcncrypto.Sign = signFunc + zcncore.SignFn = signFunc sys.Sign = func(hash, signatureScheme string, keys []sys.KeyPair) (string, error) { // js already has signatureScheme and keys return signFunc(hash) diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index 3f4759ba4..da5012a2e 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "github.com/hashicorp/golang-lru/v2/simplelru" "io" "net" "net/http" @@ -17,6 +18,7 @@ import ( "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/logger" + lru "github.com/hashicorp/golang-lru/v2" "github.com/hitenjain14/fasthttp" ) @@ -32,6 +34,7 @@ var ( Client HttpClient FastHttpClient FastClient log logger.Logger + SignCache simplelru.LRUCache[string, string] ) const ( @@ -166,6 +169,11 @@ func init() { fasthttp.SetBodySizePoolLimit(respBodyPoolLimit, respBodyPoolLimit) envProxy.initialize() log.Init(logger.DEBUG, "0box-sdk") + c, err := lru.New[string, string](1000) + if err != nil { + panic(err) + } + SignCache = c } func NewHTTPRequest(method string, url string, data []byte) (*http.Request, context.Context, context.CancelFunc, error) { @@ -195,9 +203,14 @@ func setClientInfoWithSign(req *http.Request, sig, allocation, baseURL string) e req.Header.Set(CLIENT_SIGNATURE_HEADER, sig) hashData := allocation + baseURL - sig2, err := client.Sign(encryption.Hash(hashData)) - if err != nil { - return err + sig2, ok := SignCache.Get(hashData) + if !ok { + var err error + sig2, err = client.Sign(encryption.Hash(hashData)) + SignCache.Add(hashData, sig2) + if err != nil { + return err + } } req.Header.Set(CLIENT_SIGNATURE_HEADER_V2, sig2) return nil @@ -580,7 +593,7 @@ func NewFastUploadRequest(baseURL, allocationID string, allocationTx string, bod req.SetBodyRaw(body) // set header: X-App-Client-Signature - if err := setFastClientInfoWithSign(req, allocationTx); err != nil { + if err := setFastClientInfoWithSign(req, allocationTx, baseURL); err != nil { return nil, err } @@ -588,7 +601,7 @@ func NewFastUploadRequest(baseURL, allocationID string, allocationTx string, bod return req, nil } -func setFastClientInfoWithSign(req *fasthttp.Request, allocation string) error { +func setFastClientInfoWithSign(req *fasthttp.Request, allocation, baseURL string) error { req.Header.Set("X-App-Client-ID", client.ClientID()) req.Header.Set("X-App-Client-Key", client.PublicKey()) @@ -597,7 +610,16 @@ func setFastClientInfoWithSign(req *fasthttp.Request, allocation string) error { return err } req.Header.Set(CLIENT_SIGNATURE_HEADER, sign) - + hashData := allocation + baseURL + sig2, ok := SignCache.Get(hashData) + if !ok { + sig2, err = client.Sign(encryption.Hash(hashData)) + SignCache.Add(hashData, sig2) + if err != nil { + return err + } + } + req.Header.Set(CLIENT_SIGNATURE_HEADER_V2, sig2) return nil } @@ -738,11 +760,6 @@ func NewFastDownloadRequest(baseUrl, allocationID, allocationTx string) (*fastht return nil, err } - // url := fmt.Sprintf("%s%s%s", baseUrl, DOWNLOAD_ENDPOINT, allocation) - // req, err := http.NewRequest(http.MethodGet, u.String(), nil) - // if err != nil { - // return nil, err - // } req := fasthttp.AcquireRequest() req.SetRequestURI(u.String()) req.Header.Set("X-App-Client-ID", client.ClientID()) @@ -907,8 +924,7 @@ func HttpDo(ctx context.Context, cncl context.CancelFunc, req *http.Request, f f select { case <-ctx.Done(): - // If the context is canceled or times out, return the context's error. - <-c // Wait for the goroutine to complete before returning. + <-c // Wait for f to return. return ctx.Err() case err := <-c: return err From 1dd2608b0468f379242665add60167a2215e08a6 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 3 Oct 2024 04:15:22 +0530 Subject: [PATCH 184/319] Fix sign function --- core/version/version.go | 2 +- wasmsdk/proxy.go | 1 + zcncore/wallet_base.go | 9 +++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/core/version/version.go b/core/version/version.go index 3d8903f20..7e439dc89 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-201-g9e28ad5f" +const VERSIONSTR = "v1.17.9-204-g4c4ae053" diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index dcbfc3e5e..f47235b6c 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -16,6 +16,7 @@ import ( "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/core/version" + "github.com/0chain/gosdk/core/zcncrypto" "github.com/0chain/gosdk/wasmsdk/jsbridge" "github.com/0chain/gosdk/zboxcore/sdk" "github.com/0chain/gosdk/zcncore" diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 0d2d041b6..82c77c823 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -372,3 +372,12 @@ func GetIdForUrl(url string) string { } return "" } + +var SignFn = func(hash string) (string, error) { + sigScheme := zcncrypto.NewSignatureScheme(client.SignatureScheme()) + err := sigScheme.SetPrivateKey(client.PrivateKey()) + if err != nil { + return "", err + } + return sigScheme.Sign(hash) +} From 859dddf30b23d3b30056b818ad678f4e97fcfa87 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 3 Oct 2024 04:41:44 +0530 Subject: [PATCH 185/319] Remove print statements for system tests runs --- core/client/http.go | 6 +----- core/version/version.go | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index ed3051e53..1f0a74e5a 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -121,16 +121,12 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] } urlObj.RawQuery = q.Encode() client := &http.Client{Transport: DefaultTransport} - urlStr := urlObj.String() - response, err := client.Get(urlStr) + response, err := client.Get(urlObj.String()) if err != nil { - fmt.Println("Failing url:", urlStr, "on sharder:", sharder, "Error:", err) nodeClient.sharders.Fail(sharder) return } - fmt.Println("Success url:", urlStr, "on sharder:", sharder) - defer response.Body.Close() entityBytes, _ := io.ReadAll(response.Body) mu.Lock() diff --git a/core/version/version.go b/core/version/version.go index 7e439dc89..e2a57d7d2 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-204-g4c4ae053" +const VERSIONSTR = "v1.17.9-205-g1dd2608b" From d2713add7aa8f64847376934bd68c9d6abbb22f0 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 3 Oct 2024 04:53:15 +0530 Subject: [PATCH 186/319] Debug version go 1.23.2 --- .github/workflows/build-sdks.yml | 22 +++++++++++----------- .github/workflows/tests.yml | 4 ++-- core/version/version.go | 2 +- go.mod | 4 +--- znft/example/go.mod | 4 +--- 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/.github/workflows/build-sdks.yml b/.github/workflows/build-sdks.yml index b024d1fec..c8ae4e35a 100644 --- a/.github/workflows/build-sdks.yml +++ b/.github/workflows/build-sdks.yml @@ -24,10 +24,10 @@ jobs: - name: Checkout code uses: actions/checkout@v2 - - name: Set up Go 1.22 + - name: Set up Go 1.23 uses: actions/setup-go@v3 with: - go-version: 1.22 + go-version: 1.23 - name: Clean build run: make clean-mobilesdk @@ -96,10 +96,10 @@ jobs: - name: Checkout code uses: actions/checkout@v2 - - name: Set up Go 1.22 + - name: Set up Go 1.23 uses: actions/setup-go@v3 with: - go-version: 1.22 + go-version: 1.23 - name: Install deps run: | @@ -199,10 +199,10 @@ jobs: - name: Checkout code uses: actions/checkout@v2 - - name: Set up Go 1.22 + - name: Set up Go 1.23 uses: actions/setup-go@v3 with: - go-version: 1.22 + go-version: 1.23 - name: Clean build run: make clean-mobilesdk @@ -271,10 +271,10 @@ jobs: - name: Checkout code uses: actions/checkout@v2 - - name: Set up Go 1.22 + - name: Set up Go 1.23 uses: actions/setup-go@v3 with: - go-version: 1.22 + go-version: 1.23 - name: Install deps run: | @@ -335,10 +335,10 @@ jobs: name: Build-wasm runs-on: [self-hosted, arc-runner] steps: - - name: Set up Go 1.22 + - name: Set up Go 1.23 uses: actions/setup-go@v3 with: - go-version: 1.22 + go-version: 1.23 - name: Checkout uses: actions/checkout@v3 @@ -349,7 +349,7 @@ jobs: sudo apt-get -y install build-essential nghttp2 libnghttp2-dev libssl-dev wget - name: Build - run: docker run --rm -v $PWD:/gosdk -w /gosdk golang:1.22 make wasm-build + run: docker run --rm -v $PWD:/gosdk -w /gosdk golang:1.23 make wasm-build - name: 'Upload Artifact' uses: actions/upload-artifact@v3 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 75aad9557..ad9ba23e7 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -167,10 +167,10 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Set up Go 1.22 + - name: Set up Go 1.23 uses: actions/setup-go@v2 with: - go-version: 1.22 + go-version: 1.23 - uses: actions/setup-node@v2 with: diff --git a/core/version/version.go b/core/version/version.go index e2a57d7d2..952bbff70 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-205-g1dd2608b" +const VERSIONSTR = "v1.17.9-206-g859dddf3" diff --git a/go.mod b/go.mod index d7713db2f..aad9909de 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,6 @@ module github.com/0chain/gosdk -go 1.22.0 - -toolchain go1.22.1 +go 1.23.2 require ( github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565 diff --git a/znft/example/go.mod b/znft/example/go.mod index 8f6566b01..8eebd1585 100644 --- a/znft/example/go.mod +++ b/znft/example/go.mod @@ -1,8 +1,6 @@ module example -go 1.22.0 - -toolchain go1.22.1 +go 1.23.2 require github.com/0chain/gosdk v1.8.9 From 47d4aca424c7c8102fff7299b7ac6436f13a007a Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 3 Oct 2024 04:56:04 +0530 Subject: [PATCH 187/319] Revert "Remove print statements for system tests runs" This reverts commit 859dddf3 --- core/client/http.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/client/http.go b/core/client/http.go index 1f0a74e5a..ed3051e53 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -121,12 +121,16 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] } urlObj.RawQuery = q.Encode() client := &http.Client{Transport: DefaultTransport} - response, err := client.Get(urlObj.String()) + urlStr := urlObj.String() + response, err := client.Get(urlStr) if err != nil { + fmt.Println("Failing url:", urlStr, "on sharder:", sharder, "Error:", err) nodeClient.sharders.Fail(sharder) return } + fmt.Println("Success url:", urlStr, "on sharder:", sharder) + defer response.Body.Close() entityBytes, _ := io.ReadAll(response.Body) mu.Lock() From 0bedc9b7644fe3262f4b3e41642657b18bf011e3 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Thu, 3 Oct 2024 16:18:15 +0200 Subject: [PATCH 188/319] fix: fixed bugs --- zcncore/zauth.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/zcncore/zauth.go b/zcncore/zauth.go index e011cb32c..7e5b97908 100644 --- a/zcncore/zauth.go +++ b/zcncore/zauth.go @@ -89,10 +89,6 @@ var AvailableRestrictions = map[string][]string{ }, } -type createKeyRequest struct { - Restrictions []string `json:"restrictions"` -} - type updateRestrictionsRequest struct { Restrictions []string `json:"restrictions"` } @@ -140,8 +136,8 @@ func CallZauthRetreiveKey(serverAddr, token, clientID, peerPublicKey string) (st } func CallZauthRevoke(serverAddr, token, clientID, peerPublicKey string) error { - endpoint := serverAddr + "/revoke/" + clientID - endpoint += "?peer_public_key=" + peerPublicKey + endpoint := serverAddr + "/revoke/" + clientID + "?peer_public_key=" + peerPublicKey + req, err := http.NewRequest("POST", endpoint, nil) if err != nil { return errors.Wrap(err, "failed to create HTTP request") @@ -407,8 +403,8 @@ func CallZvaultRetrieveKeys(serverAddr, token, clientID string) (string, error) } func CallZvaultDeletePrimaryKey(serverAddr, token, clientID string) error { - // Add your code here endpoint := serverAddr + "/delete/" + clientID + req, err := http.NewRequest("POST", endpoint, nil) if err != nil { return errors.Wrap(err, "failed to create HTTP request") From 338ff552eef8c88bc400cdeb58de0008c7520c07 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 3 Oct 2024 22:55:11 +0530 Subject: [PATCH 189/319] Update version --- core/version/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/version/version.go b/core/version/version.go index 952bbff70..f26bdc2ea 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-206-g859dddf3" +const VERSIONSTR = "v1.17.9-208-g47d4aca4" From 7f6acb2c924edb58923db71180fe5d738be52ad9 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 3 Oct 2024 23:25:14 +0530 Subject: [PATCH 190/319] Fix default transport --- core/client/http.go | 23 ++++++----------------- core/client/transport.go | 21 +++++++++++++++++++++ core/client/transport_wasm.go | 20 ++++++++++++++++++++ 3 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 core/client/transport.go create mode 100644 core/client/transport_wasm.go diff --git a/core/client/http.go b/core/client/http.go index ed3051e53..d493c063a 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -11,7 +11,6 @@ import ( "github.com/hashicorp/go-retryablehttp" "github.com/shopspring/decimal" "io" - "log" "net" "net/http" "net/url" @@ -20,20 +19,6 @@ import ( "time" ) -var DefaultTransport = &http.Transport{ - Proxy: EnvProxy.Proxy, - DialContext: (&net.Dialer{ - Timeout: 3 * time.Minute, - KeepAlive: 45 * time.Second, - DualStack: true, - }).DialContext, - MaxIdleConns: 100, - IdleConnTimeout: 90 * time.Second, - TLSHandshakeTimeout: 45 * time.Second, - ExpectContinueTimeout: 1 * time.Second, - MaxIdleConnsPerHost: 25, -} - // SCRestAPIHandler is a function type to handle the response from the SC Rest API // // `response` - the response from the SC Rest API @@ -52,6 +37,10 @@ const ( dialTimeout = 5 * time.Second ) +func init() { + envProxy.Initialize() +} + // NewClient creates default http.Client with timeouts. func NewClient() *http.Client { return &http.Client{ @@ -112,7 +101,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] urlString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath) urlObj, err := url.Parse(urlString) if err != nil { - log.Println(err) + fmt.Println(err.Error()) return } q := urlObj.Query() @@ -253,7 +242,7 @@ func (pfe *proxyFromEnv) Proxy(req *http.Request) (proxy *url.URL, err error) { return http.ProxyFromEnvironment(req) } -var EnvProxy proxyFromEnv +var envProxy proxyFromEnv type proxyFromEnv struct { HTTPProxy string diff --git a/core/client/transport.go b/core/client/transport.go new file mode 100644 index 000000000..ea7532139 --- /dev/null +++ b/core/client/transport.go @@ -0,0 +1,21 @@ +package client + +import ( + "net" + "net/http" + "time" +) + +var DefaultTransport = &http.Transport{ + Proxy: envProxy.Proxy, + DialContext: (&net.Dialer{ + Timeout: 3 * time.Minute, + KeepAlive: 45 * time.Second, + DualStack: true, + }).DialContext, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 45 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + MaxIdleConnsPerHost: 25, +} diff --git a/core/client/transport_wasm.go b/core/client/transport_wasm.go new file mode 100644 index 000000000..07c2ee23a --- /dev/null +++ b/core/client/transport_wasm.go @@ -0,0 +1,20 @@ +//go:build js && wasm +// +build js,wasm + +package client + +import ( + "net/http" + "time" +) + +// Run the HTTP request in a goroutine and pass the response to f. +var transport = &http.Transport{ + Proxy: http.ProxyFromEnvironment, + MaxIdleConns: 1000, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + MaxIdleConnsPerHost: 5, + ForceAttemptHTTP2: true, +} From 747fb4b049e8665d8fe366bb9293b9bed0ea2921 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 3 Oct 2024 23:26:53 +0530 Subject: [PATCH 191/319] Fix default transport --- core/version/version.go | 2 +- zboxcore/zboxutil/transport_wasm.go | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/core/version/version.go b/core/version/version.go index f26bdc2ea..a91337faf 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-208-g47d4aca4" +const VERSIONSTR = "v1.17.9-210-g7f6acb2c" diff --git a/zboxcore/zboxutil/transport_wasm.go b/zboxcore/zboxutil/transport_wasm.go index f0a14af95..553f6d807 100644 --- a/zboxcore/zboxutil/transport_wasm.go +++ b/zboxcore/zboxutil/transport_wasm.go @@ -4,13 +4,12 @@ package zboxutil import ( - coreHttp "github.com/0chain/gosdk/core/client" "net/http" "time" ) var DefaultTransport = &http.Transport{ - Proxy: coreHttp.EnvProxy.Proxy, + Proxy: envProxy.Proxy, MaxIdleConns: 100, IdleConnTimeout: 60 * time.Second, From 3e6ca43e912fa5bcc4875e2da1f5005cddd52c9d Mon Sep 17 00:00:00 2001 From: Jayash Date: Fri, 4 Oct 2024 04:33:39 +0530 Subject: [PATCH 192/319] Remove print statements --- core/client/http.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index d493c063a..57a981a09 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -113,13 +113,10 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] urlStr := urlObj.String() response, err := client.Get(urlStr) if err != nil { - fmt.Println("Failing url:", urlStr, "on sharder:", sharder, "Error:", err) nodeClient.sharders.Fail(sharder) return } - fmt.Println("Success url:", urlStr, "on sharder:", sharder) - defer response.Body.Close() entityBytes, _ := io.ReadAll(response.Body) mu.Lock() From dad2a04f084aea9fb4c0c03e8123f09bc925eeb9 Mon Sep 17 00:00:00 2001 From: Jayash Date: Fri, 4 Oct 2024 04:35:34 +0530 Subject: [PATCH 193/319] Added retries in http --- core/client/http.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index 57a981a09..e66802511 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -109,12 +109,26 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] q.Add(k, v) } urlObj.RawQuery = q.Encode() - client := &http.Client{Transport: DefaultTransport} + c := &http.Client{Transport: DefaultTransport} urlStr := urlObj.String() - response, err := client.Get(urlStr) - if err != nil { - nodeClient.sharders.Fail(sharder) - return + const maxRetries = 3 + var response *http.Response + for retries := 0; retries < maxRetries; retries++ { + response, err = c.Get(urlStr) + if err == nil { + // Success, exit the loop + break + } + + // Log the failure and retry + if retries == maxRetries-1 { + // If max retries reached, mark the sharder as failed + nodeClient.sharders.Fail(sharder) + return + } + + // Optionally: Add a delay before retrying + time.Sleep(time.Second * 1) // wait for 1 second before retrying } defer response.Body.Close() From 3ba143012670692c02ab1555097ecf9d4ec44f46 Mon Sep 17 00:00:00 2001 From: Jayash Date: Fri, 4 Oct 2024 04:35:56 +0530 Subject: [PATCH 194/319] Added retries in http --- core/version/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/version/version.go b/core/version/version.go index a91337faf..ab5dcd955 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-210-g7f6acb2c" +const VERSIONSTR = "v1.17.9-213-gdad2a04f" From 0a23674147fa34c76f582a115d89e870226f2209 Mon Sep 17 00:00:00 2001 From: Jayash Date: Fri, 4 Oct 2024 04:51:01 +0530 Subject: [PATCH 195/319] Change http request --- core/client/http.go | 37 ++++++++++++------------------------- core/version/version.go | 2 +- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index e66802511..3991c3ec2 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -109,31 +109,18 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] q.Add(k, v) } urlObj.RawQuery = q.Encode() - c := &http.Client{Transport: DefaultTransport} - urlStr := urlObj.String() - const maxRetries = 3 - var response *http.Response - for retries := 0; retries < maxRetries; retries++ { - response, err = c.Get(urlStr) - if err == nil { - // Success, exit the loop - break - } - - // Log the failure and retry - if retries == maxRetries-1 { - // If max retries reached, mark the sharder as failed - nodeClient.sharders.Fail(sharder) - return - } - - // Optionally: Add a delay before retrying - time.Sleep(time.Second * 1) // wait for 1 second before retrying + + req, err := util.NewHTTPGetRequest(urlObj.String()) + if err != nil { + fmt.Println("1Error creating request", err.Error()) + return + } + response, err := req.Get() + if err != nil { + fmt.Println("2Error getting response", err.Error()) + return } - defer response.Body.Close() - entityBytes, _ := io.ReadAll(response.Body) - mu.Lock() if response.StatusCode > http.StatusBadRequest { nodeClient.sharders.Fail(sharder) } else { @@ -146,10 +133,10 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] if isCurrentDominantStatus(response.StatusCode, responses, maxCount) { dominant = response.StatusCode - retObj = entityBytes + retObj = []byte(response.Body) } - entityResult[sharder] = entityBytes + entityResult[sharder] = []byte(response.Body) nodeClient.sharders.Success(sharder) mu.Unlock() }(sharder) diff --git a/core/version/version.go b/core/version/version.go index ab5dcd955..1c700fc77 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-213-gdad2a04f" +const VERSIONSTR = "v1.17.9-214-g3ba14301" From abe57d650922b6f74c88735814c86a732495b5fd Mon Sep 17 00:00:00 2001 From: Jayash Date: Fri, 4 Oct 2024 05:01:52 +0530 Subject: [PATCH 196/319] Change http request --- core/client/http.go | 148 +--------------------------------- core/client/transport.go | 21 ----- core/client/transport_wasm.go | 20 ----- 3 files changed, 1 insertion(+), 188 deletions(-) delete mode 100644 core/client/transport.go delete mode 100644 core/client/transport_wasm.go diff --git a/core/client/http.go b/core/client/http.go index 3991c3ec2..bd418f6fa 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -1,22 +1,15 @@ package client import ( - "crypto/sha1" - "encoding/hex" "encoding/json" "fmt" "github.com/0chain/errors" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/util" - "github.com/hashicorp/go-retryablehttp" "github.com/shopspring/decimal" - "io" - "net" "net/http" "net/url" - "os" "sync" - "time" ) // SCRestAPIHandler is a function type to handle the response from the SC Rest API @@ -26,45 +19,6 @@ import ( // `err` - the error if any type SCRestAPIHandler func(response map[string][]byte, numSharders int, err error) -const ( - // clientTimeout represents default http.Client timeout. - clientTimeout = 10 * time.Second - - // tlsHandshakeTimeout represents default http.Transport TLS handshake timeout. - tlsHandshakeTimeout = 5 * time.Second - - // dialTimeout represents default net.Dialer timeout. - dialTimeout = 5 * time.Second -) - -func init() { - envProxy.Initialize() -} - -// NewClient creates default http.Client with timeouts. -func NewClient() *http.Client { - return &http.Client{ - Timeout: clientTimeout, - Transport: &http.Transport{ - TLSHandshakeTimeout: tlsHandshakeTimeout, - DialContext: (&net.Dialer{ - Timeout: dialTimeout, - }).DialContext, - }, - } -} - -// NewRetryableClient creates default retryablehttp.Client with timeouts and embedded NewClient result. -func NewRetryableClient(retryMax int) *retryablehttp.Client { - client := retryablehttp.NewClient() - client.HTTPClient = NewClient() - client.RetryWaitMax = clientTimeout - client.RetryMax = retryMax - client.Logger = nil - - return client -} - // MakeSCRestAPICall makes a rest api call to the sharders. // - scAddress is the address of the smart contract // - relativePath is the relative path of the api @@ -73,6 +27,7 @@ func NewRetryableClient(retryMax int) *retryablehttp.Client { func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string, restApiUrls ...string) ([]byte, error) { const ( consensusThresh = float32(25.0) + ScRestApiUrl = "v1/screst/" ) restApiUrl := ScRestApiUrl @@ -183,107 +138,6 @@ func isCurrentDominantStatus(respStatus int, currentTotalPerStatus map[int]int, return currentTotalPerStatus[respStatus] == currentMax && (respStatus == 200 || currentTotalPerStatus[200] < currentMax) } -// hashAndBytesOfReader computes hash of readers data and returns hash encoded to hex and bytes of reader data. -// If error occurs while reading data from reader, it returns non nil error. -func hashAndBytesOfReader(r io.Reader) (hash string, reader []byte, err error) { //nolint:unused - h := sha1.New() - teeReader := io.TeeReader(r, h) - readerBytes, err := io.ReadAll(teeReader) - if err != nil { - return "", nil, err - } - - return hex.EncodeToString(h.Sum(nil)), readerBytes, nil -} - -// extractSharders returns string slice of randomly ordered sharders existing in the current network. -func extractSharders() []string { //nolint:unused - sharders := nodeClient.Network().Sharders - return util.GetRandom(sharders, len(sharders)) -} - -const ( - // ScRestApiUrl represents base URL path to execute smart contract rest points. - ScRestApiUrl = "v1/screst/" -) - -// makeScURL creates url.URL to make smart contract request to sharder. -func makeScURL(params map[string]string, sharder, restApiUrl, scAddress, relativePath string) *url.URL { //nolint:unused - uString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath) - - u, _ := url.Parse(uString) - q := u.Query() - for k, v := range params { - q.Add(k, v) - } - u.RawQuery = q.Encode() - - //log.Println("SC URL:", u.RawQuery) - //log.Println("Sharders:", sharder) - //log.Println("Rest API URL:", restApiUrl) - //log.Println("SC Address:", scAddress) - //log.Println("Relative Path:", relativePath) - - return u -} - -func (pfe *proxyFromEnv) Proxy(req *http.Request) (proxy *url.URL, err error) { - if pfe.isLoopback(req.URL.Host) { - switch req.URL.Scheme { - case "http": - return pfe.http, nil - case "https": - return pfe.https, nil - default: - } - } - return http.ProxyFromEnvironment(req) -} - -var envProxy proxyFromEnv - -type proxyFromEnv struct { - HTTPProxy string - HTTPSProxy string - NoProxy string - - http, https *url.URL -} - -func (pfe *proxyFromEnv) Initialize() { - pfe.HTTPProxy = getEnvAny("HTTP_PROXY", "http_proxy") - pfe.HTTPSProxy = getEnvAny("HTTPS_PROXY", "https_proxy") - pfe.NoProxy = getEnvAny("NO_PROXY", "no_proxy") - - if pfe.NoProxy != "" { - return - } - - if pfe.HTTPProxy != "" { - pfe.http, _ = url.Parse(pfe.HTTPProxy) - } - if pfe.HTTPSProxy != "" { - pfe.https, _ = url.Parse(pfe.HTTPSProxy) - } -} - -func (pfe *proxyFromEnv) isLoopback(host string) (ok bool) { - host, _, _ = net.SplitHostPort(host) - if host == "localhost" { - return true - } - return net.ParseIP(host).IsLoopback() -} - -func getEnvAny(names ...string) string { - for _, n := range names { - if val := os.Getenv(n); val != "" { - return val - } - } - return "" -} - func GetBalance(clientIDs ...string) (*GetBalanceResponse, error) { const GetBalance = "client/get/balance" var ( diff --git a/core/client/transport.go b/core/client/transport.go deleted file mode 100644 index ea7532139..000000000 --- a/core/client/transport.go +++ /dev/null @@ -1,21 +0,0 @@ -package client - -import ( - "net" - "net/http" - "time" -) - -var DefaultTransport = &http.Transport{ - Proxy: envProxy.Proxy, - DialContext: (&net.Dialer{ - Timeout: 3 * time.Minute, - KeepAlive: 45 * time.Second, - DualStack: true, - }).DialContext, - MaxIdleConns: 100, - IdleConnTimeout: 90 * time.Second, - TLSHandshakeTimeout: 45 * time.Second, - ExpectContinueTimeout: 1 * time.Second, - MaxIdleConnsPerHost: 25, -} diff --git a/core/client/transport_wasm.go b/core/client/transport_wasm.go deleted file mode 100644 index 07c2ee23a..000000000 --- a/core/client/transport_wasm.go +++ /dev/null @@ -1,20 +0,0 @@ -//go:build js && wasm -// +build js,wasm - -package client - -import ( - "net/http" - "time" -) - -// Run the HTTP request in a goroutine and pass the response to f. -var transport = &http.Transport{ - Proxy: http.ProxyFromEnvironment, - MaxIdleConns: 1000, - IdleConnTimeout: 90 * time.Second, - TLSHandshakeTimeout: 10 * time.Second, - ExpectContinueTimeout: 1 * time.Second, - MaxIdleConnsPerHost: 5, - ForceAttemptHTTP2: true, -} From 98bd273662a12e322e91ceb81fac1790339abdd9 Mon Sep 17 00:00:00 2001 From: Jayash Date: Fri, 4 Oct 2024 05:16:10 +0530 Subject: [PATCH 197/319] Change http request --- core/client/http.go | 2 -- core/util/httpnet.go | 22 +++++++++++++++++----- zboxcore/zboxutil/http.go | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index bd418f6fa..14018797f 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -37,7 +37,6 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] sharders := nodeClient.Network().Sharders responses := make(map[int]int) - mu := &sync.Mutex{} entityResult := make(map[string][]byte) var retObj []byte maxCount := 0 @@ -93,7 +92,6 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] entityResult[sharder] = []byte(response.Body) nodeClient.sharders.Success(sharder) - mu.Unlock() }(sharder) } wg.Wait() diff --git a/core/util/httpnet.go b/core/util/httpnet.go index de44210ba..c26ed3362 100644 --- a/core/util/httpnet.go +++ b/core/util/httpnet.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "fmt" "io" "net/http" "net/url" @@ -84,14 +85,17 @@ func init() { func httpDo(req *http.Request, ctx context.Context, cncl context.CancelFunc, f func(*http.Response, error) error) error { c := make(chan error, 1) + go func() { c <- f(Client.Do(req.WithContext(ctx))) }() - defer cncl() + select { case <-ctx.Done(): - transport.CancelRequest(req) //nolint - <-c // Wait for f to return. + // Use the cancel function only after trying to get the result. + <-c // Wait for f to return. return ctx.Err() case err := <-c: + // Ensure that we call cncl after we are done with the response + defer cncl() // Move this here to ensure we cancel after processing return err } } @@ -150,8 +154,11 @@ func NewHTTPPostRequest(url string, data interface{}) (*PostRequest, error) { func (r *GetRequest) Get() (*GetResponse, error) { response := &GetResponse{} presp, err := r.Post() + if err != nil { + return nil, err // Return early if there's an error + } response.PostResponse = presp - return response, err + return response, nil } func (r *PostRequest) Post() (*PostResponse, error) { @@ -162,6 +169,8 @@ func (r *PostRequest) Post() (*PostResponse, error) { } if resp.Body != nil { defer resp.Body.Close() + } else { + return fmt.Errorf("response body is nil") } rspBy, err := io.ReadAll(resp.Body) @@ -174,5 +183,8 @@ func (r *PostRequest) Post() (*PostResponse, error) { result.Body = string(rspBy) return nil }) - return result, err + if err != nil { + return nil, err // Ensure you propagate the error + } + return result, nil } diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index da5012a2e..304629b99 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -147,7 +147,7 @@ var envProxy proxyFromEnv func init() { Client = &http.Client{ - Transport: client.DefaultTransport, + Transport: http.DefaultTransport, } FastHttpClient = &fasthttp.Client{ From 7b354b7159f9848175ba45fe450fc47997258912 Mon Sep 17 00:00:00 2001 From: Jayash Date: Fri, 4 Oct 2024 05:18:26 +0530 Subject: [PATCH 198/319] Change http request --- core/version/version.go | 2 +- zcnbridge/http/client.go | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/core/version/version.go b/core/version/version.go index 1c700fc77..eea17c513 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-214-g3ba14301" +const VERSIONSTR = "v1.17.9-217-g98bd2736" diff --git a/zcnbridge/http/client.go b/zcnbridge/http/client.go index 1d2716e56..a8044cab7 100644 --- a/zcnbridge/http/client.go +++ b/zcnbridge/http/client.go @@ -1,7 +1,6 @@ package http import ( - http2 "github.com/0chain/gosdk/core/client" "net/http" "time" @@ -16,13 +15,13 @@ const ( // NewClient creates default http.Client with timeouts. func NewClient() *http.Client { return &http.Client{ - Transport: http2.DefaultTransport, + Transport: http.DefaultTransport, } } func CleanClient() *http.Client { client := &http.Client{ - Transport: http2.DefaultTransport, + Transport: http.DefaultTransport, } client.Timeout = 250 * time.Second return client @@ -32,7 +31,7 @@ func CleanClient() *http.Client { func NewRetryableClient(verbose bool) *retryablehttp.Client { client := retryablehttp.NewClient() client.HTTPClient = &http.Client{ - Transport: http2.DefaultTransport, + Transport: http.DefaultTransport, } if !verbose { From cc94d6ceaf711ae7303d1ea6a35f2f14c359c0cb Mon Sep 17 00:00:00 2001 From: Jayash Date: Fri, 4 Oct 2024 05:36:05 +0530 Subject: [PATCH 199/319] Revert go lang to 1.22 --- .github/workflows/tests.yml | 4 ++-- core/version/version.go | 2 +- go.mod | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ad9ba23e7..e67c7ddd2 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -26,7 +26,7 @@ jobs: - name: Set up Go 1.x uses: actions/setup-go@v3 with: - go-version: 1.21.5 + go-version: 1.22.0 - uses: actions/checkout@v3 @@ -51,7 +51,7 @@ jobs: - name: Set up Go 1.x uses: actions/setup-go@v3 with: - go-version: 1.21.5 + go-version: 1.22.0 - name: Install deps run: | diff --git a/core/version/version.go b/core/version/version.go index eea17c513..59c049acb 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-217-g98bd2736" +const VERSIONSTR = "v1.17.9-218-g7b354b71" diff --git a/go.mod b/go.mod index aad9909de..dc3f8468c 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/0chain/gosdk -go 1.23.2 +go 1.22.0 require ( github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565 From 08d441509e6e4457fe8c8b949f4e688a61df0b87 Mon Sep 17 00:00:00 2001 From: Jayash Date: Sat, 5 Oct 2024 01:24:23 +0530 Subject: [PATCH 200/319] Added storageVersion --- wasmsdk/allocation.go | 3 ++- zboxcore/sdk/sdk.go | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/wasmsdk/allocation.go b/wasmsdk/allocation.go index 472b7255f..7a6142832 100644 --- a/wasmsdk/allocation.go +++ b/wasmsdk/allocation.go @@ -87,7 +87,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(storageVersion, datashards, parityshards int, size int64, minReadPrice, maxReadPrice, minWritePrice, maxWritePrice int64, lock int64, blobberIds, blobberAuthTickets []string, setThirdPartyExtendable, IsEnterprise, force bool) ( *transaction.Transaction, error) { @@ -107,6 +107,7 @@ func createAllocation(datashards, parityshards int, size int64, BlobberIds: blobberIds, ThirdPartyExtendable: setThirdPartyExtendable, IsEnterprise: IsEnterprise, + StorageVersion: storageVersion, BlobberAuthTickets: blobberAuthTickets, Force: force, } diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index d91665636..506540d3d 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -736,6 +736,7 @@ type UpdateBlobber struct { IsShutdown *bool `json:"is_shutdown,omitempty"` NotAvailable *bool `json:"not_available,omitempty"` IsRestricted *bool `json:"is_restricted,omitempty"` + StorageVersion *int `json:"storage_version,omitempty"` } // ResetBlobberStatsDto represents blobber stats reset request. @@ -1153,6 +1154,7 @@ type CreateAllocationOptions struct { IsEnterprise bool FileOptionsParams *FileOptionsParameters Force bool + StorageVersion int } // CreateAllocationWith creates a new allocation with the given options for the current client using the SDK. @@ -1164,7 +1166,7 @@ func CreateAllocationWith(options CreateAllocationOptions) ( string, int64, *transaction.Transaction, error) { return CreateAllocationForOwner(client.GetClientID(), - client.GetClientPublicKey(), options.DataShards, options.ParityShards, + client.GetClientPublicKey(), options.DataShards, options.ParityShards, options.StorageVersion, options.Size, options.ReadPrice, options.WritePrice, options.Lock, options.BlobberIds, options.BlobberAuthTickets, options.ThirdPartyExtendable, options.IsEnterprise, options.Force, options.FileOptionsParams) } @@ -1186,7 +1188,7 @@ func CreateAllocationWith(options CreateAllocationOptions) ( // returns the hash of the transaction, the nonce of the transaction, the transaction object and an error if any. func CreateAllocationForOwner( owner, ownerpublickey string, - datashards, parityshards int, size int64, + datashards, parityshards, storageVersion int, size int64, readPrice, writePrice PriceRange, lock uint64, preferredBlobberIds, blobberAuthTickets []string, thirdPartyExtendable, IsEnterprise, force bool, fileOptionsParams *FileOptionsParameters, ) (hash string, nonce int64, txn *transaction.Transaction, err error) { @@ -1200,7 +1202,7 @@ func CreateAllocationForOwner( } allocationRequest, err := getNewAllocationBlobbers( - datashards, parityshards, size, readPrice, writePrice, preferredBlobberIds, blobberAuthTickets, force) + storageVersion, datashards, parityshards, size, readPrice, writePrice, preferredBlobberIds, blobberAuthTickets, force) if err != nil { return "", 0, nil, errors.New("failed_get_allocation_blobbers", "failed to get blobbers for allocation: "+err.Error()) } @@ -1214,6 +1216,7 @@ func CreateAllocationForOwner( allocationRequest["third_party_extendable"] = thirdPartyExtendable allocationRequest["file_options_changed"], allocationRequest["file_options"] = calculateAllocationFileOptions(63 /*0011 1111*/, fileOptionsParams) allocationRequest["is_enterprise"] = IsEnterprise + allocationRequest["storage_version"] = storageVersion var sn = transaction.SmartContractTxnData{ Name: transaction.NEW_ALLOCATION_REQUEST, @@ -1234,7 +1237,7 @@ func CreateAllocationForOwner( // // returns the list of blobber ids and an error if any. func GetAllocationBlobbers( - datashards, parityshards int, + storageVersion, datashards, parityshards int, size int64, isRestricted int, readPrice, writePrice PriceRange, @@ -1247,6 +1250,7 @@ func GetAllocationBlobbers( "read_price_range": readPrice, "write_price_range": writePrice, "is_restricted": isRestricted, + "storage_version": storageVersion, } allocationData, _ := json.Marshal(allocationRequest) @@ -1272,7 +1276,7 @@ func GetAllocationBlobbers( } func getNewAllocationBlobbers( - datashards, parityshards int, + storageVersion, datashards, parityshards int, size int64, readPrice, writePrice PriceRange, preferredBlobberIds, blobberAuthTickets []string, force bool, @@ -1287,12 +1291,13 @@ func getNewAllocationBlobbers( "blobber_auth_tickets": blobberAuthTickets, "read_price_range": readPrice, "write_price_range": writePrice, + "storage_version": storageVersion, }, nil } } allocBlobberIDs, err := GetAllocationBlobbers( - datashards, parityshards, size, 2, readPrice, writePrice, force, + storageVersion, datashards, parityshards, size, 2, readPrice, writePrice, force, ) if err != nil { return nil, err @@ -1321,6 +1326,7 @@ func getNewAllocationBlobbers( "blobber_auth_tickets": uniqueBlobberAuthTickets, "read_price_range": readPrice, "write_price_range": writePrice, + "storage_version": storageVersion, }, nil } From 4d227ca5c2c96c86743be17a35f841df19b17345 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sat, 5 Oct 2024 10:37:15 +0530 Subject: [PATCH 201/319] fix ut --- zboxcore/sdk/allocation_test.go | 13 +++++++------ zboxcore/sdk/copyworker_test.go | 18 +++++++++++------- zboxcore/sdk/deleteworker_test.go | 16 ++++++++++------ zboxcore/sdk/renameworker_test.go | 18 +++++++++++------- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/zboxcore/sdk/allocation_test.go b/zboxcore/sdk/allocation_test.go index cf283343c..c7a52d000 100644 --- a/zboxcore/sdk/allocation_test.go +++ b/zboxcore/sdk/allocation_test.go @@ -95,20 +95,21 @@ func setupMockHttpResponse( } func setupMockCommitRequest(a *Allocation) { - commitChan = make(map[string]chan *CommitRequest) + commitChan = make(map[string]chan CommitRequestInterface) for _, blobber := range a.Blobbers { if _, ok := commitChan[blobber.ID]; !ok { - commitChan[blobber.ID] = make(chan *CommitRequest, 1) + commitChan[blobber.ID] = make(chan CommitRequestInterface, 1) blobberChan := commitChan[blobber.ID] - go func(c <-chan *CommitRequest, blID string) { + go func(c <-chan CommitRequestInterface, blID string) { for { cm := <-c if cm != nil { - cm.result = &CommitResult{ + c := cm.(*CommitRequest) + c.result = &CommitResult{ Success: true, } - if cm.wg != nil { - cm.wg.Done() + if c.wg != nil { + c.wg.Done() } } } diff --git a/zboxcore/sdk/copyworker_test.go b/zboxcore/sdk/copyworker_test.go index 1bf5a2e00..e9b3cf089 100644 --- a/zboxcore/sdk/copyworker_test.go +++ b/zboxcore/sdk/copyworker_test.go @@ -216,7 +216,7 @@ func TestCopyRequest_copyBlobberObject(t *testing.T) { Baseurl: tt.name, }) req.copyMask = zboxutil.NewUint128(1).Lsh(uint64(len(req.blobbers))).Sub64(1) - _, err := req.copyBlobberObject(req.blobbers[0], 0) + _, err := req.copyBlobberObject(req.blobbers[0], 0, true) require.EqualValues(tt.wantErr, err != nil) if err != nil { require.Contains(errors.Top(err), tt.errMsg) @@ -335,15 +335,16 @@ func TestCopyRequest_ProcessCopy(t *testing.T) { }, nil) } - commitChan = make(map[string]chan *CommitRequest) + commitChan = make(map[string]chan CommitRequestInterface) for _, blobber := range req.blobbers { if _, ok := commitChan[blobber.ID]; !ok { - commitChan[blobber.ID] = make(chan *CommitRequest, 1) + commitChan[blobber.ID] = make(chan CommitRequestInterface, 1) } } blobberChan := commitChan go func() { - cm0 := <-blobberChan[req.blobbers[0].ID] + cm := <-blobberChan[req.blobbers[0].ID] + cm0 := cm.(*CommitRequest) require.EqualValues(t, cm0.blobber.ID, testName+mockBlobberId+strconv.Itoa(0)) cm0.result = &CommitResult{ Success: true, @@ -353,7 +354,8 @@ func TestCopyRequest_ProcessCopy(t *testing.T) { } }() go func() { - cm1 := <-blobberChan[req.blobbers[1].ID] + cm := <-blobberChan[req.blobbers[1].ID] + cm1 := cm.(*CommitRequest) require.EqualValues(t, cm1.blobber.ID, testName+mockBlobberId+strconv.Itoa(1)) cm1.result = &CommitResult{ Success: true, @@ -363,7 +365,8 @@ func TestCopyRequest_ProcessCopy(t *testing.T) { } }() go func() { - cm2 := <-blobberChan[req.blobbers[2].ID] + cm := <-blobberChan[req.blobbers[2].ID] + cm2 := cm.(*CommitRequest) require.EqualValues(t, cm2.blobber.ID, testName+mockBlobberId+strconv.Itoa(2)) cm2.result = &CommitResult{ Success: true, @@ -373,7 +376,8 @@ func TestCopyRequest_ProcessCopy(t *testing.T) { } }() go func() { - cm3 := <-blobberChan[req.blobbers[3].ID] + cm := <-blobberChan[req.blobbers[3].ID] + cm3 := cm.(*CommitRequest) require.EqualValues(t, cm3.blobber.ID, testName+mockBlobberId+strconv.Itoa(3)) cm3.result = &CommitResult{ Success: true, diff --git a/zboxcore/sdk/deleteworker_test.go b/zboxcore/sdk/deleteworker_test.go index c028be551..3c96512cd 100644 --- a/zboxcore/sdk/deleteworker_test.go +++ b/zboxcore/sdk/deleteworker_test.go @@ -262,15 +262,16 @@ func TestDeleteRequest_ProcessDelete(t *testing.T) { }, nil) } - commitChan = make(map[string]chan *CommitRequest) + commitChan = make(map[string]chan CommitRequestInterface) for _, blobber := range req.blobbers { if _, ok := commitChan[blobber.ID]; !ok { - commitChan[blobber.ID] = make(chan *CommitRequest, 1) + commitChan[blobber.ID] = make(chan CommitRequestInterface, 1) } } blobberChan := commitChan go func() { - cm0 := <-blobberChan[req.blobbers[0].ID] + cm := <-blobberChan[req.blobbers[0].ID] + cm0 := cm.(*CommitRequest) require.EqualValues(t, cm0.blobber.ID, testName+mockBlobberId+strconv.Itoa(0)) cm0.result = &CommitResult{ Success: true, @@ -280,7 +281,8 @@ func TestDeleteRequest_ProcessDelete(t *testing.T) { } }() go func() { - cm1 := <-blobberChan[req.blobbers[1].ID] + cm := <-blobberChan[req.blobbers[1].ID] + cm1 := cm.(*CommitRequest) require.EqualValues(t, cm1.blobber.ID, testName+mockBlobberId+strconv.Itoa(1)) cm1.result = &CommitResult{ Success: true, @@ -290,7 +292,8 @@ func TestDeleteRequest_ProcessDelete(t *testing.T) { } }() go func() { - cm2 := <-blobberChan[req.blobbers[2].ID] + cm := <-blobberChan[req.blobbers[2].ID] + cm2 := cm.(*CommitRequest) require.EqualValues(t, cm2.blobber.ID, testName+mockBlobberId+strconv.Itoa(2)) cm2.result = &CommitResult{ Success: true, @@ -300,7 +303,8 @@ func TestDeleteRequest_ProcessDelete(t *testing.T) { } }() go func() { - cm3 := <-blobberChan[req.blobbers[3].ID] + cm := <-blobberChan[req.blobbers[3].ID] + cm3 := cm.(*CommitRequest) require.EqualValues(t, cm3.blobber.ID, testName+mockBlobberId+strconv.Itoa(3)) cm3.result = &CommitResult{ Success: true, diff --git a/zboxcore/sdk/renameworker_test.go b/zboxcore/sdk/renameworker_test.go index 29aa6304f..49250bcb9 100644 --- a/zboxcore/sdk/renameworker_test.go +++ b/zboxcore/sdk/renameworker_test.go @@ -244,7 +244,7 @@ func TestRenameRequest_renameBlobberObject(t *testing.T) { req.blobbers = append(req.blobbers, &blockchain.StorageNode{ Baseurl: tt.name, }) - _, err := req.renameBlobberObject(req.blobbers[0], 0) + _, err := req.renameBlobberObject(req.blobbers[0], 0, true) require.EqualValues(tt.wantErr, err != nil, "Error: ", err) if err != nil { require.Contains(errors.Top(err), tt.errMsg) @@ -357,15 +357,16 @@ func TestRenameRequest_ProcessRename(t *testing.T) { }, nil) } - commitChan = make(map[string]chan *CommitRequest) + commitChan = make(map[string]chan CommitRequestInterface) for _, blobber := range req.blobbers { if _, ok := commitChan[blobber.ID]; !ok { - commitChan[blobber.ID] = make(chan *CommitRequest, 1) + commitChan[blobber.ID] = make(chan CommitRequestInterface, 1) } } blobberChan := commitChan go func() { - cm0 := <-blobberChan[req.blobbers[0].ID] + cm := <-blobberChan[req.blobbers[0].ID] + cm0 := cm.(*CommitRequest) require.EqualValues(t, cm0.blobber.ID, testName+mockBlobberId+strconv.Itoa(0)) cm0.result = &CommitResult{ Success: true, @@ -375,7 +376,8 @@ func TestRenameRequest_ProcessRename(t *testing.T) { } }() go func() { - cm1 := <-blobberChan[req.blobbers[1].ID] + cm := <-blobberChan[req.blobbers[1].ID] + cm1 := cm.(*CommitRequest) require.EqualValues(t, cm1.blobber.ID, testName+mockBlobberId+strconv.Itoa(1)) cm1.result = &CommitResult{ Success: true, @@ -385,7 +387,8 @@ func TestRenameRequest_ProcessRename(t *testing.T) { } }() go func() { - cm2 := <-blobberChan[req.blobbers[2].ID] + cm := <-blobberChan[req.blobbers[2].ID] + cm2 := cm.(*CommitRequest) require.EqualValues(t, cm2.blobber.ID, testName+mockBlobberId+strconv.Itoa(2)) cm2.result = &CommitResult{ Success: true, @@ -395,7 +398,8 @@ func TestRenameRequest_ProcessRename(t *testing.T) { } }() go func() { - cm3 := <-blobberChan[req.blobbers[3].ID] + cm := <-blobberChan[req.blobbers[3].ID] + cm3 := cm.(*CommitRequest) require.EqualValues(t, cm3.blobber.ID, testName+mockBlobberId+strconv.Itoa(3)) cm3.result = &CommitResult{ Success: true, From 07d6f358b0afc6fd62d38b45ba2a8bd62b25ac96 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sat, 5 Oct 2024 11:04:44 +0530 Subject: [PATCH 202/319] fix lint --- zboxcore/sdk/deleteworker.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index 53cef5346..aa95b08d7 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -46,8 +46,6 @@ type DeleteRequest struct { timestamp int64 } -const emptyHash = "e" - var errFileDeleted = errors.New("file_deleted", "file is already deleted") func (req *DeleteRequest) deleteBlobberFile( @@ -112,7 +110,7 @@ func (req *DeleteRequest) deleteBlobberFile( } if resp.StatusCode == http.StatusBadRequest { body, err := ioutil.ReadAll(resp.Body) - if err!= nil { + if err != nil { logger.Logger.Error("Failed to read response body", err) } From e36c5fe70112caa113b05082e36c70a89569ae8e Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sat, 5 Oct 2024 11:11:16 +0530 Subject: [PATCH 203/319] fix list and stats ut --- zboxcore/sdk/filestatsworker_test.go | 1 + zboxcore/sdk/listworker_test.go | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/zboxcore/sdk/filestatsworker_test.go b/zboxcore/sdk/filestatsworker_test.go index 60a3e17a0..18d12338d 100644 --- a/zboxcore/sdk/filestatsworker_test.go +++ b/zboxcore/sdk/filestatsworker_test.go @@ -104,6 +104,7 @@ func TestListRequest_getFileStatsInfoFromBlobber(t *testing.T) { Name: mockFileStatsName, BlobberID: mockBlobberId, BlobberURL: "Test_Success", + PathHash: fileref.GetReferenceLookup(mockAllocationId, mockRemoteFilePath), }, blobberIdx: mockBlobberIndex, respStatusCode: 200, diff --git a/zboxcore/sdk/listworker_test.go b/zboxcore/sdk/listworker_test.go index eacea00ea..9330a177a 100644 --- a/zboxcore/sdk/listworker_test.go +++ b/zboxcore/sdk/listworker_test.go @@ -125,8 +125,9 @@ func TestListRequest_getListInfoFromBlobber(t *testing.T) { parameters: parameters{ listHttpResp: listResponse{ ref: &fileref.Ref{ - AllocationID: mockAllocationId, - Type: mockType, + AllocationID: mockAllocationId, + Type: mockType, + AllocationRoot: mockAllocationRoot, }, }, ListResult: fileref.ListResult{ From 093c1556d82198239736cd576f8c20f45fd9e562 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sat, 5 Oct 2024 10:47:38 +0200 Subject: [PATCH 204/319] fix: fixed bugs --- zboxapi/sdk.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zboxapi/sdk.go b/zboxapi/sdk.go index 6fabacf3e..af733d88a 100644 --- a/zboxapi/sdk.go +++ b/zboxapi/sdk.go @@ -113,7 +113,7 @@ func (c *Client) createResty(ctx context.Context, csrfToken, userID string, head h["X-App-Timestamp"] = strconv.FormatInt(time.Now().Unix(), 10) if _, ok := h["X-App-ID-Token"]; !ok { - h["X-App-ID-Token"] = "*" //ignore firebase token in jwt requests + h["X-App-ID-Token"] = "*" } h["X-App-Type"] = c.appType From d2be48700481311c44472a4144ef07ef50c00a20 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Sat, 5 Oct 2024 14:48:55 +0530 Subject: [PATCH 205/319] fix wasm error --- wasmsdk/allocation.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wasmsdk/allocation.go b/wasmsdk/allocation.go index 7a6142832..3f3d230e3 100644 --- a/wasmsdk/allocation.go +++ b/wasmsdk/allocation.go @@ -67,7 +67,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{ From 7864f21a95115c3d9b875aaac1225880dfb3d90f Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Sat, 5 Oct 2024 16:38:55 +0530 Subject: [PATCH 206/319] use v2 as default --- wasmsdk/allocation.go | 4 ++-- zboxcore/sdk/blobber_operations.go | 6 +++--- zboxcore/sdk/sdk.go | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/wasmsdk/allocation.go b/wasmsdk/allocation.go index eb78afa10..e5ba1bbbf 100644 --- a/wasmsdk/allocation.go +++ b/wasmsdk/allocation.go @@ -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(storageVersion, datashards, parityshards int, size int64, +func createAllocation(datashards, parityshards int, size int64, minReadPrice, maxReadPrice, minWritePrice, maxWritePrice int64, lock int64, blobberIds, blobberAuthTickets []string, setThirdPartyExtendable, IsEnterprise, force bool) ( *transaction.Transaction, error) { @@ -108,7 +108,7 @@ func createAllocation(storageVersion, datashards, parityshards int, size int64, BlobberIds: blobberIds, ThirdPartyExtendable: setThirdPartyExtendable, IsEnterprise: IsEnterprise, - StorageVersion: storageVersion, + StorageVersion: sdk.StorageV2, BlobberAuthTickets: blobberAuthTickets, Force: force, } diff --git a/zboxcore/sdk/blobber_operations.go b/zboxcore/sdk/blobber_operations.go index 425df34b2..91e15774c 100644 --- a/zboxcore/sdk/blobber_operations.go +++ b/zboxcore/sdk/blobber_operations.go @@ -30,7 +30,7 @@ import ( // returns the hash of the transaction, the nonce of the transaction, the transaction object and an error if any. func CreateAllocationForOwner( owner, ownerpublickey string, - datashards, parityshards, storageVersion int, size int64, + datashards, parityshards int, size int64, readPrice, writePrice PriceRange, lock uint64, preferredBlobberIds, blobberAuthTickets []string, thirdPartyExtendable, IsEnterprise, force bool, fileOptionsParams *FileOptionsParameters, ) (hash string, nonce int64, txn *transaction.Transaction, err error) { @@ -44,7 +44,7 @@ func CreateAllocationForOwner( } allocationRequest, err := getNewAllocationBlobbers( - storageVersion, datashards, parityshards, size, readPrice, writePrice, preferredBlobberIds, blobberAuthTickets, force) + StorageV2, datashards, parityshards, size, readPrice, writePrice, preferredBlobberIds, blobberAuthTickets, force) if err != nil { return "", 0, nil, errors.New("failed_get_allocation_blobbers", "failed to get blobbers for allocation: "+err.Error()) } @@ -58,7 +58,7 @@ func CreateAllocationForOwner( allocationRequest["third_party_extendable"] = thirdPartyExtendable allocationRequest["file_options_changed"], allocationRequest["file_options"] = calculateAllocationFileOptions(63 /*0011 1111*/, fileOptionsParams) allocationRequest["is_enterprise"] = IsEnterprise - allocationRequest["storage_version"] = storageVersion + allocationRequest["storage_version"] = StorageV2 var sn = transaction.SmartContractTxnData{ Name: transaction.NEW_ALLOCATION_REQUEST, diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index 256a7d95d..a4c74f26f 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -974,7 +974,7 @@ func CreateAllocationWith(options CreateAllocationOptions) ( string, int64, *transaction.Transaction, error) { return CreateAllocationForOwner(client.GetClientID(), - client.GetClientPublicKey(), options.DataShards, options.ParityShards, options.StorageVersion, + client.GetClientPublicKey(), options.DataShards, options.ParityShards, options.Size, options.ReadPrice, options.WritePrice, options.Lock, options.BlobberIds, options.BlobberAuthTickets, options.ThirdPartyExtendable, options.IsEnterprise, options.Force, options.FileOptionsParams) } From b85d7b7e6df8d58c2dbeda70634e66836780cd4a Mon Sep 17 00:00:00 2001 From: Jayash Date: Sun, 6 Oct 2024 01:24:29 +0530 Subject: [PATCH 207/319] Fix --- core/client/http.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index 14018797f..6b4c49f8a 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -19,11 +19,6 @@ import ( // `err` - the error if any type SCRestAPIHandler func(response map[string][]byte, numSharders int, err error) -// MakeSCRestAPICall makes a rest api call to the sharders. -// - scAddress is the address of the smart contract -// - relativePath is the relative path of the api -// - params is the query parameters -// - handler is the handler function to handle the response func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string, restApiUrls ...string) ([]byte, error) { const ( consensusThresh = float32(25.0) @@ -38,10 +33,14 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] sharders := nodeClient.Network().Sharders responses := make(map[int]int) entityResult := make(map[string][]byte) - var retObj []byte - maxCount := 0 - dominant := 200 - wg := sync.WaitGroup{} + + var ( + retObj []byte + maxCount int + dominant = 200 + wg sync.WaitGroup + mu sync.Mutex // Mutex to protect shared resources + ) cfg, err := conf.GetClientConfig() if err != nil { @@ -52,6 +51,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] wg.Add(1) go func(sharder string) { defer wg.Done() + urlString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath) urlObj, err := url.Parse(urlString) if err != nil { @@ -69,17 +69,22 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] fmt.Println("1Error creating request", err.Error()) return } + response, err := req.Get() if err != nil { fmt.Println("2Error getting response", err.Error()) return } + mu.Lock() // Lock before updating shared maps + defer mu.Unlock() + if response.StatusCode > http.StatusBadRequest { nodeClient.sharders.Fail(sharder) } else { nodeClient.sharders.Success(sharder) } + responses[response.StatusCode]++ if responses[response.StatusCode] > maxCount { maxCount = responses[response.StatusCode] @@ -94,6 +99,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] nodeClient.sharders.Success(sharder) }(sharder) } + wg.Wait() rate := float32(maxCount*100) / float32(cfg.SharderConsensous) From 3221469e43e087cd1236fdf6965ea796dce30e4e Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sun, 6 Oct 2024 01:59:38 +0530 Subject: [PATCH 208/319] fetch alloc version from sharders --- zboxcore/sdk/allocation.go | 5 +++-- zboxcore/sdk/multi_operation_worker.go | 1 + zboxcore/sdk/repairworker.go | 1 - zboxcore/sdk/rollback.go | 28 ++++++++++++++++---------- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index ba1c6bbe4..e591c9967 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -435,8 +435,9 @@ func (a *Allocation) InitAllocation() { a.startWorker(a.ctx) InitCommitWorker(a.Blobbers) InitBlockDownloader(a.Blobbers, downloadWorkerCount) - a.StorageVersion = 1 - a.CheckAllocStatus() //nolint:errcheck + if a.StorageVersion == StorageV2 { + a.CheckAllocStatus() //nolint:errcheck + } a.initialized = true } diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 8a9d12be0..69227f66c 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -449,6 +449,7 @@ func (mo *MultiOperation) commitV2() error { if mo.getConsensus() != 0 { l.Logger.Info("Rolling back changes on minority blobbers") mo.allocationObj.RollbackWithMask(rollbackMask) + mo.allocationObj.checkStatus = false } for _, op := range mo.operations { op.Error(mo.allocationObj, mo.getConsensus(), err) diff --git a/zboxcore/sdk/repairworker.go b/zboxcore/sdk/repairworker.go index 0c355261e..b9243c8d9 100644 --- a/zboxcore/sdk/repairworker.go +++ b/zboxcore/sdk/repairworker.go @@ -458,7 +458,6 @@ func (r *RepairRequest) iterateDirV2(ctx context.Context) { } if len(ops) > 0 { r.repairOperation(r.allocation, ops) - ops = nil } } diff --git a/zboxcore/sdk/rollback.go b/zboxcore/sdk/rollback.go index fe865e88d..efc8e2c96 100644 --- a/zboxcore/sdk/rollback.go +++ b/zboxcore/sdk/rollback.go @@ -279,9 +279,8 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { l.Logger.Error("error during getWritemarker", zap.Error(err)) blobStatus.Status = "unavailable" } - if wr == nil { + if wr != nil { markerChan <- nil - } else { markerChan <- &RollbackBlobber{ blobber: blobber, lpm: wr, @@ -306,9 +305,11 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { versionMap := make(map[string][]*RollbackBlobber) var ( - prevVersion string - latestVersion string - highestTS int64 + prevVersion string + latestVersion string + consensusVersion string + highestTS int64 + req = a.DataShards ) for rb := range markerChan { @@ -334,10 +335,11 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { } versionMap[version] = append(versionMap[version], rb) + if len(versionMap[version]) >= req { + consensusVersion = version + } } - req := a.DataShards - if len(versionMap) == 0 { return Commit, blobberRes, nil } @@ -352,10 +354,8 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { } if len(versionMap[latestVersion]) >= req || len(versionMap[prevVersion]) >= req || len(versionMap) > 2 { - if len(versionMap[latestVersion]) >= req { - a.allocationRoot = versionMap[latestVersion][0].lpm.LatestWM.AllocationRoot - } else if len(versionMap[prevVersion]) > 0 { - a.allocationRoot = versionMap[prevVersion][0].lpm.LatestWM.AllocationRoot + if consensusVersion != "" { + a.allocationRoot = versionMap[consensusVersion][0].lpm.LatestWM.AllocationRoot } for _, rb := range versionMap[prevVersion] { blobberRes[rb.blobIndex].Status = "repair" @@ -391,6 +391,12 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { return Broken, blobberRes, common.NewError("rollback_failed", "Rollback failed") } + if versionMap[latestVersion][0].lpm.PrevWM != nil { + a.allocationRoot = versionMap[latestVersion][0].lpm.PrevWM.AllocationRoot + } else { + a.allocationRoot = "" + } + if errCnt == int32(fullConsensus) { return Repair, blobberRes, nil } From 2d561b5488e898da7adf168bc0b6b5c3b18374ee Mon Sep 17 00:00:00 2001 From: Jayash Date: Sun, 6 Oct 2024 23:52:07 +0530 Subject: [PATCH 209/319] Setup auth --- zcncore/wallet_base.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 82c77c823..1472bd319 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/util" + "net/http" "strings" "time" @@ -381,3 +382,27 @@ var SignFn = func(hash string) (string, error) { } return sigScheme.Sign(hash) } + +// SetupAuth prepare auth app with clientid, key and a set of public, private key and local publickey +// which is running on PC/Mac. +func SetupAuth(authHost, clientID, clientKey, publicKey, privateKey, localPublicKey string, cb AuthCallback) error { + go func() { + authHost = strings.TrimRight(authHost, "/") + data := map[string]string{"client_id": clientID, "client_key": clientKey, "public_key": publicKey, "private_key": privateKey, "peer_public_key": localPublicKey} + req, err := util.NewHTTPPostRequest(authHost+"/setup", data) + if err != nil { + logging.Error("new post request failed. ", err.Error()) + return + } + res, err := req.Post() + if err != nil { + logging.Error(authHost+"send error. ", err.Error()) + } + if res.StatusCode != http.StatusOK { + cb.OnSetupComplete(StatusError, res.Body) + return + } + cb.OnSetupComplete(StatusSuccess, "") + }() + return nil +} From 98606b34b1b477008f1f63d4a5f66eda4961e137 Mon Sep 17 00:00:00 2001 From: Jayash Date: Mon, 7 Oct 2024 00:32:14 +0530 Subject: [PATCH 210/319] Fix --- core/client/init_node.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/core/client/init_node.go b/core/client/init_node.go index 568f9c6e3..0b7a2993b 100644 --- a/core/client/init_node.go +++ b/core/client/init_node.go @@ -88,7 +88,7 @@ func (n *Node) ShouldUpdateNetwork() (bool, *conf.Network, error) { if err != nil { return false, nil, err } - network, err := GetNetwork(n.clientCtx, cfg.BlockWorker) + network, err := GetNetwork(n.clientCtx) if err != nil { logging.Error("Failed to get network details ", zap.Error(err), zap.String("block_worker", cfg.BlockWorker)) return false, nil, err @@ -126,7 +126,7 @@ func Init(ctx context.Context, cfg conf.Config) error { // set default value for options if unset setOptionsDefaultValue(&cfg) - network, err := GetNetwork(ctx, cfg.BlockWorker) + network, err := GetNetwork(ctx) if err != nil { logging.Error("Failed to get network details ", zap.Error(err), zap.Any("block_worker", cfg.BlockWorker)) return err @@ -180,8 +180,12 @@ func GetNode() (*Node, error) { } // GetNetwork gets current network details from 0chain network. -func GetNetwork(ctx context.Context, blockWorker string) (*conf.Network, error) { - networkUrl := blockWorker + "/network" +func GetNetwork(ctx context.Context) (*conf.Network, error) { + cfg, err := conf.GetClientConfig() + if err != nil { + return nil, err + } + networkUrl := cfg.BlockWorker + "/network" networkGetCtx, networkGetCancelCtx := context.WithTimeout(ctx, 60*time.Second) defer networkGetCancelCtx() req, err := util.NewHTTPGetRequestContext(networkGetCtx, networkUrl) From 58af8eb4bb1c1b9493da160a475f6055d08529a8 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Mon, 7 Oct 2024 18:40:08 +0530 Subject: [PATCH 211/319] add retries and update fasthttp --- zboxcore/sdk/blockdownloadworker.go | 2 +- zboxcore/sdk/chunked_upload_process_js.go | 12 ++++++++++++ zboxcore/sdk/rollback.go | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/zboxcore/sdk/blockdownloadworker.go b/zboxcore/sdk/blockdownloadworker.go index 603003903..eb62d1df2 100644 --- a/zboxcore/sdk/blockdownloadworker.go +++ b/zboxcore/sdk/blockdownloadworker.go @@ -175,7 +175,7 @@ func (req *BlockDownloadRequest) downloadBlobberBlock(fastClient *fasthttp.Clien return errors.New(RateLimitError, "Rate limit error") } - if statuscode == http.StatusInternalServerError { + if statuscode == http.StatusInternalServerError || statuscode == http.StatusBadGateway { shouldRetry = true return errors.New("internal_server_error", "Internal server error") } diff --git a/zboxcore/sdk/chunked_upload_process_js.go b/zboxcore/sdk/chunked_upload_process_js.go index 6c4022056..a4a7f0eb8 100644 --- a/zboxcore/sdk/chunked_upload_process_js.go +++ b/zboxcore/sdk/chunked_upload_process_js.go @@ -629,8 +629,12 @@ func sendUploadRequest(dataBuffers []*bytes.Buffer, contentSlice []string, blobb if err != nil { logger.Logger.Error("Upload : ", err) if errors.Is(err, fasthttp.ErrConnectionClosed) || errors.Is(err, syscall.EPIPE) || errors.Is(err, fasthttp.ErrDialTimeout) { + err = ErrNetwork return err, true } + if errors.Is(err, fasthttp.ErrTimeout) { + return ErrNetwork, false + } return fmt.Errorf("Error while doing reqeust. Error %s", err), false } @@ -652,6 +656,14 @@ func sendUploadRequest(dataBuffers []*bytes.Buffer, contentSlice []string, blobb return } + if resp.StatusCode() == http.StatusBadGateway { + logger.Logger.Error("Got bad gateway error") + time.Sleep(1 * time.Second) + shouldContinue = true + err = ErrNetwork + return + } + msg := string(respbody) logger.Logger.Error(blobberURL, " Upload error response: ", resp.StatusCode(), diff --git a/zboxcore/sdk/rollback.go b/zboxcore/sdk/rollback.go index 3a2a8485d..d15a5df1d 100644 --- a/zboxcore/sdk/rollback.go +++ b/zboxcore/sdk/rollback.go @@ -46,6 +46,7 @@ const ( var ( ErrRetryOperation = errors.New("retry_operation") ErrRepairRequired = errors.New("repair_required") + ErrNetwork = errors.New("network_error") ) type RollbackBlobber struct { From a09f65d63d18a06a994d7d734d45b4ca8e5e04e2 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Mon, 7 Oct 2024 20:11:58 +0530 Subject: [PATCH 212/319] fix lint --- zboxcore/sdk/blobber_operations_mobile.go | 3 ++- zboxcore/sdk/commitworker.go | 1 - zboxcore/sdk/deleteworker.go | 1 - zboxcore/sdk/multi_operation_worker.go | 1 - 4 files changed, 2 insertions(+), 4 deletions(-) diff --git a/zboxcore/sdk/blobber_operations_mobile.go b/zboxcore/sdk/blobber_operations_mobile.go index 2d87f4fb9..619ec9a46 100644 --- a/zboxcore/sdk/blobber_operations_mobile.go +++ b/zboxcore/sdk/blobber_operations_mobile.go @@ -45,7 +45,7 @@ func CreateAllocationForOwner( } allocationRequest, err := getNewAllocationBlobbers( - datashards, parityshards, size, readPrice, writePrice, preferredBlobberIds, blobberAuthTickets, force) + StorageV2, datashards, parityshards, size, readPrice, writePrice, preferredBlobberIds, blobberAuthTickets, force) if err != nil { return "", 0, nil, errors.New("failed_get_allocation_blobbers", "failed to get blobbers for allocation: "+err.Error()) } @@ -59,6 +59,7 @@ func CreateAllocationForOwner( allocationRequest["third_party_extendable"] = thirdPartyExtendable allocationRequest["file_options_changed"], allocationRequest["file_options"] = calculateAllocationFileOptions(63 /*0011 1111*/, fileOptionsParams) allocationRequest["is_enterprise"] = IsEnterprise + allocationRequest["storage_version"] = StorageV2 var sn = transaction.SmartContractTxnData{ Name: transaction.NEW_ALLOCATION_REQUEST, diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 9692d99c3..666f5d2e9 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -488,7 +488,6 @@ func (commitReq *CommitRequestV2) processCommit() { } rootHash := trie.GetRoot().CalcHash() rootWeight := trie.Weight() - pos = 0 elapsedProcessChanges := time.Since(now) - elapsedGetRefPath wg := sync.WaitGroup{} errSlice := make([]error, commitReq.commitMask.CountOnes()) diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index aa95b08d7..cdc9c2d90 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -502,7 +502,6 @@ func (deleteReq *DeleteRequest) processDeleteV2() ([]fileref.RefEntity, zboxutil if deleteReq.remotefilepath == "/" { return objectTreeRefs, deleteReq.deleteMask, errNoChange } - pos = 0 deleteReq.consensus.Reset() for i := deleteReq.deleteMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { pos = uint64(i.TrailingZeros()) diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 69227f66c..9d71d5279 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -314,7 +314,6 @@ func (mo *MultiOperation) Process() error { } } } - pos = 0 activeBlobbers := mo.operationMask.CountOnes() if activeBlobbers < mo.consensusThresh { return errors.New("consensus_not_met", fmt.Sprintf("Active blobbers %d is less than consensus threshold %d", activeBlobbers, mo.consensusThresh)) From 464d3efef14b49a101c010829340075bae81cbbf Mon Sep 17 00:00:00 2001 From: Jayash Date: Mon, 7 Oct 2024 22:59:51 +0530 Subject: [PATCH 213/319] Return txn hash in send tokens --- wasmsdk/sdk.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index 863c5457c..0567e71c2 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -151,10 +151,10 @@ func makeSCRestAPICall(scAddress, relativePath, paramsJson string) (string, erro // - fee is the transaction fee // - desc is the description of the transaction func send(toClientID string, tokens uint64, fee uint64, desc string) (string, error) { - _, out, _, _, err := zcncore.Send(toClientID, tokens, desc) + hash, _, _, _, err := zcncore.Send(toClientID, tokens, desc) if err != nil { return "", err } - return out, nil + return hash, nil } From 903957d455894e83724d1b10a4c417dffd45c163 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Mon, 7 Oct 2024 23:59:55 +0530 Subject: [PATCH 214/319] fix merge --- zboxcore/sdk/rollback.go | 1 - 1 file changed, 1 deletion(-) diff --git a/zboxcore/sdk/rollback.go b/zboxcore/sdk/rollback.go index efc8e2c96..9487e513b 100644 --- a/zboxcore/sdk/rollback.go +++ b/zboxcore/sdk/rollback.go @@ -280,7 +280,6 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { blobStatus.Status = "unavailable" } if wr != nil { - markerChan <- nil markerChan <- &RollbackBlobber{ blobber: blobber, lpm: wr, From 616a0bd61e846c78942f73aef849c1d0c805b8df Mon Sep 17 00:00:00 2001 From: Jayash Date: Tue, 8 Oct 2024 01:46:51 +0530 Subject: [PATCH 215/319] Update blobber settings DelegateWallet --- zboxcore/sdk/sdk.go | 1 + 1 file changed, 1 insertion(+) diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index a4c74f26f..59728aa3c 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -545,6 +545,7 @@ type UpdateBlobber struct { NotAvailable *bool `json:"not_available,omitempty"` IsRestricted *bool `json:"is_restricted,omitempty"` StorageVersion *int `json:"storage_version,omitempty"` + DelegateWallet *string `json:"delegate_wallet,omitempty"` } // ResetBlobberStatsDto represents blobber stats reset request. From c2a51df67ffd1e0b1d37c75cc55565c42ceca5e9 Mon Sep 17 00:00:00 2001 From: Jayash Date: Wed, 9 Oct 2024 02:24:23 +0530 Subject: [PATCH 216/319] Populate client --- core/client/set.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core/client/set.go b/core/client/set.go index e0840d101..531e2e69c 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -270,3 +270,14 @@ func IsSDKInitialized() bool { func SetSdkInitialized(val bool) { sdkInitialized = val } + +func PopulateClient(walletJSON string) error { + wallet := zcncrypto.Wallet{} + err := json.Unmarshal([]byte(walletJSON), &wallet) + if err != nil { + return err + } + + SetWallet(wallet) + return nil +} From 671bffb0cdaeb02909c283ddb4085aa4d1876cd6 Mon Sep 17 00:00:00 2001 From: Jayash Date: Wed, 9 Oct 2024 02:25:56 +0530 Subject: [PATCH 217/319] Populate client --- core/client/set.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/client/set.go b/core/client/set.go index 531e2e69c..f3eaf89e2 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -271,7 +271,7 @@ func SetSdkInitialized(val bool) { sdkInitialized = val } -func PopulateClient(walletJSON string) error { +func PopulateClient(walletJSON, signatureScheme string) error { wallet := zcncrypto.Wallet{} err := json.Unmarshal([]byte(walletJSON), &wallet) if err != nil { @@ -279,5 +279,6 @@ func PopulateClient(walletJSON string) error { } SetWallet(wallet) + SetSignatureScheme(signatureScheme) return nil } From 079a9f3a4df0c24cfe72ae62c07d600ca3def96a Mon Sep 17 00:00:00 2001 From: Jayash Date: Wed, 9 Oct 2024 02:28:56 +0530 Subject: [PATCH 218/319] Populate client --- core/client/set.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index f3eaf89e2..3fabe2c5e 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -271,14 +271,14 @@ func SetSdkInitialized(val bool) { sdkInitialized = val } -func PopulateClient(walletJSON, signatureScheme string) error { +func PopulateClient(walletJSON, signatureScheme string) (zcncrypto.Wallet, error) { wallet := zcncrypto.Wallet{} err := json.Unmarshal([]byte(walletJSON), &wallet) if err != nil { - return err + return wallet, err } SetWallet(wallet) SetSignatureScheme(signatureScheme) - return nil + return wallet, nil } From be698a557fc1c6fcf93f0150747c0f46cf554e1f Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Wed, 9 Oct 2024 12:27:56 +0530 Subject: [PATCH 219/319] add write chunk callback --- wasmsdk/blobber.go | 48 ++++++++++++++------- wasmsdk/demo/index.html | 4 +- wasmsdk/jsbridge/file_writer.go | 74 +++++++++++++++++++++++++++++++++ wasmsdk/player_file.go | 2 +- 4 files changed, 110 insertions(+), 18 deletions(-) diff --git a/wasmsdk/blobber.go b/wasmsdk/blobber.go index ebe163b94..9096f7c0d 100644 --- a/wasmsdk/blobber.go +++ b/wasmsdk/blobber.go @@ -999,8 +999,9 @@ func upload(allocationID, remotePath string, fileBytes, thumbnailBytes []byte, w // - remotePath : remote path of the file // - authTicket : auth ticket of the file, if the file is shared // - lookupHash : lookup hash of the file, which is used to locate the file if remotepath and allocation id are not provided +// - writeChunkFuncName : callback function name to write the chunk, if empty the function will return arrayBuffer otherwise will return nil -func downloadBlocks(allocId string, remotePath, authTicket, lookupHash string, startBlock, endBlock int64) ([]byte, error) { +func downloadBlocks(allocId string, remotePath, authTicket, lookupHash, writeChunkFuncName string, startBlock, endBlock int64) ([]byte, error) { if len(remotePath) == 0 && len(authTicket) == 0 { return nil, RequiredArg("remotePath/authTicket") @@ -1018,37 +1019,54 @@ func downloadBlocks(allocId string, remotePath, authTicket, lookupHash string, s statusBar = &StatusBar{wg: wg, totalBytesMap: make(map[string]int)} ) - pathHash := encryption.FastHash(remotePath) - fs, err := sys.Files.Open(pathHash) - if err != nil { - return nil, fmt.Errorf("could not open local file: %v", err) - } + var fh sys.File + if writeChunkFuncName == "" { + pathHash := encryption.FastHash(remotePath) + fs, err := sys.Files.Open(pathHash) + if err != nil { + return nil, fmt.Errorf("could not open local file: %v", err) + } - mf, _ := fs.(*sys.MemFile) - if mf == nil { - return nil, fmt.Errorf("invalid memfile") + mf, _ := fs.(*sys.MemFile) + if mf == nil { + return nil, fmt.Errorf("invalid memfile") + } + fh = mf + defer sys.Files.Remove(pathHash) //nolint + } else { + fh = jsbridge.NewFileCallbackWriter(writeChunkFuncName) } - defer sys.Files.Remove(pathHash) //nolint - wg.Add(1) if authTicket != "" { - err = alloc.DownloadByBlocksToFileHandlerFromAuthTicket(mf, authTicket, lookupHash, startBlock, endBlock, 100, remotePath, false, statusBar, true) + err = alloc.DownloadByBlocksToFileHandlerFromAuthTicket(fh, authTicket, lookupHash, startBlock, endBlock, 100, remotePath, false, statusBar, true, sdk.WithFileCallback( + func() { + fh.Close() //nolint:errcheck + }, + )) } else { err = alloc.DownloadByBlocksToFileHandler( - mf, + fh, remotePath, startBlock, endBlock, 100, false, - statusBar, true) + statusBar, true, sdk.WithFileCallback( + func() { + fh.Close() //nolint:errcheck + }, + )) } if err != nil { return nil, err } wg.Wait() - return mf.Buffer, nil + var buf []byte + if mf, ok := fh.(*sys.MemFile); ok { + buf = mf.Buffer + } + return buf, nil } // getBlobbers get list of active blobbers, and format them as array json string diff --git a/wasmsdk/demo/index.html b/wasmsdk/demo/index.html index b73e91cde..c46c41615 100644 --- a/wasmsdk/demo/index.html +++ b/wasmsdk/demo/index.html @@ -821,7 +821,7 @@

please download zcn.wasm from https://github.com/0chain/gosdk/releases/lates } objects.push({ - //remotePath: path, + remotePath: path, downloadOp: 1, numBlocks: 0, downloadToDisk: true, @@ -829,7 +829,7 @@

please download zcn.wasm from https://github.com/0chain/gosdk/releases/lates let stringifiedArray = JSON.stringify(objects); try { - const results = await goWasm.sdk.multiDownload('', stringifiedArray, 'eyJjbGllbnRfaWQiOiIiLCJvd25lcl9pZCI6IjI2ZTIzMjFhZWMxZmEyZDY1NGQ1MDQ5OWY3ZjhmYWJhNjNkYWMxYTExYTQwZDU3NDJkNDAzMWJmMzEzMzAxMTYiLCJhbGxvY2F0aW9uX2lkIjoiMDAwMzAzOTA1MGI3ZDdiM2FlNmI3MGEwZTVjMWU4ZjRhOTkxNzc1YWJiOTQ2NjljMDg4YzczNzJlMzYwMzkyYiIsImZpbGVfcGF0aF9oYXNoIjoiYWEzODE0NTM2ZWI2OWQwNjU4ZWM0OTgyZmE3ZTIwM2I2ZGI2ZWExYmU4ZmMxODRiMWJhOTZhMTk3NmMwM2JlOCIsImFjdHVhbF9maWxlX2hhc2giOiIxMjUwMjJhZGRiZTIwZDNhOWUzYjcxZTA0NjUzZjY3YiIsImZpbGVfbmFtZSI6InVidW50dS0yMi4wNC40LWxpdmUtc2VydmVyLWFtZDY0LmlzbyIsInJlZmVyZW5jZV90eXBlIjoiZiIsImV4cGlyYXRpb24iOjAsInRpbWVzdGFtcCI6MTcxNjM3ODIxNiwiZW5jcnlwdGVkIjpmYWxzZSwic2lnbmF0dXJlIjoiYmEzNzQ1NzlmZTczZDc1MWIwMTNiMjM2NjUzZDRiMGYyYzNjZDJlYTMyNTFkODg0MmRiNWQxNTlhNjBiN2ExMiJ9', '') + const results = await goWasm.sdk.multiDownload('', stringifiedArray, '', '') console.log(JSON.stringify(results)) } catch (e) { alert(e) diff --git a/wasmsdk/jsbridge/file_writer.go b/wasmsdk/jsbridge/file_writer.go index 21b0e17b2..5fe081a87 100644 --- a/wasmsdk/jsbridge/file_writer.go +++ b/wasmsdk/jsbridge/file_writer.go @@ -8,6 +8,9 @@ import ( "io" "io/fs" "syscall/js" + + "github.com/0chain/gosdk/core/common" + "github.com/valyala/bytebufferpool" ) type FileWriter struct { @@ -148,3 +151,74 @@ func NewFileWriterFromHandle(dirHandler js.Value, name string) (*FileWriter, err fileHandle: fileHandler[0], }, nil } + +type FileCallbackWriter struct { + writeChunk js.Value + buf []byte + offset int64 +} + +const bufCallbackCap = 4 * 1024 * 1024 //4MB + +func NewFileCallbackWriter(writeChunkFuncName string) *FileCallbackWriter { + writeChunk := js.Global().Get(writeChunkFuncName) + return &FileCallbackWriter{ + writeChunk: writeChunk, + } +} + +func (wc *FileCallbackWriter) Write(p []byte) (int, error) { + if len(wc.buf) == 0 { + buff := common.MemPool.Get() + if cap(buff.B) < bufCallbackCap { + buff.B = make([]byte, 0, bufCallbackCap) + } + wc.buf = buff.B + } + if len(wc.buf)+len(p) > cap(wc.buf) { + uint8Array := js.Global().Get("Uint8Array").New(len(wc.buf)) + js.CopyBytesToJS(uint8Array, wc.buf) + _, err := Await(wc.writeChunk.Invoke(uint8Array, wc.offset)) + if len(err) > 0 && !err[0].IsNull() { + return 0, errors.New("file_writer: " + err[0].String()) + } + wc.offset += int64(len(wc.buf)) + wc.buf = wc.buf[:0] + } + wc.buf = append(wc.buf, p...) + return len(p), nil +} + +func (wc *FileCallbackWriter) Close() error { + if len(wc.buf) > 0 { + uint8Array := js.Global().Get("Uint8Array").New(len(wc.buf)) + js.CopyBytesToJS(uint8Array, wc.buf) + _, err := Await(wc.writeChunk.Invoke(uint8Array, wc.offset)) + if len(err) > 0 && !err[0].IsNull() { + return errors.New("file_writer: " + err[0].String()) + } + wc.offset += int64(len(wc.buf)) + wc.buf = wc.buf[:0] + } + buff := &bytebufferpool.ByteBuffer{ + B: wc.buf, + } + common.MemPool.Put(buff) + return nil +} + +func (wc *FileCallbackWriter) Read(p []byte) (int, error) { + return 0, errors.New("file_writer: not supported") +} + +func (wc *FileCallbackWriter) Seek(offset int64, whence int) (int64, error) { + return 0, nil +} + +func (wc *FileCallbackWriter) Sync() error { + return nil +} + +func (wc *FileCallbackWriter) Stat() (fs.FileInfo, error) { + return nil, nil +} diff --git a/wasmsdk/player_file.go b/wasmsdk/player_file.go index ccafe75f2..82de0d4c8 100644 --- a/wasmsdk/player_file.go +++ b/wasmsdk/player_file.go @@ -66,7 +66,7 @@ func (p *FilePlayer) download(startBlock int64) { } fmt.Println("start:", startBlock, "end:", endBlock, "numBlocks:", p.numBlocks, "total:", p.playlistFile.NumBlocks) - data, err := downloadBlocks(p.allocationObj.ID, p.remotePath, p.authTicket, p.lookupHash, startBlock, endBlock) + data, err := downloadBlocks(p.allocationObj.ID, p.remotePath, p.authTicket, p.lookupHash, "", startBlock, endBlock) // data, err := downloadBlocks2(int(startBlock), int(endBlock), p.allocationObj, p.remotePath) if err != nil { PrintError(err.Error()) From 06eb4396c862591fd4956eb418a0ff800436758b Mon Sep 17 00:00:00 2001 From: Jayash Date: Thu, 10 Oct 2024 00:33:45 +0530 Subject: [PATCH 220/319] Fix get network --- core/client/init_node.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/client/init_node.go b/core/client/init_node.go index 0b7a2993b..86b6bd736 100644 --- a/core/client/init_node.go +++ b/core/client/init_node.go @@ -126,6 +126,9 @@ func Init(ctx context.Context, cfg conf.Config) error { // set default value for options if unset setOptionsDefaultValue(&cfg) + //init packages + conf.InitClientConfig(&cfg) + network, err := GetNetwork(ctx) if err != nil { logging.Error("Failed to get network details ", zap.Error(err), zap.Any("block_worker", cfg.BlockWorker)) @@ -141,8 +144,6 @@ func Init(ctx context.Context, cfg conf.Config) error { clientCtx: ctx, } - //init packages - conf.InitClientConfig(&cfg) InitCache(nodeClient.sharders) // update Network periodically From ed7e91a8a42833f5e3369ec2bc4a3c934dbab719 Mon Sep 17 00:00:00 2001 From: Jayash Date: Fri, 11 Oct 2024 02:36:08 +0530 Subject: [PATCH 221/319] Debug blobber failure --- core/client/http.go | 23 +++++++++++++++++++++-- core/transaction/get_data.go | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index 6b4c49f8a..8df41f30f 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -7,6 +7,8 @@ import ( "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/util" "github.com/shopspring/decimal" + "go.uber.org/zap" + "log" "net/http" "net/url" "sync" @@ -31,6 +33,9 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] } sharders := nodeClient.Network().Sharders + logging.Info("sharders", zap.Any("sharders", sharders)) + fmt.Println("sharders", sharders) + log.Println("sharders", sharders) responses := make(map[int]int) entityResult := make(map[string][]byte) @@ -53,6 +58,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] defer wg.Done() urlString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath) + urlObj, err := url.Parse(urlString) if err != nil { fmt.Println(err.Error()) @@ -64,7 +70,12 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] } urlObj.RawQuery = q.Encode() - req, err := util.NewHTTPGetRequest(urlObj.String()) + urlObjStr := urlObj.String() + logging.Info("urlObjStr", zap.Any("urlObjStr", urlObjStr)) + fmt.Println("urlObjStr", urlObjStr) + log.Println("urlObjStr", urlObjStr) + + req, err := util.NewHTTPGetRequest(urlObjStr) if err != nil { fmt.Println("1Error creating request", err.Error()) return @@ -104,9 +115,17 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] rate := float32(maxCount*100) / float32(cfg.SharderConsensous) if rate < consensusThresh { - err = errors.New("consensus_failed", "consensus failed on sharders") + err = errors.New("consensus_failed", "consensus failed on sharders list : "+fmt.Sprintf("%v", sharders)) } + logging.Info("maxCount", zap.Any("maxCount", maxCount)) + fmt.Println("maxCount", maxCount) + log.Println("maxCount", maxCount) + + logging.Info("cfg.SharderConsensous", zap.Any("cfg.SharderConsensous", cfg.SharderConsensous)) + fmt.Println("cfg.SharderConsensous", cfg.SharderConsensous) + log.Println("cfg.SharderConsensous", cfg.SharderConsensous) + if dominant != 200 { var objmap map[string]json.RawMessage err := json.Unmarshal(retObj, &objmap) diff --git a/core/transaction/get_data.go b/core/transaction/get_data.go index cf7a60065..ace8cec0c 100644 --- a/core/transaction/get_data.go +++ b/core/transaction/get_data.go @@ -47,7 +47,7 @@ func GetConfig(configType string) (conf *InputMap, err error) { b, err = coreHttp.MakeSCRestAPICall(scAddress, relativePath, nil) if err != nil { - return nil, errors.Wrap(err, "error requesting storage SC configs:") + return nil, errors.Wrap(err, "error requesting SC configs:") } if len(b) == 0 { return nil, errors.New("", "empty response") From 7f33c6c83c99001ea8ea3bf92ef62986fdcc960f Mon Sep 17 00:00:00 2001 From: Jayash Date: Fri, 11 Oct 2024 03:10:58 +0530 Subject: [PATCH 222/319] Debug blobber failure --- core/client/set.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index 3fabe2c5e..2c1863a85 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -227,23 +227,15 @@ func InitSDK(walletJSON string, var minConfirmation, minSubmit, confirmationChainLength, sharderConsensous int if len(options) > 1 { minConfirmation = options[1] - } else { - minConfirmation = 5 } if len(options) > 2 { minSubmit = options[2] - } else { - minSubmit = 5 } if len(options) > 3 { confirmationChainLength = options[3] - } else { - confirmationChainLength = 10 } if len(options) > 4 { sharderConsensous = options[4] - } else { - sharderConsensous = 10 } err := Init(context.Background(), conf.Config{ From accf7f1afdf016c80b34fa1ec8ca1a572611a750 Mon Sep 17 00:00:00 2001 From: Jayash Date: Fri, 11 Oct 2024 04:04:10 +0530 Subject: [PATCH 223/319] Revert "Debug blobber failure" This reverts commit ed7e91a8a42833f5e3369ec2bc4a3c934dbab719. --- core/client/http.go | 23 ++--------------------- core/transaction/get_data.go | 2 +- 2 files changed, 3 insertions(+), 22 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index 8df41f30f..6b4c49f8a 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -7,8 +7,6 @@ import ( "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/util" "github.com/shopspring/decimal" - "go.uber.org/zap" - "log" "net/http" "net/url" "sync" @@ -33,9 +31,6 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] } sharders := nodeClient.Network().Sharders - logging.Info("sharders", zap.Any("sharders", sharders)) - fmt.Println("sharders", sharders) - log.Println("sharders", sharders) responses := make(map[int]int) entityResult := make(map[string][]byte) @@ -58,7 +53,6 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] defer wg.Done() urlString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath) - urlObj, err := url.Parse(urlString) if err != nil { fmt.Println(err.Error()) @@ -70,12 +64,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] } urlObj.RawQuery = q.Encode() - urlObjStr := urlObj.String() - logging.Info("urlObjStr", zap.Any("urlObjStr", urlObjStr)) - fmt.Println("urlObjStr", urlObjStr) - log.Println("urlObjStr", urlObjStr) - - req, err := util.NewHTTPGetRequest(urlObjStr) + req, err := util.NewHTTPGetRequest(urlObj.String()) if err != nil { fmt.Println("1Error creating request", err.Error()) return @@ -115,17 +104,9 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] rate := float32(maxCount*100) / float32(cfg.SharderConsensous) if rate < consensusThresh { - err = errors.New("consensus_failed", "consensus failed on sharders list : "+fmt.Sprintf("%v", sharders)) + err = errors.New("consensus_failed", "consensus failed on sharders") } - logging.Info("maxCount", zap.Any("maxCount", maxCount)) - fmt.Println("maxCount", maxCount) - log.Println("maxCount", maxCount) - - logging.Info("cfg.SharderConsensous", zap.Any("cfg.SharderConsensous", cfg.SharderConsensous)) - fmt.Println("cfg.SharderConsensous", cfg.SharderConsensous) - log.Println("cfg.SharderConsensous", cfg.SharderConsensous) - if dominant != 200 { var objmap map[string]json.RawMessage err := json.Unmarshal(retObj, &objmap) diff --git a/core/transaction/get_data.go b/core/transaction/get_data.go index ace8cec0c..cf7a60065 100644 --- a/core/transaction/get_data.go +++ b/core/transaction/get_data.go @@ -47,7 +47,7 @@ func GetConfig(configType string) (conf *InputMap, err error) { b, err = coreHttp.MakeSCRestAPICall(scAddress, relativePath, nil) if err != nil { - return nil, errors.Wrap(err, "error requesting SC configs:") + return nil, errors.Wrap(err, "error requesting storage SC configs:") } if len(b) == 0 { return nil, errors.New("", "empty response") From b288d655c83cc878b0752b23820ae9be019c3e86 Mon Sep 17 00:00:00 2001 From: Jayash Satolia <73050737+Jayashsatolia403@users.noreply.github.com> Date: Sun, 13 Oct 2024 21:09:40 +0530 Subject: [PATCH 224/319] Merge storageV2 in "fix/refactor-zboxcore" (#1637) * add storage version for allocation * add commit for v2 * add delete change * add delete dir and get refs * fix process change for delete dir * fix delete dir * add timming log * fix setting root * send ref path request to all * add merge logs * fix collapse for short node * check refs in delete * add changesv2 * add eg for signature * set limit to 4 * remove group * fix path * remove hash signature map * remove sign timing * check timestamp * add check for lwm * implement rename move and copy for v2 * add repair for v2 * check for cancel * fix copy and move * fix no changes in connection * fix repair * fix eof check * fix repair eof * set path hash * fix update key * add prev weight * add debug log for commit size * fix update key weight * fix alloc root * fix chain hash * add fix for verifying trie root * fix delete directory * fix mask pos * fix create existing dir * fix new dir consensus * take base as dest name * Added storageVersion * fix ut * fix lint * fix list and stats ut * fix wasm error * use v2 as default * fetch alloc version from sharders * fix lint * fix merge * Update blobber settings DelegateWallet * Fix * Fix --------- Co-authored-by: hitenjain14 Co-authored-by: Yury Co-authored-by: Hitenjain14 <57557631+Hitenjain14@users.noreply.github.com> --- core/sys/util.go | 35 ++ core/version/version.go | 2 +- go.mod | 23 +- go.sum | 84 +++- wasmsdk/allocation.go | 3 +- wasmsdk/demo/index.html | 2 + wasmsdk/demo/zcn.js | 9 +- wasmsdk/jsbridge/webworker.go | 4 +- zboxcore/allocationchange/change.go | 6 + zboxcore/blockchain/entity.go | 30 +- zboxcore/fileref/fileref.go | 28 ++ zboxcore/sdk/allocation.go | 71 ++- zboxcore/sdk/allocation_test.go | 13 +- zboxcore/sdk/blobber_operations.go | 3 +- zboxcore/sdk/chunked_upload.go | 2 +- zboxcore/sdk/chunked_upload_blobber.go | 2 +- zboxcore/sdk/chunked_upload_form_builder.go | 27 +- .../chunked_upload_form_builder_bench_test.go | 4 +- zboxcore/sdk/chunked_upload_model.go | 4 +- zboxcore/sdk/chunked_upload_process.go | 10 +- zboxcore/sdk/chunked_upload_process_js.go | 9 +- zboxcore/sdk/commitworker.go | 433 ++++++++++++++++-- zboxcore/sdk/common.go | 109 +++++ zboxcore/sdk/copyworker.go | 260 ++++++++++- zboxcore/sdk/copyworker_test.go | 18 +- zboxcore/sdk/deleteworker.go | 224 ++++++++- zboxcore/sdk/deleteworker_test.go | 16 +- zboxcore/sdk/dirworker.go | 21 +- zboxcore/sdk/downloadworker.go | 11 +- zboxcore/sdk/filerefsworker.go | 193 ++++++-- zboxcore/sdk/filestatsworker.go | 1 + zboxcore/sdk/filestatsworker_test.go | 1 + zboxcore/sdk/listworker.go | 5 + zboxcore/sdk/listworker_test.go | 5 +- zboxcore/sdk/moveworker.go | 182 +++++++- zboxcore/sdk/multi_operation_worker.go | 146 +++++- zboxcore/sdk/renameworker.go | 181 +++++++- zboxcore/sdk/renameworker_test.go | 18 +- zboxcore/sdk/repairworker.go | 184 +++++++- zboxcore/sdk/rollback.go | 37 +- zboxcore/sdk/sdk.go | 12 +- zboxcore/sdk/upload_worker.go | 31 +- zboxcore/zboxutil/http.go | 45 +- 43 files changed, 2202 insertions(+), 302 deletions(-) diff --git a/core/sys/util.go b/core/sys/util.go index baf46c416..14ef762da 100644 --- a/core/sys/util.go +++ b/core/sys/util.go @@ -270,3 +270,38 @@ func (i *MemFileChanInfo) Sys() interface{} { func (i *MemFileChanInfo) Info() (fs.FileInfo, error) { return i, nil } + +// wrapper over io pipe to implement File interface +type PipeFile struct { + w *io.PipeWriter + r *io.PipeReader +} + +func NewPipeFile() *PipeFile { + r, w := io.Pipe() + return &PipeFile{w: w, r: r} +} + +func (pf *PipeFile) Write(p []byte) (int, error) { + return pf.w.Write(p) +} + +func (pf *PipeFile) Close() error { + return pf.w.Close() +} + +func (pf *PipeFile) Read(p []byte) (int, error) { + return pf.r.Read(p) +} + +func (pf *PipeFile) Stat() (fs.FileInfo, error) { + return nil, nil +} + +func (pf *PipeFile) Sync() error { + return nil +} + +func (pf *PipeFile) Seek(offset int64, whence int) (int64, error) { + return 0, nil +} diff --git a/core/version/version.go b/core/version/version.go index 59c049acb..e3bed521a 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-218-g7b354b71" +const VERSIONSTR = "v1.17.9-301-g37800139" diff --git a/go.mod b/go.mod index dc3f8468c..35f0313cf 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/0chain/gosdk go 1.22.0 require ( - github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565 + github.com/0chain/common v1.13.1-0.20240926135437-61b88d39bbbd github.com/0chain/errors v1.0.3 github.com/Luzifer/go-openssl/v3 v3.1.0 github.com/btcsuite/btcd v0.23.4 @@ -24,14 +24,14 @@ require ( github.com/pkg/errors v0.9.1 github.com/shopspring/decimal v1.3.1 github.com/spf13/viper v1.15.0 - github.com/stretchr/testify v1.8.4 + github.com/stretchr/testify v1.9.0 github.com/tyler-smith/go-bip39 v1.1.0 github.com/uptrace/bunrouter v1.0.20 go.dedis.ch/kyber/v3 v3.1.0 go.uber.org/zap v1.24.0 - golang.org/x/crypto v0.27.0 - golang.org/x/image v0.20.0 - golang.org/x/sync v0.8.0 + golang.org/x/crypto v0.21.0 + golang.org/x/image v0.14.0 + golang.org/x/sync v0.7.0 gopkg.in/cheggaaa/pb.v1 v1.0.28 gopkg.in/natefinch/lumberjack.v2 v2.2.1 gopkg.in/yaml.v2 v2.4.0 @@ -59,13 +59,14 @@ require ( github.com/edsrzf/mmap-go v1.0.0 // indirect github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect + github.com/fxamacker/cbor/v2 v2.7.0 // indirect github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect + github.com/go-kit/kit v0.9.0 // indirect github.com/go-logfmt/logfmt v0.5.1 // indirect github.com/go-ole/go-ole v1.2.6 // indirect github.com/go-stack/stack v1.8.1 // indirect github.com/golang-jwt/jwt/v4 v4.3.0 // indirect github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect - github.com/google/go-cmp v0.6.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/hashicorp/go-bexpr v0.1.10 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect @@ -74,7 +75,6 @@ require ( github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c // indirect github.com/huin/goupnp v1.0.3 // indirect github.com/jackpal/go-nat-pmp v1.0.2 // indirect - github.com/kr/pretty v0.3.1 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/matryer/is v1.4.1 // indirect github.com/mattn/go-colorable v0.1.13 // indirect @@ -96,7 +96,7 @@ require ( github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/status-im/keycard-go v0.2.0 // indirect - github.com/stretchr/objx v0.5.0 // indirect + github.com/stretchr/objx v0.5.2 // indirect github.com/subosito/gotenv v1.4.2 // indirect github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect github.com/tinylib/msgp v1.1.6 // indirect @@ -104,10 +104,11 @@ require ( github.com/tklauser/numcpus v0.6.0 // indirect github.com/urfave/cli/v2 v2.17.2-0.20221006022127-8f469abc00aa // indirect github.com/valyala/fasthttp v1.51.0 // indirect + github.com/x448/float16 v0.8.4 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect go.dedis.ch/fixbuf v1.0.3 // indirect - golang.org/x/sys v0.25.0 // indirect - golang.org/x/text v0.18.0 // indirect + golang.org/x/sys v0.18.0 // indirect + golang.org/x/text v0.14.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect @@ -122,7 +123,7 @@ require ( github.com/yusufpapurcu/wmi v1.2.2 // indirect go.uber.org/atomic v1.10.0 // indirect go.uber.org/multierr v1.9.0 // indirect - golang.org/x/net v0.29.0 // indirect + golang.org/x/net v0.23.0 // indirect golang.org/x/time v0.3.0 // indirect ) diff --git a/go.sum b/go.sum index 8d97d9986..164ab0b6a 100644 --- a/go.sum +++ b/go.sum @@ -40,13 +40,15 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565 h1:z+DtCR8mBsjPnEsT2XtRu4X7GfBiMnz9dYvWYs9V0B4= -github.com/0chain/common v0.0.6-0.20230127095721-8df4d1d72565/go.mod h1:UyDC8Qyl5z9lGkCnf9RHJPMektnFX8XtCJZHXCCVj8E= +github.com/0chain/common v1.13.1-0.20240926135437-61b88d39bbbd h1:PDnzyJJA4rF2LBW+0OQJ5y9vIwsPYYjbbFwImvgiDdY= +github.com/0chain/common v1.13.1-0.20240926135437-61b88d39bbbd/go.mod h1:vHK9Q0/nz28PxmKsLSMLzEs5iK6QVgl1LxdDQdR7y6s= github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM= github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= +github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ= +github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo= github.com/Luzifer/go-openssl/v3 v3.1.0 h1:QqKqo6kYXGGUsvtUoCpRZm8lHw+jDfhbzr36gVj+/gw= github.com/Luzifer/go-openssl/v3 v3.1.0/go.mod h1:liy3FXuuS8hfDlYh1T+l78AwQ/NjZflJz0NDvjKhwDs= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= @@ -66,6 +68,8 @@ github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLj github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c= github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ= @@ -109,9 +113,20 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= +github.com/cockroachdb/errors v1.11.3 h1:5bA+k2Y6r+oz/6Z/RFlNeVCesGARKuC6YymtcDrbC/I= +github.com/cockroachdb/errors v1.11.3/go.mod h1:m4UIW4CDjx+R5cybPsNrRbreomiFqt8o1h1wUVazSd8= +github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce h1:giXvy4KSc/6g/esnpM7Geqxka4WSqI1SZc7sMJFd3y4= +github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce/go.mod h1:9/y3cnZ5GKakj/H4y9r9GTjCvAFta7KLgSHPJJYc52M= +github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b h1:r6VH0faHjZeQy818SGhaone5OnYfxFR/+AzdY3sf5aE= +github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b/go.mod h1:Vz9DsVWQQhf3vs21MhPMZpMGSht7O/2vFW2xusFUVOs= +github.com/cockroachdb/pebble v1.1.2 h1:CUh2IPtR4swHlEj48Rhfzw6l/d0qA31fItcIszQVIsA= +github.com/cockroachdb/pebble v1.1.2/go.mod h1:4exszw1r40423ZsmkG/09AFEG83I0uDgfujJdbL6kYU= +github.com/cockroachdb/redact v1.1.5 h1:u1PMllDkdFfPWaNGMyLD1+so+aq3uUItthCFqzwPJ30= +github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg= +github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAKVxetITBuuhv3BI9cMrmStnpT18zmgmTxunpo= +github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06/go.mod h1:7nc4anLGjupUW/PeY5qiNYsdNXj7zopG+eqsS7To5IQ= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/dave/jennifer v1.2.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg= github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -150,15 +165,20 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= +github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E= +github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ= github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff h1:tY80oXqGNY4FhTFhk+o9oFHGINQ/+vhlm8HFzi6znCI= github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww= +github.com/getsentry/sentry-go v0.27.0 h1:Pv98CIbtB3LkMWmXi4Joa5OOcwbmnX88sF5qbK3r3Ps= +github.com/getsentry/sentry-go v0.27.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY= github.com/glycerine/go-unsnap-stream v0.0.0-20180323001048-9f0cb55181dd/go.mod h1:/20jfyN9Y5QPEAprSgKAUr+glWDY39ZiUEAYOEv5dsE= github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= -github.com/go-kit/kit v0.8.0 h1:Wz+5lgoB0kkuqLEc6NVmwRknTKP6dTGbSqvhZtBI/j0= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-kit/kit v0.9.0 h1:wDJmvq38kDhkVxi50ni9ykkdUr1PKgqKOoi01fa0Mdk= +github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA= @@ -172,6 +192,8 @@ github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP github.com/gofrs/uuid v3.3.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang-jwt/jwt/v4 v4.3.0 h1:kHL1vqdqWNfATmA0FNMdmZNMyZI1U6O31X4rlIPoBog= github.com/golang-jwt/jwt/v4 v4.3.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= @@ -201,6 +223,8 @@ github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvq github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= @@ -218,8 +242,8 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -353,6 +377,8 @@ github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-tty v0.0.0-20180907095812-13ff1204f104/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI= +github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM= github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= @@ -392,7 +418,6 @@ github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG github.com/philhofer/fwd v1.1.2-0.20210722190033-5c56ac6d0bb9 h1:6ob53CVz+ja2i7easAStApZJlh7sxyq3Cm7g1Di6iqA= github.com/philhofer/fwd v1.1.2-0.20210722190033-5c56ac6d0bb9/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU= github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= -github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -403,14 +428,22 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= +github.com/prometheus/client_golang v1.12.0 h1:C+UIj/QWtmqY13Arb8kwMt5j34/0Z2iKamrJ+ryC0Gg= +github.com/prometheus/client_golang v1.12.0/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/client_model v0.2.1-0.20210607210712-147c58e9608a h1:CmF68hwI0XsOQ5UwlBopMi2Ow4Pbg32akc4KIVCOm+Y= +github.com/prometheus/client_model v0.2.1-0.20210607210712-147c58e9608a/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w= github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= github.com/prometheus/common v0.6.0/go.mod h1:eBmuwkDJBwy6iBfxCBob6t6dR6ENT/y+J+Zk0j9GMYc= +github.com/prometheus/common v0.32.1 h1:hWIdL3N2HoUx3B8j3YN9mWor0qhY/NlEKZEaXxuIRh4= +github.com/prometheus/common v0.32.1/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU= +github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/tsdb v0.7.1 h1:YZcsG11NqnK4czYLrWd9mpEuAJIHVQLwdrleYfszMAA= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/remeh/sizedwaitgroup v1.0.0 h1:VNGGFwNo/R5+MJBf6yrsr110p0m4/OX4S3DCy7Kyl5E= @@ -457,8 +490,9 @@ github.com/status-im/keycard-go v0.2.0/go.mod h1:wlp8ZLbsmrF6g6WjugPAx+IzoLrkdf9 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.2.0/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= @@ -468,8 +502,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8= github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY= @@ -492,6 +526,8 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= github.com/willf/bitset v1.1.3/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4= +github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= +github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= github.com/xlab/treeprint v0.0.0-20180616005107-d6fb6747feb6/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= @@ -543,8 +579,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= -golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= +golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= +golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -558,11 +594,13 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= +golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME= +golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= -golang.org/x/image v0.20.0 h1:7cVCUjQwfL18gyBJOmYvptfSHS8Fb3YUDtfLIZ7Nbpw= -golang.org/x/image v0.20.0/go.mod h1:0a88To4CYVBAHp5FXJm8o7QbUl37Vd85ply1vyD8auM= +golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4= +golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= @@ -621,8 +659,8 @@ golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= -golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= +golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= +golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -643,8 +681,8 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= -golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -696,8 +734,8 @@ golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= -golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -705,8 +743,8 @@ golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3 golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= -golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -868,6 +906,8 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2 google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= +google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/wasmsdk/allocation.go b/wasmsdk/allocation.go index b386382a4..f36b60888 100644 --- a/wasmsdk/allocation.go +++ b/wasmsdk/allocation.go @@ -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{ @@ -108,6 +108,7 @@ func createAllocation(datashards, parityshards int, size int64, BlobberIds: blobberIds, ThirdPartyExtendable: setThirdPartyExtendable, IsEnterprise: IsEnterprise, + StorageVersion: sdk.StorageV2, BlobberAuthTickets: blobberAuthTickets, Force: force, } diff --git a/wasmsdk/demo/index.html b/wasmsdk/demo/index.html index b73e91cde..64740f57a 100644 --- a/wasmsdk/demo/index.html +++ b/wasmsdk/demo/index.html @@ -241,6 +241,8 @@

please download zcn.wasm from https://github.com/0chain/gosdk/releases/lates await wasm.setWallet(bls, clientID, publicKey, "", privateKey, publicKey, mnemonic, false); goWasm = wasm + }).catch(e => { + console.log(e) }) onClick('btnSetWallet', async () => { diff --git a/wasmsdk/demo/zcn.js b/wasmsdk/demo/zcn.js index d64388d24..63585cfb9 100644 --- a/wasmsdk/demo/zcn.js +++ b/wasmsdk/demo/zcn.js @@ -308,7 +308,7 @@ async function createWasm() { (_, key) => (...args) => // eslint-disable-next-line - new Promise(async (resolve, reject) => { + new Promise(async (resolve, reject) => { if (!go || go.exited) { return reject(new Error('The Go instance is not active.')) } @@ -368,8 +368,11 @@ async function createWasm() { ) const proxy = { - bulkUpload: bulkUpload, - setWallet: setWallet, + bulkUpload, + setWallet, + getWalletId, + getPrivateKey, + getPeerPublicKey, sdk: sdkProxy, //expose sdk methods for js jsProxy, //expose js methods for go } diff --git a/wasmsdk/jsbridge/webworker.go b/wasmsdk/jsbridge/webworker.go index d889de621..3f9fb864b 100644 --- a/wasmsdk/jsbridge/webworker.go +++ b/wasmsdk/jsbridge/webworker.go @@ -80,7 +80,9 @@ func NewWasmWebWorker(blobberID, blobberURL, clientID, clientKey, peerPublicKey, "PUBLIC_KEY=" + publicKey, "IS_SPLIT=" + strconv.FormatBool(isSplit), "MNEMONIC=" + mnemonic, - "ZAUTH_SERVER=" + gZauthServer}, + "ZAUTH_SERVER=" + gZauthServer, + "BLOBBER_ID=" + blobberID, + }, Path: "zcn.wasm", subscribers: make(map[string]chan worker.MessageEvent), } diff --git a/zboxcore/allocationchange/change.go b/zboxcore/allocationchange/change.go index 56fcd9b04..cfd8ffb33 100644 --- a/zboxcore/allocationchange/change.go +++ b/zboxcore/allocationchange/change.go @@ -1,6 +1,7 @@ package allocationchange import ( + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/gosdk/zboxcore/fileref" ) @@ -16,6 +17,11 @@ type AllocationChange interface { GetSize() int64 } +type AllocationChangeV2 interface { + ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error + GetLookupHash(changeIndex uint64) []string +} + type EmptyFileChange struct { change } diff --git a/zboxcore/blockchain/entity.go b/zboxcore/blockchain/entity.go index 91a005e18..0d4a53d17 100644 --- a/zboxcore/blockchain/entity.go +++ b/zboxcore/blockchain/entity.go @@ -1,44 +1,46 @@ +// Methods and types for blockchain entities and interactions. package blockchain import ( "sync/atomic" - "github.com/0chain/gosdk/core/common" + "github.com/0chain/gosdk/zboxcore/marker" ) // StakePoolSettings information. type StakePoolSettings struct { - DelegateWallet string `json:"delegate_wallet"` - MinStake common.Balance `json:"min_stake"` - MaxStake common.Balance `json:"max_stake"` - NumDelegates int `json:"num_delegates"` - ServiceCharge float64 `json:"service_charge"` + DelegateWallet string `json:"delegate_wallet"` + NumDelegates int `json:"num_delegates"` + ServiceCharge float64 `json:"service_charge"` } -// UpdateStakePoolSettings information. +// UpdateStakePoolSettings represent stake pool information of a provider node. type UpdateStakePoolSettings struct { - DelegateWallet *string `json:"delegate_wallet,omitempty"` - MinStake *common.Balance `json:"min_stake,omitempty"` - MaxStake *common.Balance `json:"max_stake,omitempty"` - NumDelegates *int `json:"num_delegates,omitempty"` - ServiceCharge *float64 `json:"service_charge,omitempty"` + DelegateWallet *string `json:"delegate_wallet,omitempty"` + NumDelegates *int `json:"num_delegates,omitempty"` + ServiceCharge *float64 `json:"service_charge,omitempty"` } +// ValidationNode represents a validation node (miner) type ValidationNode struct { ID string `json:"id"` BaseURL string `json:"url"` StakePoolSettings StakePoolSettings `json:"stake_pool_settings"` } +// UpdateValidationNode represents a validation node (miner) update type UpdateValidationNode struct { ID string `json:"id"` BaseURL *string `json:"url"` StakePoolSettings *UpdateStakePoolSettings `json:"stake_pool_settings"` } +// StorageNode represents a storage node (blobber) type StorageNode struct { - ID string `json:"id"` - Baseurl string `json:"url"` + ID string `json:"id"` + Baseurl string `json:"url"` + AllocationRoot string `json:"-"` + LatestWM *marker.WriteMarker skip uint64 `json:"-"` // skip on error } diff --git a/zboxcore/fileref/fileref.go b/zboxcore/fileref/fileref.go index 42995a765..2fafdd1ed 100644 --- a/zboxcore/fileref/fileref.go +++ b/zboxcore/fileref/fileref.go @@ -62,9 +62,11 @@ func (fRef *FileRef) MetaID() string { } type RefEntity interface { + GetAllocationRoot() string GetNumBlocks() int64 GetSize() int64 GetFileMetaHash() string + GetFileMetaHashV2() []byte GetHash() string CalculateHash() string GetType() string @@ -95,9 +97,11 @@ type Ref struct { ThumbnailSize int64 `json:"thumbnail_size" mapstructure:"thumbnail_size"` ActualThumbnailHash string `json:"actual_thumbnail_hash" mapstructure:"actual_thumbnail_hash"` ActualThumbnailSize int64 `json:"actual_thumbnail_size" mapstructure:"actual_thumbnail_size"` + IsEmpty bool `json:"is_empty" mapstructure:"is_empty"` HashToBeComputed bool ChildrenLoaded bool Children []RefEntity `json:"-" mapstructure:"-"` + AllocationRoot string `json:"allocation_root" mapstructure:"allocation_root"` CreatedAt common.Timestamp `json:"created_at" mapstructure:"created_at"` UpdatedAt common.Timestamp `json:"updated_at" mapstructure:"updated_at"` } @@ -162,6 +166,10 @@ func (r *Ref) GetFileMetaHash() string { return r.FileMetaHash } +func (r *Ref) GetFileMetaHashV2() []byte { + return nil +} + func (r *Ref) GetHash() string { return r.Hash } @@ -170,6 +178,10 @@ func (r *Ref) GetHashData() string { return fmt.Sprintf("%s:%s:%s", r.AllocationID, r.Path, r.FileID) } +func (r *Ref) GetAllocationRoot() string { + return r.AllocationRoot +} + func (r *Ref) GetType() string { return r.Type } @@ -250,6 +262,14 @@ func (fr *FileRef) GetFileMetaHashData() string { fr.ActualFileSize, fr.ActualFileHash) } +func (fr *FileRef) GetFileMetaHashDataV2() string { + return fmt.Sprintf( + "%s:%s:%d:%d:%s", + fr.AllocationID, + fr.Path, fr.Size, + fr.ActualFileSize, fr.ActualFileHash) +} + func (fr *FileRef) GetHashData() string { return fmt.Sprintf( "%s:%s:%s:%s:%d:%s:%s:%d:%s:%d:%s", @@ -267,6 +287,10 @@ func (fr *FileRef) GetHashData() string { ) } +func (fr *FileRef) GetFileHashDataV2(blobberID string) string { + return fmt.Sprintf("%s:%s:%s:%d:%s:%d:%s", blobberID, fr.AllocationID, fr.Path, fr.Size, fr.FixedMerkleRoot, fr.ActualFileSize, fr.ActualFileHash) +} + func (fr *FileRef) GetHash() string { return fr.Hash } @@ -278,6 +302,10 @@ func (fr *FileRef) CalculateHash() string { return fr.Hash } +func (fr *FileRef) GetFileMetaHashV2() []byte { + return encryption.RawHash(fr.GetFileMetaHashDataV2()) +} + func (fr *FileRef) GetType() string { return fr.Type } diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index b32bd7602..3ee711285 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -274,6 +274,8 @@ type Allocation struct { IsEnterprise bool `json:"is_enterprise"` + StorageVersion int `json:"storage_version"` + // FileOptions to define file restrictions on an allocation for third-parties // default 00000000 for all crud operations suggesting only owner has the below listed abilities. // enabling option/s allows any third party to perform certain ops @@ -302,6 +304,7 @@ type Allocation struct { consensusThreshold int fullconsensus int sig string `json:"-"` + allocationRoot string `json:"-"` } // OperationRequest represents an operation request with its related options. @@ -325,6 +328,7 @@ type OperationRequest struct { StreamUpload bool // Required for streaming file when actualSize is not available CancelCauseFunc context.CancelCauseFunc Opts []ChunkedUploadOption + CopyDirOnly bool } // GetReadPriceRange returns the read price range from the global configuration. @@ -428,6 +432,9 @@ func (a *Allocation) InitAllocation() { a.startWorker(a.ctx) InitCommitWorker(a.Blobbers) InitBlockDownloader(a.Blobbers, downloadWorkerCount) + if a.StorageVersion == StorageV2 { + a.CheckAllocStatus() //nolint:errcheck + } a.initialized = true } @@ -452,7 +459,7 @@ func (a *Allocation) dispatchWork(ctx context.Context) { }() case repairReq := <-a.repairChan: - l.Logger.Info(fmt.Sprintf("received a repair request for %v\n", repairReq.listDir.Path)) + l.Logger.Info(fmt.Sprintf("received a repair request for %v\n", repairReq.repairPath)) go repairReq.processRepair(ctx, a) } } @@ -513,7 +520,9 @@ func (a *Allocation) RepairFile(file sys.File, remotepath string, statusCallback if Workdir != "" { idr = Workdir } - mask = mask.Not().And(zboxutil.NewUint128(1).Lsh(uint64(len(a.Blobbers))).Sub64(1)) + if a.StorageVersion == 0 { + mask = mask.Not().And(zboxutil.NewUint128(1).Lsh(uint64(len(a.Blobbers))).Sub64(1)) + } fileMeta := FileMeta{ ActualSize: ref.ActualFileSize, MimeType: ref.MimeType, @@ -1069,7 +1078,7 @@ func (a *Allocation) DoMultiOperation(operations []OperationRequest, opts ...Mul operation = NewRenameOperation(op.RemotePath, op.DestName, mo.operationMask, mo.maskMU, mo.consensusThresh, mo.fullconsensus, mo.ctx) case constants.FileOperationCopy: - operation = NewCopyOperation(op.RemotePath, op.DestPath, mo.operationMask, mo.maskMU, mo.consensusThresh, mo.fullconsensus, mo.ctx) + operation = NewCopyOperation(mo.ctx, op.RemotePath, op.DestPath, mo.operationMask, mo.maskMU, mo.consensusThresh, mo.fullconsensus, op.CopyDirOnly) case constants.FileOperationMove: operation = NewMoveOperation(op.RemotePath, op.DestPath, mo.operationMask, mo.maskMU, mo.consensusThresh, mo.fullconsensus, mo.ctx) @@ -1082,9 +1091,9 @@ func (a *Allocation) DoMultiOperation(operations []OperationRequest, opts ...Mul case constants.FileOperationDelete: if op.Mask != nil { - operation = NewDeleteOperation(op.RemotePath, *op.Mask, mo.maskMU, mo.consensusThresh, mo.fullconsensus, mo.ctx) + operation = NewDeleteOperation(mo.ctx, op.RemotePath, *op.Mask, mo.maskMU, mo.consensusThresh, mo.fullconsensus) } else { - operation = NewDeleteOperation(op.RemotePath, mo.operationMask, mo.maskMU, mo.consensusThresh, mo.fullconsensus, mo.ctx) + operation = NewDeleteOperation(mo.ctx, op.RemotePath, mo.operationMask, mo.maskMU, mo.consensusThresh, mo.fullconsensus) } case constants.FileOperationUpdate: @@ -1411,6 +1420,7 @@ func (a *Allocation) processReadMarker(drs []*DownloadRequest) { now := time.Now() for _, dr := range drs { + dr.storageVersion = a.StorageVersion wg.Add(1) go func(dr *DownloadRequest) { defer wg.Done() @@ -1560,7 +1570,7 @@ func (a *Allocation) ListDirFromAuthTicket(authTicket string, lookupHash string, return nil, errors.New("invalid_path", "Invalid path for the list") } - listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} + listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}, storageVersion: a.StorageVersion} listReq.allocationID = a.ID listReq.allocationTx = a.Tx listReq.sig = a.sig @@ -1600,7 +1610,7 @@ func (a *Allocation) ListDir(path string, opts ...ListRequestOptions) (*ListResu if !isabs { return nil, errors.New("invalid_path", "Path should be valid and absolute") } - listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} + listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}, storageVersion: a.StorageVersion} listReq.allocationID = a.ID listReq.allocationTx = a.Tx listReq.sig = a.sig @@ -1623,7 +1633,7 @@ func (a *Allocation) ListDir(path string, opts ...ListRequestOptions) (*ListResu return nil, errors.New("list_request_failed", "Failed to get list response from the blobbers") } -func (a *Allocation) getRefs(path, pathHash, authToken, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int) (*ObjectTreeResult, error) { +func (a *Allocation) getRefs(path, pathHash, authToken, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int, opts ...ObjectTreeRequestOption) (*ObjectTreeResult, error) { if !a.isInitialized() { return nil, notInitialized } @@ -1644,9 +1654,13 @@ func (a *Allocation) getRefs(path, pathHash, authToken, offsetPath, updatedDate, fileType: fileType, refType: refType, ctx: a.ctx, + reqMask: zboxutil.NewUint128(1).Lsh(uint64(len(a.Blobbers))).Sub64(1), } oTreeReq.fullconsensus = a.fullconsensus oTreeReq.consensusThresh = a.DataShards + for _, opt := range opts { + opt(oTreeReq) + } return oTreeReq.GetRefs() } @@ -1766,15 +1780,15 @@ func (a *Allocation) GetRefsWithAuthTicket(authToken, offsetPath, updatedDate, o // - refType: the ref type to get the refs, e.g., file or directory. // - level: the level of the refs to get relative to the path root (strating from 0 as the root path). // - pageLimit: the limit of the refs to get per page. -func (a *Allocation) GetRefs(path, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int) (*ObjectTreeResult, error) { +func (a *Allocation) GetRefs(path, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int, opts ...ObjectTreeRequestOption) (*ObjectTreeResult, error) { if len(path) == 0 || !zboxutil.IsRemoteAbs(path) { return nil, errors.New("invalid_path", fmt.Sprintf("Absolute path required. Path provided: %v", path)) } - return a.getRefs(path, "", "", offsetPath, updatedDate, offsetDate, fileType, refType, level, pageLimit) + return a.getRefs(path, "", "", offsetPath, updatedDate, offsetDate, fileType, refType, level, pageLimit, opts...) } -func (a *Allocation) ListObjects(ctx context.Context, path, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int) <-chan ORef { +func (a *Allocation) ListObjects(ctx context.Context, path, offsetPath, updatedDate, offsetDate, fileType, refType string, level, pageLimit int, opts ...ObjectTreeRequestOption) <-chan ORef { oRefChan := make(chan ORef, 1) sendObjectRef := func(ref ORef) { select { @@ -1793,11 +1807,13 @@ func (a *Allocation) ListObjects(ctx context.Context, path, offsetPath, updatedD }() continuationPath := offsetPath for { - oRefs, err := a.GetRefs(path, continuationPath, updatedDate, offsetDate, fileType, refType, level, pageLimit) + oRefs, err := a.GetRefs(path, continuationPath, updatedDate, offsetDate, fileType, refType, level, pageLimit, opts...) if err != nil { - sendObjectRef(ORef{ - Err: err, - }) + if !strings.Contains(err.Error(), "invalid_path") { + sendObjectRef(ORef{ + Err: err, + }) + } return } for _, ref := range oRefs.Refs { @@ -1877,7 +1893,7 @@ func (a *Allocation) GetFileMeta(path string) (*ConsolidatedFileMeta, error) { } result := &ConsolidatedFileMeta{} - listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} + listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}, storageVersion: a.StorageVersion} listReq.allocationID = a.ID listReq.allocationTx = a.Tx listReq.sig = a.sig @@ -1917,7 +1933,7 @@ func (a *Allocation) GetFileMetaByName(fileName string) ([]*ConsolidatedFileMeta } resultArr := []*ConsolidatedFileMetaByName{} - listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} + listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}, storageVersion: a.StorageVersion} listReq.allocationID = a.ID listReq.allocationTx = a.Tx listReq.blobbers = a.Blobbers @@ -1996,7 +2012,7 @@ func (a *Allocation) GetFileMetaFromAuthTicket(authTicket string, lookupHash str return nil, errors.New("invalid_path", "Invalid path for the list") } - listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} + listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}, storageVersion: a.StorageVersion} listReq.allocationID = a.ID listReq.allocationTx = a.Tx listReq.sig = a.sig @@ -2042,7 +2058,7 @@ func (a *Allocation) GetFileStats(path string) (map[string]*FileStats, error) { if !isabs { return nil, errors.New("invalid_path", "Path should be valid and absolute") } - listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}} + listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}, storageVersion: a.StorageVersion} listReq.allocationID = a.ID listReq.allocationTx = a.Tx listReq.sig = a.sig @@ -2811,18 +2827,25 @@ func (a *Allocation) StartRepair(localRootPath, pathToRepair string, statusCB St return notInitialized } - listDir, err := a.ListDir(pathToRepair, - WithListRequestForRepair(true), - WithListRequestPageLimit(-1), + var ( + listDir *ListResult + err error ) - if err != nil { - return err + if a.StorageVersion == 0 { + listDir, err = a.ListDir(pathToRepair, + WithListRequestForRepair(true), + WithListRequestPageLimit(-1), + ) + if err != nil { + return err + } } repairReq := &RepairRequest{ listDir: listDir, localRootPath: localRootPath, statusCB: statusCB, + repairPath: pathToRepair, } repairReq.completedCallback = func() { diff --git a/zboxcore/sdk/allocation_test.go b/zboxcore/sdk/allocation_test.go index cebf13f7e..bd1bd6dce 100644 --- a/zboxcore/sdk/allocation_test.go +++ b/zboxcore/sdk/allocation_test.go @@ -95,20 +95,21 @@ func setupMockHttpResponse( } func setupMockCommitRequest(a *Allocation) { - commitChan = make(map[string]chan *CommitRequest) + commitChan = make(map[string]chan CommitRequestInterface) for _, blobber := range a.Blobbers { if _, ok := commitChan[blobber.ID]; !ok { - commitChan[blobber.ID] = make(chan *CommitRequest, 1) + commitChan[blobber.ID] = make(chan CommitRequestInterface, 1) blobberChan := commitChan[blobber.ID] - go func(c <-chan *CommitRequest, blID string) { + go func(c <-chan CommitRequestInterface, blID string) { for { cm := <-c if cm != nil { - cm.result = &CommitResult{ + c := cm.(*CommitRequest) + c.result = &CommitResult{ Success: true, } - if cm.wg != nil { - cm.wg.Done() + if c.wg != nil { + c.wg.Done() } } } diff --git a/zboxcore/sdk/blobber_operations.go b/zboxcore/sdk/blobber_operations.go index 31a5a05dc..d04163a30 100644 --- a/zboxcore/sdk/blobber_operations.go +++ b/zboxcore/sdk/blobber_operations.go @@ -39,7 +39,7 @@ func CreateAllocationForOwner( } allocationRequest, err := getNewAllocationBlobbers( - datashards, parityshards, size, readPrice, writePrice, preferredBlobberIds, blobberAuthTickets, force) + StorageV2, datashards, parityshards, size, readPrice, writePrice, preferredBlobberIds, blobberAuthTickets, force) if err != nil { return "", 0, nil, errors.New("failed_get_allocation_blobbers", "failed to get blobbers for allocation: "+err.Error()) } @@ -53,6 +53,7 @@ func CreateAllocationForOwner( allocationRequest["third_party_extendable"] = thirdPartyExtendable allocationRequest["file_options_changed"], allocationRequest["file_options"] = calculateAllocationFileOptions(63 /*0011 1111*/, fileOptionsParams) allocationRequest["is_enterprise"] = IsEnterprise + allocationRequest["storage_version"] = StorageV2 var sn = transaction.SmartContractTxnData{ Name: transaction.NEW_ALLOCATION_REQUEST, diff --git a/zboxcore/sdk/chunked_upload.go b/zboxcore/sdk/chunked_upload.go index 41a3933a6..c2728e06f 100644 --- a/zboxcore/sdk/chunked_upload.go +++ b/zboxcore/sdk/chunked_upload.go @@ -292,7 +292,7 @@ func CreateChunkedUpload( su.chunkReader = cReader - su.formBuilder = CreateChunkedUploadFormBuilder() + su.formBuilder = CreateChunkedUploadFormBuilder(su.allocationObj.StorageVersion) su.isRepair = isRepair uploadWorker, uploadRequest := calculateWorkersAndRequests(su.allocationObj.DataShards, len(su.blobbers), su.chunkNumber) diff --git a/zboxcore/sdk/chunked_upload_blobber.go b/zboxcore/sdk/chunked_upload_blobber.go index 49eb9ee1e..9aacf17f6 100644 --- a/zboxcore/sdk/chunked_upload_blobber.go +++ b/zboxcore/sdk/chunked_upload_blobber.go @@ -242,7 +242,7 @@ func (sb *ChunkedUploadBlobber) processCommit(ctx context.Context, su *ChunkedUp formWriter.Close() - req, err := zboxutil.NewCommitRequest(sb.blobber.Baseurl, su.allocationObj.ID, su.allocationObj.Tx, body) + req, err := zboxutil.NewCommitRequest(sb.blobber.Baseurl, su.allocationObj.ID, su.allocationObj.Tx, body, 0) if err != nil { logger.Logger.Error("Error creating commit req: ", err) return err diff --git a/zboxcore/sdk/chunked_upload_form_builder.go b/zboxcore/sdk/chunked_upload_form_builder.go index cc65fa3fc..51c3711ad 100644 --- a/zboxcore/sdk/chunked_upload_form_builder.go +++ b/zboxcore/sdk/chunked_upload_form_builder.go @@ -4,10 +4,12 @@ import ( "bytes" "encoding/hex" "encoding/json" + "fmt" "io" "mime/multipart" "sync" + "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/client" "golang.org/x/crypto/sha3" @@ -17,11 +19,11 @@ import ( type ChunkedUploadFormBuilder interface { // build form data Build( - fileMeta *FileMeta, hasher Hasher, connectionID string, + fileMeta *FileMeta, hasher Hasher, connectionID, blobberID string, chunkSize int64, chunkStartIndex, chunkEndIndex int, isFinal bool, encryptedKey, encryptedKeyPoint string, fileChunksData [][]byte, thumbnailChunkData []byte, shardSize int64, - ) (BlobberData, error) + ) (blobberData, error) } // ChunkedUploadFormMetadata upload form metadata @@ -35,27 +37,30 @@ type ChunkedUploadFormMetadata struct { } // CreateChunkedUploadFormBuilder create ChunkedUploadFormBuilder instance -func CreateChunkedUploadFormBuilder() ChunkedUploadFormBuilder { - return &chunkedUploadFormBuilder{} +func CreateChunkedUploadFormBuilder(storageVersion int) ChunkedUploadFormBuilder { + return &chunkedUploadFormBuilder{ + storageVersion, + } } type chunkedUploadFormBuilder struct { + storageVersion int } const MAX_BLOCKS = 80 // 5MB(CHUNK_SIZE*80) func (b *chunkedUploadFormBuilder) Build( - fileMeta *FileMeta, hasher Hasher, connectionID string, + fileMeta *FileMeta, hasher Hasher, connectionID, blobberID string, chunkSize int64, chunkStartIndex, chunkEndIndex int, isFinal bool, encryptedKey, encryptedKeyPoint string, fileChunksData [][]byte, thumbnailChunkData []byte, shardSize int64, -) (BlobberData, error) { +) (blobberData, error) { metadata := ChunkedUploadFormMetadata{ ThumbnailBytesLen: len(thumbnailChunkData), } - var res BlobberData + var res blobberData if len(fileChunksData) == 0 { return res, nil @@ -167,8 +172,12 @@ func (b *chunkedUploadFormBuilder) Build( if err != nil { return res, err } - - validationRootSignature, err := client.Sign(actualHashSignature + formData.ValidationRoot) + hash := actualHashSignature + formData.ValidationRoot + if b.storageVersion == StorageV2 { + hashData := fmt.Sprintf("%s:%s:%s:%s", fileMeta.ActualHash, formData.ValidationRoot, formData.FixedMerkleRoot, blobberID) + hash = encryption.Hash(hashData) + } + validationRootSignature, err := client.Sign(hash) if err != nil { return res, err } diff --git a/zboxcore/sdk/chunked_upload_form_builder_bench_test.go b/zboxcore/sdk/chunked_upload_form_builder_bench_test.go index c021d6528..156910efe 100644 --- a/zboxcore/sdk/chunked_upload_form_builder_bench_test.go +++ b/zboxcore/sdk/chunked_upload_form_builder_bench_test.go @@ -53,7 +53,7 @@ func BenchmarkChunkedUploadFormBuilder(b *testing.B) { for i := 0; i < b.N; i++ { - builder := CreateChunkedUploadFormBuilder() + builder := CreateChunkedUploadFormBuilder(0) isFinal := false @@ -68,7 +68,7 @@ func BenchmarkChunkedUploadFormBuilder(b *testing.B) { fileBytes := buf[begin:end] - _, err := builder.Build(fileMeta, hasher, "connectionID", int64(bm.ChunkSize), chunkIndex, chunkIndex, isFinal, "", "", [][]byte{fileBytes}, nil, getShardSize(fileMeta.ActualSize, 1, false)) + _, err := builder.Build(fileMeta, hasher, "connectionID", "blobberID", int64(bm.ChunkSize), chunkIndex, chunkIndex, isFinal, "", "", [][]byte{fileBytes}, nil, getShardSize(fileMeta.ActualSize, 1, false)) if err != nil { b.Fatal(err) return diff --git a/zboxcore/sdk/chunked_upload_model.go b/zboxcore/sdk/chunked_upload_model.go index 25b9d1c6d..356272ee1 100644 --- a/zboxcore/sdk/chunked_upload_model.go +++ b/zboxcore/sdk/chunked_upload_model.go @@ -215,10 +215,10 @@ type UploadData struct { chunkEndIndex int isFinal bool uploadLength int64 - uploadBody []BlobberData + uploadBody []blobberData } -type BlobberData struct { +type blobberData struct { dataBuffers []*bytes.Buffer formData ChunkedUploadFormMetadata contentSlice []string diff --git a/zboxcore/sdk/chunked_upload_process.go b/zboxcore/sdk/chunked_upload_process.go index aa5662bd8..72a7560b7 100644 --- a/zboxcore/sdk/chunked_upload_process.go +++ b/zboxcore/sdk/chunked_upload_process.go @@ -70,19 +70,19 @@ func (su *ChunkedUpload) processUpload(chunkStartIndex, chunkEndIndex int, var ( errCount int32 - finalBuffer []BlobberData + finalBuffer []blobberData pos uint64 wg sync.WaitGroup lastBufferOnly bool ) if isFinal { - finalBuffer = make([]BlobberData, len(su.blobbers)) + finalBuffer = make([]blobberData, len(su.blobbers)) } blobberUpload := UploadData{ chunkStartIndex: chunkStartIndex, chunkEndIndex: chunkEndIndex, isFinal: isFinal, - uploadBody: make([]BlobberData, len(su.blobbers)), + uploadBody: make([]blobberData, len(su.blobbers)), uploadLength: uploadLength, } @@ -106,7 +106,7 @@ func (su *ChunkedUpload) processUpload(chunkStartIndex, chunkEndIndex int, go func(b *ChunkedUploadBlobber, thumbnailChunkData []byte, pos uint64) { defer wg.Done() uploadData, err := su.formBuilder.Build( - &su.fileMeta, blobber.progress.Hasher, su.progress.ConnectionID, + &su.fileMeta, blobber.progress.Hasher, su.progress.ConnectionID, blobber.blobber.ID, su.chunkSize, chunkStartIndex, chunkEndIndex, isFinal, su.encryptedKey, su.progress.EncryptedKeyPoint, fileShards[pos], thumbnailChunkData, su.shardSize) if err != nil { @@ -117,7 +117,7 @@ func (su *ChunkedUpload) processUpload(chunkStartIndex, chunkEndIndex int, return } if isFinal { - finalBuffer[pos] = BlobberData{ + finalBuffer[pos] = blobberData{ dataBuffers: uploadData.dataBuffers[len(uploadData.dataBuffers)-1:], formData: uploadData.formData, contentSlice: uploadData.contentSlice[len(uploadData.contentSlice)-1:], diff --git a/zboxcore/sdk/chunked_upload_process_js.go b/zboxcore/sdk/chunked_upload_process_js.go index fdb39fef5..503f5de8c 100644 --- a/zboxcore/sdk/chunked_upload_process_js.go +++ b/zboxcore/sdk/chunked_upload_process_js.go @@ -52,6 +52,7 @@ type ChunkedUploadFormInfo struct { AllocationID string AllocationTx string OnlyHash bool + StorageVersion int } // createUploadProgress create a new UploadProgress @@ -114,6 +115,7 @@ func (su *ChunkedUpload) processUpload(chunkStartIndex, chunkEndIndex int, AllocationID: su.allocationObj.ID, AllocationTx: su.allocationObj.Tx, OnlyHash: chunkEndIndex <= su.progress.ChunkIndex, + StorageVersion: su.allocationObj.StorageVersion, } formInfoJSON, err := json.Marshal(formInfo) if err != nil { @@ -448,8 +450,9 @@ func ProcessEventData(data safejs.Value) { if formInfo.IsFinal { defer delete(hasherMap, fileMeta.RemotePath) } - formBuilder := CreateChunkedUploadFormBuilder() - uploadData, err := formBuilder.Build(fileMeta, wp.hasher, formInfo.ConnectionID, formInfo.ChunkSize, formInfo.ChunkStartIndex, formInfo.ChunkEndIndex, formInfo.IsFinal, formInfo.EncryptedKey, formInfo.EncryptedKeyPoint, + blobberID := os.Getenv("BLOBBER_ID") + formBuilder := CreateChunkedUploadFormBuilder(formInfo.StorageVersion) + uploadData, err := formBuilder.Build(fileMeta, wp.hasher, formInfo.ConnectionID, blobberID, formInfo.ChunkSize, formInfo.ChunkStartIndex, formInfo.ChunkEndIndex, formInfo.IsFinal, formInfo.EncryptedKey, formInfo.EncryptedKeyPoint, fileShards, thumbnailChunkData, formInfo.ShardSize) if err != nil { selfPostMessage(false, false, err.Error(), remotePath, formInfo.ChunkEndIndex, nil) @@ -472,7 +475,7 @@ func ProcessEventData(data safejs.Value) { if !formInfo.IsFinal { wp.wg.Add(1) } - go func(blobberData BlobberData, remotePath string, wg *sync.WaitGroup) { + go func(blobberData blobberData, remotePath string, wg *sync.WaitGroup) { if formInfo.IsFinal && len(blobberData.dataBuffers) > 1 { err = sendUploadRequest(blobberData.dataBuffers[:len(blobberData.dataBuffers)-1], blobberData.contentSlice[:len(blobberData.contentSlice)-1], blobberURL, formInfo.AllocationID, formInfo.AllocationTx, formInfo.HttpMethod) if err != nil { diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 6a0d46852..f400d80de 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -7,6 +7,8 @@ import ( "encoding/json" "fmt" "io" + "io/ioutil" + "math" "mime/multipart" "net/http" "strconv" @@ -14,9 +16,11 @@ import ( "sync" "time" + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/errors" thrown "github.com/0chain/errors" "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/zboxcore/allocationchange" "github.com/0chain/gosdk/zboxcore/blockchain" "github.com/0chain/gosdk/zboxcore/fileref" @@ -33,6 +37,12 @@ type ReferencePathResult struct { Version string `json:"version"` } +type ReferencePathResultV2 struct { + Path []byte `json:"path"` + LatestWM *marker.WriteMarker `json:"latest_write_marker"` + Version string `json:"version"` +} + type CommitResult struct { Success bool `json:"success"` ErrorMessage string `json:"error_msg,omitempty"` @@ -63,19 +73,41 @@ type CommitRequest struct { blobberInd uint64 } -var commitChan map[string]chan *CommitRequest -var initCommitMutex sync.Mutex +type CommitRequestInterface interface { + processCommit() + blobberID() string +} + +type CommitRequestV2 struct { + changes []allocationchange.AllocationChangeV2 + allocationObj *Allocation + connectionID string + sig string + wg *sync.WaitGroup + result *CommitResult + timestamp int64 + consensusThresh int + commitMask zboxutil.Uint128 + changeIndex uint64 + isRepair bool +} + +var ( + commitChan map[string]chan CommitRequestInterface + initCommitMutex sync.Mutex + errAlreadySuccessful = errors.New("alread_successful", "") +) func InitCommitWorker(blobbers []*blockchain.StorageNode) { initCommitMutex.Lock() defer initCommitMutex.Unlock() if commitChan == nil { - commitChan = make(map[string]chan *CommitRequest) + commitChan = make(map[string]chan CommitRequestInterface) } for _, blobber := range blobbers { if _, ok := commitChan[blobber.ID]; !ok { - commitChan[blobber.ID] = make(chan *CommitRequest, 1) + commitChan[blobber.ID] = make(chan CommitRequestInterface, 1) blobberChan := commitChan[blobber.ID] go startCommitWorker(blobberChan, blobber.ID) } @@ -83,7 +115,7 @@ func InitCommitWorker(blobbers []*blockchain.StorageNode) { } -func startCommitWorker(blobberChan chan *CommitRequest, blobberID string) { +func startCommitWorker(blobberChan chan CommitRequestInterface, blobberID string) { for { commitreq, open := <-blobberChan if !open { @@ -96,6 +128,10 @@ func startCommitWorker(blobberChan chan *CommitRequest, blobberID string) { delete(commitChan, blobberID) } +func (commitreq *CommitRequest) blobberID() string { + return commitreq.blobber.ID +} + func (commitreq *CommitRequest) processCommit() { defer commitreq.wg.Done() start := time.Now() @@ -126,7 +162,7 @@ func (commitreq *CommitRequest) processCommit() { if resp.StatusCode != http.StatusOK { l.Logger.Error("Ref path response : ", resp.StatusCode) } - resp_body, err := io.ReadAll(resp.Body) + resp_body, err := ioutil.ReadAll(resp.Body) if err != nil { l.Logger.Error("Ref path: Resp", err) return err @@ -271,7 +307,7 @@ func (req *CommitRequest) commitBlobber( l.Logger.Error("Creating form writer failed: ", err) return } - httpreq, err := zboxutil.NewCommitRequest(req.blobber.Baseurl, req.allocationID, req.allocationTx, body) + httpreq, err := zboxutil.NewCommitRequest(req.blobber.Baseurl, req.allocationID, req.allocationTx, body, 0) if err != nil { l.Logger.Error("Error creating commit req: ", err) return @@ -345,38 +381,199 @@ func (req *CommitRequest) commitBlobber( return thrown.New("commit_error", fmt.Sprintf("Commit failed with response status %d", resp.StatusCode)) } -func AddCommitRequest(req *CommitRequest) { - commitChan[req.blobber.ID] <- req +func AddCommitRequest(req CommitRequestInterface) { + commitChan[req.blobberID()] <- req } -func (commitreq *CommitRequest) calculateHashRequest(ctx context.Context, paths []string) error { //nolint - var req *http.Request - req, err := zboxutil.NewCalculateHashRequest(commitreq.blobber.Baseurl, commitreq.allocationID, commitreq.allocationTx, paths) - if err != nil || len(paths) == 0 { - l.Logger.Error("Creating calculate hash req", err) - return err +func (commitReq *CommitRequestV2) blobberID() string { + var pos uint64 + for i := commitReq.commitMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + commitReq.changeIndex = pos + return commitReq.allocationObj.Blobbers[pos].ID } - ctx, cncl := context.WithTimeout(ctx, (time.Second * 30)) - err = zboxutil.HttpDo(ctx, cncl, req, func(resp *http.Response, err error) error { - if err != nil { - l.Logger.Error("Calculate hash error:", err) - return err + // we should never reach here + return "" +} + +type refPathResp struct { + trie *wmpt.WeightedMerkleTrie + pos uint64 + err error +} + +func (commitReq *CommitRequestV2) processCommit() { + defer commitReq.wg.Done() + l.Logger.Debug("received a commit request") + paths := make([]string, 0) + changeIndex := commitReq.changeIndex + for i := 0; i < len(commitReq.changes); i++ { + lookupHashes := commitReq.changes[i].GetLookupHash(changeIndex) + if lookupHashes != nil { + paths = append(paths, lookupHashes...) + } else { + commitReq.changes[i] = nil } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - l.Logger.Error("Calculate hash response : ", resp.StatusCode) + } + + if commitReq.isRepair && len(paths) == 0 { + commitReq.result = SuccessCommitResult() + return + } + + var ( + pos uint64 + mu = &sync.Mutex{} + success bool + ) + now := time.Now() + respChan := make(chan refPathResp, commitReq.commitMask.CountOnes()) + for i := commitReq.commitMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + go func(ind uint64) { + blobber := commitReq.allocationObj.Blobbers[ind] + trie, err := getReferencePathV2(blobber, commitReq.allocationObj.ID, commitReq.allocationObj.Tx, commitReq.sig, paths, &success, mu) + resp := refPathResp{ + trie: trie, + err: err, + pos: ind, + } + respChan <- resp + }(pos) + } + + var ( + trie *wmpt.WeightedMerkleTrie + err error + ) + + for { + resp := <-respChan + if resp.err == nil { + trie = resp.trie + latestWM := commitReq.allocationObj.Blobbers[resp.pos].LatestWM + if latestWM != nil && commitReq.timestamp <= latestWM.Timestamp { + commitReq.timestamp = latestWM.Timestamp + 1 + } + break + } else if resp.err != errAlreadySuccessful { + commitReq.commitMask = commitReq.commitMask.And(zboxutil.NewUint128(1).Lsh(resp.pos).Not()) + if commitReq.commitMask.CountOnes() < commitReq.consensusThresh { + commitReq.result = ErrorCommitResult("Failed to get reference path " + resp.err.Error()) + return + } } - resp_body, err := io.ReadAll(resp.Body) + } + + if trie == nil { + commitReq.result = ErrorCommitResult("Failed to get reference path") + return + } + if commitReq.commitMask.CountOnes() < commitReq.consensusThresh { + commitReq.result = ErrorCommitResult("Failed to get reference path") + return + } + elapsedGetRefPath := time.Since(now) + prevWeight := trie.Weight() + for _, change := range commitReq.changes { + if change == nil { + continue + } + err = change.ProcessChangeV2(trie, changeIndex) + if err != nil && err != wmpt.ErrNotFound { + l.Logger.Error("Error processing change ", err) + commitReq.result = ErrorCommitResult("Failed to process change " + err.Error()) + return + } + } + rootHash := trie.GetRoot().CalcHash() + rootWeight := trie.Weight() + elapsedProcessChanges := time.Since(now) - elapsedGetRefPath + wg := sync.WaitGroup{} + errSlice := make([]error, commitReq.commitMask.CountOnes()) + counter := 0 + for i := commitReq.commitMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + blobber := commitReq.allocationObj.Blobbers[pos] + blobberPos := pos + wg.Add(1) + go func(ind int) { + defer wg.Done() + err = commitReq.commitBlobber(rootHash, rootWeight, prevWeight, blobberPos, blobber) + if err != nil { + l.Logger.Error("Error committing to blobber", err) + errSlice[ind] = err + mu.Lock() + commitReq.commitMask = commitReq.commitMask.And(zboxutil.NewUint128(1).Lsh(blobberPos).Not()) + mu.Unlock() + return + } + }(counter) + counter++ + } + wg.Wait() + elapsedCommit := time.Since(now) - elapsedProcessChanges - elapsedGetRefPath + if commitReq.commitMask.CountOnes() < commitReq.consensusThresh { + err = zboxutil.MajorError(errSlice) + if err == nil { + err = errors.New("consensus_not_met", fmt.Sprintf("Successfully committed to %d blobbers, but required %d", commitReq.commitMask.CountOnes(), commitReq.consensusThresh)) + } + commitReq.result = ErrorCommitResult(err.Error()) + return + } + if !commitReq.isRepair { + commitReq.allocationObj.allocationRoot = hex.EncodeToString(rootHash) + } + l.Logger.Info("[commit] ", "elapsedGetRefPath ", elapsedGetRefPath.Milliseconds(), " elapsedProcessChanges ", elapsedProcessChanges.Milliseconds(), " elapsedCommit ", elapsedCommit.Milliseconds(), " total ", time.Since(now).Milliseconds()) + commitReq.result = SuccessCommitResult() +} + +func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, prevWeight, changeIndex uint64, blobber *blockchain.StorageNode) (err error) { + hasher := sha256.New() + if blobber.LatestWM != nil { + prevChainHash, err := hex.DecodeString(blobber.LatestWM.ChainHash) if err != nil { - l.Logger.Error("Calculate hash: Resp", err) + l.Logger.Error("Error decoding prev chain hash", err) return err } - if resp.StatusCode != http.StatusOK { - return errors.New(strconv.Itoa(resp.StatusCode), fmt.Sprintf("Calculate hash error response: Body: %s ", string(resp_body))) - } - return nil - }) - return err + hasher.Write(prevChainHash) //nolint:errcheck + } + fileMetaRoot := hex.EncodeToString(rootHash) + wm := &marker.WriteMarker{} + wm.AllocationRoot = encryption.Hash(fileMetaRoot + req.allocationObj.ID) + wm.Size = (int64(rootWeight) - int64(prevWeight)) * CHUNK_SIZE + decodedAllocationRoot, _ := hex.DecodeString(wm.AllocationRoot) + hasher.Write(decodedAllocationRoot) //nolint:errcheck + chainHash := hex.EncodeToString(hasher.Sum(nil)) + wm.ChainHash = chainHash + wm.ChainSize = int64(rootWeight) * CHUNK_SIZE + if blobber.LatestWM != nil { + wm.PreviousAllocationRoot = blobber.LatestWM.AllocationRoot + } + wm.BlobberID = blobber.ID + wm.Timestamp = req.timestamp + wm.AllocationID = req.allocationObj.ID + wm.FileMetaRoot = fileMetaRoot + wm.ClientID = client.ClientID() + err = wm.Sign() + if err != nil { + l.Logger.Error("Error signing writemarker", err) + return err + } + wmData, err := json.Marshal(wm) + if err != nil { + l.Logger.Error("Error marshalling writemarker data", err) + return err + } + + err = submitWriteMarker(wmData, nil, blobber, req.connectionID, req.allocationObj.ID, req.allocationObj.Tx, req.allocationObj.StorageVersion) + if err != nil { + l.Logger.Error("Error submitting writemarker ", err) + return err + } + blobber.LatestWM = wm + blobber.AllocationRoot = wm.AllocationRoot + return } func getFormWritter(connectionID string, wmData, fileIDMetaData []byte, body *bytes.Buffer) (*multipart.Writer, error) { @@ -390,11 +587,179 @@ func getFormWritter(connectionID string, wmData, fileIDMetaData []byte, body *by if err != nil { return nil, err } + if len(fileIDMetaData) > 0 { + err = formWriter.WriteField("file_id_meta", string(fileIDMetaData)) + if err != nil { + return nil, err + } + } + formWriter.Close() + return formWriter, nil +} - err = formWriter.WriteField("file_id_meta", string(fileIDMetaData)) +func getReferencePathV2(blobber *blockchain.StorageNode, allocationID, allocationTx, sig string, paths []string, success *bool, mu *sync.Mutex) (*wmpt.WeightedMerkleTrie, error) { + if len(paths) == 0 { + var node wmpt.Node + if blobber.LatestWM != nil && len(blobber.LatestWM.FileMetaRoot) > 0 { + decodedRoot, _ := hex.DecodeString(blobber.LatestWM.FileMetaRoot) + node = wmpt.NewHashNode(decodedRoot, uint64(numBlocks(blobber.LatestWM.ChainSize))) + } + trie := wmpt.New(node, nil) + return trie, nil + } + now := time.Now() + req, err := zboxutil.NewReferencePathRequestV2(blobber.Baseurl, allocationID, allocationTx, sig, paths, false) if err != nil { + l.Logger.Error("Creating ref path req", err) return nil, err } - formWriter.Close() - return formWriter, nil + var lR ReferencePathResultV2 + ctx, cncl := context.WithTimeout(context.Background(), (time.Second * 30)) + err = zboxutil.HttpDo(ctx, cncl, req, func(resp *http.Response, err error) error { + if err != nil { + l.Logger.Error("Ref path error:", err) + return err + } + defer resp.Body.Close() + respBody, err := io.ReadAll(resp.Body) + if err != nil { + l.Logger.Error("Ref path: Resp", err) + return err + } + if resp.StatusCode != http.StatusOK { + return errors.New( + strconv.Itoa(resp.StatusCode), + fmt.Sprintf("Reference path error response: Status: %d - %s ", + resp.StatusCode, string(respBody))) + } + err = json.Unmarshal(respBody, &lR) + if err != nil { + l.Logger.Error("Reference path json decode error: ", err) + return err + } + return nil + }) + + if err != nil { + return nil, err + } + elapsedRefPath := time.Since(now) + mu.Lock() + defer mu.Unlock() + if *success { + return nil, errAlreadySuccessful + } + trie := wmpt.New(nil, nil) + if lR.LatestWM != nil { + err = lR.LatestWM.VerifySignature(client.PublicKey()) + if err != nil { + return nil, errors.New("signature_verification_failed", err.Error()) + } + err = trie.Deserialize(lR.Path) + if err != nil { + l.Logger.Error("Error deserializing trie", err) + return nil, err + } + l.Logger.Info("[getReferencePathV2] elapsedRefPath ", elapsedRefPath.Milliseconds(), " elapsedDeserialize ", (time.Since(now) - elapsedRefPath).Milliseconds()) + chainBlocks := numBlocks(lR.LatestWM.ChainSize) + if trie.Weight() != uint64(chainBlocks) { + return nil, errors.New("chain_length_mismatch", fmt.Sprintf("Expected chain length %d, got %d", chainBlocks, trie.Weight())) + } + if hex.EncodeToString(trie.Root()) != lR.LatestWM.FileMetaRoot { + return nil, errors.New("allocation_root_mismatch", fmt.Sprintf("Expected allocation root %s, got %s", lR.LatestWM.AllocationRoot, hex.EncodeToString(trie.Root()))) + } + } + *success = true + return trie, nil +} + +func submitWriteMarker(wmData, metaData []byte, blobber *blockchain.StorageNode, connectionID, allocationID, allocationTx string, apiVersion int) (err error) { + var ( + resp *http.Response + shouldContinue bool + ) + for retries := 0; retries < 6; retries++ { + err, shouldContinue = func() (err error, shouldContinue bool) { + body := new(bytes.Buffer) + formWriter, err := getFormWritter(connectionID, wmData, metaData, body) + if err != nil { + l.Logger.Error("Creating form writer failed: ", err) + return + } + httpreq, err := zboxutil.NewCommitRequest(blobber.Baseurl, allocationID, allocationTx, body, apiVersion) + if err != nil { + l.Logger.Error("Error creating commit req: ", err) + return + } + httpreq.Header.Add("Content-Type", formWriter.FormDataContentType()) + reqCtx, ctxCncl := context.WithTimeout(context.Background(), time.Second*60) + resp, err = zboxutil.Client.Do(httpreq.WithContext(reqCtx)) + defer ctxCncl() + + if err != nil { + logger.Logger.Error("Commit: ", err) + return + } + + if resp.Body != nil { + defer resp.Body.Close() + } + + var respBody []byte + respBody, err = io.ReadAll(resp.Body) + if err != nil { + logger.Logger.Error("Response read: ", err) + return + } + if resp.StatusCode == http.StatusOK { + logger.Logger.Debug(blobber.Baseurl, " committed") + return + } + + if resp.StatusCode == http.StatusTooManyRequests { + logger.Logger.Debug(blobber.Baseurl, + " got too many request error. Retrying") + + var r int + r, err = zboxutil.GetRateLimitValue(resp) + if err != nil { + logger.Logger.Error(err) + return + } + + time.Sleep(time.Duration(r) * time.Second) + shouldContinue = true + return + } + + if strings.Contains(string(respBody), "pending_markers:") { + logger.Logger.Debug("Commit pending for blobber ", + blobber.Baseurl, " Retrying") + time.Sleep(5 * time.Second) + shouldContinue = true + return + } + + if strings.Contains(string(respBody), "chain_length_exceeded") { + l.Logger.Error("Chain length exceeded for blobber ", + blobber.Baseurl, " Retrying") + time.Sleep(5 * time.Second) + shouldContinue = true + return + } + + err = thrown.New("commit_error", + fmt.Sprintf("Got error response %s with status %d", respBody, resp.StatusCode)) + return + }() + if shouldContinue { + continue + } + return + } + return thrown.New("commit_error", fmt.Sprintf("Commit failed with response status %d", resp.StatusCode)) +} + +func numBlocks(size int64) int64 { + return int64(math.Ceil(float64(size*1.0) / CHUNK_SIZE)) } diff --git a/zboxcore/sdk/common.go b/zboxcore/sdk/common.go index 3c2c7bd84..fe47986b3 100644 --- a/zboxcore/sdk/common.go +++ b/zboxcore/sdk/common.go @@ -8,6 +8,7 @@ import ( "net/http" "path" "strconv" + "strings" "sync" "time" @@ -120,3 +121,111 @@ func ValidateRemoteFileName(remotePath string) error { return nil } + +type subDirRequest struct { + opType string + subOpType string + remotefilepath string + destPath string + allocationObj *Allocation + ctx context.Context + consensusThresh int + mask zboxutil.Uint128 +} + +func (req *subDirRequest) processSubDirectories() error { + var ( + offsetPath string + pathLevel int + ) + + for { + oResult, err := req.allocationObj.GetRefs(req.remotefilepath, offsetPath, "", "", fileref.FILE, fileref.REGULAR, 0, getRefPageLimit, WithObjectContext(req.ctx), WithObjectConsensusThresh(req.consensusThresh), WithSingleBlobber(true), WithObjectMask(req.mask)) + if err != nil { + return err + } + if len(oResult.Refs) == 0 { + break + } + ops := make([]OperationRequest, 0, len(oResult.Refs)) + for _, ref := range oResult.Refs { + opMask := req.mask + if ref.Type == fileref.DIRECTORY { + continue + } + if ref.PathLevel > pathLevel { + pathLevel = ref.PathLevel + } + var destPath string + if req.subOpType == constants.FileOperationRename { + destPath = path.Dir(strings.Replace(ref.Path, req.remotefilepath, req.destPath, 1)) + } else { + basePath := strings.TrimPrefix(path.Dir(ref.Path), path.Dir(req.remotefilepath)) + destPath = path.Join(req.destPath, basePath) + } + op := OperationRequest{ + OperationType: req.opType, + RemotePath: ref.Path, + DestPath: destPath, + Mask: &opMask, + } + ops = append(ops, op) + } + err = req.allocationObj.DoMultiOperation(ops) + if err != nil { + return err + } + offsetPath = oResult.Refs[len(oResult.Refs)-1].Path + if len(oResult.Refs) < getRefPageLimit { + break + } + } + + offsetPath = "" + level := len(strings.Split(strings.TrimSuffix(req.remotefilepath, "/"), "/")) + if pathLevel == 0 { + pathLevel = level + 1 + } + + for pathLevel > level { + oResult, err := req.allocationObj.GetRefs(req.remotefilepath, offsetPath, "", "", fileref.DIRECTORY, fileref.REGULAR, pathLevel, getRefPageLimit, WithObjectContext(req.ctx), WithObjectMask(req.mask), WithObjectConsensusThresh(req.consensusThresh), WithSingleBlobber(true)) + if err != nil { + return err + } + if len(oResult.Refs) == 0 { + pathLevel-- + } else { + ops := make([]OperationRequest, 0, len(oResult.Refs)) + for _, ref := range oResult.Refs { + opMask := req.mask + if ref.Type == fileref.FILE { + continue + } + var destPath string + if req.subOpType == constants.FileOperationRename { + destPath = path.Dir(strings.Replace(ref.Path, req.remotefilepath, req.destPath, 1)) + } else { + basePath := strings.TrimPrefix(path.Dir(ref.Path), path.Dir(req.remotefilepath)) + destPath = path.Join(req.destPath, basePath) + } + op := OperationRequest{ + OperationType: req.opType, + RemotePath: ref.Path, + DestPath: destPath, + Mask: &opMask, + } + ops = append(ops, op) + } + err = req.allocationObj.DoMultiOperation(ops) + if err != nil { + return err + } + offsetPath = oResult.Refs[len(oResult.Refs)-1].Path + if len(oResult.Refs) < getRefPageLimit { + pathLevel-- + } + } + } + + return nil +} diff --git a/zboxcore/sdk/copyworker.go b/zboxcore/sdk/copyworker.go index 2942740ad..a7fbb4322 100644 --- a/zboxcore/sdk/copyworker.go +++ b/zboxcore/sdk/copyworker.go @@ -3,16 +3,20 @@ package sdk import ( "bytes" "context" + "encoding/hex" "fmt" "io" "mime/multipart" "net/http" + "path" "strings" "sync" "time" + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/errors" "github.com/google/uuid" + "go.uber.org/zap" "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/common" @@ -40,15 +44,40 @@ type CopyRequest struct { maskMU *sync.Mutex connectionID string timestamp int64 + dirOnly bool + destLookupHash string Consensus } +var errNoChange = errors.New("no_change", "No change in the operation") + +const objAlreadyExists = "Object Already exists" + func (req *CopyRequest) getObjectTreeFromBlobber(blobber *blockchain.StorageNode) (fileref.RefEntity, error) { return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.sig, req.remotefilepath, blobber) } +func (req *CopyRequest) getFileMetaFromBlobber(pos int) (fileRef *fileref.FileRef, err error) { + listReq := &ListRequest{ + allocationID: req.allocationID, + allocationTx: req.allocationTx, + blobbers: req.blobbers, + remotefilepath: req.remotefilepath, + ctx: req.ctx, + } + respChan := make(chan *fileMetaResponse) + go listReq.getFileMetaInfoFromBlobber(req.blobbers[pos], int(pos), respChan) + refRes := <-respChan + if refRes.err != nil { + err = refRes.err + return + } + fileRef = refRes.fileref + return +} + func (req *CopyRequest) copyBlobberObject( - blobber *blockchain.StorageNode, blobberIdx int) (refEntity fileref.RefEntity, err error) { + blobber *blockchain.StorageNode, blobberIdx int, fetchObjectTree bool) (refEntity fileref.RefEntity, err error) { defer func() { if err != nil { @@ -58,9 +87,11 @@ func (req *CopyRequest) copyBlobberObject( req.maskMU.Unlock() } }() - refEntity, err = req.getObjectTreeFromBlobber(req.blobbers[blobberIdx]) - if err != nil { - return nil, err + if fetchObjectTree { + refEntity, err = req.getObjectTreeFromBlobber(req.blobbers[blobberIdx]) + if err != nil { + return nil, err + } } var resp *http.Response @@ -126,7 +157,7 @@ func (req *CopyRequest) copyBlobberObject( } if resp.StatusCode == http.StatusOK { - l.Logger.Info(blobber.Baseurl, " "+req.remotefilepath, " copied.") + l.Logger.Debug(blobber.Baseurl, " "+req.remotefilepath, " copied.") req.Consensus.Done() return } @@ -163,7 +194,7 @@ func (req *CopyRequest) copyBlobberObject( fmt.Sprintf("last status code: %d, last response message: %s", latestStatusCode, latestRespMsg)) } -func (req *CopyRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) { +func (req *CopyRequest) ProcessWithBlobbers() ([]fileref.RefEntity, error) { var pos uint64 numList := len(req.blobbers) objectTreeRefs := make([]fileref.RefEntity, numList) @@ -175,7 +206,7 @@ func (req *CopyRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) { wg.Add(1) go func(blobberIdx int) { defer wg.Done() - refEntity, err := req.copyBlobberObject(req.blobbers[blobberIdx], blobberIdx) + refEntity, err := req.copyBlobberObject(req.blobbers[blobberIdx], blobberIdx, true) if err != nil { blobberErrors[blobberIdx] = err l.Logger.Debug(err.Error()) @@ -185,7 +216,82 @@ func (req *CopyRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) { }(int(pos)) } wg.Wait() - return objectTreeRefs, blobberErrors + return objectTreeRefs, zboxutil.MajorError(blobberErrors) +} + +func (req *CopyRequest) ProcessWithBlobbersV2() ([]fileref.RefEntity, error) { + var ( + pos uint64 + consensusRef *fileref.FileRef + ) + numList := len(req.blobbers) + objectTreeRefs := make([]fileref.RefEntity, numList) + blobberErrors := make([]error, numList) + versionMap := make(map[string]int) + + wg := &sync.WaitGroup{} + for i := req.copyMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + wg.Add(1) + go func(blobberIdx int) { + defer wg.Done() + // refEntity, err := req.copyBlobberObject(req.blobbers[blobberIdx], blobberIdx) + refEntity, err := req.getFileMetaFromBlobber(blobberIdx) + if err != nil { + blobberErrors[blobberIdx] = err + l.Logger.Debug(err.Error()) + return + } + refEntity.Path = path.Join(req.destPath, path.Base(refEntity.Path)) + objectTreeRefs[blobberIdx] = refEntity + req.maskMU.Lock() + versionMap[refEntity.AllocationRoot] += 1 + if versionMap[refEntity.AllocationRoot] >= req.consensusThresh { + consensusRef = refEntity + } + req.maskMU.Unlock() + }(int(pos)) + } + wg.Wait() + if consensusRef == nil { + return nil, zboxutil.MajorError(blobberErrors) + } + + if consensusRef.Type == fileref.DIRECTORY && !consensusRef.IsEmpty { + for ind, refEntity := range objectTreeRefs { + if refEntity.GetAllocationRoot() != consensusRef.AllocationRoot { + req.copyMask = req.copyMask.And(zboxutil.NewUint128(1).Lsh(uint64(ind)).Not()) + } + } + err := req.copySubDirectoriees(req.dirOnly) + if err != nil { + return nil, err + } + req.consensus = req.copyMask.CountOnes() + return objectTreeRefs, errNoChange + } + + for i := req.copyMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + wg.Add(1) + go func(blobberIdx int) { + defer wg.Done() + _, err := req.copyBlobberObject(req.blobbers[blobberIdx], blobberIdx, false) + if err != nil { + blobberErrors[blobberIdx] = err + l.Logger.Debug(err.Error()) + return + } + }(int(pos)) + } + wg.Wait() + err := zboxutil.MajorError(blobberErrors) + if err != nil && strings.Contains(err.Error(), objAlreadyExists) && consensusRef.Type == fileref.DIRECTORY { + return nil, errNoChange + } + req.destLookupHash = fileref.GetReferenceLookup(req.allocationID, consensusRef.Path) + + return objectTreeRefs, err } func (req *CopyRequest) ProcessCopy() error { @@ -194,10 +300,9 @@ func (req *CopyRequest) ProcessCopy() error { wg := &sync.WaitGroup{} var pos uint64 - objectTreeRefs, blobberErrors := req.ProcessWithBlobbers() + objectTreeRefs, err := req.ProcessWithBlobbers() if !req.isConsensusOk() { - err := zboxutil.MajorError(blobberErrors) if err != nil { return errors.New("copy_failed", fmt.Sprintf("Copy failed. %s", err.Error())) } @@ -296,10 +401,13 @@ func (req *CopyRequest) ProcessCopy() error { type CopyOperation struct { remotefilepath string destPath string + destLookupHash string + dirOnly bool ctx context.Context ctxCncl context.CancelFunc copyMask zboxutil.Uint128 maskMU *sync.Mutex + objectTreeRefs []fileref.RefEntity Consensus } @@ -319,18 +427,25 @@ func (co *CopyOperation) Process(allocObj *Allocation, connectionID string) ([]f ctxCncl: co.ctxCncl, copyMask: co.copyMask, maskMU: co.maskMU, + dirOnly: co.dirOnly, Consensus: Consensus{RWMutex: &sync.RWMutex{}}, } cR.consensusThresh = co.consensusThresh cR.fullconsensus = co.fullconsensus - - objectTreeRefs, blobberErrors := cR.ProcessWithBlobbers() + var err error + if allocObj.StorageVersion == StorageV2 { + co.objectTreeRefs, err = cR.ProcessWithBlobbersV2() + } else { + co.objectTreeRefs, err = cR.ProcessWithBlobbers() + } if !cR.isConsensusOk() { l.Logger.Error("copy failed: ", cR.remotefilepath, cR.destPath) - err := zboxutil.MajorError(blobberErrors) if err != nil { + if err == errNoChange { + return nil, cR.copyMask, err + } return nil, cR.copyMask, errors.New("copy_failed", fmt.Sprintf("Copy failed. %s", err.Error())) } @@ -338,7 +453,8 @@ func (co *CopyOperation) Process(allocObj *Allocation, connectionID string) ([]f fmt.Sprintf("Copy failed. Required consensus %d, got %d", cR.Consensus.consensusThresh, cR.Consensus.consensus)) } - return objectTreeRefs, cR.copyMask, nil + co.destLookupHash = cR.destLookupHash + return co.objectTreeRefs, cR.copyMask, err } @@ -392,7 +508,7 @@ func (co *CopyOperation) Error(allocObj *Allocation, consensus int, err error) { } -func NewCopyOperation(remotePath string, destPath string, copyMask zboxutil.Uint128, maskMU *sync.Mutex, consensusTh int, fullConsensus int, ctx context.Context) *CopyOperation { +func NewCopyOperation(ctx context.Context, remotePath string, destPath string, copyMask zboxutil.Uint128, maskMU *sync.Mutex, consensusTh, fullConsensus int, copyDirOnly bool) *CopyOperation { co := &CopyOperation{} co.remotefilepath = zboxutil.RemoteClean(remotePath) co.copyMask = copyMask @@ -404,6 +520,120 @@ func NewCopyOperation(remotePath string, destPath string, copyMask zboxutil.Uint } co.destPath = destPath co.ctx, co.ctxCncl = context.WithCancel(ctx) + co.dirOnly = copyDirOnly return co } + +func (co *CopyOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { + if co.objectTreeRefs == nil || co.objectTreeRefs[changeIndex] == nil || co.objectTreeRefs[changeIndex].GetType() == fileref.DIRECTORY { + return nil + } + decodedDestHash, _ := hex.DecodeString(co.destLookupHash) + ref := co.objectTreeRefs[changeIndex] + numBlocks := uint64(ref.GetNumBlocks()) + fileMetaRawHash := ref.GetFileMetaHashV2() + err := trie.Update(decodedDestHash, fileMetaRawHash, numBlocks) + if err != nil { + l.Logger.Error("Error updating trie", zap.Error(err)) + return err + } + return nil +} + +func (co *CopyOperation) GetLookupHash(changeIndex uint64) []string { + if co.objectTreeRefs == nil || co.objectTreeRefs[changeIndex] == nil || co.objectTreeRefs[changeIndex].GetType() == fileref.DIRECTORY { + return nil + } + return []string{co.destLookupHash} +} + +func (req *CopyRequest) copySubDirectoriees(dirOnly bool) error { + var ( + offsetPath string + pathLevel int + ) + + for { + if !dirOnly { + oResult, err := req.allocationObj.GetRefs(req.remotefilepath, offsetPath, "", "", fileref.FILE, fileref.REGULAR, 0, getRefPageLimit, WithObjectContext(req.ctx), WithObjectConsensusThresh(req.consensusThresh), WithSingleBlobber(true)) + if err != nil { + return err + } + if len(oResult.Refs) == 0 { + break + } + ops := make([]OperationRequest, 0, len(oResult.Refs)) + for _, ref := range oResult.Refs { + opMask := req.copyMask + if ref.Type == fileref.DIRECTORY { + continue + } + if ref.PathLevel > pathLevel { + pathLevel = ref.PathLevel + } + basePath := strings.TrimPrefix(path.Dir(ref.Path), path.Dir(req.remotefilepath)) + destPath := path.Join(req.destPath, basePath) + op := OperationRequest{ + OperationType: constants.FileOperationCopy, + RemotePath: ref.Path, + DestPath: destPath, + Mask: &opMask, + } + ops = append(ops, op) + } + err = req.allocationObj.DoMultiOperation(ops) + if err != nil { + return err + } + offsetPath = oResult.Refs[len(oResult.Refs)-1].Path + if len(oResult.Refs) < getRefPageLimit { + break + } + } + } + + offsetPath = "" + level := len(strings.Split(strings.TrimSuffix(req.remotefilepath, "/"), "/")) + if pathLevel == 0 { + pathLevel = level + 1 + } + + for pathLevel > level { + oResult, err := req.allocationObj.GetRefs(req.remotefilepath, offsetPath, "", "", fileref.DIRECTORY, fileref.REGULAR, pathLevel, getRefPageLimit, WithObjectContext(req.ctx), WithObjectMask(req.copyMask), WithObjectConsensusThresh(req.consensusThresh), WithSingleBlobber(true)) + if err != nil { + return err + } + if len(oResult.Refs) == 0 { + pathLevel-- + } else { + ops := make([]OperationRequest, 0, len(oResult.Refs)) + for _, ref := range oResult.Refs { + opMask := req.copyMask + if ref.Type == fileref.FILE { + continue + } + basePath := strings.TrimPrefix(path.Dir(ref.Path), path.Dir(req.remotefilepath)) + destPath := path.Join(req.destPath, basePath) + op := OperationRequest{ + OperationType: constants.FileOperationCopy, + RemotePath: ref.Path, + DestPath: destPath, + Mask: &opMask, + CopyDirOnly: true, + } + ops = append(ops, op) + } + err = req.allocationObj.DoMultiOperation(ops) + if err != nil { + return err + } + offsetPath = oResult.Refs[len(oResult.Refs)-1].Path + if len(oResult.Refs) < getRefPageLimit { + pathLevel-- + } + } + } + + return nil +} diff --git a/zboxcore/sdk/copyworker_test.go b/zboxcore/sdk/copyworker_test.go index 5037b179c..5a5bcdd40 100644 --- a/zboxcore/sdk/copyworker_test.go +++ b/zboxcore/sdk/copyworker_test.go @@ -215,7 +215,7 @@ func TestCopyRequest_copyBlobberObject(t *testing.T) { Baseurl: tt.name, }) req.copyMask = zboxutil.NewUint128(1).Lsh(uint64(len(req.blobbers))).Sub64(1) - _, err := req.copyBlobberObject(req.blobbers[0], 0) + _, err := req.copyBlobberObject(req.blobbers[0], 0, true) require.EqualValues(tt.wantErr, err != nil) if err != nil { require.Contains(errors.Top(err), tt.errMsg) @@ -333,15 +333,16 @@ func TestCopyRequest_ProcessCopy(t *testing.T) { }, nil) } - commitChan = make(map[string]chan *CommitRequest) + commitChan = make(map[string]chan CommitRequestInterface) for _, blobber := range req.blobbers { if _, ok := commitChan[blobber.ID]; !ok { - commitChan[blobber.ID] = make(chan *CommitRequest, 1) + commitChan[blobber.ID] = make(chan CommitRequestInterface, 1) } } blobberChan := commitChan go func() { - cm0 := <-blobberChan[req.blobbers[0].ID] + cm := <-blobberChan[req.blobbers[0].ID] + cm0 := cm.(*CommitRequest) require.EqualValues(t, cm0.blobber.ID, testName+mockBlobberId+strconv.Itoa(0)) cm0.result = &CommitResult{ Success: true, @@ -351,7 +352,8 @@ func TestCopyRequest_ProcessCopy(t *testing.T) { } }() go func() { - cm1 := <-blobberChan[req.blobbers[1].ID] + cm := <-blobberChan[req.blobbers[1].ID] + cm1 := cm.(*CommitRequest) require.EqualValues(t, cm1.blobber.ID, testName+mockBlobberId+strconv.Itoa(1)) cm1.result = &CommitResult{ Success: true, @@ -361,7 +363,8 @@ func TestCopyRequest_ProcessCopy(t *testing.T) { } }() go func() { - cm2 := <-blobberChan[req.blobbers[2].ID] + cm := <-blobberChan[req.blobbers[2].ID] + cm2 := cm.(*CommitRequest) require.EqualValues(t, cm2.blobber.ID, testName+mockBlobberId+strconv.Itoa(2)) cm2.result = &CommitResult{ Success: true, @@ -371,7 +374,8 @@ func TestCopyRequest_ProcessCopy(t *testing.T) { } }() go func() { - cm3 := <-blobberChan[req.blobbers[3].ID] + cm := <-blobberChan[req.blobbers[3].ID] + cm3 := cm.(*CommitRequest) require.EqualValues(t, cm3.blobber.ID, testName+mockBlobberId+strconv.Itoa(3)) cm3.result = &CommitResult{ Success: true, diff --git a/zboxcore/sdk/deleteworker.go b/zboxcore/sdk/deleteworker.go index 1e64f0b67..3b6579aaf 100644 --- a/zboxcore/sdk/deleteworker.go +++ b/zboxcore/sdk/deleteworker.go @@ -2,14 +2,18 @@ package sdk import ( "context" + "encoding/hex" "fmt" "io" + "io/ioutil" "net/http" "net/url" + "strings" "sync" "sync/atomic" "time" + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/errors" thrown "github.com/0chain/errors" "github.com/google/uuid" @@ -41,6 +45,8 @@ type DeleteRequest struct { timestamp int64 } +var errFileDeleted = errors.New("file_deleted", "file is already deleted") + func (req *DeleteRequest) deleteBlobberFile( blobber *blockchain.StorageNode, blobberIdx int) error { @@ -102,7 +108,7 @@ func (req *DeleteRequest) deleteBlobberFile( return } if resp.StatusCode == http.StatusBadRequest { - body, err := io.ReadAll(resp.Body) + body, err := ioutil.ReadAll(resp.Body) if err != nil { logger.Logger.Error("Failed to read response body", err) } @@ -133,7 +139,7 @@ func (req *DeleteRequest) deleteBlobberFile( return } - respBody, err = io.ReadAll(resp.Body) + respBody, err = ioutil.ReadAll(resp.Body) if err != nil { l.Logger.Error(blobber.Baseurl, "Response: ", string(respBody)) return @@ -346,6 +352,8 @@ type DeleteOperation struct { deleteMask zboxutil.Uint128 maskMu *sync.Mutex consensus Consensus + lookupHash string + refs []fileref.RefEntity } func (dop *DeleteOperation) Process(allocObj *Allocation, connectionID string) ([]fileref.RefEntity, zboxutil.Uint128, error) { @@ -367,6 +375,16 @@ func (dop *DeleteOperation) Process(allocObj *Allocation, connectionID string) ( } deleteReq.consensus.fullconsensus = dop.consensus.fullconsensus deleteReq.consensus.consensusThresh = dop.consensus.consensusThresh + dop.lookupHash = fileref.GetReferenceLookup(allocObj.ID, dop.remotefilepath) + if allocObj.StorageVersion == 1 { + var ( + objectTreeRefs []fileref.RefEntity + err error + ) + objectTreeRefs, deleteReq.deleteMask, err = deleteReq.processDeleteV2() + dop.refs = objectTreeRefs + return dop.refs, deleteReq.deleteMask, err + } numList := len(deleteReq.blobbers) objectTreeRefs := make([]fileref.RefEntity, numList) @@ -412,6 +430,109 @@ func (dop *DeleteOperation) Process(allocObj *Allocation, connectionID string) ( fmt.Sprintf("Delete failed. Required consensus %d, got %d", deleteReq.consensus.consensusThresh, deleteReq.consensus.consensus)) } + l.Logger.Debug("Delete Process Ended ") + dop.refs = objectTreeRefs + return objectTreeRefs, deleteReq.deleteMask, nil +} + +func (deleteReq *DeleteRequest) processDeleteV2() ([]fileref.RefEntity, zboxutil.Uint128, error) { + numList := len(deleteReq.blobbers) + objectTreeRefs := make([]fileref.RefEntity, numList) + blobberErrors := make([]error, numList) + versionMap := make(map[string]int) + var ( + pos uint64 + consensusRef *fileref.FileRef + ) + + for i := deleteReq.deleteMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + deleteReq.wg.Add(1) + go func(blobberIdx int) { + defer deleteReq.wg.Done() + refEntity, err := deleteReq.getFileMetaFromBlobber(uint64(blobberIdx)) + if errors.Is(err, constants.ErrNotFound) { + deleteReq.consensus.Done() + return + } else if err != nil { + blobberErrors[blobberIdx] = err + l.Logger.Error(err.Error()) + return + } + deleteReq.consensus.Done() + objectTreeRefs[blobberIdx] = refEntity + deleteReq.maskMu.Lock() + versionMap[refEntity.AllocationRoot] += 1 + if versionMap[refEntity.AllocationRoot] >= deleteReq.consensus.consensusThresh { + consensusRef = refEntity + } + deleteReq.maskMu.Unlock() + }(int(pos)) + } + deleteReq.wg.Wait() + if !deleteReq.consensus.isConsensusOk() { + err := zboxutil.MajorError(blobberErrors) + if err != nil { + return nil, deleteReq.deleteMask, thrown.New("delete_failed", fmt.Sprintf("Delete failed. %s", err.Error())) + } + + return nil, deleteReq.deleteMask, thrown.New("consensus_not_met", + fmt.Sprintf("Delete failed. Required consensus %d, got %d", + deleteReq.consensus.consensusThresh, deleteReq.consensus.consensus)) + } + if consensusRef == nil { + //Already deleted + return nil, deleteReq.deleteMask, errFileDeleted + } + if consensusRef.Type == fileref.DIRECTORY && !consensusRef.IsEmpty { + for ind, refEntity := range objectTreeRefs { + if refEntity == nil { + continue + } + if refEntity.GetAllocationRoot() != consensusRef.AllocationRoot { + deleteReq.deleteMask = deleteReq.deleteMask.And(zboxutil.NewUint128(1).Lsh(uint64(ind)).Not()) + } + } + err := deleteReq.deleteSubDirectories() + if err != nil { + return nil, deleteReq.deleteMask, err + } + } + if deleteReq.remotefilepath == "/" { + return objectTreeRefs, deleteReq.deleteMask, errNoChange + } + deleteReq.consensus.Reset() + for i := deleteReq.deleteMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + deleteReq.wg.Add(1) + go func(blobberIdx int) { + defer deleteReq.wg.Done() + err := deleteReq.deleteBlobberFile(deleteReq.blobbers[blobberIdx], blobberIdx) + if err != nil { + logger.Logger.Error("error during deleteBlobberFile", err) + blobberErrors[blobberIdx] = err + } + deleteReq.consensus.Done() + if singleClientMode { + lookuphash := fileref.GetReferenceLookup(deleteReq.allocationID, deleteReq.remotefilepath) + cacheKey := fileref.GetCacheKey(lookuphash, deleteReq.blobbers[blobberIdx].ID) + fileref.DeleteFileRef(cacheKey) + } + }(int(pos)) + } + deleteReq.wg.Wait() + + if !deleteReq.consensus.isConsensusOk() { + err := zboxutil.MajorError(blobberErrors) + if err != nil { + return nil, deleteReq.deleteMask, thrown.New("delete_failed", fmt.Sprintf("Delete failed. %s", err.Error())) + } + + return nil, deleteReq.deleteMask, thrown.New("consensus_not_met", + fmt.Sprintf("Delete failed. Required consensus %d, got %d", + deleteReq.consensus.consensusThresh, deleteReq.consensus.consensus)) + } + l.Logger.Debug("Delete Process Ended ") return objectTreeRefs, deleteReq.deleteMask, nil } @@ -459,7 +580,7 @@ func (dop *DeleteOperation) Error(allocObj *Allocation, consensus int, err error } -func NewDeleteOperation(remotePath string, deleteMask zboxutil.Uint128, maskMu *sync.Mutex, consensusTh int, fullConsensus int, ctx context.Context) *DeleteOperation { +func NewDeleteOperation(ctx context.Context, remotePath string, deleteMask zboxutil.Uint128, maskMu *sync.Mutex, consensusTh, fullConsensus int) *DeleteOperation { dop := &DeleteOperation{} dop.remotefilepath = zboxutil.RemoteClean(remotePath) dop.deleteMask = deleteMask @@ -469,3 +590,100 @@ func NewDeleteOperation(remotePath string, deleteMask zboxutil.Uint128, maskMu * dop.ctx, dop.ctxCncl = context.WithCancel(ctx) return dop } + +func (req *DeleteRequest) deleteSubDirectories() error { + // list all files + var ( + offsetPath string + pathLevel int + ) + for { + oResult, err := req.allocationObj.GetRefs(req.remotefilepath, offsetPath, "", "", fileref.FILE, fileref.REGULAR, 0, getRefPageLimit, WithObjectContext(req.ctx), WithObjectMask(req.deleteMask), WithObjectConsensusThresh(req.consensus.consensusThresh), WithSingleBlobber(true)) + if err != nil { + return err + } + if len(oResult.Refs) == 0 { + break + } + ops := make([]OperationRequest, 0, len(oResult.Refs)) + for _, ref := range oResult.Refs { + opMask := req.deleteMask + if ref.Type == fileref.DIRECTORY { + continue + } + if ref.PathLevel > pathLevel { + pathLevel = ref.PathLevel + } + op := OperationRequest{ + OperationType: constants.FileOperationDelete, + RemotePath: ref.Path, + Mask: &opMask, + } + ops = append(ops, op) + } + err = req.allocationObj.DoMultiOperation(ops) + if err != nil { + return err + } + offsetPath = oResult.Refs[len(oResult.Refs)-1].Path + if len(oResult.Refs) < getRefPageLimit { + break + } + } + // reset offsetPath + offsetPath = "" + level := len(strings.Split(strings.TrimSuffix(req.remotefilepath, "/"), "/")) + if pathLevel == 0 { + pathLevel = level + 1 + } + // list all directories by descending order of path level + for pathLevel > level { + oResult, err := req.allocationObj.GetRefs(req.remotefilepath, offsetPath, "", "", fileref.DIRECTORY, fileref.REGULAR, pathLevel, getRefPageLimit, WithObjectContext(req.ctx), WithObjectMask(req.deleteMask), WithObjectConsensusThresh(req.consensus.consensusThresh), WithSingleBlobber(true)) + if err != nil { + return err + } + if len(oResult.Refs) == 0 { + pathLevel-- + } else { + ops := make([]OperationRequest, 0, len(oResult.Refs)) + for _, ref := range oResult.Refs { + opMask := req.deleteMask + op := OperationRequest{ + OperationType: constants.FileOperationDelete, + RemotePath: ref.Path, + Mask: &opMask, + } + ops = append(ops, op) + } + err = req.allocationObj.DoMultiOperation(ops) + if err != nil { + return err + } + offsetPath = oResult.Refs[len(oResult.Refs)-1].Path + if len(oResult.Refs) < getRefPageLimit { + pathLevel-- + } + } + } + + return nil +} +func (dop *DeleteOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { + if dop.refs[changeIndex] == nil || dop.refs[changeIndex].GetType() == fileref.DIRECTORY { + return nil + } + decodedKey, _ := hex.DecodeString(dop.lookupHash) + err := trie.Update(decodedKey, nil, 0) + if err != nil && err != wmpt.ErrNotFound { + logger.Logger.Error("Error updating trie", err) + return err + } + return nil +} + +func (dop *DeleteOperation) GetLookupHash(changeIndex uint64) []string { + if dop.refs == nil || dop.refs[changeIndex] == nil || dop.refs[changeIndex].GetType() == fileref.DIRECTORY { + return nil + } + return []string{dop.lookupHash} +} diff --git a/zboxcore/sdk/deleteworker_test.go b/zboxcore/sdk/deleteworker_test.go index a0b517df1..15d58c7f0 100644 --- a/zboxcore/sdk/deleteworker_test.go +++ b/zboxcore/sdk/deleteworker_test.go @@ -260,15 +260,16 @@ func TestDeleteRequest_ProcessDelete(t *testing.T) { }, nil) } - commitChan = make(map[string]chan *CommitRequest) + commitChan = make(map[string]chan CommitRequestInterface) for _, blobber := range req.blobbers { if _, ok := commitChan[blobber.ID]; !ok { - commitChan[blobber.ID] = make(chan *CommitRequest, 1) + commitChan[blobber.ID] = make(chan CommitRequestInterface, 1) } } blobberChan := commitChan go func() { - cm0 := <-blobberChan[req.blobbers[0].ID] + cm := <-blobberChan[req.blobbers[0].ID] + cm0 := cm.(*CommitRequest) require.EqualValues(t, cm0.blobber.ID, testName+mockBlobberId+strconv.Itoa(0)) cm0.result = &CommitResult{ Success: true, @@ -278,7 +279,8 @@ func TestDeleteRequest_ProcessDelete(t *testing.T) { } }() go func() { - cm1 := <-blobberChan[req.blobbers[1].ID] + cm := <-blobberChan[req.blobbers[1].ID] + cm1 := cm.(*CommitRequest) require.EqualValues(t, cm1.blobber.ID, testName+mockBlobberId+strconv.Itoa(1)) cm1.result = &CommitResult{ Success: true, @@ -288,7 +290,8 @@ func TestDeleteRequest_ProcessDelete(t *testing.T) { } }() go func() { - cm2 := <-blobberChan[req.blobbers[2].ID] + cm := <-blobberChan[req.blobbers[2].ID] + cm2 := cm.(*CommitRequest) require.EqualValues(t, cm2.blobber.ID, testName+mockBlobberId+strconv.Itoa(2)) cm2.result = &CommitResult{ Success: true, @@ -298,7 +301,8 @@ func TestDeleteRequest_ProcessDelete(t *testing.T) { } }() go func() { - cm3 := <-blobberChan[req.blobbers[3].ID] + cm := <-blobberChan[req.blobbers[3].ID] + cm3 := cm.(*CommitRequest) require.EqualValues(t, cm3.blobber.ID, testName+mockBlobberId+strconv.Itoa(3)) cm3.result = &CommitResult{ Success: true, diff --git a/zboxcore/sdk/dirworker.go b/zboxcore/sdk/dirworker.go index ac58eee3f..d56f7657b 100644 --- a/zboxcore/sdk/dirworker.go +++ b/zboxcore/sdk/dirworker.go @@ -12,6 +12,7 @@ import ( "sync" "time" + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/errors" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/util" @@ -304,17 +305,21 @@ func (dirOp *DirOperation) Process(allocObj *Allocation, connectionID string) ([ } dR.Consensus = Consensus{ RWMutex: &sync.RWMutex{}, - consensusThresh: dR.consensusThresh, - fullconsensus: dR.fullconsensus, + consensusThresh: dirOp.consensusThresh, + fullconsensus: dirOp.fullconsensus, } - _ = dR.ProcessWithBlobbers(allocObj) + existCount := dR.ProcessWithBlobbers(allocObj) dirOp.alreadyExists = dR.alreadyExists if !dR.isConsensusOk() { return nil, dR.dirMask, errors.New("consensus_not_met", "directory creation failed due to consensus not met") } - return refs, dR.dirMask, nil + var err error + if allocObj.StorageVersion == StorageV2 && existCount >= dR.consensusThresh { + err = errNoChange + } + return refs, dR.dirMask, err } @@ -370,3 +375,11 @@ func NewDirOperation(remotePath, customMeta string, dirMask zboxutil.Uint128, ma dirOp.alreadyExists = make(map[uint64]bool) return dirOp } + +func (dirOp *DirOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { + return nil +} + +func (dirOp *DirOperation) GetLookupHash(changeIndex uint64) []string { + return nil +} diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index 6c5f533cd..c1819a449 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -21,6 +21,7 @@ import ( "github.com/0chain/errors" "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" + encrypt "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/zboxcore/blockchain" "github.com/0chain/gosdk/zboxcore/encryption" @@ -113,6 +114,7 @@ type DownloadRequest struct { downloadQueue downloadQueue // Always initialize this queue with max time taken isResume bool isEnterprise bool + storageVersion int } type downloadPriority struct { @@ -1208,11 +1210,16 @@ func (req *DownloadRequest) getFileMetaConsensus(fMetaResp []*fileMetaResponse) if selected.fileref.ActualFileHashSignature != fRef.ActualFileHashSignature { continue } - if !req.isEnterprise { + if !req.isEnterprise && req.shouldVerify { + hash := fRef.ActualFileHashSignature + fRef.ValidationRoot + if req.storageVersion == StorageV2 { + hashData := fmt.Sprintf("%s:%s:%s:%s", fRef.ActualFileHash, fRef.ValidationRoot, fRef.FixedMerkleRoot, req.blobbers[i].ID) + hash = encrypt.Hash(hashData) + } isValid, err := sys.VerifyWith( req.allocOwnerPubKey, fRef.ValidationRootSignature, - fRef.ActualFileHashSignature+fRef.ValidationRoot, + hash, ) if err != nil { l.Logger.Error(err, "allocOwnerPubKey: ", req.allocOwnerPubKey, " validationRootSignature: ", fRef.ValidationRootSignature, " actualFileHashSignature: ", fRef.ActualFileHashSignature, " validationRoot: ", fRef.ValidationRoot) diff --git a/zboxcore/sdk/filerefsworker.go b/zboxcore/sdk/filerefsworker.go index 33d98f497..40c72bc2b 100644 --- a/zboxcore/sdk/filerefsworker.go +++ b/zboxcore/sdk/filerefsworker.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "math" + "math/rand" "net/http" "sync" "time" @@ -14,6 +15,7 @@ import ( "github.com/0chain/errors" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/zboxcore/blockchain" + "github.com/0chain/gosdk/zboxcore/logger" l "github.com/0chain/gosdk/zboxcore/logger" "github.com/0chain/gosdk/zboxcore/marker" "github.com/0chain/gosdk/zboxcore/zboxutil" @@ -44,7 +46,9 @@ type ObjectTreeRequest struct { offsetPath string updatedDate string // must have "2006-01-02T15:04:05.99999Z07:00" format offsetDate string // must have "2006-01-02T15:04:05.99999Z07:00" format + reqMask zboxutil.Uint128 ctx context.Context + singleBlobber bool Consensus } @@ -55,23 +59,87 @@ type oTreeResponse struct { idx int } +var errTooManyRequests = errors.New("too_many_requests", "Too many requests") + +type ObjectTreeRequestOption func(*ObjectTreeRequest) + +func WithObjectContext(ctx context.Context) ObjectTreeRequestOption { + return func(o *ObjectTreeRequest) { + o.ctx = ctx + } +} + +func WithObjectMask(mask zboxutil.Uint128) ObjectTreeRequestOption { + return func(o *ObjectTreeRequest) { + o.reqMask = mask + } +} + +func WithObjectConsensusThresh(thresh int) ObjectTreeRequestOption { + return func(o *ObjectTreeRequest) { + o.consensusThresh = thresh + } +} + +func WithSingleBlobber(singleBlobber bool) ObjectTreeRequestOption { + return func(o *ObjectTreeRequest) { + o.singleBlobber = singleBlobber + } +} + // Paginated tree should not be collected as this will stall the client // It should rather be handled by application that uses gosdk func (o *ObjectTreeRequest) GetRefs() (*ObjectTreeResult, error) { - totalBlobbersCount := len(o.blobbers) - oTreeResponses := make([]oTreeResponse, totalBlobbersCount) - respChan := make(chan *oTreeResponse, totalBlobbersCount) - for i, blob := range o.blobbers { + activeCount := o.reqMask.CountOnes() + oTreeResponses := make([]oTreeResponse, activeCount) + respChan := make(chan *oTreeResponse, activeCount) + if o.singleBlobber { + var respErr error + for i := 0; i < activeCount; i++ { + var rnd = rand.New(rand.NewSource(time.Now().UnixNano())) + num := rnd.Intn(activeCount) + var blob *blockchain.StorageNode + var pos uint64 + for i := o.reqMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + num-- + if num < 0 { + blob = o.blobbers[pos] + break + } + } + l.Logger.Debug(fmt.Sprintf("Getting file refs for path %v from blobber %v", o.remotefilepath, blob.Baseurl)) + idx := num + baseURL := blob.Baseurl + go o.getFileRefs(baseURL, respChan, idx) + select { + case <-o.ctx.Done(): + return nil, o.ctx.Err() + case oTreeResponse := <-respChan: + if oTreeResponse.err != nil { + respErr = oTreeResponse.err + } else { + return oTreeResponse.oTResult, nil + } + } + } + return nil, respErr + } + var pos uint64 + for i := o.reqMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + blob := o.blobbers[pos] l.Logger.Debug(fmt.Sprintf("Getting file refs for path %v from blobber %v", o.remotefilepath, blob.Baseurl)) - idx := i + idx := int(pos) baseURL := blob.Baseurl go o.getFileRefs(baseURL, respChan, idx) } + hashCount := make(map[string]int) hashRefsMap := make(map[string]*ObjectTreeResult) - oTreeResponseErrors := make([]error, totalBlobbersCount) + oTreeResponseErrors := make([]error, activeCount) var successCount int - for i := 0; i < totalBlobbersCount; i++ { + for i := 0; i < activeCount; i++ { select { case <-o.ctx.Done(): return nil, o.ctx.Err() @@ -83,6 +151,7 @@ func (o *ObjectTreeRequest) GetRefs() (*ObjectTreeResult, error) { } continue } + oTreeResponses[oTreeResponse.idx] = *oTreeResponse successCount++ hash := oTreeResponse.hash if _, ok := hashCount[hash]; ok { @@ -96,6 +165,7 @@ func (o *ObjectTreeRequest) GetRefs() (*ObjectTreeResult, error) { } } } + var selected *ObjectTreeResult if successCount < o.consensusThresh { majorError := zboxutil.MajorError(oTreeResponseErrors) @@ -114,7 +184,7 @@ func (o *ObjectTreeRequest) GetRefs() (*ObjectTreeResult, error) { selected = &ObjectTreeResult{} minPage := int64(math.MaxInt64) for _, oTreeResponse := range oTreeResponses { - if oTreeResponse.err != nil { + if oTreeResponse.err != nil || oTreeResponse.oTResult == nil { continue } if oTreeResponse.oTResult.TotalPages < minPage { @@ -132,6 +202,14 @@ func (o *ObjectTreeRequest) GetRefs() (*ObjectTreeResult, error) { } } if len(selected.Refs) > 0 { + for _, oTreeResponse := range oTreeResponses { + if oTreeResponse.err != nil || oTreeResponse.oTResult == nil { + continue + } + if len(selected.Refs) != len(oTreeResponse.oTResult.Refs) { + l.Logger.Error("Consensus failed for refs: ", o.blobbers[oTreeResponse.idx].Baseurl) + } + } selected.OffsetPath = selected.Refs[len(selected.Refs)-1].Path return selected, nil } @@ -145,53 +223,73 @@ func (o *ObjectTreeRequest) getFileRefs(bUrl string, respChan chan *oTreeRespons defer func() { respChan <- oTR }() - oReq, err := zboxutil.NewRefsRequest( - bUrl, - o.allocationID, - o.sig, - o.allocationTx, - o.remotefilepath, - o.pathHash, - o.authToken, - o.offsetPath, - o.updatedDate, - o.offsetDate, - o.fileType, - o.refType, - o.level, - o.pageLimit, - ) - if err != nil { - oTR.err = err - return - } + oResult := ObjectTreeResult{} - ctx, cncl := context.WithTimeout(o.ctx, 2*time.Minute) - err = zboxutil.HttpDo(ctx, cncl, oReq, func(resp *http.Response, err error) error { + for i := 0; i < 3; i++ { + oReq, err := zboxutil.NewRefsRequest( + bUrl, + o.allocationID, + o.sig, + o.allocationTx, + o.remotefilepath, + o.pathHash, + o.authToken, + o.offsetPath, + o.updatedDate, + o.offsetDate, + o.fileType, + o.refType, + o.level, + o.pageLimit, + ) if err != nil { - l.Logger.Error(err) - return err + oTR.err = err + return } - defer resp.Body.Close() - respBody, err := io.ReadAll(resp.Body) - if err != nil { - l.Logger.Error(err) - return err - } - if resp.StatusCode == http.StatusOK { - err := json.Unmarshal(respBody, &oResult) + ctx, cncl := context.WithTimeout(o.ctx, 2*time.Minute) + defer cncl() + err = zboxutil.HttpDo(ctx, cncl, oReq, func(resp *http.Response, err error) error { if err != nil { l.Logger.Error(err) return err } - return nil - } else { - return errors.New("response_error", fmt.Sprintf("got status %d, err: %s", resp.StatusCode, respBody)) + defer resp.Body.Close() + respBody, err := io.ReadAll(resp.Body) + if err != nil { + l.Logger.Error(err) + return err + } + if resp.StatusCode == http.StatusOK { + err := json.Unmarshal(respBody, &oResult) + if err != nil { + l.Logger.Error(err) + return err + } + return nil + } else { + if resp.StatusCode == http.StatusTooManyRequests { + l.Logger.Error("Too many requests") + r, err := zboxutil.GetRateLimitValue(resp) + if err != nil { + logger.Logger.Error(err) + return err + } + + time.Sleep(time.Duration(r) * time.Second) + return errTooManyRequests + } + + return errors.New("response_error", fmt.Sprintf("got status %d, err: %s", resp.StatusCode, respBody)) + } + }) + if err != nil { + if err == errTooManyRequests && i < 2 { + continue + } + oTR.err = err + return } - }) - if err != nil { - oTR.err = err - return + break } oTR.oTResult = &oResult similarFieldRefs := make([]byte, 0, 32*len(oResult.Refs)) @@ -225,6 +323,7 @@ type SimilarField struct { PathLevel int `json:"level"` Size int64 `json:"size"` EncryptedKey string `json:"encrypted_key"` + EncryptedKeyPoint string `json:"encrypted_key_point"` ActualFileSize int64 `json:"actual_file_size"` ActualFileHash string `json:"actual_file_hash"` MimeType string `json:"mimetype"` diff --git a/zboxcore/sdk/filestatsworker.go b/zboxcore/sdk/filestatsworker.go index 4e86e6f40..c80c4dd96 100644 --- a/zboxcore/sdk/filestatsworker.go +++ b/zboxcore/sdk/filestatsworker.go @@ -101,6 +101,7 @@ func (req *ListRequest) getFileStatsInfoFromBlobber(blobber *blockchain.StorageN } fileStats.BlobberID = blobber.ID fileStats.BlobberURL = blobber.Baseurl + fileStats.PathHash = req.remotefilepathhash return nil } return errors.New(resp.Status, s.String()) diff --git a/zboxcore/sdk/filestatsworker_test.go b/zboxcore/sdk/filestatsworker_test.go index 9126d4626..66cb940af 100644 --- a/zboxcore/sdk/filestatsworker_test.go +++ b/zboxcore/sdk/filestatsworker_test.go @@ -103,6 +103,7 @@ func TestListRequest_getFileStatsInfoFromBlobber(t *testing.T) { Name: mockFileStatsName, BlobberID: mockBlobberId, BlobberURL: "Test_Success", + PathHash: fileref.GetReferenceLookup(mockAllocationId, mockRemoteFilePath), }, blobberIdx: mockBlobberIndex, respStatusCode: 200, diff --git a/zboxcore/sdk/listworker.go b/zboxcore/sdk/listworker.go index 5bf6e6554..02672f147 100644 --- a/zboxcore/sdk/listworker.go +++ b/zboxcore/sdk/listworker.go @@ -36,6 +36,7 @@ type ListRequest struct { listOnly bool offset int pageLimit int + storageVersion int Consensus } @@ -154,6 +155,7 @@ func (req *ListRequest) getListInfoFromBlobber(blobber *blockchain.StorageNode, if err != nil { return errors.Wrap(err, "error getting the dir tree from list response:") } + ref.AllocationRoot = listResult.AllocationRoot return nil } @@ -182,6 +184,9 @@ func (req *ListRequest) getlistFromBlobbers() ([]*listResponse, error) { continue } hash := listInfos[i].ref.FileMetaHash + if req.storageVersion == 1 { + hash = listInfos[i].ref.AllocationRoot + } consensusMap[hash] = append(consensusMap[hash], req.blobbers[listInfos[i].blobberIdx]) if len(consensusMap[hash]) >= req.consensusThresh { consensusHash = hash diff --git a/zboxcore/sdk/listworker_test.go b/zboxcore/sdk/listworker_test.go index 01df9c576..44ed5a62c 100644 --- a/zboxcore/sdk/listworker_test.go +++ b/zboxcore/sdk/listworker_test.go @@ -124,8 +124,9 @@ func TestListRequest_getListInfoFromBlobber(t *testing.T) { parameters: parameters{ listHttpResp: listResponse{ ref: &fileref.Ref{ - AllocationID: mockAllocationId, - Type: mockType, + AllocationID: mockAllocationId, + Type: mockType, + AllocationRoot: mockAllocationRoot, }, }, ListResult: fileref.ListResult{ diff --git a/zboxcore/sdk/moveworker.go b/zboxcore/sdk/moveworker.go index 8eba03258..75614c9db 100644 --- a/zboxcore/sdk/moveworker.go +++ b/zboxcore/sdk/moveworker.go @@ -3,17 +3,21 @@ package sdk import ( "bytes" "context" + "encoding/hex" "fmt" "io" "mime/multipart" "net/http" + "path" "strings" "sync" "time" + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/errors" thrown "github.com/0chain/errors" "github.com/google/uuid" + "go.uber.org/zap" "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/common" @@ -40,6 +44,7 @@ type MoveRequest struct { maskMU *sync.Mutex connectionID string timestamp int64 + destLookupHash string Consensus } @@ -47,8 +52,27 @@ func (req *MoveRequest) getObjectTreeFromBlobber(blobber *blockchain.StorageNode return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.sig, req.remotefilepath, blobber) } +func (req *MoveRequest) getFileMetaFromBlobber(pos int) (fileRef *fileref.FileRef, err error) { + listReq := &ListRequest{ + allocationID: req.allocationID, + allocationTx: req.allocationTx, + blobbers: req.blobbers, + remotefilepath: req.remotefilepath, + ctx: req.ctx, + } + respChan := make(chan *fileMetaResponse) + go listReq.getFileMetaInfoFromBlobber(req.blobbers[pos], int(pos), respChan) + refRes := <-respChan + if refRes.err != nil { + err = refRes.err + return + } + fileRef = refRes.fileref + return +} + func (req *MoveRequest) moveBlobberObject( - blobber *blockchain.StorageNode, blobberIdx int) (refEntity fileref.RefEntity, err error) { + blobber *blockchain.StorageNode, blobberIdx int, fetchObjectTree bool) (refEntity fileref.RefEntity, err error) { defer func() { if err != nil { @@ -58,9 +82,11 @@ func (req *MoveRequest) moveBlobberObject( req.maskMU.Unlock() } }() - refEntity, err = req.getObjectTreeFromBlobber(req.blobbers[blobberIdx]) - if err != nil { - return nil, err + if fetchObjectTree { + refEntity, err = req.getObjectTreeFromBlobber(req.blobbers[blobberIdx]) + if err != nil { + return nil, err + } } var resp *http.Response @@ -160,7 +186,7 @@ func (req *MoveRequest) moveBlobberObject( fmt.Sprintf("last status code: %d, last response message: %s", latestStatusCode, latestRespMsg)) } -func (req *MoveRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) { +func (req *MoveRequest) ProcessWithBlobbers() ([]fileref.RefEntity, error) { var pos uint64 numList := len(req.blobbers) objectTreeRefs := make([]fileref.RefEntity, numList) @@ -171,7 +197,7 @@ func (req *MoveRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) { wg.Add(1) go func(blobberIdx int) { defer wg.Done() - refEntity, err := req.moveBlobberObject(req.blobbers[blobberIdx], blobberIdx) + refEntity, err := req.moveBlobberObject(req.blobbers[blobberIdx], blobberIdx, true) if err != nil { blobberErrors[blobberIdx] = err l.Logger.Error(err.Error()) @@ -181,7 +207,98 @@ func (req *MoveRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) { }(int(pos)) } wg.Wait() - return objectTreeRefs, blobberErrors + return objectTreeRefs, zboxutil.MajorError(blobberErrors) +} + +func (req *MoveRequest) ProcessWithBlobbersV2() ([]fileref.RefEntity, error) { + + var ( + pos uint64 + consensusRef *fileref.FileRef + ) + numList := len(req.blobbers) + objectTreeRefs := make([]fileref.RefEntity, numList) + blobberErrors := make([]error, numList) + versionMap := make(map[string]int) + wg := &sync.WaitGroup{} + for i := req.moveMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + wg.Add(1) + go func(blobberIdx int) { + defer wg.Done() + refEntity, err := req.getFileMetaFromBlobber(blobberIdx) + if err != nil { + blobberErrors[blobberIdx] = err + l.Logger.Debug(err.Error()) + return + } + refEntity.Path = path.Join(req.destPath, path.Base(refEntity.Path)) + objectTreeRefs[blobberIdx] = refEntity + req.maskMU.Lock() + versionMap[refEntity.AllocationRoot] += 1 + if versionMap[refEntity.AllocationRoot] >= req.consensusThresh { + consensusRef = refEntity + } + req.maskMU.Unlock() + }(int(pos)) + } + wg.Wait() + if consensusRef == nil { + return nil, zboxutil.MajorError(blobberErrors) + } + + if consensusRef.Type == fileref.DIRECTORY && !consensusRef.IsEmpty { + for ind, refEntity := range objectTreeRefs { + if refEntity.GetAllocationRoot() != consensusRef.AllocationRoot { + req.moveMask = req.moveMask.And(zboxutil.NewUint128(1).Lsh(uint64(ind)).Not()) + } + } + subRequest := &subDirRequest{ + allocationObj: req.allocationObj, + remotefilepath: req.remotefilepath, + destPath: req.destPath, + ctx: req.ctx, + consensusThresh: req.consensusThresh, + opType: constants.FileOperationMove, + subOpType: constants.FileOperationMove, + mask: req.moveMask, + } + err := subRequest.processSubDirectories() + if err != nil { + return nil, err + } + op := OperationRequest{ + OperationType: constants.FileOperationDelete, + RemotePath: req.remotefilepath, + } + err = req.allocationObj.DoMultiOperation([]OperationRequest{op}) + if err != nil { + return nil, err + } + req.consensus = req.moveMask.CountOnes() + return nil, errNoChange + } + + for i := req.moveMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + wg.Add(1) + go func(blobberIdx int) { + defer wg.Done() + _, err := req.moveBlobberObject(req.blobbers[blobberIdx], blobberIdx, false) + if err != nil { + blobberErrors[blobberIdx] = err + l.Logger.Debug(err.Error()) + return + } + }(int(pos)) + } + wg.Wait() + err := zboxutil.MajorError(blobberErrors) + if err != nil && strings.Contains(err.Error(), objAlreadyExists) && consensusRef.Type == fileref.DIRECTORY { + return nil, errNoChange + } + req.destLookupHash = fileref.GetReferenceLookup(req.allocationID, consensusRef.Path) + return objectTreeRefs, err } func (req *MoveRequest) ProcessMove() error { @@ -190,10 +307,9 @@ func (req *MoveRequest) ProcessMove() error { wg := &sync.WaitGroup{} var pos uint64 - objectTreeRefs, blobberErrors := req.ProcessWithBlobbers() + objectTreeRefs, err := req.ProcessWithBlobbers() if !req.isConsensusOk() { - err := zboxutil.MajorError(blobberErrors) if err != nil { return errors.New("move_failed", fmt.Sprintf("Move failed. %s", err.Error())) } @@ -290,11 +406,14 @@ func (req *MoveRequest) ProcessMove() error { type MoveOperation struct { remotefilepath string destPath string + srcLookupHash string + destLookupHash string ctx context.Context ctxCncl context.CancelFunc moveMask zboxutil.Uint128 maskMU *sync.Mutex consensus Consensus + objectTreeRefs []fileref.RefEntity } func (mo *MoveOperation) Process(allocObj *Allocation, connectionID string) ([]fileref.RefEntity, zboxutil.Uint128, error) { @@ -315,12 +434,18 @@ func (mo *MoveOperation) Process(allocObj *Allocation, connectionID string) ([]f } mR.Consensus.fullconsensus = mo.consensus.fullconsensus mR.Consensus.consensusThresh = mo.consensus.consensusThresh - - objectTreeRefs, blobberErrors := mR.ProcessWithBlobbers() + var err error + if allocObj.StorageVersion == StorageV2 { + mo.objectTreeRefs, err = mR.ProcessWithBlobbersV2() + } else { + mo.objectTreeRefs, err = mR.ProcessWithBlobbers() + } if !mR.Consensus.isConsensusOk() { - err := zboxutil.MajorError(blobberErrors) if err != nil { + if err == errNoChange { + return nil, mR.moveMask, err + } return nil, mR.moveMask, thrown.New("move_failed", fmt.Sprintf("Move failed. %s", err.Error())) } @@ -328,7 +453,9 @@ func (mo *MoveOperation) Process(allocObj *Allocation, connectionID string) ([]f fmt.Sprintf("Move failed. Required consensus %d, got %d", mR.Consensus.consensusThresh, mR.Consensus.consensus)) } - return objectTreeRefs, mR.moveMask, nil + mo.destLookupHash = mR.destLookupHash + mo.srcLookupHash = fileref.GetReferenceLookup(mR.allocationID, mR.remotefilepath) + return mo.objectTreeRefs, mR.moveMask, err } func (mo *MoveOperation) buildChange(refs []fileref.RefEntity, uid uuid.UUID) []allocationchange.AllocationChange { @@ -397,3 +524,32 @@ func NewMoveOperation(remotePath string, destPath string, moveMask zboxutil.Uint mo.ctx, mo.ctxCncl = context.WithCancel(ctx) return mo } + +func (mo *MoveOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { + if mo.objectTreeRefs == nil || mo.objectTreeRefs[changeIndex] == nil || mo.objectTreeRefs[changeIndex].GetType() == fileref.DIRECTORY { + return nil + } + decodedSrcHash, _ := hex.DecodeString(mo.srcLookupHash) + err := trie.Update(decodedSrcHash, nil, 0) + if err != nil { + l.Logger.Error("Error updating trie", zap.Error(err)) + return err + } + decodedDestHash, _ := hex.DecodeString(mo.destLookupHash) + ref := mo.objectTreeRefs[changeIndex] + numBlocks := uint64(ref.GetNumBlocks()) + fileMetaRawHash := ref.GetFileMetaHashV2() + err = trie.Update(decodedDestHash, fileMetaRawHash, numBlocks) + if err != nil { + l.Logger.Error("Error updating trie", zap.Error(err)) + return err + } + return nil +} + +func (mo *MoveOperation) GetLookupHash(changeIndex uint64) []string { + if mo.objectTreeRefs == nil || mo.objectTreeRefs[changeIndex] == nil || mo.objectTreeRefs[changeIndex].GetType() == fileref.DIRECTORY { + return nil + } + return []string{mo.destLookupHash, mo.srcLookupHash} +} diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 25d3534af..f906463a2 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -10,6 +10,7 @@ import ( "sync" "time" + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/errors" "github.com/remeh/sizedwaitgroup" @@ -26,6 +27,7 @@ import ( const ( DefaultCreateConnectionTimeOut = 45 * time.Second + StorageV2 = 1 ) var BatchSize = 6 @@ -45,6 +47,8 @@ type Operationer interface { Verify(allocObj *Allocation) error Completed(allocObj *Allocation) Error(allocObj *Allocation, consensus int, err error) + ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error + GetLookupHash(changeIndex uint64) []string } type MultiOperation struct { @@ -56,8 +60,9 @@ type MultiOperation struct { operationMask zboxutil.Uint128 maskMU *sync.Mutex Consensus - changes [][]allocationchange.AllocationChange - isRepair bool + changes [][]allocationchange.AllocationChange + changesV2 []allocationchange.AllocationChangeV2 + isRepair bool } func (mo *MultiOperation) createConnectionObj(blobberIdx int) (err error) { @@ -160,13 +165,19 @@ func (mo *MultiOperation) createConnectionObj(blobberIdx int) (err error) { func (mo *MultiOperation) Process() error { l.Logger.Debug("MultiOperation Process start") wg := &sync.WaitGroup{} - mo.changes = make([][]allocationchange.AllocationChange, len(mo.operations)) + if mo.allocationObj.StorageVersion == 0 { + mo.changes = make([][]allocationchange.AllocationChange, len(mo.operations)) + } else { + mo.changesV2 = make([]allocationchange.AllocationChangeV2, 0, len(mo.operations)) + } ctx := mo.ctx ctxCncl := mo.ctxCncl defer ctxCncl(nil) swg := sizedwaitgroup.New(BatchSize) errsSlice := make([]error, len(mo.operations)) - mo.operationMask = zboxutil.NewUint128(0) + if mo.allocationObj.StorageVersion != StorageV2 { + mo.operationMask = zboxutil.NewUint128(0) + } for idx, op := range mo.operations { uid := util.GetNewUUID() swg.Add() @@ -182,16 +193,28 @@ func (mo *MultiOperation) Process() error { refs, mask, err := op.Process(mo.allocationObj, mo.connectionID) // Process with each blobber if err != nil { - l.Logger.Error(err) - errsSlice[idx] = errors.New("", err.Error()) - ctxCncl(err) + if err != errFileDeleted && err != errNoChange { + l.Logger.Error(err) + errsSlice[idx] = errors.New("", err.Error()) + ctxCncl(err) + } return } mo.maskMU.Lock() - mo.operationMask = mo.operationMask.Or(mask) - mo.maskMU.Unlock() - changes := op.buildChange(refs, uid) - mo.changes[idx] = changes + if mo.allocationObj.StorageVersion == StorageV2 { + if mo.isRepair { + mo.operationMask = mo.operationMask.Or(mask) + } else { + mo.operationMask = mo.operationMask.And(mask) + } + mo.changesV2 = append(mo.changesV2, op) + mo.maskMU.Unlock() + } else { + mo.operationMask = mo.operationMask.Or(mask) + mo.maskMU.Unlock() + changes := op.buildChange(refs, uid) + mo.changes[idx] = changes + } }(op, idx) } swg.Wait() @@ -212,12 +235,18 @@ func (mo *MultiOperation) Process() error { return nil } + if mo.allocationObj.StorageVersion == StorageV2 && len(mo.changesV2) == 0 { + return nil + } + // Take transpose of mo.change because it will be easier to iterate mo if it contains blobber changes // in row instead of column. Currently mo.change[0] contains allocationChange for operation 1 and so on. // But we want mo.changes[0] to have allocationChange for blobber 1 and mo.changes[1] to have allocationChange for // blobber 2 and so on. start := time.Now() - mo.changes = zboxutil.Transpose(mo.changes) + if mo.allocationObj.StorageVersion != StorageV2 { + mo.changes = zboxutil.Transpose(mo.changes) + } writeMarkerMutex, err := CreateWriteMarkerMutex(mo.allocationObj) if err != nil { @@ -274,11 +303,26 @@ func (mo *MultiOperation) Process() error { } logger.Logger.Debug("[checkAllocStatus]", time.Since(start).Milliseconds()) mo.Consensus.Reset() + var pos uint64 + if !mo.isRepair && mo.allocationObj.StorageVersion == StorageV2 { + for i := mo.operationMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + if mo.allocationObj.Blobbers[pos].AllocationRoot != mo.allocationObj.allocationRoot { + l.Logger.Info("Blobber allocation root mismatch", mo.allocationObj.Blobbers[pos].Baseurl, mo.allocationObj.Blobbers[pos].AllocationRoot, mo.allocationObj.allocationRoot) + mo.operationMask = mo.operationMask.And(zboxutil.NewUint128(1).Lsh(pos).Not()) + } + } + } activeBlobbers := mo.operationMask.CountOnes() + if activeBlobbers < mo.consensusThresh { + return errors.New("consensus_not_met", fmt.Sprintf("Active blobbers %d is less than consensus threshold %d", activeBlobbers, mo.consensusThresh)) + } + if mo.allocationObj.StorageVersion == StorageV2 { + return mo.commitV2() + } commitReqs := make([]*CommitRequest, activeBlobbers) start = time.Now() wg.Add(activeBlobbers) - var pos uint64 var counter = 0 timestamp := int64(common.Now()) for i := mo.operationMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { @@ -340,3 +384,79 @@ func (mo *MultiOperation) Process() error { return nil } + +func (mo *MultiOperation) commitV2() error { + + rootMap := make(map[string]zboxutil.Uint128) + var pos uint64 + for i := mo.operationMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + rootMap[mo.allocationObj.Blobbers[pos].AllocationRoot] = rootMap[mo.allocationObj.Blobbers[pos].AllocationRoot].Or(zboxutil.NewUint128(1).Lsh(pos)) + } + commitReqs := make([]*CommitRequestV2, len(rootMap)) + counter := 0 + timestamp := int64(common.Now()) + wg := &sync.WaitGroup{} + for _, mask := range rootMap { + wg.Add(1) + var changes []allocationchange.AllocationChangeV2 + if len(rootMap) > 1 { + changes = make([]allocationchange.AllocationChangeV2, 0, len(mo.operations)) + changes = append(changes, mo.changesV2...) + } else { + changes = mo.changesV2 + } + commitReq := &CommitRequestV2{ + allocationObj: mo.allocationObj, + connectionID: mo.connectionID, + sig: mo.allocationObj.sig, + wg: wg, + timestamp: timestamp, + commitMask: mask, + consensusThresh: mo.consensusThresh, + changes: changes, + isRepair: mo.isRepair, + } + commitReqs[counter] = commitReq + counter++ + go AddCommitRequest(commitReq) + } + wg.Wait() + rollbackMask := zboxutil.NewUint128(0) + errSlice := make([]error, len(commitReqs)) + for idx, commitReq := range commitReqs { + if commitReq.result != nil { + if commitReq.result.Success { + mo.consensus += commitReq.commitMask.CountOnes() + } else { + errSlice[idx] = errors.New("commit_failed", commitReq.result.ErrorMessage) + l.Logger.Error("Commit failed ", commitReq.result.ErrorMessage) + } + if !mo.isRepair { + rollbackMask = rollbackMask.Or(commitReq.commitMask) + } + } else { + l.Logger.Debug("Commit result not set") + } + } + if !mo.isConsensusOk() { + err := zboxutil.MajorError(errSlice) + if err == nil { + err = errors.New("consensus_not_met", fmt.Sprintf("Successfully committed to %d blobbers, but required %d", mo.consensus, len(mo.allocationObj.Blobbers))) + } + if mo.getConsensus() != 0 { + l.Logger.Info("Rolling back changes on minority blobbers") + mo.allocationObj.RollbackWithMask(rollbackMask) + mo.allocationObj.checkStatus = false + } + for _, op := range mo.operations { + op.Error(mo.allocationObj, mo.getConsensus(), err) + } + return err + } else { + for _, op := range mo.operations { + op.Completed(mo.allocationObj) + } + } + return nil +} diff --git a/zboxcore/sdk/renameworker.go b/zboxcore/sdk/renameworker.go index c23fc1ffc..5f32fd093 100644 --- a/zboxcore/sdk/renameworker.go +++ b/zboxcore/sdk/renameworker.go @@ -3,16 +3,20 @@ package sdk import ( "bytes" "context" + "encoding/hex" "fmt" "io" "mime/multipart" "net/http" + "path" "path/filepath" "sync" "time" + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/errors" "github.com/google/uuid" + "go.uber.org/zap" "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/common" @@ -47,8 +51,27 @@ func (req *RenameRequest) getObjectTreeFromBlobber(blobber *blockchain.StorageNo return getObjectTreeFromBlobber(req.ctx, req.allocationID, req.allocationTx, req.sig, req.remotefilepath, blobber) } +func (req *RenameRequest) getFileMetaFromBlobber(pos int) (fileRef *fileref.FileRef, err error) { + listReq := &ListRequest{ + allocationID: req.allocationID, + allocationTx: req.allocationTx, + blobbers: req.blobbers, + remotefilepath: req.remotefilepath, + ctx: req.ctx, + } + respChan := make(chan *fileMetaResponse) + go listReq.getFileMetaInfoFromBlobber(req.blobbers[pos], int(pos), respChan) + refRes := <-respChan + if refRes.err != nil { + err = refRes.err + return + } + fileRef = refRes.fileref + return +} + func (req *RenameRequest) renameBlobberObject( - blobber *blockchain.StorageNode, blobberIdx int) (refEntity fileref.RefEntity, err error) { + blobber *blockchain.StorageNode, blobberIdx int, fetchObjectTree bool) (refEntity fileref.RefEntity, err error) { defer func() { if err != nil { @@ -57,10 +80,11 @@ func (req *RenameRequest) renameBlobberObject( req.maskMU.Unlock() } }() - - refEntity, err = req.getObjectTreeFromBlobber(req.blobbers[blobberIdx]) - if err != nil { - return nil, err + if fetchObjectTree { + refEntity, err = req.getObjectTreeFromBlobber(req.blobbers[blobberIdx]) + if err != nil { + return nil, err + } } var ( @@ -159,7 +183,7 @@ func (req *RenameRequest) renameBlobberObject( return } -func (req *RenameRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) { +func (req *RenameRequest) ProcessWithBlobbers() ([]fileref.RefEntity, error) { var pos uint64 numList := len(req.blobbers) objectTreeRefs := make([]fileref.RefEntity, numList) @@ -170,7 +194,7 @@ func (req *RenameRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) { req.wg.Add(1) go func(blobberIdx int) { defer req.wg.Done() - refEntity, err := req.renameBlobberObject(req.blobbers[blobberIdx], blobberIdx) + refEntity, err := req.renameBlobberObject(req.blobbers[blobberIdx], blobberIdx, true) if err != nil { blobberErrors[blobberIdx] = err l.Logger.Error(err.Error()) @@ -180,16 +204,101 @@ func (req *RenameRequest) ProcessWithBlobbers() ([]fileref.RefEntity, []error) { }(int(pos)) } req.wg.Wait() - return objectTreeRefs, blobberErrors + return objectTreeRefs, zboxutil.MajorError(blobberErrors) +} + +func (req *RenameRequest) ProcessWithBlobbersV2() ([]fileref.RefEntity, error) { + var ( + pos uint64 + consensusRef *fileref.FileRef + ) + numList := len(req.blobbers) + objectTreeRefs := make([]fileref.RefEntity, numList) + blobberErrors := make([]error, numList) + versionMap := make(map[string]int) + req.wg = &sync.WaitGroup{} + for i := req.renameMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + req.wg.Add(1) + go func(blobberIdx int) { + defer req.wg.Done() + refEntity, err := req.getFileMetaFromBlobber(blobberIdx) + if err != nil { + blobberErrors[blobberIdx] = err + l.Logger.Error(err.Error()) + return + } + refEntity.Path = path.Join(path.Dir(req.remotefilepath), req.newName) + objectTreeRefs[blobberIdx] = refEntity + req.maskMU.Lock() + versionMap[refEntity.AllocationRoot] += 1 + if versionMap[refEntity.AllocationRoot] >= req.consensus.consensusThresh { + consensusRef = refEntity + } + req.maskMU.Unlock() + }(int(pos)) + } + req.wg.Wait() + if consensusRef == nil { + return nil, zboxutil.MajorError(blobberErrors) + } + if consensusRef.Type == fileref.DIRECTORY && !consensusRef.IsEmpty { + for ind, refEntity := range objectTreeRefs { + if refEntity.GetAllocationRoot() != consensusRef.AllocationRoot { + req.renameMask = req.renameMask.And(zboxutil.NewUint128(1).Lsh(uint64(ind)).Not()) + } + } + subRequest := &subDirRequest{ + allocationObj: req.allocationObj, + remotefilepath: req.remotefilepath, + destPath: path.Join(path.Dir(req.remotefilepath), req.newName), + ctx: req.ctx, + consensusThresh: req.consensus.consensusThresh, + opType: constants.FileOperationMove, + subOpType: constants.FileOperationRename, + mask: req.renameMask, + } + err := subRequest.processSubDirectories() + if err != nil { + return nil, err + } + op := OperationRequest{ + OperationType: constants.FileOperationDelete, + RemotePath: req.remotefilepath, + Mask: &req.renameMask, + } + err = req.allocationObj.DoMultiOperation([]OperationRequest{op}) + if err != nil { + return nil, err + } + req.consensus.consensus = req.renameMask.CountOnes() + return nil, errNoChange + } + + for i := req.renameMask; !i.Equals64(0); i = i.And(zboxutil.NewUint128(1).Lsh(pos).Not()) { + pos = uint64(i.TrailingZeros()) + req.wg.Add(1) + go func(blobberIdx int) { + defer req.wg.Done() + _, err := req.renameBlobberObject(req.blobbers[blobberIdx], blobberIdx, false) + if err != nil { + blobberErrors[blobberIdx] = err + l.Logger.Debug(err.Error()) + return + } + }(int(pos)) + } + req.wg.Wait() + + return objectTreeRefs, zboxutil.MajorError(blobberErrors) } func (req *RenameRequest) ProcessRename() error { defer req.ctxCncl() - objectTreeRefs, blobberErrors := req.ProcessWithBlobbers() + objectTreeRefs, err := req.ProcessWithBlobbers() if !req.consensus.isConsensusOk() { - err := zboxutil.MajorError(blobberErrors) if err != nil { return errors.New("rename_failed", fmt.Sprintf("Rename failed. %s", err.Error())) @@ -293,11 +402,14 @@ func (req *RenameRequest) ProcessRename() error { type RenameOperation struct { remotefilepath string + srcLookupHash string + destLookupHash string ctx context.Context ctxCncl context.CancelFunc renameMask zboxutil.Uint128 newName string maskMU *sync.Mutex + objectTreeRefs []fileref.RefEntity consensus Consensus } @@ -325,12 +437,18 @@ func (ro *RenameOperation) Process(allocObj *Allocation, connectionID string) ([ } rR.consensus.fullconsensus = ro.consensus.fullconsensus rR.consensus.consensusThresh = ro.consensus.consensusThresh - - objectTreeRefs, blobberErrors := rR.ProcessWithBlobbers() + var err error + if allocObj.StorageVersion == StorageV2 { + ro.objectTreeRefs, err = rR.ProcessWithBlobbersV2() + } else { + ro.objectTreeRefs, err = rR.ProcessWithBlobbers() + } if !rR.consensus.isConsensusOk() { - err := zboxutil.MajorError(blobberErrors) if err != nil { + if err == errNoChange { + return nil, rR.renameMask, err + } return nil, rR.renameMask, errors.New("rename_failed", fmt.Sprintf("Renamed failed. %s", err.Error())) } @@ -338,8 +456,10 @@ func (ro *RenameOperation) Process(allocObj *Allocation, connectionID string) ([ fmt.Sprintf("Rename failed. Required consensus %d, got %d", rR.consensus.consensusThresh, rR.consensus.consensus)) } - l.Logger.Info("Rename Processs Ended ") - return objectTreeRefs, rR.renameMask, nil + ro.srcLookupHash = fileref.GetReferenceLookup(rR.allocationID, rR.remotefilepath) + destPath := path.Join(path.Dir(rR.remotefilepath), rR.newName) + ro.destLookupHash = fileref.GetReferenceLookup(rR.allocationID, destPath) + return ro.objectTreeRefs, rR.renameMask, err } func (ro *RenameOperation) buildChange(refs []fileref.RefEntity, uid uuid.UUID) []allocationchange.AllocationChange { @@ -401,7 +521,7 @@ func (ro *RenameOperation) Error(allocObj *Allocation, consensus int, err error) func NewRenameOperation(remotePath string, destName string, renameMask zboxutil.Uint128, maskMU *sync.Mutex, consensusTh int, fullConsensus int, ctx context.Context) *RenameOperation { ro := &RenameOperation{} ro.remotefilepath = zboxutil.RemoteClean(remotePath) - ro.newName = destName + ro.newName = path.Base(destName) ro.renameMask = renameMask ro.maskMU = maskMU ro.consensus.consensusThresh = consensusTh @@ -410,3 +530,32 @@ func NewRenameOperation(remotePath string, destName string, renameMask zboxutil. return ro } + +func (ro *RenameOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { + if ro.objectTreeRefs == nil || ro.objectTreeRefs[changeIndex] == nil || ro.objectTreeRefs[changeIndex].GetType() == fileref.DIRECTORY { + return nil + } + decodedSrcHash, _ := hex.DecodeString(ro.srcLookupHash) + err := trie.Update(decodedSrcHash, nil, 0) + if err != nil { + l.Logger.Error("Error updating trie", zap.Error(err)) + return err + } + decodedDestHash, _ := hex.DecodeString(ro.destLookupHash) + ref := ro.objectTreeRefs[changeIndex] + numBlocks := uint64(ref.GetNumBlocks()) + fileMetaRawHash := ref.GetFileMetaHashV2() + err = trie.Update(decodedDestHash, fileMetaRawHash, numBlocks) + if err != nil { + l.Logger.Error("Error updating trie", zap.Error(err)) + return err + } + return nil +} + +func (ro *RenameOperation) GetLookupHash(changeIndex uint64) []string { + if ro.objectTreeRefs == nil || ro.objectTreeRefs[changeIndex] == nil || ro.objectTreeRefs[changeIndex].GetType() == fileref.DIRECTORY { + return nil + } + return []string{ro.destLookupHash, ro.srcLookupHash} +} diff --git a/zboxcore/sdk/renameworker_test.go b/zboxcore/sdk/renameworker_test.go index e40a9aacf..a4221a1f5 100644 --- a/zboxcore/sdk/renameworker_test.go +++ b/zboxcore/sdk/renameworker_test.go @@ -243,7 +243,7 @@ func TestRenameRequest_renameBlobberObject(t *testing.T) { req.blobbers = append(req.blobbers, &blockchain.StorageNode{ Baseurl: tt.name, }) - _, err := req.renameBlobberObject(req.blobbers[0], 0) + _, err := req.renameBlobberObject(req.blobbers[0], 0, true) require.EqualValues(tt.wantErr, err != nil, "Error: ", err) if err != nil { require.Contains(errors.Top(err), tt.errMsg) @@ -355,15 +355,16 @@ func TestRenameRequest_ProcessRename(t *testing.T) { }, nil) } - commitChan = make(map[string]chan *CommitRequest) + commitChan = make(map[string]chan CommitRequestInterface) for _, blobber := range req.blobbers { if _, ok := commitChan[blobber.ID]; !ok { - commitChan[blobber.ID] = make(chan *CommitRequest, 1) + commitChan[blobber.ID] = make(chan CommitRequestInterface, 1) } } blobberChan := commitChan go func() { - cm0 := <-blobberChan[req.blobbers[0].ID] + cm := <-blobberChan[req.blobbers[0].ID] + cm0 := cm.(*CommitRequest) require.EqualValues(t, cm0.blobber.ID, testName+mockBlobberId+strconv.Itoa(0)) cm0.result = &CommitResult{ Success: true, @@ -373,7 +374,8 @@ func TestRenameRequest_ProcessRename(t *testing.T) { } }() go func() { - cm1 := <-blobberChan[req.blobbers[1].ID] + cm := <-blobberChan[req.blobbers[1].ID] + cm1 := cm.(*CommitRequest) require.EqualValues(t, cm1.blobber.ID, testName+mockBlobberId+strconv.Itoa(1)) cm1.result = &CommitResult{ Success: true, @@ -383,7 +385,8 @@ func TestRenameRequest_ProcessRename(t *testing.T) { } }() go func() { - cm2 := <-blobberChan[req.blobbers[2].ID] + cm := <-blobberChan[req.blobbers[2].ID] + cm2 := cm.(*CommitRequest) require.EqualValues(t, cm2.blobber.ID, testName+mockBlobberId+strconv.Itoa(2)) cm2.result = &CommitResult{ Success: true, @@ -393,7 +396,8 @@ func TestRenameRequest_ProcessRename(t *testing.T) { } }() go func() { - cm3 := <-blobberChan[req.blobbers[3].ID] + cm := <-blobberChan[req.blobbers[3].ID] + cm3 := cm.(*CommitRequest) require.EqualValues(t, cm3.blobber.ID, testName+mockBlobberId+strconv.Itoa(3)) cm3.result = &CommitResult{ Success: true, diff --git a/zboxcore/sdk/repairworker.go b/zboxcore/sdk/repairworker.go index bfb42fd8c..b9243c8d9 100644 --- a/zboxcore/sdk/repairworker.go +++ b/zboxcore/sdk/repairworker.go @@ -2,6 +2,7 @@ package sdk import ( "context" + "errors" "fmt" "io" "sync" @@ -23,6 +24,7 @@ type RepairRequest struct { filesRepaired int wg *sync.WaitGroup allocation *Allocation + repairPath string } type RepairStatusCB struct { @@ -67,6 +69,8 @@ func (r *RepairRequest) processRepair(ctx context.Context, a *Allocation) { if r.checkForCancel(a) { return } + ctx, cancel := context.WithCancel(ctx) + defer cancel() SetNumBlockDownloads(RepairBlocks) currBatchSize := BatchSize BatchSize = BatchSize / 2 @@ -77,7 +81,12 @@ func (r *RepairRequest) processRepair(ctx context.Context, a *Allocation) { SetSingleClietnMode(true) defer SetSingleClietnMode(false) } - r.iterateDir(a, r.listDir) + r.allocation = a + if a.StorageVersion == StorageV2 { + r.iterateDirV2(ctx) + } else { + r.iterateDir(a, r.listDir) + } if r.statusCB != nil { r.statusCB.RepairCompleted(r.filesRepaired) } @@ -234,11 +243,8 @@ func (r *RepairRequest) repairFile(a *Allocation, file *ListResult) []OperationR if r.checkForCancel(a) { return nil } - memFile := &sys.MemChanFile{ - Buffer: make(chan []byte, 100), - ChunkWriteSize: int(a.GetChunkReadSize(ref.EncryptedKey != "")), - } - op = a.RepairFile(memFile, file.Path, statusCB, found, ref) + pipeFile := sys.NewPipeFile() + op = a.RepairFile(pipeFile, file.Path, statusCB, found, ref) if op.FileMeta.ActualSize > 0 { op.DownloadFile = true } @@ -316,12 +322,168 @@ func checkFileExists(localPath string) bool { } func (r *RepairRequest) checkForCancel(a *Allocation) bool { - if r.isRepairCanceled { - l.Logger.Info("Repair Cancelled by the user") + return r.isRepairCanceled +} + +type diffRef struct { + tgtRef ORef + tgtChan <-chan ORef + tgtEOF bool + mask zboxutil.Uint128 +} + +func (r *RepairRequest) iterateDirV2(ctx context.Context) { + versionMap := make(map[string]*diffRef) + r.allocation.CheckAllocStatus() //nolint:errcheck + latestRoot := r.allocation.allocationRoot + for idx, blobber := range r.allocation.Blobbers { + if versionMap[blobber.AllocationRoot] == nil { + versionMap[blobber.AllocationRoot] = &diffRef{} + } + versionMap[blobber.AllocationRoot].mask = versionMap[blobber.AllocationRoot].mask.Or(zboxutil.NewUint128(1).Lsh(uint64(idx))) + } + if versionMap[latestRoot].mask.CountOnes() < r.allocation.DataShards { + l.Logger.Error("No consensus on latest allocation root: ", latestRoot) if r.statusCB != nil { - r.statusCB.RepairCompleted(r.filesRepaired) + r.statusCB.Error(r.allocation.ID, r.repairPath, OpRepair, errors.New("no consensus on latest allocation root")) + } + return + } + if len(versionMap) == 1 { + return + } + // get the src list channel + srcChan := r.allocation.ListObjects(ctx, r.repairPath, "", "", "", fileref.FILE, fileref.REGULAR, 0, getRefPageLimit, WithSingleBlobber(true), WithObjectMask(versionMap[latestRoot].mask), WithObjectContext(ctx)) + + for root, diff := range versionMap { + if root == latestRoot { + continue + } + diff.tgtChan = r.allocation.ListObjects(ctx, r.repairPath, "", "", "", fileref.FILE, fileref.REGULAR, 0, getRefPageLimit, WithSingleBlobber(true), WithObjectMask(diff.mask), WithObjectContext(ctx)) + diff.tgtRef, diff.tgtEOF = <-diff.tgtChan + } + var ( + toNextRef = true + srcRef ORef + srcEOF = true + ops []OperationRequest + ) + for { + if r.checkForCancel(r.allocation) { + return + } + if toNextRef { + if !srcEOF { + break + } + srcRef, srcEOF = <-srcChan + if srcRef.Err != nil { + l.Logger.Error("Failed to get source file reference ", srcRef.Err.Error()) + if r.statusCB != nil { + r.statusCB.Error(r.allocation.ID, r.repairPath, OpRepair, srcRef.Err) + } + return + } + } + l.Logger.Debug("Checking file for the path :", srcRef.Path) + toNextRef = true + var ( + uploadMask zboxutil.Uint128 + deleteMask zboxutil.Uint128 + ) + for root, diff := range versionMap { + if root == latestRoot { + continue + } + + // check if both target and source are at EOF + if !srcEOF && !diff.tgtEOF { + continue + } + // if target is at EOF, upload the src file + if !diff.tgtEOF { + uploadMask = uploadMask.Or(diff.mask) + continue + } + // if source is at EOF, delete the target file + if !srcEOF { + delMask := diff.mask + op := OperationRequest{ + OperationType: constants.FileOperationDelete, + RemotePath: diff.tgtRef.Path, + Mask: &delMask, + } + ops = append(ops, op) + diff.tgtRef, diff.tgtEOF = <-diff.tgtChan + toNextRef = false + continue + } + if diff.tgtRef.Err != nil { + l.Logger.Error("Failed to get target file reference ", diff.tgtRef.Err.Error()) + if r.statusCB != nil { + r.statusCB.Error(r.allocation.ID, r.repairPath, OpRepair, diff.tgtRef.Err) + } + continue + } + // if both source and target are at same path + if diff.tgtRef.Path == srcRef.Path { + // if both source and target are at same path and hash is different + if diff.tgtRef.ActualFileHash != srcRef.ActualFileHash { + deleteMask = deleteMask.Or(diff.mask) + uploadMask = uploadMask.Or(diff.mask) + } + diff.tgtRef, diff.tgtEOF = <-diff.tgtChan + } else if diff.tgtRef.Path < srcRef.Path { + deleteMask = deleteMask.Or(diff.mask) + toNextRef = false + diff.tgtRef, diff.tgtEOF = <-diff.tgtChan + } + } + if deleteMask.CountOnes() > 0 { + op := OperationRequest{ + OperationType: constants.FileOperationDelete, + RemotePath: srcRef.Path, + Mask: &deleteMask, + } + ops = append(ops, op) + } + if uploadMask.CountOnes() > 0 { + op := r.uploadFileOp(srcRef, uploadMask) + ops = append(ops, op) } - return true + if len(ops) >= RepairBatchSize { + r.repairOperation(r.allocation, ops) + ops = nil + } + } + if len(ops) > 0 { + r.repairOperation(r.allocation, ops) + } + +} + +func (r *RepairRequest) uploadFileOp(file ORef, opMask zboxutil.Uint128) OperationRequest { + var wg sync.WaitGroup + wg.Add(1) + statusCB := &RepairStatusCB{ + wg: &wg, + statusCB: r.statusCB, + } + + ref := &fileref.FileRef{ + ActualFileSize: file.ActualFileSize, + MimeType: file.MimeType, + CustomMeta: file.CustomMeta, + Ref: fileref.Ref{ + Name: file.Name, + }, + EncryptedKey: file.EncryptedKey, + EncryptedKeyPoint: file.EncryptedKeyPoint, + } + pipeFile := sys.NewPipeFile() + op := r.allocation.RepairFile(pipeFile, file.Path, statusCB, opMask, ref) + if op.FileMeta.ActualSize > 0 { + op.DownloadFile = true } - return false + return *op } diff --git a/zboxcore/sdk/rollback.go b/zboxcore/sdk/rollback.go index 00d009371..42fab72be 100644 --- a/zboxcore/sdk/rollback.go +++ b/zboxcore/sdk/rollback.go @@ -256,6 +256,8 @@ func (rb *RollbackBlobber) processRollback(ctx context.Context, tx string) error if shouldContinue { continue } + rb.blobber.LatestWM = wm + rb.blobber.AllocationRoot = wm.AllocationRoot return nil } @@ -289,15 +291,17 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { l.Logger.Error("error during getWritemarker", zap.Error(err)) blobStatus.Status = "unavailable" } - if wr == nil { - markerChan <- nil - } else { + if wr != nil { markerChan <- &RollbackBlobber{ blobber: blobber, lpm: wr, commitResult: &CommitResult{}, blobIndex: ind, } + if wr.LatestWM != nil { + blobber.AllocationRoot = wr.LatestWM.AllocationRoot + blobber.LatestWM = wr.LatestWM + } } blobberRes[ind] = blobStatus }(blobber, ind) @@ -312,9 +316,11 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { versionMap := make(map[string][]*RollbackBlobber) var ( - prevVersion string - latestVersion string - highestTS int64 + prevVersion string + latestVersion string + consensusVersion string + highestTS int64 + req = a.DataShards ) for rb := range markerChan { @@ -340,19 +346,28 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { } versionMap[version] = append(versionMap[version], rb) + if len(versionMap[version]) >= req { + consensusVersion = version + } } - req := a.DataShards - if len(versionMap) == 0 { return Commit, blobberRes, nil } if len(versionMap[latestVersion]) > req || len(versionMap[prevVersion]) > req { + if len(versionMap[latestVersion]) > req { + a.allocationRoot = versionMap[latestVersion][0].lpm.LatestWM.AllocationRoot + } else { + a.allocationRoot = versionMap[prevVersion][0].lpm.LatestWM.AllocationRoot + } return Commit, blobberRes, nil } if len(versionMap[latestVersion]) >= req || len(versionMap[prevVersion]) >= req || len(versionMap) > 2 { + if consensusVersion != "" { + a.allocationRoot = versionMap[consensusVersion][0].lpm.LatestWM.AllocationRoot + } for _, rb := range versionMap[prevVersion] { blobberRes[rb.blobIndex].Status = "repair" } @@ -387,6 +402,12 @@ func (a *Allocation) CheckAllocStatus() (AllocStatus, []BlobberStatus, error) { return Broken, blobberRes, common.NewError("rollback_failed", "Rollback failed") } + if versionMap[latestVersion][0].lpm.PrevWM != nil { + a.allocationRoot = versionMap[latestVersion][0].lpm.PrevWM.AllocationRoot + } else { + a.allocationRoot = "" + } + if errCnt == int32(fullConsensus) { return Repair, blobberRes, nil } diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index b08927e52..a2af0b663 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -366,6 +366,8 @@ type UpdateBlobber struct { IsShutdown *bool `json:"is_shutdown,omitempty"` NotAvailable *bool `json:"not_available,omitempty"` IsRestricted *bool `json:"is_restricted,omitempty"` + StorageVersion *int `json:"storage_version,omitempty"` + DelegateWallet *string `json:"delegate_wallet,omitempty"` } // ResetBlobberStatsDto represents blobber stats reset request. @@ -772,6 +774,7 @@ type CreateAllocationOptions struct { IsEnterprise bool FileOptionsParams *FileOptionsParameters Force bool + StorageVersion int } // CreateAllocationWith creates a new allocation with the given options for the current client using the SDK. @@ -799,7 +802,7 @@ func CreateAllocationWith(options CreateAllocationOptions) ( // // returns the list of blobber ids and an error if any. func GetAllocationBlobbers( - datashards, parityshards int, + storageVersion, datashards, parityshards int, size int64, isRestricted int, readPrice, writePrice PriceRange, @@ -812,6 +815,7 @@ func GetAllocationBlobbers( "read_price_range": readPrice, "write_price_range": writePrice, "is_restricted": isRestricted, + "storage_version": storageVersion, } allocationData, _ := json.Marshal(allocationRequest) @@ -837,7 +841,7 @@ func GetAllocationBlobbers( } func getNewAllocationBlobbers( - datashards, parityshards int, + storageVersion, datashards, parityshards int, size int64, readPrice, writePrice PriceRange, preferredBlobberIds, blobberAuthTickets []string, force bool, @@ -852,12 +856,13 @@ func getNewAllocationBlobbers( "blobber_auth_tickets": blobberAuthTickets, "read_price_range": readPrice, "write_price_range": writePrice, + "storage_version": storageVersion, }, nil } } allocBlobberIDs, err := GetAllocationBlobbers( - datashards, parityshards, size, 2, readPrice, writePrice, force, + storageVersion, datashards, parityshards, size, 2, readPrice, writePrice, force, ) if err != nil { return nil, err @@ -886,6 +891,7 @@ func getNewAllocationBlobbers( "blobber_auth_tickets": uniqueBlobberAuthTickets, "read_price_range": readPrice, "write_price_range": writePrice, + "storage_version": storageVersion, }, nil } diff --git a/zboxcore/sdk/upload_worker.go b/zboxcore/sdk/upload_worker.go index 76021f904..34cf88de4 100644 --- a/zboxcore/sdk/upload_worker.go +++ b/zboxcore/sdk/upload_worker.go @@ -3,10 +3,12 @@ package sdk import ( "bytes" "context" + "encoding/hex" "errors" "io" "strings" + "github.com/0chain/common/core/util/wmpt" "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/zboxcore/allocationchange" @@ -23,13 +25,14 @@ type UploadOperation struct { chunkedUpload *ChunkedUpload isUpdate bool isDownload bool + lookupHash string } var ErrPauseUpload = errors.New("upload paused by user") func (uo *UploadOperation) Process(allocObj *Allocation, connectionID string) ([]fileref.RefEntity, zboxutil.Uint128, error) { if uo.isDownload { - if f, ok := uo.chunkedUpload.fileReader.(*sys.MemChanFile); ok { + if f, ok := uo.chunkedUpload.fileReader.(sys.File); ok { err := allocObj.DownloadFileToFileHandler(f, uo.chunkedUpload.fileMeta.RemotePath, false, nil, true, WithFileCallback(func() { f.Close() //nolint:errcheck })) @@ -59,7 +62,8 @@ func (uo *UploadOperation) Process(allocObj *Allocation, connectionID string) ([ fileref.DeleteFileRef(cacheKey) } } - l.Logger.Info("UploadOperation Success", zap.String("name", uo.chunkedUpload.fileMeta.RemoteName)) + uo.lookupHash = fileref.GetReferenceLookup(uo.chunkedUpload.allocationObj.ID, uo.chunkedUpload.fileMeta.RemotePath) + l.Logger.Debug("UploadOperation Success", zap.String("name", uo.chunkedUpload.fileMeta.RemoteName)) return nil, uo.chunkedUpload.uploadMask, nil } @@ -141,3 +145,26 @@ func NewUploadOperation(ctx context.Context, workdir string, allocObj *Allocatio uo.isDownload = isMemoryDownload return uo, cu.progress.ConnectionID, nil } + +func (uo *UploadOperation) ProcessChangeV2(trie *wmpt.WeightedMerkleTrie, changeIndex uint64) error { + if uo.refs[changeIndex] == nil { + return nil + } + ref := uo.refs[changeIndex] + ref.NumBlocks = int64(numBlocks(ref.Size)) + decodedKey, _ := hex.DecodeString(uo.lookupHash) + fileMetaRawHash := ref.GetFileMetaHashV2() + err := trie.Update(decodedKey, fileMetaRawHash, uint64(ref.NumBlocks)) + if err != nil { + l.Logger.Error("Error updating trie", zap.Error(err)) + return err + } + return nil +} + +func (uo *UploadOperation) GetLookupHash(changeIndex uint64) []string { + if uo.refs[changeIndex] == nil { + return nil + } + return []string{uo.lookupHash} +} diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index 304629b99..c91e12056 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -53,8 +53,10 @@ const ( MOVE_ENDPOINT = "/v1/file/move/" LIST_ENDPOINT = "/v1/file/list/" REFERENCE_ENDPOINT = "/v1/file/referencepath/" + REFERENCE_ENDPOINT_V2 = "/v2/file/referencepath/" CONNECTION_ENDPOINT = "/v1/connection/details/" COMMIT_ENDPOINT = "/v1/connection/commit/" + COMMIT_ENDPOINT_V2 = "/v2/connection/commit/" DOWNLOAD_ENDPOINT = "/v1/file/download/" LATEST_READ_MARKER = "/v1/readmarker/latest" FILE_META_ENDPOINT = "/v1/file/meta/" @@ -216,8 +218,16 @@ func setClientInfoWithSign(req *http.Request, sig, allocation, baseURL string) e return nil } -func NewCommitRequest(baseUrl, allocationID string, allocationTx string, body io.Reader) (*http.Request, error) { - u, err := joinUrl(baseUrl, COMMIT_ENDPOINT, allocationTx) +func NewCommitRequest(baseUrl, allocationID string, allocationTx string, body io.Reader, apiVersion int) (*http.Request, error) { + var ( + u *url.URL + err error + ) + if apiVersion == 1 { + u, err = joinUrl(baseUrl, COMMIT_ENDPOINT_V2, allocationTx) + } else { + u, err = joinUrl(baseUrl, COMMIT_ENDPOINT, allocationTx) + } if err != nil { return nil, err } @@ -262,6 +272,37 @@ func NewReferencePathRequest(baseUrl, allocationID string, allocationTx string, return req, nil } +func NewReferencePathRequestV2(baseUrl, allocationID, allocationTx, sig string, paths []string, loadOnly bool) (*http.Request, error) { + nurl, err := joinUrl(baseUrl, REFERENCE_ENDPOINT_V2, allocationTx) + if err != nil { + return nil, err + } + + pathBytes, err := json.Marshal(paths) + if err != nil { + return nil, err + } + params := url.Values{} + params.Add("paths", string(pathBytes)) + if loadOnly { + params.Add("load", "true") + } + nurl.RawQuery = params.Encode() // Escape Query Parameters + + req, err := http.NewRequest(http.MethodGet, nurl.String(), nil) + if err != nil { + return nil, err + } + + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { + return nil, err + } + + req.Header.Set(ALLOCATION_ID_HEADER, allocationID) + + return req, nil +} + func NewCalculateHashRequest(baseUrl, allocationID string, allocationTx string, paths []string) (*http.Request, error) { nurl, err := joinUrl(baseUrl, CALCULATE_HASH_ENDPOINT, allocationTx) if err != nil { From 8d9d162b1d4c7b359dcb67bf5feab7f089f10147 Mon Sep 17 00:00:00 2001 From: Jayash Date: Sun, 13 Oct 2024 21:14:24 +0530 Subject: [PATCH 225/319] Update version --- core/version/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/version/version.go b/core/version/version.go index e3bed521a..93d75f50f 100644 --- a/core/version/version.go +++ b/core/version/version.go @@ -2,4 +2,4 @@ package version -const VERSIONSTR = "v1.17.9-301-g37800139" +const VERSIONSTR = "v1.17.9-300-g01867b7a" From 40b069ff8f8433c724050f48ff28974c55647316 Mon Sep 17 00:00:00 2001 From: Jayash Date: Mon, 14 Oct 2024 01:45:47 +0530 Subject: [PATCH 226/319] Fix --- core/client/http.go | 67 +++++++++++++++------------ core/transaction/entity.go | 93 +++++++++++++++++++++----------------- core/util/httpnet.go | 28 ++++++++++++ 3 files changed, 117 insertions(+), 71 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index 6b4c49f8a..68a587e63 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -1,6 +1,7 @@ package client import ( + "context" "encoding/json" "fmt" "github.com/0chain/errors" @@ -21,8 +22,8 @@ type SCRestAPIHandler func(response map[string][]byte, numSharders int, err erro func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string, restApiUrls ...string) ([]byte, error) { const ( - consensusThresh = float32(25.0) - ScRestApiUrl = "v1/screst/" + consensusThresh = float32(25.0) // Minimum threshold for consensus + ScRestApiUrl = "v1/screst/" // Default API URL ) restApiUrl := ScRestApiUrl @@ -30,24 +31,31 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] restApiUrl = restApiUrls[0] } - sharders := nodeClient.Network().Sharders + sharders := nodeClient.Sharders() responses := make(map[int]int) entityResult := make(map[string][]byte) var ( - retObj []byte - maxCount int - dominant = 200 - wg sync.WaitGroup - mu sync.Mutex // Mutex to protect shared resources + retObj []byte + maxCount int + dominant = 200 + wg sync.WaitGroup + mu sync.Mutex + ctx, cancel = context.WithCancel(context.Background()) // Context for early exit + ctxCancelled bool ) + defer func() { + if !ctxCancelled { + cancel() + } + }() cfg, err := conf.GetClientConfig() if err != nil { return nil, err } - for _, sharder := range sharders { + for _, sharder := range sharders.Healthy() { wg.Add(1) go func(sharder string) { defer wg.Done() @@ -58,6 +66,8 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] fmt.Println(err.Error()) return } + + // Add query parameters q := urlObj.Query() for k, v := range params { q.Add(k, v) @@ -66,17 +76,18 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] req, err := util.NewHTTPGetRequest(urlObj.String()) if err != nil { - fmt.Println("1Error creating request", err.Error()) + fmt.Println("Error creating request:", err.Error()) return } - response, err := req.Get() + // Perform HTTP request with cancellation support + response, err := req.GetWithContext(ctx) if err != nil { - fmt.Println("2Error getting response", err.Error()) + fmt.Println("Error getting response:", err.Error()) return } - mu.Lock() // Lock before updating shared maps + mu.Lock() defer mu.Unlock() if response.StatusCode > http.StatusBadRequest { @@ -88,45 +99,41 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] responses[response.StatusCode]++ if responses[response.StatusCode] > maxCount { maxCount = responses[response.StatusCode] - } - - if isCurrentDominantStatus(response.StatusCode, responses, maxCount) { dominant = response.StatusCode retObj = []byte(response.Body) } entityResult[sharder] = []byte(response.Body) - nodeClient.sharders.Success(sharder) + + // Check if consensus is reached and cancel other requests + rate := float32(maxCount*100) / float32(cfg.SharderConsensous) + if rate >= consensusThresh { + ctxCancelled = true + cancel() // Stop further processing + } }(sharder) } - wg.Wait() + wg.Wait() // Wait for all goroutines to complete - rate := float32(maxCount*100) / float32(cfg.SharderConsensous) - if rate < consensusThresh { - err = errors.New("consensus_failed", "consensus failed on sharders") + if float32(maxCount*100)/float32(cfg.SharderConsensous) < consensusThresh { + return nil, errors.New("consensus_failed", "consensus failed on sharders") } if dominant != 200 { var objmap map[string]json.RawMessage - err := json.Unmarshal(retObj, &objmap) - if err != nil { + if err := json.Unmarshal(retObj, &objmap); err != nil { return nil, errors.New("", string(retObj)) } var parsed string - err = json.Unmarshal(objmap["error"], &parsed) - if err != nil || parsed == "" { + if err := json.Unmarshal(objmap["error"], &parsed); err != nil || parsed == "" { return nil, errors.New("", string(retObj)) } - return nil, errors.New("", parsed) } - if rate > consensusThresh { - return retObj, nil - } - return nil, err + return retObj, nil } // isCurrentDominantStatus determines whether the current response status is the dominant status among responses. diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 9b4a22ed3..c39ee28a6 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -2,6 +2,7 @@ package transaction import ( + "context" "encoding/json" "fmt" "github.com/0chain/gosdk/core/client" @@ -247,59 +248,69 @@ func (t *Transaction) VerifySigWith(pubkey string, verifyHandler VerifyFunc) (bo return verifyHandler(pubkey, t.Signature, t.Hash) } -func SendTransactionSync(txn *Transaction, miners []string) error { - wg := sync.WaitGroup{} - wg.Add(len(miners)) - fails := make(chan error, len(miners)) +// SendTransactionSync sends transactions to all miners in parallel and returns as soon as minSubmit is reached. +func SendTransactionSync(txn *Transaction, miners []string, minSubmit int) error { + if minSubmit > len(miners) { + return fmt.Errorf("minSubmit cannot be greater than total miners") + } + + // Context with 1-minute timeout + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) + defer cancel() + successCh := make(chan struct{}, len(miners)) + failCh := make(chan error, len(miners)) + + // Send transactions in parallel for _, miner := range miners { url := fmt.Sprintf("%v/%v", miner, TXN_SUBMIT_URL) - go func() { - _, err := sendTransactionToURL(url, txn, &wg) + go func(url string) { + err := sendTransactionToURL(ctx, url, txn) if err != nil { - fails <- err + failCh <- err + } else { + successCh <- struct{}{} } - wg.Done() - }() //nolint - } - wg.Wait() - close(fails) - - failureCount := 0 - messages := make(map[string]int) - for e := range fails { - if e != nil { - failureCount++ - messages[e.Error()] += 1 - } - } - - max := 0 - dominant := "" - for m, s := range messages { - if s > max { - dominant = m + }(url) + } + + // Track successful responses + successCount := 0 + for { + select { + case <-ctx.Done(): // If the context times out + return fmt.Errorf("operation timed out: %v", ctx.Err()) + case <-successCh: + successCount++ + if (successCount*100)/len(miners) >= minSubmit { + cancel() // Cancel remaining requests + return nil + } + case err := <-failCh: + // Log the error (optional) + fmt.Printf("Transaction failed: %v\n", err) } } +} - if failureCount == len(miners) { - return fmt.Errorf(dominant) +// sendTransactionToURL sends a transaction to a given URL and respects cancellation. +func sendTransactionToURL(ctx context.Context, url string, txn *Transaction) error { + req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, nil) // Assume `txn` is serialized inside + if err != nil { + return fmt.Errorf("failed to create request: %v", err) } - return nil -} - -func sendTransactionToURL(url string, txn *Transaction, wg *sync.WaitGroup) ([]byte, error) { - postReq, err := util.NewHTTPPostRequest(url, txn) + client := &http.Client{Timeout: 5 * time.Second} + resp, err := client.Do(req) if err != nil { - //Logger.Error("Error in serializing the transaction", txn, err.Error()) - return nil, err + return fmt.Errorf("failed to send transaction: %v", err) } - postResponse, err := postReq.Post() - if postResponse.StatusCode >= 200 && postResponse.StatusCode <= 299 { - return []byte(postResponse.Body), nil + defer resp.Body.Close() + + if resp.StatusCode >= 200 && resp.StatusCode <= 299 { + return nil } - return nil, errors.Wrap(err, errors.New("submit transaction failed", postResponse.Body)) + return fmt.Errorf("transaction failed with status: %d", resp.StatusCode) } type cachedObject struct { @@ -543,7 +554,7 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, Logger.Info(msg) Logger.Info("estimated txn fee: ", txn.TransactionFee) - err = SendTransactionSync(txn, nodeClient.GetStableMiners()) + err = SendTransactionSync(txn, nodeClient.GetStableMiners(), cfg.MinSubmit) if err != nil { Logger.Info("transaction submission failed", zap.Error(err)) client.Cache.Evict(txn.ClientID) diff --git a/core/util/httpnet.go b/core/util/httpnet.go index c26ed3362..113c8155d 100644 --- a/core/util/httpnet.go +++ b/core/util/httpnet.go @@ -161,6 +161,34 @@ func (r *GetRequest) Get() (*GetResponse, error) { return response, nil } +func (r *GetRequest) GetWithContext(ctx context.Context) (*GetResponse, error) { + response := &GetResponse{} + + // Use a channel to handle cancellation and errors. + done := make(chan struct{}) + var err error + + go func() { + defer close(done) + var presp *PostResponse + presp, err = r.Post() + if err != nil { + return // Error will be handled outside the goroutine. + } + response.PostResponse = presp + }() + + select { + case <-ctx.Done(): // Context was cancelled + return nil, ctx.Err() + case <-done: // Request completed successfully + if err != nil { + return nil, err // Return the error if any occurred. + } + return response, nil + } +} + func (r *PostRequest) Post() (*PostResponse, error) { result := &PostResponse{} err := httpDo(r.req, r.ctx, r.cncl, func(resp *http.Response, err error) error { From cb91e5a7dfd1e84038efe3fa1b899c1a54a5909e Mon Sep 17 00:00:00 2001 From: Jayash Date: Mon, 14 Oct 2024 01:48:23 +0530 Subject: [PATCH 227/319] Empty commit to trigger pipeline From 62138bb2a3bafaf3336a713b06bde2fd9dad73d6 Mon Sep 17 00:00:00 2001 From: Jayash Date: Mon, 14 Oct 2024 02:10:14 +0530 Subject: [PATCH 228/319] Fix --- core/transaction/entity.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index c39ee28a6..925d7e2e2 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -251,7 +251,7 @@ func (t *Transaction) VerifySigWith(pubkey string, verifyHandler VerifyFunc) (bo // SendTransactionSync sends transactions to all miners in parallel and returns as soon as minSubmit is reached. func SendTransactionSync(txn *Transaction, miners []string, minSubmit int) error { if minSubmit > len(miners) { - return fmt.Errorf("minSubmit cannot be greater than total miners") + return fmt.Errorf("minSubmit cannot be greater than total miners : %v < %v", minSubmit, len(miners)) } // Context with 1-minute timeout From f1ea661dd569347daa5a61bb61260b179bd89294 Mon Sep 17 00:00:00 2001 From: Jayash Date: Mon, 14 Oct 2024 02:44:49 +0530 Subject: [PATCH 229/319] Fix --- core/transaction/entity.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 925d7e2e2..926a572c2 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -250,10 +250,6 @@ func (t *Transaction) VerifySigWith(pubkey string, verifyHandler VerifyFunc) (bo // SendTransactionSync sends transactions to all miners in parallel and returns as soon as minSubmit is reached. func SendTransactionSync(txn *Transaction, miners []string, minSubmit int) error { - if minSubmit > len(miners) { - return fmt.Errorf("minSubmit cannot be greater than total miners : %v < %v", minSubmit, len(miners)) - } - // Context with 1-minute timeout ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) defer cancel() From 32652db3c17c15ba2f420d7138e9c330ca46f6dc Mon Sep 17 00:00:00 2001 From: Jayash Date: Mon, 14 Oct 2024 20:19:54 +0530 Subject: [PATCH 230/319] Debug --- core/transaction/entity.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 926a572c2..6e0a5a9ea 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -251,7 +251,7 @@ func (t *Transaction) VerifySigWith(pubkey string, verifyHandler VerifyFunc) (bo // SendTransactionSync sends transactions to all miners in parallel and returns as soon as minSubmit is reached. func SendTransactionSync(txn *Transaction, miners []string, minSubmit int) error { // Context with 1-minute timeout - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) defer cancel() successCh := make(chan struct{}, len(miners)) From a88f07fe19ca09eac2ad7d8db38c7cd00b73c0bf Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 19 Oct 2024 20:55:40 +0530 Subject: [PATCH 231/319] Fix get refs --- zboxcore/zboxutil/http.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index c91e12056..4d6090da6 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -373,7 +373,7 @@ func NewRefsRequest(baseUrl, allocationID, sig, allocationTx, path, pathHash, au req.Header.Set(ALLOCATION_ID_HEADER, allocationID) - if err := setClientInfoWithSign(req, sig, allocationID, baseUrl); err != nil { + if err := setClientInfoWithSign(req, sig, allocationTx, baseUrl); err != nil { return nil, err } From 24aad2c5153e07b4650b017f5e364d0af89649f6 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 19 Oct 2024 21:38:20 +0530 Subject: [PATCH 232/319] Revert "Merge pull request #1638 from 0chain/optimise/get-and-send" This reverts commit 6fec102cf5c2ae446bce65216867284c91a70f1a, reversing changes made to bceb3f933c4aaad6e5c6a72b85e915340ae41fd0. --- core/client/http.go | 67 +++++++++++++--------------- core/transaction/entity.go | 89 ++++++++++++++++++-------------------- core/util/httpnet.go | 28 ------------ 3 files changed, 71 insertions(+), 113 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index 68a587e63..6b4c49f8a 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -1,7 +1,6 @@ package client import ( - "context" "encoding/json" "fmt" "github.com/0chain/errors" @@ -22,8 +21,8 @@ type SCRestAPIHandler func(response map[string][]byte, numSharders int, err erro func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string, restApiUrls ...string) ([]byte, error) { const ( - consensusThresh = float32(25.0) // Minimum threshold for consensus - ScRestApiUrl = "v1/screst/" // Default API URL + consensusThresh = float32(25.0) + ScRestApiUrl = "v1/screst/" ) restApiUrl := ScRestApiUrl @@ -31,31 +30,24 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] restApiUrl = restApiUrls[0] } - sharders := nodeClient.Sharders() + sharders := nodeClient.Network().Sharders responses := make(map[int]int) entityResult := make(map[string][]byte) var ( - retObj []byte - maxCount int - dominant = 200 - wg sync.WaitGroup - mu sync.Mutex - ctx, cancel = context.WithCancel(context.Background()) // Context for early exit - ctxCancelled bool + retObj []byte + maxCount int + dominant = 200 + wg sync.WaitGroup + mu sync.Mutex // Mutex to protect shared resources ) - defer func() { - if !ctxCancelled { - cancel() - } - }() cfg, err := conf.GetClientConfig() if err != nil { return nil, err } - for _, sharder := range sharders.Healthy() { + for _, sharder := range sharders { wg.Add(1) go func(sharder string) { defer wg.Done() @@ -66,8 +58,6 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] fmt.Println(err.Error()) return } - - // Add query parameters q := urlObj.Query() for k, v := range params { q.Add(k, v) @@ -76,18 +66,17 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] req, err := util.NewHTTPGetRequest(urlObj.String()) if err != nil { - fmt.Println("Error creating request:", err.Error()) + fmt.Println("1Error creating request", err.Error()) return } - // Perform HTTP request with cancellation support - response, err := req.GetWithContext(ctx) + response, err := req.Get() if err != nil { - fmt.Println("Error getting response:", err.Error()) + fmt.Println("2Error getting response", err.Error()) return } - mu.Lock() + mu.Lock() // Lock before updating shared maps defer mu.Unlock() if response.StatusCode > http.StatusBadRequest { @@ -99,41 +88,45 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] responses[response.StatusCode]++ if responses[response.StatusCode] > maxCount { maxCount = responses[response.StatusCode] + } + + if isCurrentDominantStatus(response.StatusCode, responses, maxCount) { dominant = response.StatusCode retObj = []byte(response.Body) } entityResult[sharder] = []byte(response.Body) - - // Check if consensus is reached and cancel other requests - rate := float32(maxCount*100) / float32(cfg.SharderConsensous) - if rate >= consensusThresh { - ctxCancelled = true - cancel() // Stop further processing - } + nodeClient.sharders.Success(sharder) }(sharder) } - wg.Wait() // Wait for all goroutines to complete + wg.Wait() - if float32(maxCount*100)/float32(cfg.SharderConsensous) < consensusThresh { - return nil, errors.New("consensus_failed", "consensus failed on sharders") + rate := float32(maxCount*100) / float32(cfg.SharderConsensous) + if rate < consensusThresh { + err = errors.New("consensus_failed", "consensus failed on sharders") } if dominant != 200 { var objmap map[string]json.RawMessage - if err := json.Unmarshal(retObj, &objmap); err != nil { + err := json.Unmarshal(retObj, &objmap) + if err != nil { return nil, errors.New("", string(retObj)) } var parsed string - if err := json.Unmarshal(objmap["error"], &parsed); err != nil || parsed == "" { + err = json.Unmarshal(objmap["error"], &parsed) + if err != nil || parsed == "" { return nil, errors.New("", string(retObj)) } + return nil, errors.New("", parsed) } - return retObj, nil + if rate > consensusThresh { + return retObj, nil + } + return nil, err } // isCurrentDominantStatus determines whether the current response status is the dominant status among responses. diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 6e0a5a9ea..9b4a22ed3 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -2,7 +2,6 @@ package transaction import ( - "context" "encoding/json" "fmt" "github.com/0chain/gosdk/core/client" @@ -248,65 +247,59 @@ func (t *Transaction) VerifySigWith(pubkey string, verifyHandler VerifyFunc) (bo return verifyHandler(pubkey, t.Signature, t.Hash) } -// SendTransactionSync sends transactions to all miners in parallel and returns as soon as minSubmit is reached. -func SendTransactionSync(txn *Transaction, miners []string, minSubmit int) error { - // Context with 1-minute timeout - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) - defer cancel() +func SendTransactionSync(txn *Transaction, miners []string) error { + wg := sync.WaitGroup{} + wg.Add(len(miners)) + fails := make(chan error, len(miners)) - successCh := make(chan struct{}, len(miners)) - failCh := make(chan error, len(miners)) - - // Send transactions in parallel for _, miner := range miners { url := fmt.Sprintf("%v/%v", miner, TXN_SUBMIT_URL) - go func(url string) { - err := sendTransactionToURL(ctx, url, txn) + go func() { + _, err := sendTransactionToURL(url, txn, &wg) if err != nil { - failCh <- err - } else { - successCh <- struct{}{} - } - }(url) - } - - // Track successful responses - successCount := 0 - for { - select { - case <-ctx.Done(): // If the context times out - return fmt.Errorf("operation timed out: %v", ctx.Err()) - case <-successCh: - successCount++ - if (successCount*100)/len(miners) >= minSubmit { - cancel() // Cancel remaining requests - return nil + fails <- err } - case err := <-failCh: - // Log the error (optional) - fmt.Printf("Transaction failed: %v\n", err) + wg.Done() + }() //nolint + } + wg.Wait() + close(fails) + + failureCount := 0 + messages := make(map[string]int) + for e := range fails { + if e != nil { + failureCount++ + messages[e.Error()] += 1 } } -} -// sendTransactionToURL sends a transaction to a given URL and respects cancellation. -func sendTransactionToURL(ctx context.Context, url string, txn *Transaction) error { - req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, nil) // Assume `txn` is serialized inside - if err != nil { - return fmt.Errorf("failed to create request: %v", err) + max := 0 + dominant := "" + for m, s := range messages { + if s > max { + dominant = m + } } - client := &http.Client{Timeout: 5 * time.Second} - resp, err := client.Do(req) - if err != nil { - return fmt.Errorf("failed to send transaction: %v", err) + if failureCount == len(miners) { + return fmt.Errorf(dominant) } - defer resp.Body.Close() - if resp.StatusCode >= 200 && resp.StatusCode <= 299 { - return nil + return nil +} + +func sendTransactionToURL(url string, txn *Transaction, wg *sync.WaitGroup) ([]byte, error) { + postReq, err := util.NewHTTPPostRequest(url, txn) + if err != nil { + //Logger.Error("Error in serializing the transaction", txn, err.Error()) + return nil, err + } + postResponse, err := postReq.Post() + if postResponse.StatusCode >= 200 && postResponse.StatusCode <= 299 { + return []byte(postResponse.Body), nil } - return fmt.Errorf("transaction failed with status: %d", resp.StatusCode) + return nil, errors.Wrap(err, errors.New("submit transaction failed", postResponse.Body)) } type cachedObject struct { @@ -550,7 +543,7 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, Logger.Info(msg) Logger.Info("estimated txn fee: ", txn.TransactionFee) - err = SendTransactionSync(txn, nodeClient.GetStableMiners(), cfg.MinSubmit) + err = SendTransactionSync(txn, nodeClient.GetStableMiners()) if err != nil { Logger.Info("transaction submission failed", zap.Error(err)) client.Cache.Evict(txn.ClientID) diff --git a/core/util/httpnet.go b/core/util/httpnet.go index 113c8155d..c26ed3362 100644 --- a/core/util/httpnet.go +++ b/core/util/httpnet.go @@ -161,34 +161,6 @@ func (r *GetRequest) Get() (*GetResponse, error) { return response, nil } -func (r *GetRequest) GetWithContext(ctx context.Context) (*GetResponse, error) { - response := &GetResponse{} - - // Use a channel to handle cancellation and errors. - done := make(chan struct{}) - var err error - - go func() { - defer close(done) - var presp *PostResponse - presp, err = r.Post() - if err != nil { - return // Error will be handled outside the goroutine. - } - response.PostResponse = presp - }() - - select { - case <-ctx.Done(): // Context was cancelled - return nil, ctx.Err() - case <-done: // Request completed successfully - if err != nil { - return nil, err // Return the error if any occurred. - } - return response, nil - } -} - func (r *PostRequest) Post() (*PostResponse, error) { result := &PostResponse{} err := httpDo(r.req, r.ctx, r.cncl, func(resp *http.Response, err error) error { From 690bd2fdf7648b38d077e03097210e627c41b0af Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Sun, 20 Oct 2024 11:57:17 +0530 Subject: [PATCH 233/319] add log for active blobbers --- zboxcore/sdk/multi_operation_worker.go | 1 + 1 file changed, 1 insertion(+) diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 9d71d5279..7e9dacbb1 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -315,6 +315,7 @@ func (mo *MultiOperation) Process() error { } } activeBlobbers := mo.operationMask.CountOnes() + l.Logger.Info("Active blobbers ", activeBlobbers, " Consensus threshold ", mo.consensusThresh) if activeBlobbers < mo.consensusThresh { return errors.New("consensus_not_met", fmt.Sprintf("Active blobbers %d is less than consensus threshold %d", activeBlobbers, mo.consensusThresh)) } From 35959959c5e643493e9132a37d7752c1883cc061 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Sun, 20 Oct 2024 12:12:01 +0530 Subject: [PATCH 234/319] add error log for consensus thresh --- zboxcore/sdk/commitworker.go | 2 +- zboxcore/sdk/multi_operation_worker.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 666f5d2e9..fa6fb8b17 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -522,7 +522,7 @@ func (commitReq *CommitRequestV2) processCommit() { return } if !commitReq.isRepair { - commitReq.allocationObj.allocationRoot = hex.EncodeToString(rootHash) + commitReq.allocationObj.allocationRoot = encryption.Hash(hex.EncodeToString(rootHash) + commitReq.allocationObj.ID) } l.Logger.Info("[commit] ", "elapsedGetRefPath ", elapsedGetRefPath.Milliseconds(), " elapsedProcessChanges ", elapsedProcessChanges.Milliseconds(), " elapsedCommit ", elapsedCommit.Milliseconds(), " total ", time.Since(now).Milliseconds()) commitReq.result = SuccessCommitResult() diff --git a/zboxcore/sdk/multi_operation_worker.go b/zboxcore/sdk/multi_operation_worker.go index 7e9dacbb1..88678198e 100644 --- a/zboxcore/sdk/multi_operation_worker.go +++ b/zboxcore/sdk/multi_operation_worker.go @@ -315,8 +315,8 @@ func (mo *MultiOperation) Process() error { } } activeBlobbers := mo.operationMask.CountOnes() - l.Logger.Info("Active blobbers ", activeBlobbers, " Consensus threshold ", mo.consensusThresh) if activeBlobbers < mo.consensusThresh { + l.Logger.Error("consensus not met", activeBlobbers, mo.consensusThresh) return errors.New("consensus_not_met", fmt.Sprintf("Active blobbers %d is less than consensus threshold %d", activeBlobbers, mo.consensusThresh)) } if mo.allocationObj.StorageVersion == StorageV2 { From e56a76b45577789d4a4410fe06e9739acc2bab59 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Sun, 20 Oct 2024 12:25:25 +0530 Subject: [PATCH 235/319] remove change index --- zboxcore/sdk/commitworker.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index fa6fb8b17..622089767 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -499,7 +499,7 @@ func (commitReq *CommitRequestV2) processCommit() { wg.Add(1) go func(ind int) { defer wg.Done() - err = commitReq.commitBlobber(rootHash, rootWeight, prevWeight, blobberPos, blobber) + err = commitReq.commitBlobber(rootHash, rootWeight, prevWeight, blobber) if err != nil { l.Logger.Error("Error committing to blobber", err) errSlice[ind] = err @@ -528,7 +528,7 @@ func (commitReq *CommitRequestV2) processCommit() { commitReq.result = SuccessCommitResult() } -func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, prevWeight, changeIndex uint64, blobber *blockchain.StorageNode) (err error) { +func (req *CommitRequestV2) commitBlobber(rootHash []byte, rootWeight, prevWeight uint64, blobber *blockchain.StorageNode) (err error) { hasher := sha256.New() if blobber.LatestWM != nil { prevChainHash, err := hex.DecodeString(blobber.LatestWM.ChainHash) From c91ab327b692bbdc01add57f651bc2e4633ec6c9 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 20 Oct 2024 16:18:42 +0530 Subject: [PATCH 236/319] Fix dir worker --- zboxcore/sdk/dirworker.go | 1 + 1 file changed, 1 insertion(+) diff --git a/zboxcore/sdk/dirworker.go b/zboxcore/sdk/dirworker.go index 5dcc5fe44..a1b4ef26f 100644 --- a/zboxcore/sdk/dirworker.go +++ b/zboxcore/sdk/dirworker.go @@ -289,6 +289,7 @@ type DirOperation struct { func (dirOp *DirOperation) Process(allocObj *Allocation, connectionID string) ([]fileref.RefEntity, zboxutil.Uint128, error) { refs := make([]fileref.RefEntity, len(allocObj.Blobbers)) dR := &DirRequest{ + allocationObj: allocObj, allocationID: allocObj.ID, allocationTx: allocObj.Tx, connectionID: connectionID, From 0054357393630b4aac01bb8188a1f052988f63a0 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 20 Oct 2024 20:55:35 +0530 Subject: [PATCH 237/319] Fix --- core/transaction/entity.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 8c450ddb8..ba6724ff1 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -519,7 +519,7 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, txn.TransactionFee = fee txn.TransactionType = TxnTypeSmartContract - if len(clients) > 0 { + if len(clients) > 0 && clients[0] != "" { txn.ClientID = clients[0] } if len(clients) > 1 { From de2e7e136fdea7e6f75d8f72a5b4e66aaa952a84 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 20 Oct 2024 21:00:23 +0530 Subject: [PATCH 238/319] Fix --- core/client/set.go | 5 +---- core/transaction/entity.go | 6 ++---- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index 737515297..924b1a690 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -202,14 +202,11 @@ func PrivateKey() string { func Id(clients ...string) string { if len(clients) > 0 { if client.wallets[clients[0]] == nil { - fmt.Println("Id is empty") + fmt.Println("Id is empty : ", clients[0]) return "" } return client.wallets[clients[0]].ClientID } - if client.wallet.ClientID == "" { - fmt.Println("Id is empty") - } return client.wallet.ClientID } diff --git a/core/transaction/entity.go b/core/transaction/entity.go index ba6724ff1..b3af2ee55 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -490,7 +490,7 @@ func SmartContractTxnValueFeeWithRetry(scAddress string, sn SmartContractTxnData func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, value, fee uint64, clients ...string) (hash, out string, nonce int64, t *Transaction, err error) { - clientId := client.Id(clients...) + clientId := client.Id() if len(clients) > 0 && clients[0] != "" { clientId = clients[0] } @@ -518,10 +518,8 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, txn.Value = value txn.TransactionFee = fee txn.TransactionType = TxnTypeSmartContract + txn.ClientID = clientId - if len(clients) > 0 && clients[0] != "" { - txn.ClientID = clients[0] - } if len(clients) > 1 { txn.ToClientID = clients[1] txn.TransactionType = TxnTypeSend From 02a4c881feab0b118d0d2fd3cfb6a0e44e9ea66d Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Mon, 21 Oct 2024 14:09:33 +0530 Subject: [PATCH 239/319] change payload for path hash --- wasmsdk/blobber.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wasmsdk/blobber.go b/wasmsdk/blobber.go index 82b388154..248d2e685 100644 --- a/wasmsdk/blobber.go +++ b/wasmsdk/blobber.go @@ -1020,7 +1020,7 @@ func downloadBlocks(allocId string, remotePath, authTicket, lookupHash, writeChu var fh sys.File if writeChunkFuncName == "" { - pathHash := encryption.FastHash(remotePath) + pathHash := encryption.FastHash(fmt.Sprintf("%s:%d:%d", remotePath, startBlock, endBlock)) fs, err := sys.Files.Open(pathHash) if err != nil { return nil, fmt.Errorf("could not open local file: %v", err) From a6243f275dc256e0e6920bf786bfeb025bd64faf Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Mon, 21 Oct 2024 18:44:15 +0530 Subject: [PATCH 240/319] Keep miners count 3 at least --- core/client/init_node.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/client/init_node.go b/core/client/init_node.go index 86b6bd736..f7a2ad901 100644 --- a/core/client/init_node.go +++ b/core/client/init_node.go @@ -52,7 +52,7 @@ func (n *Node) ResetStableMiners() { n.networkGuard.Lock() defer n.networkGuard.Unlock() cfg, _ := conf.GetClientConfig() - reqMiners := util.MaxInt(1, int(math.Ceil(float64(cfg.MinSubmit)*float64(len(n.network.Miners))/100))) + reqMiners := util.MaxInt(3, int(math.Ceil(float64(cfg.MinSubmit)*float64(len(n.network.Miners))/100))) n.stableMiners = util.GetRandom(n.network.Miners, reqMiners) } @@ -135,7 +135,7 @@ func Init(ctx context.Context, cfg conf.Config) error { return err } - reqMiners := util.MaxInt(1, int(math.Ceil(float64(cfg.MinSubmit)*float64(len(network.Miners))/100))) + reqMiners := util.MaxInt(3, int(math.Ceil(float64(cfg.MinSubmit)*float64(len(network.Miners))/100))) sharders := NewHolder(network.Sharders, util.MinInt(len(network.Sharders), util.MaxInt(cfg.SharderConsensous, conf.DefaultSharderConsensous))) nodeClient = &Node{ stableMiners: util.GetRandom(network.Miners, reqMiners), From 49d82b88125f56bb71b60231f9efd88e5dbbf128 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Mon, 21 Oct 2024 19:04:58 +0530 Subject: [PATCH 241/319] Optimise send and get to miners and sharders --- core/transaction/entity.go | 40 ++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 9b4a22ed3..d519079a2 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -2,6 +2,7 @@ package transaction import ( + "context" "encoding/json" "fmt" "github.com/0chain/gosdk/core/client" @@ -248,29 +249,49 @@ func (t *Transaction) VerifySigWith(pubkey string, verifyHandler VerifyFunc) (bo } func SendTransactionSync(txn *Transaction, miners []string) error { + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + wg := sync.WaitGroup{} wg.Add(len(miners)) fails := make(chan error, len(miners)) for _, miner := range miners { url := fmt.Sprintf("%v/%v", miner, TXN_SUBMIT_URL) - go func() { - _, err := sendTransactionToURL(url, txn, &wg) - if err != nil { - fails <- err + go func(url string) { + defer wg.Done() + select { + case <-ctx.Done(): + fails <- ctx.Err() // Timeout or cancellation + default: + _, err := sendTransactionToURL(url, txn, nil) + if err != nil { + fails <- err + } } - wg.Done() - }() //nolint + }(url) } - wg.Wait() - close(fails) + done := make(chan struct{}) + go func() { + wg.Wait() + close(fails) + close(done) + }() + + select { + case <-done: // All requests completed + case <-ctx.Done(): // Timeout reached + return ctx.Err() + } + + // Error processing logic here (same as original) failureCount := 0 messages := make(map[string]int) for e := range fails { if e != nil { failureCount++ - messages[e.Error()] += 1 + messages[e.Error()]++ } } @@ -285,7 +306,6 @@ func SendTransactionSync(txn *Transaction, miners []string) error { if failureCount == len(miners) { return fmt.Errorf(dominant) } - return nil } From 388d2259326a557cee2600a7c61c30fda98838ce Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Mon, 21 Oct 2024 19:06:16 +0530 Subject: [PATCH 242/319] Empty commit From b0205972ddf05f795e2d106191fb4682409d832d Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Mon, 21 Oct 2024 21:14:42 +0530 Subject: [PATCH 243/319] Fix --- wasmsdk/sdk.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index 0567e71c2..189d99ab6 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -39,7 +39,7 @@ func initSDKs(chainID, blockWorker, signatureScheme string, zboxApiClient.SetRequest(zboxHost, zboxAppType) - err := client.InitSDK("{}", blockWorker, chainID, signatureScheme, 0, false, false, minConfirmation, minSubmit, confirmationChainLength, sharderConsensous) + err := client.InitSDK("{}", blockWorker, chainID, signatureScheme, 0, isSplit, false, minConfirmation, minSubmit, confirmationChainLength, sharderConsensous) if err != nil { fmt.Println("wasm: InitStorageSDK ", err) return err From a679e66de1baadb3d57e3ced70009f6f1bb244d9 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Tue, 22 Oct 2024 20:12:32 +0530 Subject: [PATCH 244/319] fix release buffer in upload --- zboxcore/sdk/chunked_upload.go | 1 + zboxcore/sdk/chunked_upload_chunk_reader.go | 7 ++++++- zboxcore/sdk/commitworker.go | 8 ++++---- zboxcore/sdk/downloadworker.go | 3 ++- zboxcore/zboxutil/download_buffer.go | 2 ++ 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/zboxcore/sdk/chunked_upload.go b/zboxcore/sdk/chunked_upload.go index 6f46397e9..c0b042579 100644 --- a/zboxcore/sdk/chunked_upload.go +++ b/zboxcore/sdk/chunked_upload.go @@ -412,6 +412,7 @@ func (su *ChunkedUpload) process() error { su.statusCallback.Started(su.allocationObj.ID, su.fileMeta.RemotePath, su.opCode, int(su.fileMeta.ActualSize)+int(su.fileMeta.ActualThumbnailSize)) } su.startProcessor() + defer su.chunkReader.Release() defer su.chunkReader.Close() defer su.ctxCncl(nil) for { diff --git a/zboxcore/sdk/chunked_upload_chunk_reader.go b/zboxcore/sdk/chunked_upload_chunk_reader.go index c576ebd92..a54159b3c 100644 --- a/zboxcore/sdk/chunked_upload_chunk_reader.go +++ b/zboxcore/sdk/chunked_upload_chunk_reader.go @@ -33,6 +33,8 @@ type ChunkedUploadChunkReader interface { GetFileHash() (string, error) //Reset reset offset Reset() + //Release Buffer + Release() } // chunkedUploadChunkReader read chunk bytes from io.Reader. see detail on https://github.com/0chain/blobber/wiki/Protocols#what-is-fixedmerkletree @@ -315,7 +317,6 @@ func (r *chunkedUploadChunkReader) Close() { r.closeOnce.Do(func() { close(r.hasherDataChan) r.hasherWG.Wait() - uploadPool.Put(r.fileShardsDataBuffer) }) } @@ -328,6 +329,10 @@ func (r *chunkedUploadChunkReader) GetFileHash() (string, error) { return r.hasher.GetFileHash() } +func (r *chunkedUploadChunkReader) Release() { + uploadPool.Put(r.fileShardsDataBuffer) +} + func (r *chunkedUploadChunkReader) hashData() { defer r.hasherWG.Done() for data := range r.hasherDataChan { diff --git a/zboxcore/sdk/commitworker.go b/zboxcore/sdk/commitworker.go index 622089767..c5d069e14 100644 --- a/zboxcore/sdk/commitworker.go +++ b/zboxcore/sdk/commitworker.go @@ -499,10 +499,10 @@ func (commitReq *CommitRequestV2) processCommit() { wg.Add(1) go func(ind int) { defer wg.Done() - err = commitReq.commitBlobber(rootHash, rootWeight, prevWeight, blobber) - if err != nil { - l.Logger.Error("Error committing to blobber", err) - errSlice[ind] = err + commitErr := commitReq.commitBlobber(rootHash, rootWeight, prevWeight, blobber) + if commitErr != nil { + l.Logger.Error("Error committing to blobber", commitErr) + errSlice[ind] = commitErr mu.Lock() commitReq.commitMask = commitReq.commitMask.And(zboxutil.NewUint128(1).Lsh(blobberPos).Not()) mu.Unlock() diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index 1f38c9df4..e070e14d0 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -561,7 +561,7 @@ func (req *DownloadRequest) processDownload() { req.bufferMap[blobberIdx] = zboxutil.NewDownloadBufferWithChan(sz, bufBlocks, req.effectiveBlockSize) } else { bufMask := zboxutil.NewDownloadBufferWithMask(sz, bufBlocks, req.effectiveBlockSize) - bufMask.SetNumBlocks(int(numBlocks)) + bufMask.SetNumBlocks(int(bufBlocks)) req.bufferMap[blobberIdx] = bufMask } } @@ -998,6 +998,7 @@ func (req *DownloadRequest) errorCB(err error, remotePathCB string) { return } req.skip = true + logger.Logger.Error("Download failed: ", err, " remotefilepath: ", remotePathCB) if req.localFilePath != "" { if info, err := req.fileHandler.Stat(); err == nil && info.Size() == 0 { os.Remove(req.localFilePath) //nolint: errcheck diff --git a/zboxcore/zboxutil/download_buffer.go b/zboxcore/zboxutil/download_buffer.go index ab1e17f70..661fec10d 100644 --- a/zboxcore/zboxutil/download_buffer.go +++ b/zboxcore/zboxutil/download_buffer.go @@ -118,6 +118,8 @@ func (r *DownloadBufferWithMask) RequestChunk(ctx context.Context, num int) []by buff := BufferPool.Get() if cap(buff.B) < r.reqSize { buff.B = make([]byte, r.reqSize) + } else { + buff.B = buff.B[:r.reqSize] } r.downloadBuf[num] = buff } From 2a06c04b60918fbd6b8332b7aed0e510cd875981 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Tue, 22 Oct 2024 23:41:12 +0530 Subject: [PATCH 245/319] Fix split key --- core/client/set.go | 27 +++++++++++++++++++-------- wasmsdk/proxy.go | 2 +- zcncore/wallet_base.go | 3 ++- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index 2c1863a85..4342f3d4f 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -16,6 +16,9 @@ import ( var ( client Client sdkInitialized bool + + Sign SignFunc + sigC = make(chan struct{}, 1) ) type SignFunc func(hash string) (string, error) @@ -33,12 +36,24 @@ type Client struct { func init() { sys.Sign = signHash - client = Client{ - wallet: &zcncrypto.Wallet{}, - sign: func(hash string) (string, error) { + sys.SignWithAuth = signHash + + sigC <- struct{}{} + + // initialize SignFunc as default implementation + Sign = func(hash string) (string, error) { + if client.wallet.PeerPublicKey == "" { return sys.Sign(hash, client.signatureScheme, GetClientSysKeys()) - }, + } + + // get sign lock + <-sigC + fmt.Println("Sign: with sys.SignWithAuth:", sys.SignWithAuth, "sysKeys:", GetClientSysKeys()) + sig, err := sys.SignWithAuth(hash, client.signatureScheme, GetClientSysKeys()) + sigC <- struct{}{} + return sig, err } + sys.Verify = verifySignature sys.VerifyWith = verifySignatureWith } @@ -157,10 +172,6 @@ func TxnFee() uint64 { return client.txnFee } -func Sign(hash string) (string, error) { - return client.sign(hash) -} - func IsWalletSet() bool { return client.wallet.ClientID != "" } diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index a7162bc07..27eda5962 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -85,7 +85,7 @@ func main() { data, err := json.Marshal(zcncore.AuthMessage{ Hash: hash, Signature: sig, - ClientID: client.GetClient().ClientID, + ClientID: client.ClientID(), }) if err != nil { return "", err diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index 1472bd319..a5bdef2d7 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -3,6 +3,7 @@ package zcncore import ( "encoding/hex" "fmt" + "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/util" "net/http" @@ -11,7 +12,6 @@ import ( "errors" - "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/core/version" @@ -254,6 +254,7 @@ func SplitKeys(privateKey string, numSplits int) (string, error) { if err != nil { return "", errors.New("split key failed." + err.Error()) } + w.IsSplit = true wStr, err := w.Marshal() if err != nil { return "", errors.New("wallet encoding failed." + err.Error()) From 354e002b7f295aa6e622c88fa727bca39df13a09 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Tue, 22 Oct 2024 23:43:00 +0530 Subject: [PATCH 246/319] Empty commit From 653c149f172bfa5c519de7709629f536b71a1190 Mon Sep 17 00:00:00 2001 From: dabasov Date: Tue, 22 Oct 2024 21:47:42 +0300 Subject: [PATCH 247/319] added logging --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 35f0313cf..35a83cc5d 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/0chain/gosdk go 1.22.0 require ( - github.com/0chain/common v1.13.1-0.20240926135437-61b88d39bbbd + github.com/0chain/common v1.18.0 github.com/0chain/errors v1.0.3 github.com/Luzifer/go-openssl/v3 v3.1.0 github.com/btcsuite/btcd v0.23.4 diff --git a/go.sum b/go.sum index 164ab0b6a..d29ed5c80 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/0chain/common v1.13.1-0.20240926135437-61b88d39bbbd h1:PDnzyJJA4rF2LBW+0OQJ5y9vIwsPYYjbbFwImvgiDdY= -github.com/0chain/common v1.13.1-0.20240926135437-61b88d39bbbd/go.mod h1:vHK9Q0/nz28PxmKsLSMLzEs5iK6QVgl1LxdDQdR7y6s= +github.com/0chain/common v1.18.0 h1:vCkD869DR0zTZS4CDl5MMN3qixxyfTNxDDcHp7cTRrc= +github.com/0chain/common v1.18.0/go.mod h1:vHK9Q0/nz28PxmKsLSMLzEs5iK6QVgl1LxdDQdR7y6s= github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM= github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= From 906c087aa2408484e189fca2b373661eee8a378e Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 23 Oct 2024 00:31:42 +0530 Subject: [PATCH 248/319] Fix --- core/client/set.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index a64f9cd06..da8d78347 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -43,14 +43,19 @@ func init() { // initialize SignFunc as default implementation Sign = func(hash string, clients ...string) (string, error) { - if client.wallet.PeerPublicKey == "" { - return sys.Sign(hash, client.signatureScheme, GetClientSysKeys()) + wallet := client.wallet + if len(clients) > 0 { + wallet = client.wallets[clients[0]] + } + + if wallet.PeerPublicKey == "" { + return sys.Sign(hash, client.signatureScheme, GetClientSysKeys(clients...)) } // get sign lock <-sigC - fmt.Println("Sign: with sys.SignWithAuth:", sys.SignWithAuth, "sysKeys:", GetClientSysKeys()) - sig, err := sys.SignWithAuth(hash, client.signatureScheme, GetClientSysKeys()) + fmt.Println("Sign: with sys.SignWithAuth:", sys.SignWithAuth, "sysKeys:", GetClientSysKeys(clients...)) + sig, err := sys.SignWithAuth(hash, client.signatureScheme, GetClientSysKeys(clients...)) sigC <- struct{}{} return sig, err } @@ -184,7 +189,6 @@ func TxnFee() uint64 { return client.txnFee } - func IsWalletSet() bool { return client.wallet.ClientID != "" } From 07a5cd26e9e56eaa6adeaf0aba7a9109cd6ca960 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 23 Oct 2024 00:48:52 +0530 Subject: [PATCH 249/319] Add verify signature functions --- core/client/set.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/core/client/set.go b/core/client/set.go index 924b1a690..889a2d7f5 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -297,3 +297,21 @@ func PopulateClient(walletJSON, signatureScheme string) (zcncrypto.Wallet, error SetSignatureScheme(signatureScheme) return wallet, nil } + +func VerifySignature(signature string, msg string) (bool, error) { + ss := zcncrypto.NewSignatureScheme(client.signatureScheme) + if err := ss.SetPublicKey(client.wallet.ClientID); err != nil { + return false, err + } + + return ss.Verify(signature, msg) +} + +func VerifySignatureWith(pubKey, signature, hash string) (bool, error) { + sch := zcncrypto.NewSignatureScheme(client.signatureScheme) + err := sch.SetPublicKey(pubKey) + if err != nil { + return false, err + } + return sch.Verify(signature, hash) +} From 7af166711ea0effffb391ed2b639751448980b6a Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 23 Oct 2024 00:53:16 +0530 Subject: [PATCH 250/319] Fix --- core/client/set.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/client/set.go b/core/client/set.go index 889a2d7f5..b8801a582 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -300,7 +300,7 @@ func PopulateClient(walletJSON, signatureScheme string) (zcncrypto.Wallet, error func VerifySignature(signature string, msg string) (bool, error) { ss := zcncrypto.NewSignatureScheme(client.signatureScheme) - if err := ss.SetPublicKey(client.wallet.ClientID); err != nil { + if err := ss.SetPublicKey(PublicKey()); err != nil { return false, err } From 11dab86c0ff3159eab1625d659c10c065954cf2d Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 23 Oct 2024 02:28:15 +0530 Subject: [PATCH 251/319] Fix --- zcnbridge/wallet/status.go | 152 +++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 zcnbridge/wallet/status.go diff --git a/zcnbridge/wallet/status.go b/zcnbridge/wallet/status.go new file mode 100644 index 000000000..12b1979a2 --- /dev/null +++ b/zcnbridge/wallet/status.go @@ -0,0 +1,152 @@ +package wallet + +import ( + "encoding/json" + "errors" + "fmt" + "os" + "sync" + + "github.com/0chain/gosdk/zcncore" +) + +// ZCNStatus represents the status of a ZCN operation. +type ZCNStatus struct { + walletString string + balance int64 + value interface{} + Wg *sync.WaitGroup + Success bool + Err error +} + +// NewZCNStatus creates a new ZCNStatus instance. +// - value: value to be stored in the ZCNStatus instance +func NewZCNStatus(value interface{}) (zcns *ZCNStatus) { + return &ZCNStatus{ + Wg: new(sync.WaitGroup), + value: value, + } +} + +// Begin starts the wait group +func (zcn *ZCNStatus) Begin() { + zcn.Wg.Add(1) +} + +// Wait waits for the wait group to finish +func (zcn *ZCNStatus) Wait() error { + zcn.Wg.Wait() + return zcn.Err +} + +// OnBalanceAvailable callback when balance is available +// - status: status of the operation +// - value: balance value +// - third parameter is not used, it is kept for compatibility +func (zcn *ZCNStatus) OnBalanceAvailable(status int, value int64, _ string) { + defer zcn.Wg.Done() + if status == zcncore.StatusSuccess { + zcn.Success = true + } else { + zcn.Success = false + } + zcn.balance = value +} + +// OnWalletCreateComplete callback when a wallet is created +// - status: status of the operation +// - wallet: wallet json string +func (zcn *ZCNStatus) OnWalletCreateComplete(status int, wallet string, err string) { + defer zcn.Wg.Done() + if status != zcncore.StatusSuccess { + zcn.Success = false + zcn.Err = errors.New(err) + zcn.walletString = "" + return + } + zcn.Success = true + zcn.Err = nil + zcn.walletString = wallet +} + +// OnInfoAvailable callback when information is available +// - op`: operation type (check `zcncore.Op* constants) +// - status: status of the operation +// - info: information represneted as a string +// - err: error message +func (zcn *ZCNStatus) OnInfoAvailable(op int, status int, info string, err string) { + defer zcn.Wg.Done() + + if status != zcncore.StatusSuccess { + zcn.Err = errors.New(err) + zcn.Success = false + return + } + + if info == "" || info == "{}" { + zcn.Err = errors.New("empty response") + zcn.Success = false + return + } + + var errm error + if errm = json.Unmarshal([]byte(info), zcn.value); errm != nil { + zcn.Err = fmt.Errorf("decoding response: %v", errm) + zcn.Success = false + return + } + + zcn.Err = nil + zcn.Success = true +} + +// OnSetupComplete callback when setup is completed. +// Paramters are not used in this implementation, +// just kept for compatibility. +func (zcn *ZCNStatus) OnSetupComplete(_ int, _ string) { + defer zcn.Wg.Done() +} + +// OnAuthorizeSendComplete callback when authorization is completed +// - status: status of the operation +// - 2nd parameter is not used, it is kept for compatibility +// - 3rd parameter is not used, it is kept for compatibility +// - 4th parameter is not used, it is kept for compatibility +// - creationDate: timestamp of the creation date +// - signature: signature of the operation +func (zcn *ZCNStatus) OnAuthorizeSendComplete(status int, _ string, _ int64, _ string, creationDate int64, signature string) { + defer zcn.Wg.Done() + + Logger.Info("Status: ", status) + Logger.Info("Timestamp:", creationDate) + Logger.Info("Signature:", signature) +} + +// OnVoteComplete callback when a multisig vote is completed +// - status: status of the operation +// - proposal: proposal json string +// - err: error message +func (zcn *ZCNStatus) OnVoteComplete(status int, proposal string, err string) { + defer zcn.Wg.Done() + if status != zcncore.StatusSuccess { + zcn.Success = false + zcn.Err = errors.New(err) + zcn.walletString = "" + return + } + zcn.Success = true + zcn.Err = nil + zcn.walletString = proposal +} + +//goland:noinspection ALL +func PrintError(v ...interface{}) { + fmt.Fprintln(os.Stderr, v...) +} + +//goland:noinspection ALL +func ExitWithError(v ...interface{}) { + fmt.Fprintln(os.Stderr, v...) + os.Exit(1) +} From f952c5fe523d26668567041d898fca6c950dcb20 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Wed, 23 Oct 2024 11:05:27 +0530 Subject: [PATCH 252/319] change payload from path to lookuphash --- wasmsdk/blobber.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wasmsdk/blobber.go b/wasmsdk/blobber.go index 248d2e685..096c6a388 100644 --- a/wasmsdk/blobber.go +++ b/wasmsdk/blobber.go @@ -1000,7 +1000,7 @@ func upload(allocationID, remotePath string, fileBytes, thumbnailBytes []byte, w // - lookupHash : lookup hash of the file, which is used to locate the file if remotepath and allocation id are not provided // - writeChunkFuncName : callback function name to write the chunk, if empty the function will return arrayBuffer otherwise will return nil -func downloadBlocks(allocId string, remotePath, authTicket, lookupHash, writeChunkFuncName string, startBlock, endBlock int64) ([]byte, error) { +func downloadBlocks(allocId, remotePath, authTicket, lookupHash, writeChunkFuncName string, startBlock, endBlock int64) ([]byte, error) { if len(remotePath) == 0 && len(authTicket) == 0 { return nil, RequiredArg("remotePath/authTicket") @@ -1020,7 +1020,7 @@ func downloadBlocks(allocId string, remotePath, authTicket, lookupHash, writeChu var fh sys.File if writeChunkFuncName == "" { - pathHash := encryption.FastHash(fmt.Sprintf("%s:%d:%d", remotePath, startBlock, endBlock)) + pathHash := encryption.FastHash(fmt.Sprintf("%s:%d:%d", lookupHash, startBlock, endBlock)) fs, err := sys.Files.Open(pathHash) if err != nil { return nil, fmt.Errorf("could not open local file: %v", err) From a2d464bfce83ef7fe05bd313b34e5303bf81ec87 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Wed, 23 Oct 2024 11:28:58 +0530 Subject: [PATCH 253/319] update wasm exec to 1.22 --- wasmsdk/jsbridge/zcnworker.js.tpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wasmsdk/jsbridge/zcnworker.js.tpl b/wasmsdk/jsbridge/zcnworker.js.tpl index 2f4d3d4ac..d082ff1d9 100644 --- a/wasmsdk/jsbridge/zcnworker.js.tpl +++ b/wasmsdk/jsbridge/zcnworker.js.tpl @@ -1,4 +1,4 @@ -importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.21.5/misc/wasm/wasm_exec.js','https://cdn.jsdelivr.net/gh/herumi/bls-wasm@v1.1.1/browser/bls.js'); +importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.22.5/misc/wasm/wasm_exec.js','https://cdn.jsdelivr.net/gh/herumi/bls-wasm@v1.1.1/browser/bls.js'); const go = new Go(); go.argv = {{.ArgsToJS}} From bcd693c7cca5c6c0f94abc4902b76cd2e58f9efb Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 23 Oct 2024 14:26:32 +0530 Subject: [PATCH 254/319] Fix --- wasmsdk/proxy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index 27eda5962..ab165eb2e 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -85,7 +85,7 @@ func main() { data, err := json.Marshal(zcncore.AuthMessage{ Hash: hash, Signature: sig, - ClientID: client.ClientID(), + ClientID: client.Wallet().ClientID, }) if err != nil { return "", err From a2670fbee6a5091aeb91c033fcd7ac57a83f54f3 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 23 Oct 2024 14:32:31 +0530 Subject: [PATCH 255/319] Fix --- zboxcore/sdk/allocation_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/zboxcore/sdk/allocation_test.go b/zboxcore/sdk/allocation_test.go index 86aa55102..de7faf47a 100644 --- a/zboxcore/sdk/allocation_test.go +++ b/zboxcore/sdk/allocation_test.go @@ -657,6 +657,7 @@ func TestAllocation_RepairRequired(t *testing.T) { DataShards: 2, ParityShards: 2, FileOptions: 63, + Owner: mockClientId, } a.InitAllocation() client.SetSdkInitialized(true) From f99019aa4be3acd481cc7672353834eeb9db263d Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 23 Oct 2024 14:56:44 +0530 Subject: [PATCH 256/319] Fix --- zboxcore/sdk/allocation_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/zboxcore/sdk/allocation_test.go b/zboxcore/sdk/allocation_test.go index de7faf47a..da155c2bb 100644 --- a/zboxcore/sdk/allocation_test.go +++ b/zboxcore/sdk/allocation_test.go @@ -1022,6 +1022,7 @@ func TestAllocation_GetRefs(t *testing.T) { functionName := "TestAllocation_GetRefs" t.Run("Test_Get_Refs_Returns_Slice_Of_Length_0_When_File_Not_Present", func(t *testing.T) { a := &Allocation{ + Owner: mockClientId, DataShards: 2, ParityShards: 2, } @@ -1121,6 +1122,7 @@ func TestAllocation_GetFileMeta(t *testing.T) { t.Run(tt.name, func(t *testing.T) { require := require.New(t) a := &Allocation{ + Owner: mockClientId, DataShards: 2, ParityShards: 2, FileOptions: 63, From 8485a81682bbb93b0b06e22c925cf198e75efa46 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 23 Oct 2024 15:12:17 +0530 Subject: [PATCH 257/319] Fix --- zboxcore/sdk/allocation_test.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/zboxcore/sdk/allocation_test.go b/zboxcore/sdk/allocation_test.go index da155c2bb..0be94fd7a 100644 --- a/zboxcore/sdk/allocation_test.go +++ b/zboxcore/sdk/allocation_test.go @@ -1183,7 +1183,7 @@ func TestAllocation_GetAuthTicketForShare(t *testing.T) { ClientKey: mockClientKey, }) require := require.New(t) - a := &Allocation{DataShards: 1, ParityShards: 1, FileOptions: 63} + a := &Allocation{DataShards: 1, ParityShards: 1, FileOptions: 63, Owner: mockClientId} a.InitAllocation() for i := 0; i < numberBlobbers; i++ { a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{}) @@ -1390,6 +1390,7 @@ func TestAllocation_GetAuthTicket(t *testing.T) { require := require.New(t) a := &Allocation{ + Owner: mockClientId, DataShards: 1, ParityShards: 1, FileOptions: 63, @@ -1562,8 +1563,9 @@ func TestAllocation_ListDirFromAuthTicket(t *testing.T) { authTicket: authTicket, lookupHash: mockLookupHash, expectedResult: &ListResult{ - Type: mockType, - Size: 0, + ClientId: mockClientId, + Type: mockType, + Size: 0, }, }, setup: func(t *testing.T, testCaseName string, a *Allocation, mockClient *mocks.HttpClient) (teardown func(t *testing.T)) { @@ -1599,6 +1601,7 @@ func TestAllocation_ListDirFromAuthTicket(t *testing.T) { a := &Allocation{ ID: mockAllocationId, Tx: mockAllocationTxId, + Owner: mockClientId, FileOptions: 63, DataShards: 2, ParityShards: 2, @@ -2009,6 +2012,7 @@ func TestAllocation_GetFileMetaFromAuthTicket(t *testing.T) { }) a := &Allocation{ + Owner: mockClientId, ID: mockAllocationId, Tx: mockAllocationTxId, DataShards: 2, @@ -2389,6 +2393,7 @@ func getMockAuthTicket(t *testing.T) string { ClientKey: mockClientKey, }) a := &Allocation{ + Owner: mockClientId, ID: mockAllocationId, Tx: mockAllocationTxId, DataShards: 1, From ba28e756e4cfc429e4e0b72fdc839d3225667923 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 23 Oct 2024 16:28:31 +0530 Subject: [PATCH 258/319] Fix --- core/client/set.go | 9 +++++---- zboxcore/zboxutil/http.go | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index b3cfdef78..8a69de06c 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -44,7 +44,8 @@ func init() { // initialize SignFunc as default implementation Sign = func(hash string, clients ...string) (string, error) { wallet := client.wallet - if len(clients) > 0 { + + if len(clients) > 0 && clients[0] != "" { wallet = client.wallets[clients[0]] } @@ -105,7 +106,7 @@ func verifySignatureWith(pubKey, signature, hash string) (bool, error) { func GetClientSysKeys(clients ...string) []sys.KeyPair { var wallet *zcncrypto.Wallet - if len(clients) > 0 { + if len(clients) > 0 && clients[0] != "" { wallet = client.wallets[clients[0]] } else { wallet = client.wallet @@ -194,7 +195,7 @@ func IsWalletSet() bool { } func PublicKey(clients ...string) string { - if len(clients) > 0 { + if len(clients) > 0 && clients[0] != "" { if client.wallets[clients[0]] == nil { fmt.Println("Public key is empty") return "" @@ -216,7 +217,7 @@ func PrivateKey() string { } func Id(clients ...string) string { - if len(clients) > 0 { + if len(clients) > 0 && clients[0] != "" { if client.wallets[clients[0]] == nil { fmt.Println("Id is empty : ", clients[0]) return "" diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index 6a87576c2..8613a78c8 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -202,7 +202,7 @@ func setClientInfo(req *http.Request) { func setClientInfoWithSign(req *http.Request, sig, allocation, baseURL string, clients ...string) error { var clientID string - if len(clients) > 0 { + if len(clients) > 0 && clients[0] != "" { clientID = clients[0] } else { clientID = client.Id() From 75d7434e883e188181fafcb3ca1bf5461313fe57 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 23 Oct 2024 18:22:54 +0530 Subject: [PATCH 259/319] Fix --- core/client/set.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/client/set.go b/core/client/set.go index 8a69de06c..8a05a29af 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -45,7 +45,7 @@ func init() { Sign = func(hash string, clients ...string) (string, error) { wallet := client.wallet - if len(clients) > 0 && clients[0] != "" { + if len(clients) > 0 && clients[0] != "" && client.wallets[clients[0]] != nil { wallet = client.wallets[clients[0]] } From ea1fc07c959f805474c76b798c75ea5a3a51dd7f Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 23 Oct 2024 19:50:29 +0530 Subject: [PATCH 260/319] Fix --- core/client/set.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index 8a05a29af..94bab79ca 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -106,7 +106,7 @@ func verifySignatureWith(pubKey, signature, hash string) (bool, error) { func GetClientSysKeys(clients ...string) []sys.KeyPair { var wallet *zcncrypto.Wallet - if len(clients) > 0 && clients[0] != "" { + if len(clients) > 0 && clients[0] != "" && client.wallets[clients[0]] != nil { wallet = client.wallets[clients[0]] } else { wallet = client.wallet @@ -195,7 +195,7 @@ func IsWalletSet() bool { } func PublicKey(clients ...string) string { - if len(clients) > 0 && clients[0] != "" { + if len(clients) > 0 && clients[0] != "" && client.wallets[clients[0]] != nil { if client.wallets[clients[0]] == nil { fmt.Println("Public key is empty") return "" @@ -217,7 +217,7 @@ func PrivateKey() string { } func Id(clients ...string) string { - if len(clients) > 0 && clients[0] != "" { + if len(clients) > 0 && clients[0] != "" && client.wallets[clients[0]] != nil { if client.wallets[clients[0]] == nil { fmt.Println("Id is empty : ", clients[0]) return "" From 65b69f974c688c9cdb633cacf4d91013dcf64049 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 23 Oct 2024 20:50:18 +0530 Subject: [PATCH 261/319] Optional verify txn --- core/transaction/entity.go | 74 ++++++++++++++++-------------- zboxcore/sdk/blobber_operations.go | 8 ++-- zboxcore/sdk/sdk.go | 4 +- zcnbridge/bridge.go | 4 +- zcncore/execute_transactions.go | 38 +++++++-------- 5 files changed, 66 insertions(+), 62 deletions(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 256c2f3f0..e0a2d6d52 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -486,29 +486,29 @@ 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, clients ...string) ( +func SmartContractTxn(scAddress string, sn SmartContractTxnData, verifyTxn bool, clients ...string) ( hash, out string, nonce int64, txn *Transaction, err error) { - return SmartContractTxnValue(scAddress, sn, 0, clients...) + return SmartContractTxnValue(scAddress, sn, 0, verifyTxn, clients...) } -func SmartContractTxnValue(scAddress string, sn SmartContractTxnData, value uint64, clients ...string) ( +func SmartContractTxnValue(scAddress string, sn SmartContractTxnData, value uint64, verifyTxn bool, clients ...string) ( hash, out string, nonce int64, txn *Transaction, err error) { - return SmartContractTxnValueFeeWithRetry(scAddress, sn, value, client.TxnFee(), clients...) + return SmartContractTxnValueFeeWithRetry(scAddress, sn, value, client.TxnFee(), verifyTxn, clients...) } func SmartContractTxnValueFeeWithRetry(scAddress string, sn SmartContractTxnData, - 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...) + value, fee uint64, verifyTxn bool, clients ...string) (hash, out string, nonce int64, t *Transaction, err error) { + hash, out, nonce, t, err = SmartContractTxnValueFee(scAddress, sn, value, fee, verifyTxn, clients...) if err != nil && strings.Contains(err.Error(), "invalid transaction nonce") { - return SmartContractTxnValueFee(scAddress, sn, value, fee, clients...) + return SmartContractTxnValueFee(scAddress, sn, value, fee, verifyTxn, clients...) } return } func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, - value, fee uint64, clients ...string) (hash, out string, nonce int64, t *Transaction, err error) { + value, fee uint64, verifyTxn bool, clients ...string) (hash, out string, nonce int64, t *Transaction, err error) { clientId := client.Id() if len(clients) > 0 && clients[0] != "" { @@ -577,40 +577,44 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, return } - var ( - querySleepTime = time.Duration(cfg.QuerySleepTime) * time.Second - retries = 0 - ) + if verifyTxn { + var ( + querySleepTime = time.Duration(cfg.QuerySleepTime) * time.Second + retries = 0 + ) - sys.Sleep(querySleepTime) + sys.Sleep(querySleepTime) - for retries < cfg.MaxTxnQuery { - t, err = VerifyTransaction(txn.Hash) - if err == nil { - break + for retries < cfg.MaxTxnQuery { + t, err = VerifyTransaction(txn.Hash) + if err == nil { + break + } + retries++ + sys.Sleep(querySleepTime) } - retries++ - sys.Sleep(querySleepTime) - } - if err != nil { - Logger.Error("Error verifying the transaction", err.Error(), txn.Hash) - client.Cache.Evict(txn.ClientID) - return - } + if err != nil { + Logger.Error("Error verifying the transaction", err.Error(), txn.Hash) + client.Cache.Evict(txn.ClientID) + return + } - if t == nil { - return "", "", 0, txn, errors.New("transaction_validation_failed", - "Failed to get the transaction confirmation") - } + if t == nil { + return "", "", 0, txn, errors.New("transaction_validation_failed", + "Failed to get the transaction confirmation") + } - if t.Status == TxnFail { - return t.Hash, t.TransactionOutput, 0, t, errors.New("", t.TransactionOutput) - } + if t.Status == TxnFail { + return t.Hash, t.TransactionOutput, 0, t, errors.New("", t.TransactionOutput) + } + + if t.Status == TxnChargeableError { + return t.Hash, t.TransactionOutput, t.TransactionNonce, t, errors.New("", t.TransactionOutput) + } - if t.Status == TxnChargeableError { - return t.Hash, t.TransactionOutput, t.TransactionNonce, t, errors.New("", t.TransactionOutput) + return t.Hash, t.TransactionOutput, t.TransactionNonce, t, nil } - return t.Hash, t.TransactionOutput, t.TransactionNonce, t, nil + return txn.Hash, "", txn.TransactionNonce, txn, nil } diff --git a/zboxcore/sdk/blobber_operations.go b/zboxcore/sdk/blobber_operations.go index c207b775c..9f27ad8db 100644 --- a/zboxcore/sdk/blobber_operations.go +++ b/zboxcore/sdk/blobber_operations.go @@ -195,7 +195,7 @@ func StakePoolLock(providerType ProviderType, providerID string, value, fee uint return "", 0, errors.Newf("stake_pool_lock", "unsupported provider type: %v", providerType) } - hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, value, fee) + hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, value, fee, true) return } @@ -246,7 +246,7 @@ func StakePoolUnlock(providerType ProviderType, providerID string, fee uint64) ( } var out string - if _, out, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, 0, fee); err != nil { + if _, out, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, 0, fee, true); err != nil { return // an error } @@ -279,7 +279,7 @@ func WritePoolLock(allocID string, tokens, fee uint64) (hash string, nonce int64 InputArgs: &req, } - hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, tokens, fee) + hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, tokens, fee, true) return } @@ -302,6 +302,6 @@ func WritePoolUnlock(allocID string, fee uint64) (hash string, nonce int64, err Name: transaction.STORAGESC_WRITE_POOL_UNLOCK, InputArgs: &req, } - hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee) + hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee, true) return } diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index 51151bb4f..ffd625d1f 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -1112,7 +1112,7 @@ func CollectRewards(providerId string, providerType ProviderType) (string, int64 return "", 0, fmt.Errorf("collect rewards provider type %v not implimented", providerType) } - hash, _, n, _, err := transaction.SmartContractTxn(scAddress, sn) + hash, _, n, _, err := transaction.SmartContractTxn(scAddress, sn, true) return hash, n, err } @@ -1227,7 +1227,7 @@ func storageSmartContractTxnValue(sn transaction.SmartContractTxnData, value uin hash, out string, nonce int64, txn *transaction.Transaction, err error) { // Fee is set during sdk initialization. - return transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, value, client.TxnFee()) + return transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, value, client.TxnFee(), true) } func CommitToFabric(metaTxnData, fabricConfigJSON string) (string, error) { diff --git a/zcnbridge/bridge.go b/zcnbridge/bridge.go index e9e7e033d..a8c399df1 100644 --- a/zcnbridge/bridge.go +++ b/zcnbridge/bridge.go @@ -631,7 +631,7 @@ func (b *BridgeClient) MintZCN(payload *zcnsc.MintPayload) (string, error) { hash, _, _, _, err := coreTransaction.SmartContractTxn(wallet.ZCNSCSmartContractAddress, coreTransaction.SmartContractTxnData{ Name: wallet.MintFunc, InputArgs: payload, - }) + }, true) if err != nil { return "", errors.Wrap(err, fmt.Sprintf("failed to execute smart contract, hash = %s", hash)) @@ -663,7 +663,7 @@ func (b *BridgeClient) BurnZCN(amount uint64) (string, string, error) { hash, out, _, _, err := coreTransaction.SmartContractTxnValue(wallet.ZCNSCSmartContractAddress, coreTransaction.SmartContractTxnData{ Name: wallet.BurnFunc, InputArgs: payload, - }, amount) + }, true, amount) if err != nil { Logger.Error("Burn ZCN transaction FAILED", zap.Error(err)) return hash, out, errors.Wrap(err, fmt.Sprintf("failed to execute smart contract, hash = %s", hash)) diff --git a/zcncore/execute_transactions.go b/zcncore/execute_transactions.go index bac4b4909..45c49a5ad 100644 --- a/zcncore/execute_transactions.go +++ b/zcncore/execute_transactions.go @@ -39,7 +39,7 @@ func MinerSCLock(providerId string, providerType Provider, lock uint64, client . ProviderID: providerId, ProviderType: providerType, }, - }, lock, client...) + }, lock, true, client...) } @@ -50,7 +50,7 @@ func MinerSCUnlock(providerId string, providerType Provider, client ...string) ( ProviderID: providerId, ProviderType: providerType, }, - }, client...) + }, true, client...) } func MinerSCCollectReward(providerId string, providerType Provider, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { @@ -60,7 +60,7 @@ func MinerSCCollectReward(providerId string, providerType Provider, client ...st ProviderId: providerId, ProviderType: int(providerType), }, - }, client...) + }, true, client...) } @@ -82,7 +82,7 @@ func MinerSCKill(providerId string, providerType Provider, client ...string) (ha return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: name, InputArgs: pr, - }, client...) + }, true, client...) } @@ -93,7 +93,7 @@ func StorageSCCollectReward(providerId string, providerType Provider, client ... ProviderId: providerId, ProviderType: int(providerType), }, - }, client...) + }, true, client...) } @@ -101,7 +101,7 @@ func MinerScUpdateConfig(input interface{}, client ...string) (hash, out string, return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_UPDATE_SETTINGS, InputArgs: input, - }, client...) + }, true, client...) } @@ -109,7 +109,7 @@ func MinerScUpdateGlobals(input interface{}, client ...string) (hash, out string return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_UPDATE_GLOBALS, InputArgs: input, - }, client...) + }, true, client...) } @@ -117,7 +117,7 @@ func StorageScUpdateConfig(input interface{}, client ...string) (hash, out strin return transaction.SmartContractTxn(StorageSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.STORAGESC_UPDATE_SETTINGS, InputArgs: input, - }, client...) + }, true, client...) } @@ -125,7 +125,7 @@ func AddHardfork(input interface{}, client ...string) (hash, out string, nonce i return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ADD_HARDFORK, InputArgs: input, - }, client...) + }, true, client...) } @@ -133,7 +133,7 @@ func ZCNSCUpdateGlobalConfig(input *InputMap, client ...string) (hash, out strin return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_UPDATE_GLOBAL_CONFIG, InputArgs: input, - }, client...) + }, true, client...) } @@ -141,7 +141,7 @@ func MinerSCMinerSettings(input *MinerSCMinerInfo, client ...string) (hash, out return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_MINER_SETTINGS, InputArgs: input, - }, client...) + }, true, client...) } @@ -149,7 +149,7 @@ func MinerSCSharderSettings(input *MinerSCMinerInfo, client ...string) (hash, ou return transaction.SmartContractTxn(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.MINERSC_SHARDER_SETTINGS, InputArgs: input, - }, client...) + }, true, client...) } @@ -157,7 +157,7 @@ func ZCNSCUpdateAuthorizerConfig(input *AuthorizerNode, client ...string) (hash, return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_UPDATE_AUTHORIZER_CONFIG, InputArgs: input, - }, client...) + }, true, client...) } @@ -165,7 +165,7 @@ func ZCNSCAddAuthorizer(input *AddAuthorizerPayload, client ...string) (hash, ou return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_ADD_AUTHORIZER, InputArgs: input, - }, client...) + }, true, client...) } @@ -173,7 +173,7 @@ func ZCNSCAuthorizerHealthCheck(input *AuthorizerHealthCheckPayload, client ...s return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_AUTHORIZER_HEALTH_CHECK, InputArgs: input, - }, client...) + }, true, client...) } @@ -181,7 +181,7 @@ func ZCNSCDeleteAuthorizer(input *DeleteAuthorizerPayload, client ...string) (ha return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_DELETE_AUTHORIZER, InputArgs: input, - }, client...) + }, true, client...) } @@ -194,7 +194,7 @@ func ZCNSCCollectReward(providerId string, providerType Provider, client ...stri return transaction.SmartContractTxn(ZCNSCSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.ZCNSC_COLLECT_REWARD, InputArgs: pr, - }, client...) + }, true, client...) } type SendTxnData struct { @@ -211,12 +211,12 @@ func Send(toClientID string, tokens uint64, desc string, client ...string) (hash return transaction.SmartContractTxnValue(MinerSmartContractAddress, transaction.SmartContractTxnData{ Name: "transfer", InputArgs: SendTxnData{Note: desc}, - }, tokens, client...) + }, tokens, true, client...) } func Faucet(tokens uint64, input string, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxnValue(FaucetSmartContractAddress, transaction.SmartContractTxnData{ Name: "pour", InputArgs: input, - }, tokens, client...) + }, tokens, true, client...) } From 97c7405e693679c3cd8db750aea824b8b9bc314a Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 23 Oct 2024 21:34:18 +0530 Subject: [PATCH 262/319] Fix --- zcnbridge/bridge.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zcnbridge/bridge.go b/zcnbridge/bridge.go index a8c399df1..fd3f0eeed 100644 --- a/zcnbridge/bridge.go +++ b/zcnbridge/bridge.go @@ -663,7 +663,7 @@ func (b *BridgeClient) BurnZCN(amount uint64) (string, string, error) { hash, out, _, _, err := coreTransaction.SmartContractTxnValue(wallet.ZCNSCSmartContractAddress, coreTransaction.SmartContractTxnData{ Name: wallet.BurnFunc, InputArgs: payload, - }, true, amount) + }, amount, true) if err != nil { Logger.Error("Burn ZCN transaction FAILED", zap.Error(err)) return hash, out, errors.Wrap(err, fmt.Sprintf("failed to execute smart contract, hash = %s", hash)) From c58af319493b477456d8afde64789911faf50a11 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 23 Oct 2024 23:46:05 +0530 Subject: [PATCH 263/319] Log --- core/client/cache.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/client/cache.go b/core/client/cache.go index 5652a3cdb..cfeb3566d 100644 --- a/core/client/cache.go +++ b/core/client/cache.go @@ -1,6 +1,7 @@ package client import ( + "github.com/0chain/gosdk/core/logger" "sync" ) @@ -38,6 +39,9 @@ func (nc *NonceCache) GetNextNonce(clientId string) int64 { } nc.cache[clientId] += 1 + + logger.Log.Info("GetNextNonce", "clientId", clientId, "nonce", nc.cache[clientId]) + return nc.cache[clientId] } From 8d5ec2f500678b9d1a838ca83c5a152aa1927b18 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 24 Oct 2024 01:00:03 +0530 Subject: [PATCH 264/319] Retry --- core/transaction/entity.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index e0a2d6d52..ac63ef731 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -501,7 +501,7 @@ func SmartContractTxnValueFeeWithRetry(scAddress string, sn SmartContractTxnData value, fee uint64, verifyTxn bool, clients ...string) (hash, out string, nonce int64, t *Transaction, err error) { hash, out, nonce, t, err = SmartContractTxnValueFee(scAddress, sn, value, fee, verifyTxn, clients...) - if err != nil && strings.Contains(err.Error(), "invalid transaction nonce") { + if err != nil && (strings.Contains(err.Error(), "invalid transaction nonce") || strings.Contains(err.Error(), "invalid future transaction")) { return SmartContractTxnValueFee(scAddress, sn, value, fee, verifyTxn, clients...) } return From 2eea1d629637f93def5977ddfa4fe66178b4c25f Mon Sep 17 00:00:00 2001 From: dabasov Date: Thu, 24 Oct 2024 16:05:30 +0300 Subject: [PATCH 265/319] updated common --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 35a83cc5d..9f3034e56 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/0chain/gosdk go 1.22.0 require ( - github.com/0chain/common v1.18.0 + github.com/0chain/common v1.18.1 github.com/0chain/errors v1.0.3 github.com/Luzifer/go-openssl/v3 v3.1.0 github.com/btcsuite/btcd v0.23.4 diff --git a/go.sum b/go.sum index d29ed5c80..7dcd8d7c3 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/0chain/common v1.18.0 h1:vCkD869DR0zTZS4CDl5MMN3qixxyfTNxDDcHp7cTRrc= -github.com/0chain/common v1.18.0/go.mod h1:vHK9Q0/nz28PxmKsLSMLzEs5iK6QVgl1LxdDQdR7y6s= +github.com/0chain/common v1.18.1 h1:QKXdEjK6SB3SqzDS/XbK74jy20d3tc+6PA8J8dFpuGw= +github.com/0chain/common v1.18.1/go.mod h1:Lapu2Tj7z5Sm4r+X141e7vsz4NDODTEypeElYAP3iSw= github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM= github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= From bd8828335b4dee5c81f65fcf5db0728999b64930 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Sat, 26 Oct 2024 18:41:21 +0530 Subject: [PATCH 266/319] increase rollback retries --- zboxcore/sdk/rollback.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/zboxcore/sdk/rollback.go b/zboxcore/sdk/rollback.go index 3bdffd5df..4a4ace12a 100644 --- a/zboxcore/sdk/rollback.go +++ b/zboxcore/sdk/rollback.go @@ -191,7 +191,7 @@ func (rb *RollbackBlobber) processRollback(ctx context.Context, tx string) error shouldContinue bool ) - for retries := 0; retries < 3; retries++ { + for retries := 0; retries < 6; retries++ { err, shouldContinue = func() (err error, shouldContinue bool) { reqCtx, ctxCncl := context.WithTimeout(ctx, DefaultUploadTimeOut) resp, err := zboxutil.Client.Do(req.WithContext(reqCtx)) @@ -251,13 +251,13 @@ func (rb *RollbackBlobber) processRollback(ctx context.Context, tx string) error return }() + if shouldContinue && retries < 5 { + continue + } if err != nil { l.Logger.Error(err) return err } - if shouldContinue { - continue - } rb.blobber.LatestWM = wm rb.blobber.AllocationRoot = wm.AllocationRoot return nil From 875f6c139b5b60788f1c9cbf8f2eb88f345054e5 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Sun, 27 Oct 2024 12:41:23 +0530 Subject: [PATCH 267/319] log base url --- zboxcore/sdk/chunked_upload_blobber.go | 2 +- zboxcore/sdk/chunked_upload_process_js.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/zboxcore/sdk/chunked_upload_blobber.go b/zboxcore/sdk/chunked_upload_blobber.go index af87cb0ee..8a60737b2 100644 --- a/zboxcore/sdk/chunked_upload_blobber.go +++ b/zboxcore/sdk/chunked_upload_blobber.go @@ -92,7 +92,7 @@ func (sb *ChunkedUploadBlobber) sendUploadRequest( err = zboxutil.FastHttpClient.DoTimeout(req, resp, su.uploadTimeOut) fasthttp.ReleaseRequest(req) if err != nil { - logger.Logger.Error("Upload : ", err) + logger.Logger.Error("Upload : ", err, " baseurl: ", sb.blobber.Baseurl) if errors.Is(err, fasthttp.ErrConnectionClosed) || errors.Is(err, syscall.EPIPE) { return err, true } diff --git a/zboxcore/sdk/chunked_upload_process_js.go b/zboxcore/sdk/chunked_upload_process_js.go index c7b874e7a..1e22f867b 100644 --- a/zboxcore/sdk/chunked_upload_process_js.go +++ b/zboxcore/sdk/chunked_upload_process_js.go @@ -634,7 +634,7 @@ func sendUploadRequest(dataBuffers []*bytes.Buffer, contentSlice []string, blobb err = zboxutil.FastHttpClient.DoTimeout(req, resp, DefaultUploadTimeOut) fasthttp.ReleaseRequest(req) if err != nil { - logger.Logger.Error("Upload : ", err) + logger.Logger.Error("Upload : ", err, " baseURL ", blobberURL) if errors.Is(err, fasthttp.ErrConnectionClosed) || errors.Is(err, syscall.EPIPE) || errors.Is(err, fasthttp.ErrDialTimeout) { err = ErrNetwork return err, true From 224d6522c4e65eae2c78f18d55f2f2063c0cf2a0 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Sun, 27 Oct 2024 14:20:22 +0530 Subject: [PATCH 268/319] add session key --- core/client/set.go | 13 ++- core/sys/vars.go | 2 + zboxcore/fileref/fileref.go | 1 + zboxcore/sdk/allocation.go | 38 ++++++++- zboxcore/sdk/chunked_upload.go | 2 +- zboxcore/sdk/chunked_upload_form_builder.go | 51 +++++++++--- .../chunked_upload_form_builder_bench_test.go | 2 +- zboxcore/sdk/chunked_upload_model.go | 16 ++-- zboxcore/sdk/chunked_upload_process_js.go | 5 +- zboxcore/sdk/downloadworker.go | 82 ++++++++++++------- 10 files changed, 155 insertions(+), 57 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index 94bab79ca..d87931c22 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -5,9 +5,10 @@ import ( "encoding/json" "errors" "fmt" - "github.com/0chain/gosdk/core/conf" "strings" + "github.com/0chain/gosdk/core/conf" + "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/core/zcncrypto" @@ -63,6 +64,7 @@ func init() { sys.Verify = verifySignature sys.VerifyWith = verifySignatureWith + sys.VerifyEd25519With = verifyEd25519With } func signHash(hash string, signatureScheme string, keys []sys.KeyPair) (string, error) { @@ -104,6 +106,15 @@ func verifySignatureWith(pubKey, signature, hash string) (bool, error) { return sch.Verify(signature, hash) } +func verifyEd25519With(pubKey, signature, hash string) (bool, error) { + sch := zcncrypto.NewSignatureScheme(constants.ED25519.String()) + err := sch.SetPublicKey(pubKey) + if err != nil { + return false, err + } + return sch.Verify(signature, hash) +} + func GetClientSysKeys(clients ...string) []sys.KeyPair { var wallet *zcncrypto.Wallet if len(clients) > 0 && clients[0] != "" && client.wallets[clients[0]] != nil { diff --git a/core/sys/vars.go b/core/sys/vars.go index b1b53876e..f1f0fde5c 100644 --- a/core/sys/vars.go +++ b/core/sys/vars.go @@ -26,6 +26,8 @@ var ( Authorize AuthorizeFunc AuthCommon AuthorizeFunc + + VerifyEd25519With VerifyWithFunc ) // SetAuthorize sets the authorize callback function diff --git a/zboxcore/fileref/fileref.go b/zboxcore/fileref/fileref.go index 2fafdd1ed..0c51a19da 100644 --- a/zboxcore/fileref/fileref.go +++ b/zboxcore/fileref/fileref.go @@ -51,6 +51,7 @@ type FileRef struct { EncryptedKey string `json:"encrypted_key" mapstructure:"encrypted_key"` EncryptedKeyPoint string `json:"encrypted_key_point" mapstructure:"encrypted_key_point"` Collaborators []Collaborator `json:"collaborators" mapstructure:"collaborators"` + SignatureVersion int `json:"signature_version" mapstructure:"signature_version"` } func (fRef *FileRef) MetaID() string { diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index dd10bb750..ef02f8b81 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -3,11 +3,11 @@ package sdk import ( "bytes" "context" + "crypto/ed25519" "encoding/base64" + "encoding/hex" "encoding/json" "fmt" - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/transaction" "io" "math" "mime/multipart" @@ -22,6 +22,10 @@ import ( "sync/atomic" "time" + "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/encryption" + "github.com/0chain/gosdk/core/transaction" + "github.com/0chain/common/core/currency" "github.com/0chain/errors" thrown "github.com/0chain/errors" @@ -276,6 +280,9 @@ type Allocation struct { StorageVersion int `json:"storage_version"` + // Owner ecdsa public key + OwnerSigningPublicKey string `json:"owner_signing_public_key"` + // FileOptions to define file restrictions on an allocation for third-parties // default 00000000 for all crud operations suggesting only owner has the below listed abilities. // enabling option/s allows any third party to perform certain ops @@ -303,8 +310,9 @@ type Allocation struct { // conseususes consensusThreshold int fullconsensus int - sig string `json:"-"` - allocationRoot string `json:"-"` + sig string `json:"-"` + allocationRoot string `json:"-"` + privateSigningKey ed25519.PrivateKey `json:"-"` } // OperationRequest represents an operation request with its related options. @@ -429,6 +437,7 @@ func (a *Allocation) InitAllocation() { } } } + a.generateAndSetOwnerSigningPublicKey() a.startWorker(a.ctx) InitCommitWorker(a.Blobbers) InitBlockDownloader(a.Blobbers, downloadWorkerCount) @@ -438,6 +447,25 @@ func (a *Allocation) InitAllocation() { a.initialized = true } +func (a *Allocation) generateAndSetOwnerSigningPublicKey() { + //create ecdsa public key from signature + hashData := fmt.Sprintf("%s:%s:%s", a.ID, a.Owner, "owner_signing_public_key") + sig, err := client.Sign(encryption.Hash(hashData), a.Owner) + if err != nil { + logger.Logger.Error("error during sign", zap.Error(err)) + return + } + //use this signature as entropy to generate ecdsa key pair + decodedSig, _ := hex.DecodeString(sig) + privateSigningKey := ed25519.NewKeyFromSeed(decodedSig[:32]) + if a.OwnerSigningPublicKey == "" { + pubKey := privateSigningKey.Public().(ed25519.PublicKey) + a.OwnerSigningPublicKey = hex.EncodeToString(pubKey) + //TODO: save this public key to blockchain + } + a.privateSigningKey = privateSigningKey +} + func (a *Allocation) isInitialized() bool { return a.initialized && client.IsSDKInitialized() } @@ -1339,6 +1367,7 @@ func (a *Allocation) generateDownloadRequest( downloadReq.allocOwnerID = a.Owner downloadReq.sig = a.sig downloadReq.allocOwnerPubKey = a.OwnerPublicKey + downloadReq.allocOwnerSigningPubKey = a.OwnerSigningPublicKey downloadReq.ctx, downloadReq.ctxCncl = context.WithCancel(a.ctx) downloadReq.fileHandler = fileHandler downloadReq.localFilePath = localFilePath @@ -2772,6 +2801,7 @@ func (a *Allocation) downloadFromAuthTicket(fileHandler sys.File, authTicket str downloadReq.sig = a.sig downloadReq.allocOwnerID = a.Owner downloadReq.allocOwnerPubKey = a.OwnerPublicKey + downloadReq.allocOwnerSigningPubKey = a.OwnerSigningPublicKey downloadReq.ctx, downloadReq.ctxCncl = context.WithCancel(a.ctx) downloadReq.fileHandler = fileHandler downloadReq.localFilePath = localFilePath diff --git a/zboxcore/sdk/chunked_upload.go b/zboxcore/sdk/chunked_upload.go index 8eeee45b6..c91c04e68 100644 --- a/zboxcore/sdk/chunked_upload.go +++ b/zboxcore/sdk/chunked_upload.go @@ -292,7 +292,7 @@ func CreateChunkedUpload( su.chunkReader = cReader - su.formBuilder = CreateChunkedUploadFormBuilder(su.allocationObj.StorageVersion) + su.formBuilder = CreateChunkedUploadFormBuilder(su.allocationObj.StorageVersion, su.allocationObj.privateSigningKey) su.isRepair = isRepair uploadWorker, uploadRequest := calculateWorkersAndRequests(su.allocationObj.DataShards, len(su.blobbers), su.chunkNumber) diff --git a/zboxcore/sdk/chunked_upload_form_builder.go b/zboxcore/sdk/chunked_upload_form_builder.go index 51c3711ad..794fb5b68 100644 --- a/zboxcore/sdk/chunked_upload_form_builder.go +++ b/zboxcore/sdk/chunked_upload_form_builder.go @@ -2,6 +2,8 @@ package sdk import ( "bytes" + "crypto" + "crypto/ed25519" "encoding/hex" "encoding/json" "fmt" @@ -9,8 +11,8 @@ import ( "mime/multipart" "sync" - "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/encryption" "golang.org/x/crypto/sha3" ) @@ -37,14 +39,16 @@ type ChunkedUploadFormMetadata struct { } // CreateChunkedUploadFormBuilder create ChunkedUploadFormBuilder instance -func CreateChunkedUploadFormBuilder(storageVersion int) ChunkedUploadFormBuilder { +func CreateChunkedUploadFormBuilder(storageVersion int, privateSigningKey ed25519.PrivateKey) ChunkedUploadFormBuilder { return &chunkedUploadFormBuilder{ storageVersion, + privateSigningKey, } } type chunkedUploadFormBuilder struct { - storageVersion int + storageVersion int + privateSigningKey ed25519.PrivateKey } const MAX_BLOCKS = 80 // 5MB(CHUNK_SIZE*80) @@ -96,6 +100,10 @@ func (b *chunkedUploadFormBuilder) Build( CustomMeta: fileMeta.CustomMeta, } + if b.privateSigningKey != nil { + formData.SignatureVersion = SignatureV2 + } + for i := 0; i < numBodies; i++ { startRange := i * MAX_BLOCKS @@ -168,25 +176,42 @@ func (b *chunkedUploadFormBuilder) Build( for err := range errChan { return res, err } - actualHashSignature, err := client.Sign(fileMeta.ActualHash) - if err != nil { - return res, err + if b.privateSigningKey != nil { + decodedHash, _ := hex.DecodeString(fileMeta.ActualHash) + sig, err := b.privateSigningKey.Sign(nil, decodedHash, crypto.Hash(0)) + if err != nil { + return res, err + } + formData.ActualFileHashSignature = hex.EncodeToString(sig) + } else { + sig, err := client.Sign(fileMeta.ActualHash) + if err != nil { + return res, err + } + formData.ActualFileHashSignature = sig } - hash := actualHashSignature + formData.ValidationRoot + hash := formData.ActualFileHashSignature + formData.ValidationRoot if b.storageVersion == StorageV2 { hashData := fmt.Sprintf("%s:%s:%s:%s", fileMeta.ActualHash, formData.ValidationRoot, formData.FixedMerkleRoot, blobberID) hash = encryption.Hash(hashData) } - validationRootSignature, err := client.Sign(hash) - if err != nil { - return res, err + if b.privateSigningKey != nil { + decodedHash, _ := hex.DecodeString(hash) + sig, err := b.privateSigningKey.Sign(nil, decodedHash, crypto.Hash(0)) + if err != nil { + return res, err + } + formData.ValidationRootSignature = hex.EncodeToString(sig) + } else { + rootSig, err := client.Sign(hash) + if err != nil { + return res, err + } + formData.ValidationRootSignature = rootSig } formData.ActualHash = fileMeta.ActualHash - formData.ActualFileHashSignature = actualHashSignature - formData.ValidationRootSignature = validationRootSignature formData.ActualSize = fileMeta.ActualSize - } thumbnailSize := len(thumbnailChunkData) diff --git a/zboxcore/sdk/chunked_upload_form_builder_bench_test.go b/zboxcore/sdk/chunked_upload_form_builder_bench_test.go index 156910efe..eab4164ad 100644 --- a/zboxcore/sdk/chunked_upload_form_builder_bench_test.go +++ b/zboxcore/sdk/chunked_upload_form_builder_bench_test.go @@ -53,7 +53,7 @@ func BenchmarkChunkedUploadFormBuilder(b *testing.B) { for i := 0; i < b.N; i++ { - builder := CreateChunkedUploadFormBuilder(0) + builder := CreateChunkedUploadFormBuilder(0, nil) isFinal := false diff --git a/zboxcore/sdk/chunked_upload_model.go b/zboxcore/sdk/chunked_upload_model.go index 0eb5b9724..07474366c 100644 --- a/zboxcore/sdk/chunked_upload_model.go +++ b/zboxcore/sdk/chunked_upload_model.go @@ -20,6 +20,8 @@ import ( "golang.org/x/crypto/sha3" ) +const SignatureV2 = 1 + // ChunkedUpload upload manager with chunked upload feature type ChunkedUpload struct { consensus Consensus @@ -164,13 +166,13 @@ type UploadFormData struct { EncryptedKey string `json:"encrypted_key,omitempty"` EncryptedKeyPoint string `json:"encrypted_key_point,omitempty"` - IsFinal bool `json:"is_final,omitempty"` // all of chunks are uploaded - ChunkStartIndex int `json:"chunk_start_index,omitempty"` // start index of chunks. - ChunkEndIndex int `json:"chunk_end_index,omitempty"` // end index of chunks. all chunks MUST be uploaded one by one because of streaming merkle hash - ChunkSize int64 `json:"chunk_size,omitempty"` // the size of a chunk. 64*1024 is default - UploadOffset int64 `json:"upload_offset,omitempty"` // It is next position that new incoming chunk should be append to - Size int64 `json:"size"` // total size of shard - + IsFinal bool `json:"is_final,omitempty"` // all of chunks are uploaded + ChunkStartIndex int `json:"chunk_start_index,omitempty"` // start index of chunks. + ChunkEndIndex int `json:"chunk_end_index,omitempty"` // end index of chunks. all chunks MUST be uploaded one by one because of streaming merkle hash + ChunkSize int64 `json:"chunk_size,omitempty"` // the size of a chunk. 64*1024 is default + UploadOffset int64 `json:"upload_offset,omitempty"` // It is next position that new incoming chunk should be append to + Size int64 `json:"size"` // total size of shard + SignatureVersion int `json:"signature_version,omitempty"` } // UploadProgress progress of upload diff --git a/zboxcore/sdk/chunked_upload_process_js.go b/zboxcore/sdk/chunked_upload_process_js.go index c7b874e7a..64724b1fa 100644 --- a/zboxcore/sdk/chunked_upload_process_js.go +++ b/zboxcore/sdk/chunked_upload_process_js.go @@ -6,6 +6,7 @@ package sdk import ( "bytes" "context" + "crypto/ed25519" "encoding/json" "fmt" "net/http" @@ -54,6 +55,7 @@ type ChunkedUploadFormInfo struct { AllocationTx string OnlyHash bool StorageVersion int + PrivateSigningKey ed25519.PrivateKey } // createUploadProgress create a new UploadProgress @@ -118,6 +120,7 @@ func (su *ChunkedUpload) processUpload(chunkStartIndex, chunkEndIndex int, AllocationTx: su.allocationObj.Tx, OnlyHash: chunkEndIndex <= su.progress.ChunkIndex, StorageVersion: su.allocationObj.StorageVersion, + PrivateSigningKey: su.allocationObj.privateSigningKey, } formInfoJSON, err := json.Marshal(formInfo) if err != nil { @@ -455,7 +458,7 @@ func ProcessEventData(data safejs.Value) { defer delete(hasherMap, fileMeta.RemotePath) } blobberID := os.Getenv("BLOBBER_ID") - formBuilder := CreateChunkedUploadFormBuilder(formInfo.StorageVersion) + formBuilder := CreateChunkedUploadFormBuilder(formInfo.StorageVersion, formInfo.PrivateSigningKey) uploadData, err := formBuilder.Build(fileMeta, wp.hasher, formInfo.ConnectionID, blobberID, formInfo.ChunkSize, formInfo.ChunkStartIndex, formInfo.ChunkEndIndex, formInfo.IsFinal, formInfo.EncryptedKey, formInfo.EncryptedKeyPoint, fileShards, thumbnailChunkData, formInfo.ShardSize) if err != nil { diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index 63a00b34f..2503c5225 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -96,26 +96,27 @@ type DownloadRequest struct { fileCallback func() contentMode string Consensus - effectiveBlockSize int // blocksize - encryptionOverHead - ecEncoder reedsolomon.Encoder - maskMu *sync.Mutex - encScheme encryption.EncryptionScheme - shouldVerify bool - blocksPerShard int64 - connectionID string - skip bool - freeRead bool - fRef *fileref.FileRef - chunksPerShard int64 - size int64 - offset int64 - bufferMap map[int]zboxutil.DownloadBuffer - downloadStorer DownloadProgressStorer - workdir string - downloadQueue downloadQueue // Always initialize this queue with max time taken - isResume bool - isEnterprise bool - storageVersion int + effectiveBlockSize int // blocksize - encryptionOverHead + ecEncoder reedsolomon.Encoder + maskMu *sync.Mutex + encScheme encryption.EncryptionScheme + shouldVerify bool + blocksPerShard int64 + connectionID string + skip bool + freeRead bool + fRef *fileref.FileRef + chunksPerShard int64 + size int64 + offset int64 + bufferMap map[int]zboxutil.DownloadBuffer + downloadStorer DownloadProgressStorer + workdir string + downloadQueue downloadQueue // Always initialize this queue with max time taken + isResume bool + isEnterprise bool + storageVersion int + allocOwnerSigningPubKey string } type downloadPriority struct { @@ -1164,12 +1165,23 @@ func (req *DownloadRequest) getFileMetaConsensus(fMetaResp []*fileMetaResponse) } actualHash := fmr.fileref.ActualFileHash actualFileHashSignature := fmr.fileref.ActualFileHashSignature - - isValid, err := sys.VerifyWith( - req.allocOwnerPubKey, - actualFileHashSignature, - actualHash, + var ( + isValid bool + err error ) + if fmr.fileref.SignatureVersion == SignatureV2 { + isValid, err = sys.VerifyEd25519With( + req.allocOwnerSigningPubKey, + actualFileHashSignature, + actualHash, + ) + } else { + isValid, err = sys.VerifyWith( + req.allocOwnerPubKey, + actualFileHashSignature, + actualHash, + ) + } if err != nil { l.Logger.Error(err) continue @@ -1219,11 +1231,23 @@ func (req *DownloadRequest) getFileMetaConsensus(fMetaResp []*fileMetaResponse) hashData := fmt.Sprintf("%s:%s:%s:%s", fRef.ActualFileHash, fRef.ValidationRoot, fRef.FixedMerkleRoot, req.blobbers[i].ID) hash = encrypt.Hash(hashData) } - isValid, err := sys.VerifyWith( - req.allocOwnerPubKey, - fRef.ValidationRootSignature, - hash, + var ( + isValid bool + err error ) + if fRef.SignatureVersion == SignatureV2 { + isValid, err = sys.VerifyEd25519With( + req.allocOwnerSigningPubKey, + fRef.ValidationRootSignature, + hash, + ) + } else { + isValid, err = sys.VerifyWith( + req.allocOwnerPubKey, + fRef.ValidationRootSignature, + hash, + ) + } if err != nil { l.Logger.Error(err, "allocOwnerPubKey: ", req.allocOwnerPubKey, " validationRootSignature: ", fRef.ValidationRootSignature, " actualFileHashSignature: ", fRef.ActualFileHashSignature, " validationRoot: ", fRef.ValidationRoot) continue From c1e68802b6f3e83eaa3da60607adc37c5bb388f8 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Mon, 28 Oct 2024 12:54:27 +0530 Subject: [PATCH 269/319] same session key for wallet --- zboxcore/sdk/allocation.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index ef02f8b81..21aa1ecca 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -449,7 +449,7 @@ func (a *Allocation) InitAllocation() { func (a *Allocation) generateAndSetOwnerSigningPublicKey() { //create ecdsa public key from signature - hashData := fmt.Sprintf("%s:%s:%s", a.ID, a.Owner, "owner_signing_public_key") + hashData := fmt.Sprintf("%s:%s", a.OwnerPublicKey, "owner_signing_public_key") sig, err := client.Sign(encryption.Hash(hashData), a.Owner) if err != nil { logger.Logger.Error("error during sign", zap.Error(err)) From eccb82c2d76c75273bcbd5ac2fe4408b0ff2c867 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Mon, 28 Oct 2024 13:25:05 +0530 Subject: [PATCH 270/319] add pk in create allocation request --- zboxcore/sdk/allocation.go | 9 ++------ zboxcore/sdk/blobber_operations.go | 33 +++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 21aa1ecca..7b2b345de 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -23,7 +23,6 @@ import ( "time" "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/transaction" "github.com/0chain/common/core/currency" @@ -449,15 +448,11 @@ func (a *Allocation) InitAllocation() { func (a *Allocation) generateAndSetOwnerSigningPublicKey() { //create ecdsa public key from signature - hashData := fmt.Sprintf("%s:%s", a.OwnerPublicKey, "owner_signing_public_key") - sig, err := client.Sign(encryption.Hash(hashData), a.Owner) + privateSigningKey, err := generateOwnerSigningKey(a.OwnerPublicKey, a.Owner) if err != nil { - logger.Logger.Error("error during sign", zap.Error(err)) + l.Logger.Error("Failed to generate owner signing key", zap.Error(err)) return } - //use this signature as entropy to generate ecdsa key pair - decodedSig, _ := hex.DecodeString(sig) - privateSigningKey := ed25519.NewKeyFromSeed(decodedSig[:32]) if a.OwnerSigningPublicKey == "" { pubKey := privateSigningKey.Public().(ed25519.PublicKey) a.OwnerSigningPublicKey = hex.EncodeToString(pubKey) diff --git a/zboxcore/sdk/blobber_operations.go b/zboxcore/sdk/blobber_operations.go index 9f27ad8db..525ec58d4 100644 --- a/zboxcore/sdk/blobber_operations.go +++ b/zboxcore/sdk/blobber_operations.go @@ -1,11 +1,18 @@ package sdk import ( + "crypto/ed25519" + "encoding/hex" "encoding/json" + "fmt" + "math" + "github.com/0chain/errors" "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/transaction" - "math" + "github.com/0chain/gosdk/zboxcore/logger" + "go.uber.org/zap" ) // CreateAllocationForOwner creates a new allocation with the given options (txn: `storagesc.new_allocation_request`). @@ -48,12 +55,20 @@ func CreateAllocationForOwner( return "", 0, nil, sdkNotInitialized } + privateSigningKey, err := generateOwnerSigningKey(ownerpublickey, owner) + if err != nil { + return "", 0, nil, errors.New("failed_generate_owner_signing_key", "failed to generate owner signing key: "+err.Error()) + } + pub := privateSigningKey.Public().(ed25519.PublicKey) + pk := hex.EncodeToString(pub) + allocationRequest["owner_id"] = owner allocationRequest["owner_public_key"] = ownerpublickey allocationRequest["third_party_extendable"] = thirdPartyExtendable allocationRequest["file_options_changed"], allocationRequest["file_options"] = calculateAllocationFileOptions(63 /*0011 1111*/, fileOptionsParams) allocationRequest["is_enterprise"] = IsEnterprise allocationRequest["storage_version"] = StorageV2 + allocationRequest["owner_signing_public_key"] = pk var sn = transaction.SmartContractTxnData{ Name: transaction.NEW_ALLOCATION_REQUEST, @@ -305,3 +320,19 @@ func WritePoolUnlock(allocID string, fee uint64) (hash string, nonce int64, err hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee, true) return } + +func generateOwnerSigningKey(ownerPublicKey, ownerID string) (ed25519.PrivateKey, error) { + if ownerPublicKey == "" { + return nil, errors.New("owner_public_key_required", "owner public key is required") + } + hashData := fmt.Sprintf("%s:%s", ownerPublicKey, "owner_signing_public_key") + sig, err := client.Sign(encryption.Hash(hashData), ownerID) + if err != nil { + logger.Logger.Error("error during sign", zap.Error(err)) + return nil, err + } + //use this signature as entropy to generate ecdsa key pair + decodedSig, _ := hex.DecodeString(sig) + privateSigningKey := ed25519.NewKeyFromSeed(decodedSig[:32]) + return privateSigningKey, nil +} From f039417746fdbf30201c86fdb8f9c505490e0705 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Mon, 28 Oct 2024 21:22:20 +0530 Subject: [PATCH 271/319] Added owner signin public key --- mobilesdk/sdk/sdk.go | 2 +- wasmsdk/allocation.go | 14 ++++++++------ zboxcore/sdk/allocation.go | 14 ++++++++++---- zboxcore/sdk/blobber_operations.go | 3 ++- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/mobilesdk/sdk/sdk.go b/mobilesdk/sdk/sdk.go index e20147348..3fab257b4 100644 --- a/mobilesdk/sdk/sdk.go +++ b/mobilesdk/sdk/sdk.go @@ -368,7 +368,7 @@ func (s *StorageSDK) UpdateAllocation(size int64, extend bool, allocationID stri return "", errors.Errorf("int64 overflow in lock") } - hash, _, err = sdk.UpdateAllocation(size, extend, allocationID, lock, "", "", "", false, &sdk.FileOptionsParameters{}) + hash, _, err = sdk.UpdateAllocation(size, extend, allocationID, lock, "", "", "", "", false, &sdk.FileOptionsParameters{}) return hash, err } diff --git a/wasmsdk/allocation.go b/wasmsdk/allocation.go index f36b60888..5ee6458c6 100644 --- a/wasmsdk/allocation.go +++ b/wasmsdk/allocation.go @@ -168,7 +168,8 @@ func UpdateForbidAllocation(allocationID string, forbidupload, forbiddelete, for "", //addBlobberId, "", //addBlobberAuthTicket "", //removeBlobberId, - false, //thirdPartyExtendable, + "", //thirdPartyExtendable, + false, // ownerSigninPublicKey &sdk.FileOptionsParameters{ ForbidUpload: sdk.FileOptionParam{Changed: forbidupload, Value: forbidupload}, ForbidDelete: sdk.FileOptionParam{Changed: forbiddelete, Value: forbiddelete}, @@ -196,7 +197,8 @@ func freezeAllocation(allocationID string) (string, error) { "", //addBlobberId, "", //addBlobberAuthTicket "", //removeBlobberId, - false, //thirdPartyExtendable, + "", //thirdPartyExtendable, + false, // ownerSigninPublicKey &sdk.FileOptionsParameters{ ForbidUpload: sdk.FileOptionParam{Changed: true, Value: true}, ForbidDelete: sdk.FileOptionParam{Changed: true, Value: true}, @@ -242,7 +244,7 @@ func updateAllocationWithRepair(allocationID string, size int64, extend bool, lock int64, - addBlobberId, addBlobberAuthTicket, removeBlobberId, callbackFuncName string) (string, error) { + addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, callbackFuncName string) (string, error) { sdk.SetWasm() allocationObj, err := sdk.GetAllocation(allocationID) if err != nil { @@ -259,7 +261,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, extend, uint64(lock), addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, false, &sdk.FileOptionsParameters{}, statusBar) if err != nil { return hash, err } @@ -295,8 +297,8 @@ func updateAllocationWithRepair(allocationID string, func updateAllocation(allocationID string, size 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, extend, allocationID, uint64(lock), addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, setThirdPartyExtendable, &sdk.FileOptionsParameters{}) if err == nil { clearAllocation(allocationID) diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 7b2b345de..74f0ee50f 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -457,6 +457,12 @@ func (a *Allocation) generateAndSetOwnerSigningPublicKey() { pubKey := privateSigningKey.Public().(ed25519.PublicKey) a.OwnerSigningPublicKey = hex.EncodeToString(pubKey) //TODO: save this public key to blockchain + hash, _, err := UpdateAllocation(0, false, a.ID, 0, "", "", "", a.OwnerSigningPublicKey, false, nil) + if err != nil { + l.Logger.Error("Failed to update owner signing public key", zap.Error(err)) + return + } + l.Logger.Info("Owner signing public key updated with transaction : ", zap.String("hash", hash)) } a.privateSigningKey = privateSigningKey } @@ -3138,11 +3144,11 @@ func (a *Allocation) UpdateWithRepair( size int64, extend bool, lock uint64, - addBlobberId, addBlobberAuthTicket, removeBlobberId string, + addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey string, setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, statusCB StatusCallback, ) (string, error) { - updatedAlloc, hash, isRepairRequired, err := a.UpdateWithStatus(size, extend, lock, addBlobberId, addBlobberAuthTicket, removeBlobberId, setThirdPartyExtendable, fileOptionsParams, statusCB) + updatedAlloc, hash, isRepairRequired, err := a.UpdateWithStatus(size, extend, lock, addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, setThirdPartyExtendable, fileOptionsParams, statusCB) if err != nil { return hash, err } @@ -3173,7 +3179,7 @@ func (a *Allocation) UpdateWithStatus( size int64, extend bool, lock uint64, - addBlobberId, addBlobberAuthTicket, removeBlobberId string, + addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey string, setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, statusCB StatusCallback, ) (*Allocation, string, bool, error) { @@ -3186,7 +3192,7 @@ func (a *Allocation) UpdateWithStatus( } l.Logger.Info("Updating allocation") - hash, _, err := UpdateAllocation(size, extend, a.ID, lock, addBlobberId, addBlobberAuthTicket, removeBlobberId, setThirdPartyExtendable, fileOptionsParams) + hash, _, err := UpdateAllocation(size, extend, a.ID, lock, addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, setThirdPartyExtendable, fileOptionsParams) if err != nil { return alloc, "", isRepairRequired, err } diff --git a/zboxcore/sdk/blobber_operations.go b/zboxcore/sdk/blobber_operations.go index 525ec58d4..6f870b1be 100644 --- a/zboxcore/sdk/blobber_operations.go +++ b/zboxcore/sdk/blobber_operations.go @@ -128,7 +128,7 @@ func UpdateAllocation( extend bool, allocationID string, lock uint64, - addBlobberId, addBlobberAuthTicket, removeBlobberId string, + addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey string, setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, ) (hash string, nonce int64, err error) { @@ -155,6 +155,7 @@ func UpdateAllocation( updateAllocationRequest["add_blobber_auth_ticket"] = addBlobberAuthTicket updateAllocationRequest["remove_blobber_id"] = removeBlobberId updateAllocationRequest["set_third_party_extendable"] = setThirdPartyExtendable + updateAllocationRequest["owner_signing_public_key"] = ownerSigninPublicKey updateAllocationRequest["file_options_changed"], updateAllocationRequest["file_options"] = calculateAllocationFileOptions(alloc.FileOptions, fileOptionsParams) sn := transaction.SmartContractTxnData{ From 58494d4137880b6f1c3a92032240ab9025834627 Mon Sep 17 00:00:00 2001 From: Hitenjain14 <57557631+Hitenjain14@users.noreply.github.com> Date: Tue, 29 Oct 2024 01:52:38 +0530 Subject: [PATCH 272/319] Revert "Session key" --- core/client/set.go | 13 +-- core/sys/vars.go | 2 - zboxcore/fileref/fileref.go | 1 - zboxcore/sdk/allocation.go | 33 +------- zboxcore/sdk/blobber_operations.go | 33 +------- zboxcore/sdk/chunked_upload.go | 2 +- zboxcore/sdk/chunked_upload_form_builder.go | 51 +++--------- .../chunked_upload_form_builder_bench_test.go | 2 +- zboxcore/sdk/chunked_upload_model.go | 16 ++-- zboxcore/sdk/chunked_upload_process_js.go | 5 +- zboxcore/sdk/downloadworker.go | 82 +++++++------------ 11 files changed, 58 insertions(+), 182 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index d87931c22..94bab79ca 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -5,9 +5,8 @@ import ( "encoding/json" "errors" "fmt" - "strings" - "github.com/0chain/gosdk/core/conf" + "strings" "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/sys" @@ -64,7 +63,6 @@ func init() { sys.Verify = verifySignature sys.VerifyWith = verifySignatureWith - sys.VerifyEd25519With = verifyEd25519With } func signHash(hash string, signatureScheme string, keys []sys.KeyPair) (string, error) { @@ -106,15 +104,6 @@ func verifySignatureWith(pubKey, signature, hash string) (bool, error) { return sch.Verify(signature, hash) } -func verifyEd25519With(pubKey, signature, hash string) (bool, error) { - sch := zcncrypto.NewSignatureScheme(constants.ED25519.String()) - err := sch.SetPublicKey(pubKey) - if err != nil { - return false, err - } - return sch.Verify(signature, hash) -} - func GetClientSysKeys(clients ...string) []sys.KeyPair { var wallet *zcncrypto.Wallet if len(clients) > 0 && clients[0] != "" && client.wallets[clients[0]] != nil { diff --git a/core/sys/vars.go b/core/sys/vars.go index f1f0fde5c..b1b53876e 100644 --- a/core/sys/vars.go +++ b/core/sys/vars.go @@ -26,8 +26,6 @@ var ( Authorize AuthorizeFunc AuthCommon AuthorizeFunc - - VerifyEd25519With VerifyWithFunc ) // SetAuthorize sets the authorize callback function diff --git a/zboxcore/fileref/fileref.go b/zboxcore/fileref/fileref.go index 0c51a19da..2fafdd1ed 100644 --- a/zboxcore/fileref/fileref.go +++ b/zboxcore/fileref/fileref.go @@ -51,7 +51,6 @@ type FileRef struct { EncryptedKey string `json:"encrypted_key" mapstructure:"encrypted_key"` EncryptedKeyPoint string `json:"encrypted_key_point" mapstructure:"encrypted_key_point"` Collaborators []Collaborator `json:"collaborators" mapstructure:"collaborators"` - SignatureVersion int `json:"signature_version" mapstructure:"signature_version"` } func (fRef *FileRef) MetaID() string { diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 7b2b345de..dd10bb750 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -3,11 +3,11 @@ package sdk import ( "bytes" "context" - "crypto/ed25519" "encoding/base64" - "encoding/hex" "encoding/json" "fmt" + "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/transaction" "io" "math" "mime/multipart" @@ -22,9 +22,6 @@ import ( "sync/atomic" "time" - "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/transaction" - "github.com/0chain/common/core/currency" "github.com/0chain/errors" thrown "github.com/0chain/errors" @@ -279,9 +276,6 @@ type Allocation struct { StorageVersion int `json:"storage_version"` - // Owner ecdsa public key - OwnerSigningPublicKey string `json:"owner_signing_public_key"` - // FileOptions to define file restrictions on an allocation for third-parties // default 00000000 for all crud operations suggesting only owner has the below listed abilities. // enabling option/s allows any third party to perform certain ops @@ -309,9 +303,8 @@ type Allocation struct { // conseususes consensusThreshold int fullconsensus int - sig string `json:"-"` - allocationRoot string `json:"-"` - privateSigningKey ed25519.PrivateKey `json:"-"` + sig string `json:"-"` + allocationRoot string `json:"-"` } // OperationRequest represents an operation request with its related options. @@ -436,7 +429,6 @@ func (a *Allocation) InitAllocation() { } } } - a.generateAndSetOwnerSigningPublicKey() a.startWorker(a.ctx) InitCommitWorker(a.Blobbers) InitBlockDownloader(a.Blobbers, downloadWorkerCount) @@ -446,21 +438,6 @@ func (a *Allocation) InitAllocation() { a.initialized = true } -func (a *Allocation) generateAndSetOwnerSigningPublicKey() { - //create ecdsa public key from signature - privateSigningKey, err := generateOwnerSigningKey(a.OwnerPublicKey, a.Owner) - if err != nil { - l.Logger.Error("Failed to generate owner signing key", zap.Error(err)) - return - } - if a.OwnerSigningPublicKey == "" { - pubKey := privateSigningKey.Public().(ed25519.PublicKey) - a.OwnerSigningPublicKey = hex.EncodeToString(pubKey) - //TODO: save this public key to blockchain - } - a.privateSigningKey = privateSigningKey -} - func (a *Allocation) isInitialized() bool { return a.initialized && client.IsSDKInitialized() } @@ -1362,7 +1339,6 @@ func (a *Allocation) generateDownloadRequest( downloadReq.allocOwnerID = a.Owner downloadReq.sig = a.sig downloadReq.allocOwnerPubKey = a.OwnerPublicKey - downloadReq.allocOwnerSigningPubKey = a.OwnerSigningPublicKey downloadReq.ctx, downloadReq.ctxCncl = context.WithCancel(a.ctx) downloadReq.fileHandler = fileHandler downloadReq.localFilePath = localFilePath @@ -2796,7 +2772,6 @@ func (a *Allocation) downloadFromAuthTicket(fileHandler sys.File, authTicket str downloadReq.sig = a.sig downloadReq.allocOwnerID = a.Owner downloadReq.allocOwnerPubKey = a.OwnerPublicKey - downloadReq.allocOwnerSigningPubKey = a.OwnerSigningPublicKey downloadReq.ctx, downloadReq.ctxCncl = context.WithCancel(a.ctx) downloadReq.fileHandler = fileHandler downloadReq.localFilePath = localFilePath diff --git a/zboxcore/sdk/blobber_operations.go b/zboxcore/sdk/blobber_operations.go index 525ec58d4..9f27ad8db 100644 --- a/zboxcore/sdk/blobber_operations.go +++ b/zboxcore/sdk/blobber_operations.go @@ -1,18 +1,11 @@ package sdk import ( - "crypto/ed25519" - "encoding/hex" "encoding/json" - "fmt" - "math" - "github.com/0chain/errors" "github.com/0chain/gosdk/core/client" - "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/transaction" - "github.com/0chain/gosdk/zboxcore/logger" - "go.uber.org/zap" + "math" ) // CreateAllocationForOwner creates a new allocation with the given options (txn: `storagesc.new_allocation_request`). @@ -55,20 +48,12 @@ func CreateAllocationForOwner( return "", 0, nil, sdkNotInitialized } - privateSigningKey, err := generateOwnerSigningKey(ownerpublickey, owner) - if err != nil { - return "", 0, nil, errors.New("failed_generate_owner_signing_key", "failed to generate owner signing key: "+err.Error()) - } - pub := privateSigningKey.Public().(ed25519.PublicKey) - pk := hex.EncodeToString(pub) - allocationRequest["owner_id"] = owner allocationRequest["owner_public_key"] = ownerpublickey allocationRequest["third_party_extendable"] = thirdPartyExtendable allocationRequest["file_options_changed"], allocationRequest["file_options"] = calculateAllocationFileOptions(63 /*0011 1111*/, fileOptionsParams) allocationRequest["is_enterprise"] = IsEnterprise allocationRequest["storage_version"] = StorageV2 - allocationRequest["owner_signing_public_key"] = pk var sn = transaction.SmartContractTxnData{ Name: transaction.NEW_ALLOCATION_REQUEST, @@ -320,19 +305,3 @@ func WritePoolUnlock(allocID string, fee uint64) (hash string, nonce int64, err hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee, true) return } - -func generateOwnerSigningKey(ownerPublicKey, ownerID string) (ed25519.PrivateKey, error) { - if ownerPublicKey == "" { - return nil, errors.New("owner_public_key_required", "owner public key is required") - } - hashData := fmt.Sprintf("%s:%s", ownerPublicKey, "owner_signing_public_key") - sig, err := client.Sign(encryption.Hash(hashData), ownerID) - if err != nil { - logger.Logger.Error("error during sign", zap.Error(err)) - return nil, err - } - //use this signature as entropy to generate ecdsa key pair - decodedSig, _ := hex.DecodeString(sig) - privateSigningKey := ed25519.NewKeyFromSeed(decodedSig[:32]) - return privateSigningKey, nil -} diff --git a/zboxcore/sdk/chunked_upload.go b/zboxcore/sdk/chunked_upload.go index c91c04e68..8eeee45b6 100644 --- a/zboxcore/sdk/chunked_upload.go +++ b/zboxcore/sdk/chunked_upload.go @@ -292,7 +292,7 @@ func CreateChunkedUpload( su.chunkReader = cReader - su.formBuilder = CreateChunkedUploadFormBuilder(su.allocationObj.StorageVersion, su.allocationObj.privateSigningKey) + su.formBuilder = CreateChunkedUploadFormBuilder(su.allocationObj.StorageVersion) su.isRepair = isRepair uploadWorker, uploadRequest := calculateWorkersAndRequests(su.allocationObj.DataShards, len(su.blobbers), su.chunkNumber) diff --git a/zboxcore/sdk/chunked_upload_form_builder.go b/zboxcore/sdk/chunked_upload_form_builder.go index 794fb5b68..51c3711ad 100644 --- a/zboxcore/sdk/chunked_upload_form_builder.go +++ b/zboxcore/sdk/chunked_upload_form_builder.go @@ -2,8 +2,6 @@ package sdk import ( "bytes" - "crypto" - "crypto/ed25519" "encoding/hex" "encoding/json" "fmt" @@ -11,8 +9,8 @@ import ( "mime/multipart" "sync" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/encryption" + "github.com/0chain/gosdk/core/client" "golang.org/x/crypto/sha3" ) @@ -39,16 +37,14 @@ type ChunkedUploadFormMetadata struct { } // CreateChunkedUploadFormBuilder create ChunkedUploadFormBuilder instance -func CreateChunkedUploadFormBuilder(storageVersion int, privateSigningKey ed25519.PrivateKey) ChunkedUploadFormBuilder { +func CreateChunkedUploadFormBuilder(storageVersion int) ChunkedUploadFormBuilder { return &chunkedUploadFormBuilder{ storageVersion, - privateSigningKey, } } type chunkedUploadFormBuilder struct { - storageVersion int - privateSigningKey ed25519.PrivateKey + storageVersion int } const MAX_BLOCKS = 80 // 5MB(CHUNK_SIZE*80) @@ -100,10 +96,6 @@ func (b *chunkedUploadFormBuilder) Build( CustomMeta: fileMeta.CustomMeta, } - if b.privateSigningKey != nil { - formData.SignatureVersion = SignatureV2 - } - for i := 0; i < numBodies; i++ { startRange := i * MAX_BLOCKS @@ -176,42 +168,25 @@ func (b *chunkedUploadFormBuilder) Build( for err := range errChan { return res, err } - if b.privateSigningKey != nil { - decodedHash, _ := hex.DecodeString(fileMeta.ActualHash) - sig, err := b.privateSigningKey.Sign(nil, decodedHash, crypto.Hash(0)) - if err != nil { - return res, err - } - formData.ActualFileHashSignature = hex.EncodeToString(sig) - } else { - sig, err := client.Sign(fileMeta.ActualHash) - if err != nil { - return res, err - } - formData.ActualFileHashSignature = sig + actualHashSignature, err := client.Sign(fileMeta.ActualHash) + if err != nil { + return res, err } - hash := formData.ActualFileHashSignature + formData.ValidationRoot + hash := actualHashSignature + formData.ValidationRoot if b.storageVersion == StorageV2 { hashData := fmt.Sprintf("%s:%s:%s:%s", fileMeta.ActualHash, formData.ValidationRoot, formData.FixedMerkleRoot, blobberID) hash = encryption.Hash(hashData) } - if b.privateSigningKey != nil { - decodedHash, _ := hex.DecodeString(hash) - sig, err := b.privateSigningKey.Sign(nil, decodedHash, crypto.Hash(0)) - if err != nil { - return res, err - } - formData.ValidationRootSignature = hex.EncodeToString(sig) - } else { - rootSig, err := client.Sign(hash) - if err != nil { - return res, err - } - formData.ValidationRootSignature = rootSig + validationRootSignature, err := client.Sign(hash) + if err != nil { + return res, err } formData.ActualHash = fileMeta.ActualHash + formData.ActualFileHashSignature = actualHashSignature + formData.ValidationRootSignature = validationRootSignature formData.ActualSize = fileMeta.ActualSize + } thumbnailSize := len(thumbnailChunkData) diff --git a/zboxcore/sdk/chunked_upload_form_builder_bench_test.go b/zboxcore/sdk/chunked_upload_form_builder_bench_test.go index eab4164ad..156910efe 100644 --- a/zboxcore/sdk/chunked_upload_form_builder_bench_test.go +++ b/zboxcore/sdk/chunked_upload_form_builder_bench_test.go @@ -53,7 +53,7 @@ func BenchmarkChunkedUploadFormBuilder(b *testing.B) { for i := 0; i < b.N; i++ { - builder := CreateChunkedUploadFormBuilder(0, nil) + builder := CreateChunkedUploadFormBuilder(0) isFinal := false diff --git a/zboxcore/sdk/chunked_upload_model.go b/zboxcore/sdk/chunked_upload_model.go index 07474366c..0eb5b9724 100644 --- a/zboxcore/sdk/chunked_upload_model.go +++ b/zboxcore/sdk/chunked_upload_model.go @@ -20,8 +20,6 @@ import ( "golang.org/x/crypto/sha3" ) -const SignatureV2 = 1 - // ChunkedUpload upload manager with chunked upload feature type ChunkedUpload struct { consensus Consensus @@ -166,13 +164,13 @@ type UploadFormData struct { EncryptedKey string `json:"encrypted_key,omitempty"` EncryptedKeyPoint string `json:"encrypted_key_point,omitempty"` - IsFinal bool `json:"is_final,omitempty"` // all of chunks are uploaded - ChunkStartIndex int `json:"chunk_start_index,omitempty"` // start index of chunks. - ChunkEndIndex int `json:"chunk_end_index,omitempty"` // end index of chunks. all chunks MUST be uploaded one by one because of streaming merkle hash - ChunkSize int64 `json:"chunk_size,omitempty"` // the size of a chunk. 64*1024 is default - UploadOffset int64 `json:"upload_offset,omitempty"` // It is next position that new incoming chunk should be append to - Size int64 `json:"size"` // total size of shard - SignatureVersion int `json:"signature_version,omitempty"` + IsFinal bool `json:"is_final,omitempty"` // all of chunks are uploaded + ChunkStartIndex int `json:"chunk_start_index,omitempty"` // start index of chunks. + ChunkEndIndex int `json:"chunk_end_index,omitempty"` // end index of chunks. all chunks MUST be uploaded one by one because of streaming merkle hash + ChunkSize int64 `json:"chunk_size,omitempty"` // the size of a chunk. 64*1024 is default + UploadOffset int64 `json:"upload_offset,omitempty"` // It is next position that new incoming chunk should be append to + Size int64 `json:"size"` // total size of shard + } // UploadProgress progress of upload diff --git a/zboxcore/sdk/chunked_upload_process_js.go b/zboxcore/sdk/chunked_upload_process_js.go index db397c44a..1e22f867b 100644 --- a/zboxcore/sdk/chunked_upload_process_js.go +++ b/zboxcore/sdk/chunked_upload_process_js.go @@ -6,7 +6,6 @@ package sdk import ( "bytes" "context" - "crypto/ed25519" "encoding/json" "fmt" "net/http" @@ -55,7 +54,6 @@ type ChunkedUploadFormInfo struct { AllocationTx string OnlyHash bool StorageVersion int - PrivateSigningKey ed25519.PrivateKey } // createUploadProgress create a new UploadProgress @@ -120,7 +118,6 @@ func (su *ChunkedUpload) processUpload(chunkStartIndex, chunkEndIndex int, AllocationTx: su.allocationObj.Tx, OnlyHash: chunkEndIndex <= su.progress.ChunkIndex, StorageVersion: su.allocationObj.StorageVersion, - PrivateSigningKey: su.allocationObj.privateSigningKey, } formInfoJSON, err := json.Marshal(formInfo) if err != nil { @@ -458,7 +455,7 @@ func ProcessEventData(data safejs.Value) { defer delete(hasherMap, fileMeta.RemotePath) } blobberID := os.Getenv("BLOBBER_ID") - formBuilder := CreateChunkedUploadFormBuilder(formInfo.StorageVersion, formInfo.PrivateSigningKey) + formBuilder := CreateChunkedUploadFormBuilder(formInfo.StorageVersion) uploadData, err := formBuilder.Build(fileMeta, wp.hasher, formInfo.ConnectionID, blobberID, formInfo.ChunkSize, formInfo.ChunkStartIndex, formInfo.ChunkEndIndex, formInfo.IsFinal, formInfo.EncryptedKey, formInfo.EncryptedKeyPoint, fileShards, thumbnailChunkData, formInfo.ShardSize) if err != nil { diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index 2503c5225..63a00b34f 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -96,27 +96,26 @@ type DownloadRequest struct { fileCallback func() contentMode string Consensus - effectiveBlockSize int // blocksize - encryptionOverHead - ecEncoder reedsolomon.Encoder - maskMu *sync.Mutex - encScheme encryption.EncryptionScheme - shouldVerify bool - blocksPerShard int64 - connectionID string - skip bool - freeRead bool - fRef *fileref.FileRef - chunksPerShard int64 - size int64 - offset int64 - bufferMap map[int]zboxutil.DownloadBuffer - downloadStorer DownloadProgressStorer - workdir string - downloadQueue downloadQueue // Always initialize this queue with max time taken - isResume bool - isEnterprise bool - storageVersion int - allocOwnerSigningPubKey string + effectiveBlockSize int // blocksize - encryptionOverHead + ecEncoder reedsolomon.Encoder + maskMu *sync.Mutex + encScheme encryption.EncryptionScheme + shouldVerify bool + blocksPerShard int64 + connectionID string + skip bool + freeRead bool + fRef *fileref.FileRef + chunksPerShard int64 + size int64 + offset int64 + bufferMap map[int]zboxutil.DownloadBuffer + downloadStorer DownloadProgressStorer + workdir string + downloadQueue downloadQueue // Always initialize this queue with max time taken + isResume bool + isEnterprise bool + storageVersion int } type downloadPriority struct { @@ -1165,23 +1164,12 @@ func (req *DownloadRequest) getFileMetaConsensus(fMetaResp []*fileMetaResponse) } actualHash := fmr.fileref.ActualFileHash actualFileHashSignature := fmr.fileref.ActualFileHashSignature - var ( - isValid bool - err error + + isValid, err := sys.VerifyWith( + req.allocOwnerPubKey, + actualFileHashSignature, + actualHash, ) - if fmr.fileref.SignatureVersion == SignatureV2 { - isValid, err = sys.VerifyEd25519With( - req.allocOwnerSigningPubKey, - actualFileHashSignature, - actualHash, - ) - } else { - isValid, err = sys.VerifyWith( - req.allocOwnerPubKey, - actualFileHashSignature, - actualHash, - ) - } if err != nil { l.Logger.Error(err) continue @@ -1231,23 +1219,11 @@ func (req *DownloadRequest) getFileMetaConsensus(fMetaResp []*fileMetaResponse) hashData := fmt.Sprintf("%s:%s:%s:%s", fRef.ActualFileHash, fRef.ValidationRoot, fRef.FixedMerkleRoot, req.blobbers[i].ID) hash = encrypt.Hash(hashData) } - var ( - isValid bool - err error + isValid, err := sys.VerifyWith( + req.allocOwnerPubKey, + fRef.ValidationRootSignature, + hash, ) - if fRef.SignatureVersion == SignatureV2 { - isValid, err = sys.VerifyEd25519With( - req.allocOwnerSigningPubKey, - fRef.ValidationRootSignature, - hash, - ) - } else { - isValid, err = sys.VerifyWith( - req.allocOwnerPubKey, - fRef.ValidationRootSignature, - hash, - ) - } if err != nil { l.Logger.Error(err, "allocOwnerPubKey: ", req.allocOwnerPubKey, " validationRootSignature: ", fRef.ValidationRootSignature, " actualFileHashSignature: ", fRef.ActualFileHashSignature, " validationRoot: ", fRef.ValidationRoot) continue From 8430cbb6bda898786e966f50a5346d03027c8453 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Tue, 29 Oct 2024 22:34:49 +0530 Subject: [PATCH 273/319] Fix send transaction --- core/transaction/entity.go | 75 ++++++++++++++++++-------------------- core/util/httpnet.go | 30 +++++++++++++++ 2 files changed, 66 insertions(+), 39 deletions(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index ac63ef731..d8dade51d 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -2,7 +2,6 @@ package transaction import ( - "context" "encoding/json" "fmt" "github.com/0chain/gosdk/core/client" @@ -249,77 +248,75 @@ func (t *Transaction) VerifySigWith(pubkey string, verifyHandler VerifyFunc) (bo } func SendTransactionSync(txn *Transaction, miners []string) error { - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) - defer cancel() + const requestTimeout = 30 * time.Second // Timeout for each request - wg := sync.WaitGroup{} - wg.Add(len(miners)) fails := make(chan error, len(miners)) + var wg sync.WaitGroup for _, miner := range miners { - url := fmt.Sprintf("%v/%v", miner, TXN_SUBMIT_URL) + wg.Add(1) + minerURL := fmt.Sprintf("%v/%v", miner, TXN_SUBMIT_URL) + go func(url string) { defer wg.Done() - select { - case <-ctx.Done(): - fails <- ctx.Err() // Timeout or cancellation - default: - _, err := sendTransactionToURL(url, txn, nil) - if err != nil { - fails <- err - } + _, err := sendTransactionToURL(requestTimeout, url, txn) + if err != nil { + fails <- err } - }(url) + }(minerURL) } - done := make(chan struct{}) + // Wait for all requests to finish go func() { wg.Wait() close(fails) - close(done) }() - select { - case <-done: // All requests completed - case <-ctx.Done(): // Timeout reached - return ctx.Err() - } - - // Error processing logic here (same as original) - failureCount := 0 + // Collect errors from all requests + var failureCount int messages := make(map[string]int) - for e := range fails { - if e != nil { + + for err := range fails { + if err != nil { failureCount++ - messages[e.Error()]++ + messages[err.Error()]++ } } - max := 0 - dominant := "" - for m, s := range messages { - if s > max { - dominant = m + // Identify the most frequent error + var maxCount int + var dominantErr string + for msg, count := range messages { + if count > maxCount { + maxCount = count + dominantErr = msg } } if failureCount == len(miners) { - return fmt.Errorf(dominant) + return fmt.Errorf("all miners failed: %s", dominantErr) } return nil } -func sendTransactionToURL(url string, txn *Transaction, wg *sync.WaitGroup) ([]byte, error) { +func sendTransactionToURL(timeout time.Duration, url string, txn *Transaction) ([]byte, error) { + // Create a new request with the provided context postReq, err := util.NewHTTPPostRequest(url, txn) if err != nil { - //Logger.Error("Error in serializing the transaction", txn, err.Error()) - return nil, err + return nil, fmt.Errorf("error creating HTTP request: %w", err) } - postResponse, err := postReq.Post() + + // Set the request to be executed with the provided context (handling timeouts) + postResponse, err := postReq.PostWithTimeout(timeout) + if err != nil { + return nil, fmt.Errorf("submit transaction failed: %w", err) + } + if postResponse.StatusCode >= 200 && postResponse.StatusCode <= 299 { return []byte(postResponse.Body), nil } - return nil, errors.Wrap(err, errors.New("submit transaction failed", postResponse.Body)) + + return nil, fmt.Errorf("submit transaction failed: %s", postResponse.Body) } type cachedObject struct { diff --git a/core/util/httpnet.go b/core/util/httpnet.go index c26ed3362..46de6e73c 100644 --- a/core/util/httpnet.go +++ b/core/util/httpnet.go @@ -188,3 +188,33 @@ func (r *PostRequest) Post() (*PostResponse, error) { } return result, nil } + +func (r *PostRequest) PostWithTimeout(timeout time.Duration) (*PostResponse, error) { + result := &PostResponse{} + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + err := httpDo(r.req.WithContext(ctx), ctx, cancel, func(resp *http.Response, err error) error { + if err != nil { + return err + } + if resp.Body != nil { + defer resp.Body.Close() + } else { + return fmt.Errorf("response body is nil") + } + + rspBy, err := io.ReadAll(resp.Body) + if err != nil { + return err + } + result.Url = r.url + result.StatusCode = resp.StatusCode + result.Status = resp.Status + result.Body = string(rspBy) + return nil + }) + if err != nil { + return nil, err // Ensure you propagate the error + } + return result, nil +} From 164234cc56f7b5856b99fce881c47f2ae072bc52 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Wed, 30 Oct 2024 14:38:26 +0530 Subject: [PATCH 274/319] add signing logs --- zboxcore/sdk/allocation.go | 6 +++++- zboxcore/sdk/chunked_upload_form_builder.go | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 74f0ee50f..6ede3e39c 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -462,7 +462,11 @@ func (a *Allocation) generateAndSetOwnerSigningPublicKey() { l.Logger.Error("Failed to update owner signing public key", zap.Error(err)) return } - l.Logger.Info("Owner signing public key updated with transaction : ", zap.String("hash", hash)) + l.Logger.Info("Owner signing public key updated with transaction : ", hash, " ownerSigningPublicKey : ", a.OwnerSigningPublicKey) + a.Tx = hash + } else { + pubKey := privateSigningKey.Public().(ed25519.PublicKey) + l.Logger.Info("Owner signing public key already exists: ", a.OwnerSigningPublicKey, " generated: ", hex.EncodeToString(pubKey)) } a.privateSigningKey = privateSigningKey } diff --git a/zboxcore/sdk/chunked_upload_form_builder.go b/zboxcore/sdk/chunked_upload_form_builder.go index 794fb5b68..3a5422817 100644 --- a/zboxcore/sdk/chunked_upload_form_builder.go +++ b/zboxcore/sdk/chunked_upload_form_builder.go @@ -13,6 +13,7 @@ import ( "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/encryption" + "github.com/0chain/gosdk/zboxcore/logger" "golang.org/x/crypto/sha3" ) @@ -182,12 +183,14 @@ func (b *chunkedUploadFormBuilder) Build( if err != nil { return res, err } + logger.Logger.Info("sign using private signing key: ", formData.SignatureVersion) formData.ActualFileHashSignature = hex.EncodeToString(sig) } else { sig, err := client.Sign(fileMeta.ActualHash) if err != nil { return res, err } + logger.Logger.Info("sign using client: ", formData.SignatureVersion) formData.ActualFileHashSignature = sig } hash := formData.ActualFileHashSignature + formData.ValidationRoot From fb7c3b7b8b9785b430dd3bcdc1d048adfd4b3e0b Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 30 Oct 2024 15:36:26 +0530 Subject: [PATCH 275/319] Fix send transaction --- core/transaction/entity.go | 19 +++++++++++++------ core/util/httpnet.go | 38 ++++---------------------------------- 2 files changed, 17 insertions(+), 40 deletions(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index d8dade51d..69a7975bc 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -2,6 +2,7 @@ package transaction import ( + "context" "encoding/json" "fmt" "github.com/0chain/gosdk/core/client" @@ -259,14 +260,19 @@ func SendTransactionSync(txn *Transaction, miners []string) error { go func(url string) { defer wg.Done() - _, err := sendTransactionToURL(requestTimeout, url, txn) + + // Create a context with a 30-second timeout for each request + ctx, cancel := context.WithTimeout(context.Background(), requestTimeout) + defer cancel() + + _, err := sendTransactionToURL(ctx, url, txn) if err != nil { fails <- err } }(minerURL) } - // Wait for all requests to finish + // Close the channel when all requests are finished go func() { wg.Wait() close(fails) @@ -299,15 +305,16 @@ func SendTransactionSync(txn *Transaction, miners []string) error { return nil } -func sendTransactionToURL(timeout time.Duration, url string, txn *Transaction) ([]byte, error) { - // Create a new request with the provided context +func sendTransactionToURL(ctx context.Context, url string, txn *Transaction) ([]byte, error) { + // Create a new HTTP POST request with context postReq, err := util.NewHTTPPostRequest(url, txn) if err != nil { return nil, fmt.Errorf("error creating HTTP request: %w", err) } - // Set the request to be executed with the provided context (handling timeouts) - postResponse, err := postReq.PostWithTimeout(timeout) + // Use the provided context in the request's Post method + postReq.Ctx = ctx + postResponse, err := postReq.Post() if err != nil { return nil, fmt.Errorf("submit transaction failed: %w", err) } diff --git a/core/util/httpnet.go b/core/util/httpnet.go index 46de6e73c..bcc5f2b77 100644 --- a/core/util/httpnet.go +++ b/core/util/httpnet.go @@ -22,7 +22,7 @@ type GetResponse struct { type PostRequest struct { req *http.Request - ctx context.Context + Ctx context.Context cncl context.CancelFunc url string } @@ -129,7 +129,7 @@ func NewHTTPGetRequestContext(ctx context.Context, url string) (*GetRequest, err gr.PostRequest = &PostRequest{} gr.url = url gr.req = req - gr.ctx, gr.cncl = context.WithCancel(ctx) + gr.Ctx, gr.cncl = context.WithCancel(ctx) return gr, nil } @@ -147,7 +147,7 @@ func NewHTTPPostRequest(url string, data interface{}) (*PostRequest, error) { req.Header.Set("Access-Control-Allow-Origin", "*") pr.url = url pr.req = req - pr.ctx, pr.cncl = context.WithTimeout(context.Background(), time.Second*60) + pr.Ctx, pr.cncl = context.WithTimeout(context.Background(), time.Second*60) return pr, nil } @@ -163,37 +163,7 @@ func (r *GetRequest) Get() (*GetResponse, error) { func (r *PostRequest) Post() (*PostResponse, error) { result := &PostResponse{} - err := httpDo(r.req, r.ctx, r.cncl, func(resp *http.Response, err error) error { - if err != nil { - return err - } - if resp.Body != nil { - defer resp.Body.Close() - } else { - return fmt.Errorf("response body is nil") - } - - rspBy, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - result.Url = r.url - result.StatusCode = resp.StatusCode - result.Status = resp.Status - result.Body = string(rspBy) - return nil - }) - if err != nil { - return nil, err // Ensure you propagate the error - } - return result, nil -} - -func (r *PostRequest) PostWithTimeout(timeout time.Duration) (*PostResponse, error) { - result := &PostResponse{} - ctx, cancel := context.WithTimeout(context.Background(), timeout) - defer cancel() - err := httpDo(r.req.WithContext(ctx), ctx, cancel, func(resp *http.Response, err error) error { + err := httpDo(r.req, r.Ctx, r.cncl, func(resp *http.Response, err error) error { if err != nil { return err } From 13680c060c655f826db58b481f98e37372fab181 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Wed, 30 Oct 2024 18:45:01 +0530 Subject: [PATCH 276/319] log error --- zboxcore/sdk/allocation.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 6ede3e39c..ca55b23d6 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -459,7 +459,7 @@ func (a *Allocation) generateAndSetOwnerSigningPublicKey() { //TODO: save this public key to blockchain hash, _, err := UpdateAllocation(0, false, a.ID, 0, "", "", "", a.OwnerSigningPublicKey, false, nil) if err != nil { - l.Logger.Error("Failed to update owner signing public key", zap.Error(err)) + l.Logger.Error("Failed to update owner signing public key ", err) return } l.Logger.Info("Owner signing public key updated with transaction : ", hash, " ownerSigningPublicKey : ", a.OwnerSigningPublicKey) From a32db36c12b892f29fb4ade43e94eced76d06752 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Wed, 30 Oct 2024 18:53:30 +0530 Subject: [PATCH 277/319] log hash and id --- zboxcore/sdk/allocation.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index ca55b23d6..2fe5f8f1f 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -459,7 +459,7 @@ func (a *Allocation) generateAndSetOwnerSigningPublicKey() { //TODO: save this public key to blockchain hash, _, err := UpdateAllocation(0, false, a.ID, 0, "", "", "", a.OwnerSigningPublicKey, false, nil) if err != nil { - l.Logger.Error("Failed to update owner signing public key ", err) + l.Logger.Error("Failed to update owner signing public key ", err, " allocationID: ", a.ID, " hash: ", hash) return } l.Logger.Info("Owner signing public key updated with transaction : ", hash, " ownerSigningPublicKey : ", a.OwnerSigningPublicKey) From 46e80a285901bb3a6559216c0e3a9e69e30776a6 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Wed, 30 Oct 2024 20:07:51 +0530 Subject: [PATCH 278/319] use get allocation for update --- zboxcore/sdk/blobber_operations.go | 2 +- zboxcore/sdk/sdk.go | 28 ++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/zboxcore/sdk/blobber_operations.go b/zboxcore/sdk/blobber_operations.go index 6f870b1be..fa88b9a1c 100644 --- a/zboxcore/sdk/blobber_operations.go +++ b/zboxcore/sdk/blobber_operations.go @@ -140,7 +140,7 @@ func UpdateAllocation( return "", 0, sdkNotInitialized } - alloc, err := GetAllocation(allocationID) + alloc, err := GetAllocationForUpdate(allocationID) if err != nil { return "", 0, allocationNotFound } diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index ffd625d1f..37789b11f 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -4,15 +4,16 @@ import ( "encoding/base64" "encoding/json" "fmt" - "github.com/0chain/common/core/currency" - "github.com/0chain/errors" - "github.com/0chain/gosdk/core/logger" - "gopkg.in/natefinch/lumberjack.v2" "io" "math" "net/http" "strconv" + "github.com/0chain/common/core/currency" + "github.com/0chain/errors" + "github.com/0chain/gosdk/core/logger" + "gopkg.in/natefinch/lumberjack.v2" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/transaction" @@ -637,6 +638,25 @@ func GetAllocation(allocationID string) (*Allocation, error) { return allocationObj, nil } +// GetAllocationForUpdate - get allocation for update from given allocation id without calling init allocation +func GetAllocationForUpdate(allocationID string) (*Allocation, error) { + if !client.IsSDKInitialized() { + return nil, sdkNotInitialized + } + params := make(map[string]string) + params["allocation"] = allocationID + allocationBytes, err := client.MakeSCRestAPICall(STORAGE_SCADDRESS, "/allocation", params) + if err != nil { + return nil, errors.New("allocation_fetch_error", "Error fetching the allocation."+err.Error()) + } + allocationObj := &Allocation{} + err = json.Unmarshal(allocationBytes, allocationObj) + if err != nil { + return nil, errors.New("allocation_decode_error", "Error decoding the allocation: "+err.Error()+" "+string(allocationBytes)) + } + return allocationObj, nil +} + func GetAllocationUpdates(allocation *Allocation) error { if allocation == nil { return errors.New("allocation_not_initialized", "") From 53a1aa0ff31f9cb7c52a0a267a8394b996570f6f Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 30 Oct 2024 21:20:48 +0530 Subject: [PATCH 279/319] Fix --- core/transaction/entity.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index ac63ef731..205f21b8c 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -602,7 +602,7 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, if t == nil { return "", "", 0, txn, errors.New("transaction_validation_failed", - "Failed to get the transaction confirmation") + "Failed to get the transaction confirmation : "+txn.Hash) } if t.Status == TxnFail { From b57aa3916d69bcf4e62714dc74fbc6dea9dc26ec Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Wed, 30 Oct 2024 21:28:08 +0530 Subject: [PATCH 280/319] Fix --- core/client/set.go | 13 ++- core/sys/vars.go | 2 + zboxcore/fileref/fileref.go | 1 + zboxcore/sdk/blobber_operations.go | 33 +++++++- zboxcore/sdk/chunked_upload.go | 2 +- .../chunked_upload_form_builder_bench_test.go | 2 +- zboxcore/sdk/chunked_upload_model.go | 16 ++-- zboxcore/sdk/chunked_upload_process_js.go | 5 +- zboxcore/sdk/downloadworker.go | 82 ++++++++++++------- 9 files changed, 115 insertions(+), 41 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index 94bab79ca..d87931c22 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -5,9 +5,10 @@ import ( "encoding/json" "errors" "fmt" - "github.com/0chain/gosdk/core/conf" "strings" + "github.com/0chain/gosdk/core/conf" + "github.com/0chain/gosdk/constants" "github.com/0chain/gosdk/core/sys" "github.com/0chain/gosdk/core/zcncrypto" @@ -63,6 +64,7 @@ func init() { sys.Verify = verifySignature sys.VerifyWith = verifySignatureWith + sys.VerifyEd25519With = verifyEd25519With } func signHash(hash string, signatureScheme string, keys []sys.KeyPair) (string, error) { @@ -104,6 +106,15 @@ func verifySignatureWith(pubKey, signature, hash string) (bool, error) { return sch.Verify(signature, hash) } +func verifyEd25519With(pubKey, signature, hash string) (bool, error) { + sch := zcncrypto.NewSignatureScheme(constants.ED25519.String()) + err := sch.SetPublicKey(pubKey) + if err != nil { + return false, err + } + return sch.Verify(signature, hash) +} + func GetClientSysKeys(clients ...string) []sys.KeyPair { var wallet *zcncrypto.Wallet if len(clients) > 0 && clients[0] != "" && client.wallets[clients[0]] != nil { diff --git a/core/sys/vars.go b/core/sys/vars.go index b1b53876e..f1f0fde5c 100644 --- a/core/sys/vars.go +++ b/core/sys/vars.go @@ -26,6 +26,8 @@ var ( Authorize AuthorizeFunc AuthCommon AuthorizeFunc + + VerifyEd25519With VerifyWithFunc ) // SetAuthorize sets the authorize callback function diff --git a/zboxcore/fileref/fileref.go b/zboxcore/fileref/fileref.go index 2fafdd1ed..0c51a19da 100644 --- a/zboxcore/fileref/fileref.go +++ b/zboxcore/fileref/fileref.go @@ -51,6 +51,7 @@ type FileRef struct { EncryptedKey string `json:"encrypted_key" mapstructure:"encrypted_key"` EncryptedKeyPoint string `json:"encrypted_key_point" mapstructure:"encrypted_key_point"` Collaborators []Collaborator `json:"collaborators" mapstructure:"collaborators"` + SignatureVersion int `json:"signature_version" mapstructure:"signature_version"` } func (fRef *FileRef) MetaID() string { diff --git a/zboxcore/sdk/blobber_operations.go b/zboxcore/sdk/blobber_operations.go index 15aff9eb0..fa88b9a1c 100644 --- a/zboxcore/sdk/blobber_operations.go +++ b/zboxcore/sdk/blobber_operations.go @@ -1,11 +1,18 @@ package sdk import ( + "crypto/ed25519" + "encoding/hex" "encoding/json" + "fmt" + "math" + "github.com/0chain/errors" "github.com/0chain/gosdk/core/client" + "github.com/0chain/gosdk/core/encryption" "github.com/0chain/gosdk/core/transaction" - "math" + "github.com/0chain/gosdk/zboxcore/logger" + "go.uber.org/zap" ) // CreateAllocationForOwner creates a new allocation with the given options (txn: `storagesc.new_allocation_request`). @@ -48,12 +55,20 @@ func CreateAllocationForOwner( return "", 0, nil, sdkNotInitialized } + privateSigningKey, err := generateOwnerSigningKey(ownerpublickey, owner) + if err != nil { + return "", 0, nil, errors.New("failed_generate_owner_signing_key", "failed to generate owner signing key: "+err.Error()) + } + pub := privateSigningKey.Public().(ed25519.PublicKey) + pk := hex.EncodeToString(pub) + allocationRequest["owner_id"] = owner allocationRequest["owner_public_key"] = ownerpublickey allocationRequest["third_party_extendable"] = thirdPartyExtendable allocationRequest["file_options_changed"], allocationRequest["file_options"] = calculateAllocationFileOptions(63 /*0011 1111*/, fileOptionsParams) allocationRequest["is_enterprise"] = IsEnterprise allocationRequest["storage_version"] = StorageV2 + allocationRequest["owner_signing_public_key"] = pk var sn = transaction.SmartContractTxnData{ Name: transaction.NEW_ALLOCATION_REQUEST, @@ -306,3 +321,19 @@ func WritePoolUnlock(allocID string, fee uint64) (hash string, nonce int64, err hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee, true) return } + +func generateOwnerSigningKey(ownerPublicKey, ownerID string) (ed25519.PrivateKey, error) { + if ownerPublicKey == "" { + return nil, errors.New("owner_public_key_required", "owner public key is required") + } + hashData := fmt.Sprintf("%s:%s", ownerPublicKey, "owner_signing_public_key") + sig, err := client.Sign(encryption.Hash(hashData), ownerID) + if err != nil { + logger.Logger.Error("error during sign", zap.Error(err)) + return nil, err + } + //use this signature as entropy to generate ecdsa key pair + decodedSig, _ := hex.DecodeString(sig) + privateSigningKey := ed25519.NewKeyFromSeed(decodedSig[:32]) + return privateSigningKey, nil +} diff --git a/zboxcore/sdk/chunked_upload.go b/zboxcore/sdk/chunked_upload.go index 8eeee45b6..c91c04e68 100644 --- a/zboxcore/sdk/chunked_upload.go +++ b/zboxcore/sdk/chunked_upload.go @@ -292,7 +292,7 @@ func CreateChunkedUpload( su.chunkReader = cReader - su.formBuilder = CreateChunkedUploadFormBuilder(su.allocationObj.StorageVersion) + su.formBuilder = CreateChunkedUploadFormBuilder(su.allocationObj.StorageVersion, su.allocationObj.privateSigningKey) su.isRepair = isRepair uploadWorker, uploadRequest := calculateWorkersAndRequests(su.allocationObj.DataShards, len(su.blobbers), su.chunkNumber) diff --git a/zboxcore/sdk/chunked_upload_form_builder_bench_test.go b/zboxcore/sdk/chunked_upload_form_builder_bench_test.go index 156910efe..eab4164ad 100644 --- a/zboxcore/sdk/chunked_upload_form_builder_bench_test.go +++ b/zboxcore/sdk/chunked_upload_form_builder_bench_test.go @@ -53,7 +53,7 @@ func BenchmarkChunkedUploadFormBuilder(b *testing.B) { for i := 0; i < b.N; i++ { - builder := CreateChunkedUploadFormBuilder(0) + builder := CreateChunkedUploadFormBuilder(0, nil) isFinal := false diff --git a/zboxcore/sdk/chunked_upload_model.go b/zboxcore/sdk/chunked_upload_model.go index 0eb5b9724..07474366c 100644 --- a/zboxcore/sdk/chunked_upload_model.go +++ b/zboxcore/sdk/chunked_upload_model.go @@ -20,6 +20,8 @@ import ( "golang.org/x/crypto/sha3" ) +const SignatureV2 = 1 + // ChunkedUpload upload manager with chunked upload feature type ChunkedUpload struct { consensus Consensus @@ -164,13 +166,13 @@ type UploadFormData struct { EncryptedKey string `json:"encrypted_key,omitempty"` EncryptedKeyPoint string `json:"encrypted_key_point,omitempty"` - IsFinal bool `json:"is_final,omitempty"` // all of chunks are uploaded - ChunkStartIndex int `json:"chunk_start_index,omitempty"` // start index of chunks. - ChunkEndIndex int `json:"chunk_end_index,omitempty"` // end index of chunks. all chunks MUST be uploaded one by one because of streaming merkle hash - ChunkSize int64 `json:"chunk_size,omitempty"` // the size of a chunk. 64*1024 is default - UploadOffset int64 `json:"upload_offset,omitempty"` // It is next position that new incoming chunk should be append to - Size int64 `json:"size"` // total size of shard - + IsFinal bool `json:"is_final,omitempty"` // all of chunks are uploaded + ChunkStartIndex int `json:"chunk_start_index,omitempty"` // start index of chunks. + ChunkEndIndex int `json:"chunk_end_index,omitempty"` // end index of chunks. all chunks MUST be uploaded one by one because of streaming merkle hash + ChunkSize int64 `json:"chunk_size,omitempty"` // the size of a chunk. 64*1024 is default + UploadOffset int64 `json:"upload_offset,omitempty"` // It is next position that new incoming chunk should be append to + Size int64 `json:"size"` // total size of shard + SignatureVersion int `json:"signature_version,omitempty"` } // UploadProgress progress of upload diff --git a/zboxcore/sdk/chunked_upload_process_js.go b/zboxcore/sdk/chunked_upload_process_js.go index 1e22f867b..db397c44a 100644 --- a/zboxcore/sdk/chunked_upload_process_js.go +++ b/zboxcore/sdk/chunked_upload_process_js.go @@ -6,6 +6,7 @@ package sdk import ( "bytes" "context" + "crypto/ed25519" "encoding/json" "fmt" "net/http" @@ -54,6 +55,7 @@ type ChunkedUploadFormInfo struct { AllocationTx string OnlyHash bool StorageVersion int + PrivateSigningKey ed25519.PrivateKey } // createUploadProgress create a new UploadProgress @@ -118,6 +120,7 @@ func (su *ChunkedUpload) processUpload(chunkStartIndex, chunkEndIndex int, AllocationTx: su.allocationObj.Tx, OnlyHash: chunkEndIndex <= su.progress.ChunkIndex, StorageVersion: su.allocationObj.StorageVersion, + PrivateSigningKey: su.allocationObj.privateSigningKey, } formInfoJSON, err := json.Marshal(formInfo) if err != nil { @@ -455,7 +458,7 @@ func ProcessEventData(data safejs.Value) { defer delete(hasherMap, fileMeta.RemotePath) } blobberID := os.Getenv("BLOBBER_ID") - formBuilder := CreateChunkedUploadFormBuilder(formInfo.StorageVersion) + formBuilder := CreateChunkedUploadFormBuilder(formInfo.StorageVersion, formInfo.PrivateSigningKey) uploadData, err := formBuilder.Build(fileMeta, wp.hasher, formInfo.ConnectionID, blobberID, formInfo.ChunkSize, formInfo.ChunkStartIndex, formInfo.ChunkEndIndex, formInfo.IsFinal, formInfo.EncryptedKey, formInfo.EncryptedKeyPoint, fileShards, thumbnailChunkData, formInfo.ShardSize) if err != nil { diff --git a/zboxcore/sdk/downloadworker.go b/zboxcore/sdk/downloadworker.go index 63a00b34f..2503c5225 100644 --- a/zboxcore/sdk/downloadworker.go +++ b/zboxcore/sdk/downloadworker.go @@ -96,26 +96,27 @@ type DownloadRequest struct { fileCallback func() contentMode string Consensus - effectiveBlockSize int // blocksize - encryptionOverHead - ecEncoder reedsolomon.Encoder - maskMu *sync.Mutex - encScheme encryption.EncryptionScheme - shouldVerify bool - blocksPerShard int64 - connectionID string - skip bool - freeRead bool - fRef *fileref.FileRef - chunksPerShard int64 - size int64 - offset int64 - bufferMap map[int]zboxutil.DownloadBuffer - downloadStorer DownloadProgressStorer - workdir string - downloadQueue downloadQueue // Always initialize this queue with max time taken - isResume bool - isEnterprise bool - storageVersion int + effectiveBlockSize int // blocksize - encryptionOverHead + ecEncoder reedsolomon.Encoder + maskMu *sync.Mutex + encScheme encryption.EncryptionScheme + shouldVerify bool + blocksPerShard int64 + connectionID string + skip bool + freeRead bool + fRef *fileref.FileRef + chunksPerShard int64 + size int64 + offset int64 + bufferMap map[int]zboxutil.DownloadBuffer + downloadStorer DownloadProgressStorer + workdir string + downloadQueue downloadQueue // Always initialize this queue with max time taken + isResume bool + isEnterprise bool + storageVersion int + allocOwnerSigningPubKey string } type downloadPriority struct { @@ -1164,12 +1165,23 @@ func (req *DownloadRequest) getFileMetaConsensus(fMetaResp []*fileMetaResponse) } actualHash := fmr.fileref.ActualFileHash actualFileHashSignature := fmr.fileref.ActualFileHashSignature - - isValid, err := sys.VerifyWith( - req.allocOwnerPubKey, - actualFileHashSignature, - actualHash, + var ( + isValid bool + err error ) + if fmr.fileref.SignatureVersion == SignatureV2 { + isValid, err = sys.VerifyEd25519With( + req.allocOwnerSigningPubKey, + actualFileHashSignature, + actualHash, + ) + } else { + isValid, err = sys.VerifyWith( + req.allocOwnerPubKey, + actualFileHashSignature, + actualHash, + ) + } if err != nil { l.Logger.Error(err) continue @@ -1219,11 +1231,23 @@ func (req *DownloadRequest) getFileMetaConsensus(fMetaResp []*fileMetaResponse) hashData := fmt.Sprintf("%s:%s:%s:%s", fRef.ActualFileHash, fRef.ValidationRoot, fRef.FixedMerkleRoot, req.blobbers[i].ID) hash = encrypt.Hash(hashData) } - isValid, err := sys.VerifyWith( - req.allocOwnerPubKey, - fRef.ValidationRootSignature, - hash, + var ( + isValid bool + err error ) + if fRef.SignatureVersion == SignatureV2 { + isValid, err = sys.VerifyEd25519With( + req.allocOwnerSigningPubKey, + fRef.ValidationRootSignature, + hash, + ) + } else { + isValid, err = sys.VerifyWith( + req.allocOwnerPubKey, + fRef.ValidationRootSignature, + hash, + ) + } if err != nil { l.Logger.Error(err, "allocOwnerPubKey: ", req.allocOwnerPubKey, " validationRootSignature: ", fRef.ValidationRootSignature, " actualFileHashSignature: ", fRef.ActualFileHashSignature, " validationRoot: ", fRef.ValidationRoot) continue From 72f15c4a52675dc040f54e829c16cad4a4861d4b Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 31 Oct 2024 01:01:37 +0530 Subject: [PATCH 281/319] Fix config max txn query --- core/conf/vars.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/core/conf/vars.go b/core/conf/vars.go index c1e7be234..82d4e656a 100644 --- a/core/conf/vars.go +++ b/core/conf/vars.go @@ -42,12 +42,16 @@ func GetClientConfig() (*Config, error) { // InitClientConfig set global client config func InitClientConfig(c *Config) { onceCfg.Do(func() { - sharderConsensous := c.SharderConsensous - if sharderConsensous < 1 { - sharderConsensous = DefaultSharderConsensous + if c.SharderConsensous < 1 { + cfg.SharderConsensous = DefaultSharderConsensous + } + if c.MaxTxnQuery <= 0 { + c.MaxTxnQuery = DefaultMaxTxnQuery + } + if c.MinSubmit <= 0 { + c.MinSubmit = DefaultMinSubmit } cfg = c - cfg.SharderConsensous = sharderConsensous }) } From 8e193a01f263dba23eea1b6b8d575d41dbc27dd5 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 31 Oct 2024 01:04:13 +0530 Subject: [PATCH 282/319] Fix config max txn query --- core/conf/vars.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/conf/vars.go b/core/conf/vars.go index 82d4e656a..202c88023 100644 --- a/core/conf/vars.go +++ b/core/conf/vars.go @@ -42,16 +42,16 @@ func GetClientConfig() (*Config, error) { // InitClientConfig set global client config func InitClientConfig(c *Config) { onceCfg.Do(func() { - if c.SharderConsensous < 1 { + cfg = c + if cfg.SharderConsensous < 1 { cfg.SharderConsensous = DefaultSharderConsensous } - if c.MaxTxnQuery <= 0 { - c.MaxTxnQuery = DefaultMaxTxnQuery + if cfg.MaxTxnQuery <= 0 { + cfg.MaxTxnQuery = DefaultMaxTxnQuery } - if c.MinSubmit <= 0 { - c.MinSubmit = DefaultMinSubmit + if cfg.MinSubmit <= 0 { + cfg.MinSubmit = DefaultMinSubmit } - cfg = c }) } From 3399b4d4739b0a92f99c0e21e3a97c61341a6b59 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Thu, 31 Oct 2024 01:04:27 +0530 Subject: [PATCH 283/319] Fix config max txn query --- core/conf/vars.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/conf/vars.go b/core/conf/vars.go index 202c88023..a79e570f2 100644 --- a/core/conf/vars.go +++ b/core/conf/vars.go @@ -46,10 +46,10 @@ func InitClientConfig(c *Config) { if cfg.SharderConsensous < 1 { cfg.SharderConsensous = DefaultSharderConsensous } - if cfg.MaxTxnQuery <= 0 { + if cfg.MaxTxnQuery < 1 { cfg.MaxTxnQuery = DefaultMaxTxnQuery } - if cfg.MinSubmit <= 0 { + if cfg.MinSubmit < 1 { cfg.MinSubmit = DefaultMinSubmit } }) From 8fd2d070359f3e250714dca76948b0f687d0bb28 Mon Sep 17 00:00:00 2001 From: dabasov Date: Thu, 31 Oct 2024 14:36:30 +0200 Subject: [PATCH 284/319] reverted changes to transaction execution logic --- core/transaction/entity.go | 89 ++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 46 deletions(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 191753771..6cdb71ae0 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -5,15 +5,16 @@ import ( "context" "encoding/json" "fmt" + "net/http" + "strings" + "sync" + "time" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/core/sys" "go.uber.org/zap" - "net/http" - "strings" - "sync" - "time" "github.com/0chain/errors" "github.com/0chain/gosdk/core/common" @@ -249,81 +250,77 @@ func (t *Transaction) VerifySigWith(pubkey string, verifyHandler VerifyFunc) (bo } func SendTransactionSync(txn *Transaction, miners []string) error { - const requestTimeout = 30 * time.Second // Timeout for each request + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + wg := sync.WaitGroup{} + wg.Add(len(miners)) fails := make(chan error, len(miners)) - var wg sync.WaitGroup for _, miner := range miners { - wg.Add(1) - minerURL := fmt.Sprintf("%v/%v", miner, TXN_SUBMIT_URL) - + url := fmt.Sprintf("%v/%v", miner, TXN_SUBMIT_URL) go func(url string) { defer wg.Done() - - // Create a context with a 30-second timeout for each request - ctx, cancel := context.WithTimeout(context.Background(), requestTimeout) - defer cancel() - - _, err := sendTransactionToURL(ctx, url, txn) - if err != nil { - fails <- err + select { + case <-ctx.Done(): + fails <- ctx.Err() // Timeout or cancellation + default: + _, err := sendTransactionToURL(url, txn, nil) + if err != nil { + fails <- err + } } - }(minerURL) + }(url) } - // Close the channel when all requests are finished + done := make(chan struct{}) go func() { wg.Wait() close(fails) + close(done) }() - // Collect errors from all requests - var failureCount int - messages := make(map[string]int) + select { + case <-done: // All requests completed + case <-ctx.Done(): // Timeout reached + return ctx.Err() + } - for err := range fails { - if err != nil { + // Error processing logic here (same as original) + failureCount := 0 + messages := make(map[string]int) + for e := range fails { + if e != nil { failureCount++ - messages[err.Error()]++ + messages[e.Error()]++ } } - // Identify the most frequent error - var maxCount int - var dominantErr string - for msg, count := range messages { - if count > maxCount { - maxCount = count - dominantErr = msg + max := 0 + dominant := "" + for m, s := range messages { + if s > max { + dominant = m } } if failureCount == len(miners) { - return fmt.Errorf("all miners failed: %s", dominantErr) + return fmt.Errorf(dominant) } return nil } -func sendTransactionToURL(ctx context.Context, url string, txn *Transaction) ([]byte, error) { - // Create a new HTTP POST request with context +func sendTransactionToURL(url string, txn *Transaction, wg *sync.WaitGroup) ([]byte, error) { postReq, err := util.NewHTTPPostRequest(url, txn) if err != nil { - return nil, fmt.Errorf("error creating HTTP request: %w", err) + //Logger.Error("Error in serializing the transaction", txn, err.Error()) + return nil, err } - - // Use the provided context in the request's Post method - postReq.Ctx = ctx postResponse, err := postReq.Post() - if err != nil { - return nil, fmt.Errorf("submit transaction failed: %w", err) - } - if postResponse.StatusCode >= 200 && postResponse.StatusCode <= 299 { return []byte(postResponse.Body), nil } - - return nil, fmt.Errorf("submit transaction failed: %s", postResponse.Body) + return nil, errors.Wrap(err, errors.New("submit transaction failed", postResponse.Body)) } type cachedObject struct { @@ -606,7 +603,7 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, if t == nil { return "", "", 0, txn, errors.New("transaction_validation_failed", - "Failed to get the transaction confirmation : "+txn.Hash) + "Failed to get the transaction confirmation") } if t.Status == TxnFail { From 3992e5a5e1ac17bca9c41d83595430f5248fe82b Mon Sep 17 00:00:00 2001 From: dabasov Date: Thu, 31 Oct 2024 17:39:59 +0200 Subject: [PATCH 285/319] updated gosdk --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 9f3034e56..2fa0e6739 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/0chain/gosdk go 1.22.0 require ( - github.com/0chain/common v1.18.1 + github.com/0chain/common v1.18.2 github.com/0chain/errors v1.0.3 github.com/Luzifer/go-openssl/v3 v3.1.0 github.com/btcsuite/btcd v0.23.4 diff --git a/go.sum b/go.sum index 7dcd8d7c3..6cb76057d 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/0chain/common v1.18.1 h1:QKXdEjK6SB3SqzDS/XbK74jy20d3tc+6PA8J8dFpuGw= -github.com/0chain/common v1.18.1/go.mod h1:Lapu2Tj7z5Sm4r+X141e7vsz4NDODTEypeElYAP3iSw= +github.com/0chain/common v1.18.2 h1:VGWfd3Xqio9xbmebPFnUbuk5QN0pK0xzvifaUggJF5g= +github.com/0chain/common v1.18.2/go.mod h1:Lapu2Tj7z5Sm4r+X141e7vsz4NDODTEypeElYAP3iSw= github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM= github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= From 067908102d15a68e30c3aaa62c783a8082e9603d Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Fri, 1 Nov 2024 02:27:03 +0530 Subject: [PATCH 286/319] only set pub signing key when pub key is same --- zboxcore/sdk/blobber_operations.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/zboxcore/sdk/blobber_operations.go b/zboxcore/sdk/blobber_operations.go index fa88b9a1c..e546d6cd8 100644 --- a/zboxcore/sdk/blobber_operations.go +++ b/zboxcore/sdk/blobber_operations.go @@ -55,12 +55,15 @@ func CreateAllocationForOwner( return "", 0, nil, sdkNotInitialized } - privateSigningKey, err := generateOwnerSigningKey(ownerpublickey, owner) - if err != nil { - return "", 0, nil, errors.New("failed_generate_owner_signing_key", "failed to generate owner signing key: "+err.Error()) + if client.PublicKey() == ownerpublickey { + privateSigningKey, err := generateOwnerSigningKey(ownerpublickey, owner) + if err != nil { + return "", 0, nil, errors.New("failed_generate_owner_signing_key", "failed to generate owner signing key: "+err.Error()) + } + pub := privateSigningKey.Public().(ed25519.PublicKey) + pk := hex.EncodeToString(pub) + allocationRequest["owner_signing_public_key"] = pk } - pub := privateSigningKey.Public().(ed25519.PublicKey) - pk := hex.EncodeToString(pub) allocationRequest["owner_id"] = owner allocationRequest["owner_public_key"] = ownerpublickey @@ -68,7 +71,6 @@ func CreateAllocationForOwner( allocationRequest["file_options_changed"], allocationRequest["file_options"] = calculateAllocationFileOptions(63 /*0011 1111*/, fileOptionsParams) allocationRequest["is_enterprise"] = IsEnterprise allocationRequest["storage_version"] = StorageV2 - allocationRequest["owner_signing_public_key"] = pk var sn = transaction.SmartContractTxnData{ Name: transaction.NEW_ALLOCATION_REQUEST, From 6d2b9806e1398fd30d09d95c45e4fba071f6f287 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 1 Nov 2024 11:53:07 +0530 Subject: [PATCH 287/319] Fix query sleep time --- core/conf/vars.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/conf/vars.go b/core/conf/vars.go index a79e570f2..8551c1cd9 100644 --- a/core/conf/vars.go +++ b/core/conf/vars.go @@ -49,6 +49,9 @@ func InitClientConfig(c *Config) { if cfg.MaxTxnQuery < 1 { cfg.MaxTxnQuery = DefaultMaxTxnQuery } + if cfg.QuerySleepTime < 1 { + cfg.QuerySleepTime = DefaultQuerySleepTime + } if cfg.MinSubmit < 1 { cfg.MinSubmit = DefaultMinSubmit } From 009c24e3d9d449e8daf0e62eba51325765631b33 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 1 Nov 2024 18:45:44 +0530 Subject: [PATCH 288/319] Fix txn split key --- core/transaction/entity.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index ac63ef731..0260a0fb0 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -198,6 +198,36 @@ func (t *Transaction) ComputeHashAndSignWithWallet(signHandler SignWithWallet, s return nil } +func (t *Transaction) getAuthorize() (string, error) { + err := t.ComputeHashAndSign(client.Sign) + if err != nil { + return "", errors.Wrap(err, "signing error.") + } + + jsonByte, err := json.Marshal(t) + if err != nil { + return "", err + } + + if sys.Authorize == nil { + return "", errors.New("not_initialized", "no authorize func is set, define it in native code and set in sys") + } + authorize, err := sys.Authorize(string(jsonByte)) + if err != nil { + return "", err + } + + // Verify the split key signed signature + ok, err := t.VerifySigWith(client.PublicKey(), sys.VerifyWith) + if err != nil { + return "", errors.New("", "verification failed for auth response") + } + if !ok { + return "", errors.New("", "verification failed for auth response") + } + return authorize, nil +} + func (t *Transaction) ComputeHashAndSign(signHandler SignFunc) error { t.ComputeHashData() var err error @@ -561,7 +591,8 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, txn.TransactionNonce = client.Cache.GetNextNonce(txn.ClientID) } - if err = txn.ComputeHashAndSign(client.Sign); err != nil { + txn.Signature, err = txn.getAuthorize() + if err != nil { return } From 8bd60c35ca867bcd95f33f8bed09f500f62121b7 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Fri, 1 Nov 2024 14:27:46 +0100 Subject: [PATCH 289/319] fix: added split key mode check --- core/transaction/entity.go | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 0260a0fb0..98a5a2b3a 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -5,15 +5,16 @@ import ( "context" "encoding/json" "fmt" + "net/http" + "strings" + "sync" + "time" + "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/core/sys" "go.uber.org/zap" - "net/http" - "strings" - "sync" - "time" "github.com/0chain/errors" "github.com/0chain/gosdk/core/common" @@ -199,11 +200,6 @@ func (t *Transaction) ComputeHashAndSignWithWallet(signHandler SignWithWallet, s } func (t *Transaction) getAuthorize() (string, error) { - err := t.ComputeHashAndSign(client.Sign) - if err != nil { - return "", errors.Wrap(err, "signing error.") - } - jsonByte, err := json.Marshal(t) if err != nil { return "", err @@ -212,19 +208,21 @@ func (t *Transaction) getAuthorize() (string, error) { if sys.Authorize == nil { return "", errors.New("not_initialized", "no authorize func is set, define it in native code and set in sys") } + authorize, err := sys.Authorize(string(jsonByte)) if err != nil { return "", err } - // Verify the split key signed signature ok, err := t.VerifySigWith(client.PublicKey(), sys.VerifyWith) if err != nil { return "", errors.New("", "verification failed for auth response") } + if !ok { return "", errors.New("", "verification failed for auth response") } + return authorize, nil } @@ -591,11 +589,18 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, txn.TransactionNonce = client.Cache.GetNextNonce(txn.ClientID) } - txn.Signature, err = txn.getAuthorize() + err = txn.ComputeHashAndSign(client.Sign) if err != nil { return } + if client.GetClient().IsSplit { + txn.Signature, err = txn.getAuthorize() + if err != nil { + return + } + } + msg := fmt.Sprintf("executing transaction '%s' with hash %s ", sn.Name, txn.Hash) Logger.Info(msg) Logger.Info("estimated txn fee: ", txn.TransactionFee) From 4e3ef602cfd02db50039c3210fb4e836c6378640 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 1 Nov 2024 21:24:41 +0530 Subject: [PATCH 290/319] Revert "reverted changes to transaction execution logic" This reverts commit 8fd2d070359f3e250714dca76948b0f687d0bb28. --- core/transaction/entity.go | 89 ++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 43 deletions(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 6cdb71ae0..191753771 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -5,16 +5,15 @@ import ( "context" "encoding/json" "fmt" - "net/http" - "strings" - "sync" - "time" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/logger" "github.com/0chain/gosdk/core/sys" "go.uber.org/zap" + "net/http" + "strings" + "sync" + "time" "github.com/0chain/errors" "github.com/0chain/gosdk/core/common" @@ -250,77 +249,81 @@ func (t *Transaction) VerifySigWith(pubkey string, verifyHandler VerifyFunc) (bo } func SendTransactionSync(txn *Transaction, miners []string) error { - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) - defer cancel() + const requestTimeout = 30 * time.Second // Timeout for each request - wg := sync.WaitGroup{} - wg.Add(len(miners)) fails := make(chan error, len(miners)) + var wg sync.WaitGroup for _, miner := range miners { - url := fmt.Sprintf("%v/%v", miner, TXN_SUBMIT_URL) + wg.Add(1) + minerURL := fmt.Sprintf("%v/%v", miner, TXN_SUBMIT_URL) + go func(url string) { defer wg.Done() - select { - case <-ctx.Done(): - fails <- ctx.Err() // Timeout or cancellation - default: - _, err := sendTransactionToURL(url, txn, nil) - if err != nil { - fails <- err - } + + // Create a context with a 30-second timeout for each request + ctx, cancel := context.WithTimeout(context.Background(), requestTimeout) + defer cancel() + + _, err := sendTransactionToURL(ctx, url, txn) + if err != nil { + fails <- err } - }(url) + }(minerURL) } - done := make(chan struct{}) + // Close the channel when all requests are finished go func() { wg.Wait() close(fails) - close(done) }() - select { - case <-done: // All requests completed - case <-ctx.Done(): // Timeout reached - return ctx.Err() - } - - // Error processing logic here (same as original) - failureCount := 0 + // Collect errors from all requests + var failureCount int messages := make(map[string]int) - for e := range fails { - if e != nil { + + for err := range fails { + if err != nil { failureCount++ - messages[e.Error()]++ + messages[err.Error()]++ } } - max := 0 - dominant := "" - for m, s := range messages { - if s > max { - dominant = m + // Identify the most frequent error + var maxCount int + var dominantErr string + for msg, count := range messages { + if count > maxCount { + maxCount = count + dominantErr = msg } } if failureCount == len(miners) { - return fmt.Errorf(dominant) + return fmt.Errorf("all miners failed: %s", dominantErr) } return nil } -func sendTransactionToURL(url string, txn *Transaction, wg *sync.WaitGroup) ([]byte, error) { +func sendTransactionToURL(ctx context.Context, url string, txn *Transaction) ([]byte, error) { + // Create a new HTTP POST request with context postReq, err := util.NewHTTPPostRequest(url, txn) if err != nil { - //Logger.Error("Error in serializing the transaction", txn, err.Error()) - return nil, err + return nil, fmt.Errorf("error creating HTTP request: %w", err) } + + // Use the provided context in the request's Post method + postReq.Ctx = ctx postResponse, err := postReq.Post() + if err != nil { + return nil, fmt.Errorf("submit transaction failed: %w", err) + } + if postResponse.StatusCode >= 200 && postResponse.StatusCode <= 299 { return []byte(postResponse.Body), nil } - return nil, errors.Wrap(err, errors.New("submit transaction failed", postResponse.Body)) + + return nil, fmt.Errorf("submit transaction failed: %s", postResponse.Body) } type cachedObject struct { @@ -603,7 +606,7 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, if t == nil { return "", "", 0, txn, errors.New("transaction_validation_failed", - "Failed to get the transaction confirmation") + "Failed to get the transaction confirmation : "+txn.Hash) } if t.Status == TxnFail { From f6160cbc3d56d0c228e2cb53565e0ad7dee19dde Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 1 Nov 2024 23:29:28 +0530 Subject: [PATCH 291/319] Fix logging --- core/transaction/entity.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 191753771..53e151801 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -300,7 +300,7 @@ func SendTransactionSync(txn *Transaction, miners []string) error { } if failureCount == len(miners) { - return fmt.Errorf("all miners failed: %s", dominantErr) + return fmt.Errorf(dominantErr) } return nil } From 551d7b20b41cd1c931ce261175eacebfbbe476d0 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sat, 2 Nov 2024 03:41:40 +0100 Subject: [PATCH 292/319] fix: debug --- wasmsdk/proxy.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index ab165eb2e..e6a16c09a 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -59,6 +59,9 @@ func main() { if c == nil || len(c.Keys) == 0 { return "", errors.New("no keys found") } + + fmt.Println(c, "CLIENT") + pk := c.Keys[0].PrivateKey result, err := jsbridge.Await(jsSign.Invoke(hash, pk)) @@ -349,6 +352,9 @@ func main() { if c == nil || len(c.Keys) == 0 { return "", errors.New("no keys found") } + + fmt.Println(c, "CLIENT") + pk := c.Keys[0].PrivateKey result, err := jsbridge.Await(jsSign.Invoke(hash, pk)) From a8bf59428eff807f8e17c7850087efcc2d603bcd Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sat, 2 Nov 2024 03:59:37 +0100 Subject: [PATCH 293/319] fix: debug --- wasmsdk/proxy.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index e6a16c09a..ab66caf7b 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -68,6 +68,9 @@ func main() { if len(err) > 0 && !err[0].IsNull() { return "", errors.New("sign: " + err[0].String()) } + + fmt.Println(c.Keys[0].PrivateKey, result[0].String(), "RESULT") + return result[0].String(), nil } From 466d9c6e6cb88ab3d50ee2bbcf1db5a8c68df5ee Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sat, 2 Nov 2024 10:07:04 +0100 Subject: [PATCH 294/319] fix: debug --- core/transaction/entity.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 524c7fdb7..7251decf3 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -599,6 +599,8 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, } if client.GetClient().IsSplit { + fmt.Println(txn.Signature, "RESULT GENERATED") + txn.Signature, err = txn.getAuthorize() if err != nil { return From 88697365eca82a08f26ac31d94039837bdb0d38f Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sat, 2 Nov 2024 10:25:47 +0100 Subject: [PATCH 295/319] fix: debug --- core/client/set.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index d87931c22..f4c6780ea 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -50,14 +50,21 @@ func init() { wallet = client.wallets[clients[0]] } - if wallet.PeerPublicKey == "" { - return sys.Sign(hash, client.signatureScheme, GetClientSysKeys(clients...)) + if !wallet.IsSplit { + s, _ := sys.Sign(hash, client.signatureScheme, GetClientSysKeys(clients...)) + + fmt.Println(s, "RESULT BROKEN WITHOUT SPLIT KEY") + + return s, nil } // get sign lock <-sigC fmt.Println("Sign: with sys.SignWithAuth:", sys.SignWithAuth, "sysKeys:", GetClientSysKeys(clients...)) sig, err := sys.SignWithAuth(hash, client.signatureScheme, GetClientSysKeys(clients...)) + + fmt.Println(sig, "RESULT BROKEN") + sigC <- struct{}{} return sig, err } @@ -125,11 +132,16 @@ func GetClientSysKeys(clients ...string) []sys.KeyPair { var keys []sys.KeyPair for _, kv := range wallet.Keys { + fmt.Println(kv.PrivateKey, kv.PublicKey, "GET CLIENT SYS KEYS") + keys = append(keys, sys.KeyPair{ PrivateKey: kv.PrivateKey, PublicKey: kv.PublicKey, }) } + + fmt.Println(keys, len(keys), "SIZE") + return keys } From 81450934e93216619c65deb538fd0aeab1d18d03 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sat, 2 Nov 2024 11:29:10 +0100 Subject: [PATCH 296/319] fix: debug --- core/client/set.go | 8 ++------ wasmsdk/proxy.go | 6 ------ 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index f4c6780ea..41c9a8f7b 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -51,11 +51,7 @@ func init() { } if !wallet.IsSplit { - s, _ := sys.Sign(hash, client.signatureScheme, GetClientSysKeys(clients...)) - - fmt.Println(s, "RESULT BROKEN WITHOUT SPLIT KEY") - - return s, nil + return sys.Sign(hash, client.signatureScheme, GetClientSysKeys(clients...)) } // get sign lock @@ -63,7 +59,7 @@ func init() { fmt.Println("Sign: with sys.SignWithAuth:", sys.SignWithAuth, "sysKeys:", GetClientSysKeys(clients...)) sig, err := sys.SignWithAuth(hash, client.signatureScheme, GetClientSysKeys(clients...)) - fmt.Println(sig, "RESULT BROKEN") + fmt.Println(sig, GetClientSysKeys(clients...), "RESULT BROKEN") sigC <- struct{}{} return sig, err diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index ab66caf7b..32884bfbc 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -60,8 +60,6 @@ func main() { return "", errors.New("no keys found") } - fmt.Println(c, "CLIENT") - pk := c.Keys[0].PrivateKey result, err := jsbridge.Await(jsSign.Invoke(hash, pk)) @@ -69,8 +67,6 @@ func main() { return "", errors.New("sign: " + err[0].String()) } - fmt.Println(c.Keys[0].PrivateKey, result[0].String(), "RESULT") - return result[0].String(), nil } @@ -356,8 +352,6 @@ func main() { return "", errors.New("no keys found") } - fmt.Println(c, "CLIENT") - pk := c.Keys[0].PrivateKey result, err := jsbridge.Await(jsSign.Invoke(hash, pk)) From a7f5bb0e75a4cf7ac1f021d7fe0307f65675a284 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sat, 2 Nov 2024 11:53:52 +0100 Subject: [PATCH 297/319] fix: debug --- core/client/set.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/client/set.go b/core/client/set.go index 41c9a8f7b..57cac3ad2 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -74,6 +74,9 @@ func signHash(hash string, signatureScheme string, keys []sys.KeyPair) (string, retSignature := "" for _, kv := range keys { ss := zcncrypto.NewSignatureScheme(signatureScheme) + + fmt.Println(kv.PrivateKey, hash, "PRIVATE KEY") + err := ss.SetPrivateKey(kv.PrivateKey) if err != nil { return "", err @@ -88,6 +91,9 @@ func signHash(hash string, signatureScheme string, keys []sys.KeyPair) (string, return "", err } } + + fmt.Println(hash, "RESULT PRIVATE KEY") + return retSignature, nil } From 45d363677789375703661ad44e5832d7b9f170ac Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sat, 2 Nov 2024 12:48:29 +0100 Subject: [PATCH 298/319] fix: debug --- wasmsdk/proxy.go | 57 ++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index 32884bfbc..c4a80f47c 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -84,34 +84,35 @@ func main() { return "", fmt.Errorf("failed to sign with split key: %v", err) } - data, err := json.Marshal(zcncore.AuthMessage{ - Hash: hash, - Signature: sig, - ClientID: client.Wallet().ClientID, - }) - if err != nil { - return "", err - } - - if sys.AuthCommon == nil { - return "", errors.New("authCommon is not set") - } - - rsp, err := sys.AuthCommon(string(data)) - if err != nil { - return "", err - } - - var sigpk struct { - Sig string `json:"sig"` - } - - err = json.Unmarshal([]byte(rsp), &sigpk) - if err != nil { - return "", err - } - - return sigpk.Sig, nil + return sig, nil + // data, err := json.Marshal(zcncore.AuthMessage{ + // Hash: hash, + // Signature: sig, + // ClientID: client.Wallet().ClientID, + // }) + // if err != nil { + // return "", err + // } + + // if sys.AuthCommon == nil { + // return "", errors.New("authCommon is not set") + // } + + // rsp, err := sys.AuthCommon(string(data)) + // if err != nil { + // return "", err + // } + + // var sigpk struct { + // Sig string `json:"sig"` + // } + + // err = json.Unmarshal([]byte(rsp), &sigpk) + // if err != nil { + // return "", err + // } + + // return sigpk.Sig, nil } } else { PrintError("__zcn_wasm__.jsProxy.sign is not installed yet") From f3e4ae8c19e912da8672a7a5c8c285438726887b Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sat, 2 Nov 2024 13:44:36 +0100 Subject: [PATCH 299/319] fix: fixed bugs --- core/client/set.go | 11 ++++++++ core/transaction/entity.go | 4 +-- wasmsdk/proxy.go | 58 ++++++++++++++++++-------------------- zcncore/wallet_base.go | 16 +++-------- 4 files changed, 45 insertions(+), 44 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index 57cac3ad2..a0b933e66 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -70,6 +70,17 @@ func init() { sys.VerifyEd25519With = verifyEd25519With } +var SignFn = func(hash string) (string, error) { + ss := zcncrypto.NewSignatureScheme(client.signatureScheme) + + err := ss.SetPrivateKey(client.wallet.Keys[0].PrivateKey) + if err != nil { + return "", err + } + + return ss.Sign(hash) +} + func signHash(hash string, signatureScheme string, keys []sys.KeyPair) (string, error) { retSignature := "" for _, kv := range keys { diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 7251decf3..1be20ec94 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -164,7 +164,7 @@ const ( FEES_TABLE = `/v1/fees_table` ) -type SignFunc = func(msg string, clientId ...string) (string, error) +type SignFunc = func(msg string) (string, error) type VerifyFunc = func(publicKey, signature, msgHash string) (bool, error) type SignWithWallet = func(msg string, wallet interface{}) (string, error) @@ -593,7 +593,7 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, txn.TransactionNonce = client.Cache.GetNextNonce(txn.ClientID) } - err = txn.ComputeHashAndSign(client.Sign) + err = txn.ComputeHashAndSign(client.SignFn) if err != nil { return } diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index c4a80f47c..928b7b7e5 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -84,35 +84,34 @@ func main() { return "", fmt.Errorf("failed to sign with split key: %v", err) } - return sig, nil - // data, err := json.Marshal(zcncore.AuthMessage{ - // Hash: hash, - // Signature: sig, - // ClientID: client.Wallet().ClientID, - // }) - // if err != nil { - // return "", err - // } - - // if sys.AuthCommon == nil { - // return "", errors.New("authCommon is not set") - // } - - // rsp, err := sys.AuthCommon(string(data)) - // if err != nil { - // return "", err - // } - - // var sigpk struct { - // Sig string `json:"sig"` - // } - - // err = json.Unmarshal([]byte(rsp), &sigpk) - // if err != nil { - // return "", err - // } - - // return sigpk.Sig, nil + data, err := json.Marshal(zcncore.AuthMessage{ + Hash: hash, + Signature: sig, + ClientID: client.Wallet().ClientID, + }) + if err != nil { + return "", err + } + + if sys.AuthCommon == nil { + return "", errors.New("authCommon is not set") + } + + rsp, err := sys.AuthCommon(string(data)) + if err != nil { + return "", err + } + + var sigpk struct { + Sig string `json:"sig"` + } + + err = json.Unmarshal([]byte(rsp), &sigpk) + if err != nil { + return "", err + } + + return sigpk.Sig, nil } } else { PrintError("__zcn_wasm__.jsProxy.sign is not installed yet") @@ -363,7 +362,6 @@ func main() { } //update sign with js sign zcncrypto.Sign = signFunc - zcncore.SignFn = signFunc sys.Sign = func(hash, signatureScheme string, keys []sys.KeyPair) (string, error) { // js already has signatureScheme and keys return signFunc(hash) diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index a5bdef2d7..34c16b4ad 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -3,13 +3,14 @@ package zcncore import ( "encoding/hex" "fmt" - "github.com/0chain/gosdk/constants" - "github.com/0chain/gosdk/core/common" - "github.com/0chain/gosdk/core/util" "net/http" "strings" "time" + "github.com/0chain/gosdk/constants" + "github.com/0chain/gosdk/core/common" + "github.com/0chain/gosdk/core/util" + "errors" "github.com/0chain/gosdk/core/client" @@ -375,15 +376,6 @@ func GetIdForUrl(url string) string { return "" } -var SignFn = func(hash string) (string, error) { - sigScheme := zcncrypto.NewSignatureScheme(client.SignatureScheme()) - err := sigScheme.SetPrivateKey(client.PrivateKey()) - if err != nil { - return "", err - } - return sigScheme.Sign(hash) -} - // SetupAuth prepare auth app with clientid, key and a set of public, private key and local publickey // which is running on PC/Mac. func SetupAuth(authHost, clientID, clientKey, publicKey, privateKey, localPublicKey string, cb AuthCallback) error { From 891ea4a7d93a592c400955dfbe46e0b4b4f7ec11 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sat, 2 Nov 2024 13:47:58 +0100 Subject: [PATCH 300/319] fix: fixed bugs --- wasmsdk/proxy.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index 928b7b7e5..18a1ef5c2 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -72,7 +72,7 @@ func main() { //update sign with js sign zcncrypto.Sign = signFunc - zcncore.SignFn = signFunc + client.SignFn = signFunc sys.Sign = func(hash, signatureScheme string, keys []sys.KeyPair) (string, error) { // js already has signatureScheme and keys return signFunc(hash) @@ -362,6 +362,7 @@ func main() { } //update sign with js sign zcncrypto.Sign = signFunc + client.SignFn = signFunc sys.Sign = func(hash, signatureScheme string, keys []sys.KeyPair) (string, error) { // js already has signatureScheme and keys return signFunc(hash) From 52c1005a450f969cd4e4a6516c3d81e21db8fce7 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sat, 2 Nov 2024 14:16:08 +0100 Subject: [PATCH 301/319] fix: fixed bugs --- core/client/set.go | 11 ----------- core/transaction/entity.go | 2 -- 2 files changed, 13 deletions(-) diff --git a/core/client/set.go b/core/client/set.go index a0b933e66..6b8468cc5 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -58,9 +58,6 @@ func init() { <-sigC fmt.Println("Sign: with sys.SignWithAuth:", sys.SignWithAuth, "sysKeys:", GetClientSysKeys(clients...)) sig, err := sys.SignWithAuth(hash, client.signatureScheme, GetClientSysKeys(clients...)) - - fmt.Println(sig, GetClientSysKeys(clients...), "RESULT BROKEN") - sigC <- struct{}{} return sig, err } @@ -86,8 +83,6 @@ func signHash(hash string, signatureScheme string, keys []sys.KeyPair) (string, for _, kv := range keys { ss := zcncrypto.NewSignatureScheme(signatureScheme) - fmt.Println(kv.PrivateKey, hash, "PRIVATE KEY") - err := ss.SetPrivateKey(kv.PrivateKey) if err != nil { return "", err @@ -103,8 +98,6 @@ func signHash(hash string, signatureScheme string, keys []sys.KeyPair) (string, } } - fmt.Println(hash, "RESULT PRIVATE KEY") - return retSignature, nil } @@ -145,16 +138,12 @@ func GetClientSysKeys(clients ...string) []sys.KeyPair { var keys []sys.KeyPair for _, kv := range wallet.Keys { - fmt.Println(kv.PrivateKey, kv.PublicKey, "GET CLIENT SYS KEYS") - keys = append(keys, sys.KeyPair{ PrivateKey: kv.PrivateKey, PublicKey: kv.PublicKey, }) } - fmt.Println(keys, len(keys), "SIZE") - return keys } diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 1be20ec94..c48bb8b39 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -599,8 +599,6 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, } if client.GetClient().IsSplit { - fmt.Println(txn.Signature, "RESULT GENERATED") - txn.Signature, err = txn.getAuthorize() if err != nil { return From 5e66d116bc7672dba0bc3f96ce958c3cfad77bbb Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sat, 2 Nov 2024 23:16:13 +0530 Subject: [PATCH 302/319] Add storage node version update txn --- core/transaction/entity.go | 1 + zboxcore/sdk/sdk.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 53e151801..20ff5ae3c 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -132,6 +132,7 @@ const ( STORAGESC_SHUTDOWN_VALIDATOR = "shutdown_validator" STORAGESC_RESET_BLOBBER_STATS = "reset_blobber_stats" STORAGESC_RESET_ALLOCATION_STATS = "reset_allocation_stats" + STORAGESC_RESET_BLOBBER_VERSION = "update_blobber_version" MINERSC_LOCK = "addToDelegatePool" MINERSC_UNLOCK = "deleteFromDelegatePool" diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index 37789b11f..e3c0121fc 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -1218,6 +1218,23 @@ func ResetBlobberStats(rbs *ResetBlobberStatsDto) (string, int64, error) { return hash, n, err } +type StorageNodeVersion struct { + Id string `json:"id"` +} + +func ResetBlobberVersion(snv *StorageNodeVersion) (string, int64, error) { + if !client.IsSDKInitialized() { + return "", 0, sdkNotInitialized + } + + var sn = transaction.SmartContractTxnData{ + Name: transaction.STORAGESC_RESET_BLOBBER_VERSION, + InputArgs: snv, + } + hash, _, n, _, err := storageSmartContractTxn(sn) + return hash, n, err +} + func ResetAllocationStats(allocationId string) (string, int64, error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized From 0db2aea48072f2cd05414e6b48c98674fbda6787 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sat, 2 Nov 2024 22:38:56 +0100 Subject: [PATCH 303/319] fix: fixed bugs --- core/transaction/entity.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index c48bb8b39..cbb2de463 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -214,15 +214,6 @@ func (t *Transaction) getAuthorize() (string, error) { return "", err } - ok, err := t.VerifySigWith(client.PublicKey(), sys.VerifyWith) - if err != nil { - return "", errors.New("", "verification failed for auth response") - } - - if !ok { - return "", errors.New("", "verification failed for auth response") - } - return authorize, nil } @@ -605,6 +596,16 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, } } + ok, err := txn.VerifySigWith(client.PublicKey(), sys.VerifyWith) + if err != nil { + return + } + + if !ok { + err = errors.New("", "verification failed for auth response") + return + } + msg := fmt.Sprintf("executing transaction '%s' with hash %s ", sn.Name, txn.Hash) Logger.Info(msg) Logger.Info("estimated txn fee: ", txn.TransactionFee) From e0ce67b6e7a97496f710b18f62772f0980eebc79 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sat, 2 Nov 2024 23:59:47 +0100 Subject: [PATCH 304/319] fix: debug --- core/transaction/entity.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index cbb2de463..1e665f007 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -596,8 +596,11 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, } } + fmt.Println(client.PublicKey(), "PUBLIC KEY") + ok, err := txn.VerifySigWith(client.PublicKey(), sys.VerifyWith) if err != nil { + err = errors.New("", "verification failed for auth response") return } From 9c92d4dd209b64f9524c4a0b13aa3fd3821c70fc Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 3 Nov 2024 04:49:02 +0530 Subject: [PATCH 305/319] Added insert killed ID --- core/transaction/entity.go | 1 + zboxcore/sdk/sdk.go | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 20ff5ae3c..e0f9d0afd 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -133,6 +133,7 @@ const ( STORAGESC_RESET_BLOBBER_STATS = "reset_blobber_stats" STORAGESC_RESET_ALLOCATION_STATS = "reset_allocation_stats" STORAGESC_RESET_BLOBBER_VERSION = "update_blobber_version" + STORAGESC_INSERT_KILLED_PROVIDER_ID = "insert_killed_provider_id" MINERSC_LOCK = "addToDelegatePool" MINERSC_UNLOCK = "deleteFromDelegatePool" diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index e3c0121fc..65948326a 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -1218,18 +1218,31 @@ func ResetBlobberStats(rbs *ResetBlobberStatsDto) (string, int64, error) { return hash, n, err } -type StorageNodeVersion struct { +type StorageNodeIdField struct { Id string `json:"id"` } -func ResetBlobberVersion(snv *StorageNodeVersion) (string, int64, error) { +func ResetBlobberVersion(snId *StorageNodeIdField) (string, int64, error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } var sn = transaction.SmartContractTxnData{ Name: transaction.STORAGESC_RESET_BLOBBER_VERSION, - InputArgs: snv, + InputArgs: snId, + } + hash, _, n, _, err := storageSmartContractTxn(sn) + return hash, n, err +} + +func InsertKilledProviderID(snId *StorageNodeIdField) (string, int64, error) { + if !client.IsSDKInitialized() { + return "", 0, sdkNotInitialized + } + + var sn = transaction.SmartContractTxnData{ + Name: transaction.STORAGESC_INSERT_KILLED_PROVIDER_ID, + InputArgs: snId, } hash, _, n, _, err := storageSmartContractTxn(sn) return hash, n, err From 95912337cbca77e43a51ac0fc5260fbf31d84cbd Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sun, 3 Nov 2024 10:18:48 +0100 Subject: [PATCH 306/319] fix: debug --- core/transaction/entity.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 1e665f007..e169f3487 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -596,9 +596,11 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, } } - fmt.Println(client.PublicKey(), "PUBLIC KEY") + // fmt.Println(client.PublicKey(), "PUBLIC KEY") - ok, err := txn.VerifySigWith(client.PublicKey(), sys.VerifyWith) + // try client.PublicKey(clientId) + + ok, err := txn.VerifySigWith(txn.PublicKey, sys.VerifyWith) if err != nil { err = errors.New("", "verification failed for auth response") return From d2340e579a2183302405ea4af944c60c9742ae1d Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sun, 3 Nov 2024 10:51:27 +0100 Subject: [PATCH 307/319] fix: debug --- core/transaction/entity.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index e169f3487..3bd30c801 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -600,6 +600,8 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, // try client.PublicKey(clientId) + fmt.Println(txn.PublicKey, txn.Signature, txn.Hash, client.PublicKey(clientId), "SETOF") + ok, err := txn.VerifySigWith(txn.PublicKey, sys.VerifyWith) if err != nil { err = errors.New("", "verification failed for auth response") From 0ae69ed21090997632e7e00ce8402a014f4b7451 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sun, 3 Nov 2024 11:17:11 +0100 Subject: [PATCH 308/319] fix: debug --- zcncore/zauth.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/zcncore/zauth.go b/zcncore/zauth.go index fd07af164..e3723388f 100644 --- a/zcncore/zauth.go +++ b/zcncore/zauth.go @@ -553,12 +553,14 @@ func ZauthSignTxn(serverAddr string) sys.AuthorizeFunc { return "", errors.Errorf("unexpected status code: %d, res: %s", resp.StatusCode, string(rsp)) } - d, err := io.ReadAll(resp.Body) + var d string + + err = json.NewDecoder(resp.Body).Decode(&d) if err != nil { return "", errors.Wrap(err, "failed to read response body") } - return string(d), nil + return d, nil } } From 175883762fa544d4c1dd2d71ec9490971c9bc2c4 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sun, 3 Nov 2024 11:35:08 +0100 Subject: [PATCH 309/319] fix: fixed bugs --- core/transaction/entity.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 3bd30c801..c7f301ad9 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -596,12 +596,6 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData, } } - // fmt.Println(client.PublicKey(), "PUBLIC KEY") - - // try client.PublicKey(clientId) - - fmt.Println(txn.PublicKey, txn.Signature, txn.Hash, client.PublicKey(clientId), "SETOF") - ok, err := txn.VerifySigWith(txn.PublicKey, sys.VerifyWith) if err != nil { err = errors.New("", "verification failed for auth response") From 577e89395e059b6ff68691e8ed0776198c5baf4e Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Mon, 4 Nov 2024 07:48:15 +0530 Subject: [PATCH 310/319] add err check in repair --- wasmsdk/blobber.go | 14 +++++++++++++- zboxcore/sdk/repairworker.go | 4 ++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/wasmsdk/blobber.go b/wasmsdk/blobber.go index 096c6a388..1f73be2ba 100644 --- a/wasmsdk/blobber.go +++ b/wasmsdk/blobber.go @@ -1104,7 +1104,19 @@ func repairAllocation(allocationID, callbackFuncName string) error { return err } wg.Wait() - return statusBar.err + if statusBar.err != nil { + fmt.Println("Error in repair allocation: ", statusBar.err) + return statusBar.err + } + status, _, err := alloc.CheckAllocStatus() + if err != nil { + return err + } + if status == sdk.Repair || status == sdk.Broken { + fmt.Println("allocation repair failed") + return errors.New("allocation repair failed") + } + return nil } // checkAllocStatus check the status of the allocation, either it is ok, needs repair or broken diff --git a/zboxcore/sdk/repairworker.go b/zboxcore/sdk/repairworker.go index b9243c8d9..9e4e21b33 100644 --- a/zboxcore/sdk/repairworker.go +++ b/zboxcore/sdk/repairworker.go @@ -81,6 +81,10 @@ func (r *RepairRequest) processRepair(ctx context.Context, a *Allocation) { SetSingleClietnMode(true) defer SetSingleClietnMode(false) } + if shouldSaveProgress { + SetSaveProgress(false) + defer SetSaveProgress(true) + } r.allocation = a if a.StorageVersion == StorageV2 { r.iterateDirV2(ctx) From 01de19514847ae37317a45edb68e51f121c67eee Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Tue, 5 Nov 2024 04:32:46 +0530 Subject: [PATCH 311/319] Fix make sc rest api calls --- core/client/http.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/core/client/http.go b/core/client/http.go index de7f14b11..744ce0d4b 100644 --- a/core/client/http.go +++ b/core/client/http.go @@ -7,6 +7,7 @@ import ( "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/util" "github.com/shopspring/decimal" + "log" "net/http" "net/url" "sync" @@ -30,7 +31,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] restApiUrl = restApiUrls[0] } - sharders := nodeClient.Network().Sharders + sharders := nodeClient.sharders.Healthy() responses := make(map[int]int) entityResult := make(map[string][]byte) @@ -55,7 +56,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] urlString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath) urlObj, err := url.Parse(urlString) if err != nil { - fmt.Println(err.Error()) + log.Println(err.Error()) return } q := urlObj.Query() @@ -66,13 +67,14 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string] req, err := util.NewHTTPGetRequest(urlObj.String()) if err != nil { - fmt.Println("1Error creating request", err.Error()) + log.Println("Error creating request", err.Error()) return } response, err := req.Get() if err != nil { - fmt.Println("2Error getting response", err.Error()) + nodeClient.sharders.Fail(sharder) + log.Println("Error getting response", err.Error()) return } From df6ff8d790634167ec5ae5251e7445a65ef47221 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Thu, 7 Nov 2024 02:05:42 +0530 Subject: [PATCH 312/319] set lookuphash if empty --- wasmsdk/blobber.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/wasmsdk/blobber.go b/wasmsdk/blobber.go index 1f73be2ba..8047507ee 100644 --- a/wasmsdk/blobber.go +++ b/wasmsdk/blobber.go @@ -1018,6 +1018,10 @@ func downloadBlocks(allocId, remotePath, authTicket, lookupHash, writeChunkFuncN statusBar = &StatusBar{wg: wg, totalBytesMap: make(map[string]int)} ) + if lookupHash == "" { + lookupHash = getLookupHash(allocId, remotePath) + } + var fh sys.File if writeChunkFuncName == "" { pathHash := encryption.FastHash(fmt.Sprintf("%s:%d:%d", lookupHash, startBlock, endBlock)) From 8c3952c7ed61f07d6e464d046ef79203fd9d23c3 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Thu, 7 Nov 2024 22:49:17 +0530 Subject: [PATCH 313/319] add storage version in list --- zboxcore/sdk/listworker.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/zboxcore/sdk/listworker.go b/zboxcore/sdk/listworker.go index f8664f7d7..db02a3149 100644 --- a/zboxcore/sdk/listworker.go +++ b/zboxcore/sdk/listworker.go @@ -69,11 +69,12 @@ type ListResult struct { ActualThumbnailHash string `json:"actual_thumbnail_hash"` ActualThumbnailSize int64 `json:"actual_thumbnail_size"` - CreatedAt common.Timestamp `json:"created_at"` - UpdatedAt common.Timestamp `json:"updated_at"` - Children []*ListResult `json:"list"` - Consensus `json:"-"` - deleteMask zboxutil.Uint128 `json:"-"` + CreatedAt common.Timestamp `json:"created_at"` + UpdatedAt common.Timestamp `json:"updated_at"` + Children []*ListResult `json:"list"` + StorageVersion int `json:"storage_version"` + Consensus `json:"-"` + deleteMask zboxutil.Uint128 `json:"-"` } type ListRequestOptions func(req *ListRequest) @@ -233,8 +234,9 @@ func (req *ListRequest) GetListFromBlobbers() (*ListResult, error) { return nil, err } result := &ListResult{ - ClientId: req.ClientId, - deleteMask: zboxutil.NewUint128(1).Lsh(uint64(len(req.blobbers))).Sub64(1), + ClientId: req.ClientId, + deleteMask: zboxutil.NewUint128(1).Lsh(uint64(len(req.blobbers))).Sub64(1), + StorageVersion: req.storageVersion, } selected := make(map[string]*ListResult) childResultMap := make(map[string]*ListResult) @@ -313,7 +315,8 @@ func (lr *ListResult) populateChildren(children []fileref.RefEntity, childResult consensus: 0, fullconsensus: req.fullconsensus, }, - LookupHash: child.GetLookupHash(), + LookupHash: child.GetLookupHash(), + StorageVersion: req.storageVersion, } childResultMap[actualHash] = childResult } From 5217a55d34962b1f6788997d932e493bbf5310cb Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi <53532703+YarikRevich@users.noreply.github.com> Date: Thu, 7 Nov 2024 23:42:29 +0100 Subject: [PATCH 314/319] Feature: add method to set wallet mode in WASM (#1666) * feature: added opportunity to set wallet mode in a separate function * fix: fixed bugs * fix: fixed bugs --- core/client/set.go | 5 +++++ wasmsdk/proxy.go | 1 + wasmsdk/wallet.go | 6 ++++++ 3 files changed, 12 insertions(+) diff --git a/core/client/set.go b/core/client/set.go index 6b8468cc5..73511fcfb 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -156,6 +156,11 @@ func SetWallet(w zcncrypto.Wallet) { client.wallets[w.ClientID] = &w } +// SetWalletMode sets current wallet split key mode. +func SetWalletMode(mode bool) { + client.wallet.IsSplit = mode +} + // splitKeyWallet parameter is valid only if SignatureScheme is "BLS0Chain" func SetSplitKeyWallet(isSplitKeyWallet bool) error { if client.signatureScheme == constants.BLS0CHAIN.String() { diff --git a/wasmsdk/proxy.go b/wasmsdk/proxy.go index 18a1ef5c2..680e191e8 100644 --- a/wasmsdk/proxy.go +++ b/wasmsdk/proxy.go @@ -205,6 +205,7 @@ func main() { //sdk "init": initSDKs, "setWallet": setWallet, + "setWalletMode": setWalletMode, "getPublicEncryptionKey": zcncore.GetPublicEncryptionKey, "hideLogs": hideLogs, "showLogs": showLogs, diff --git a/wasmsdk/wallet.go b/wasmsdk/wallet.go index 2cbba300c..24f9c34ab 100644 --- a/wasmsdk/wallet.go +++ b/wasmsdk/wallet.go @@ -57,3 +57,9 @@ func setWallet(clientID, clientKey, peerPublicKey, publicKey, privateKey, mnemon return nil } + +func setWalletMode(mode bool) { + client.SetWalletMode(mode) + + fmt.Println("gosdk setWalletMode: ", "is split:", mode) +} From 1140e0029d479e6b414a5444a77011b54fd08952 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi <53532703+YarikRevich@users.noreply.github.com> Date: Sat, 9 Nov 2024 11:36:34 +0100 Subject: [PATCH 315/319] fix: added additional check (#1667) --- core/client/set.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/client/set.go b/core/client/set.go index 73511fcfb..0ad28f865 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -158,7 +158,9 @@ func SetWallet(w zcncrypto.Wallet) { // SetWalletMode sets current wallet split key mode. func SetWalletMode(mode bool) { - client.wallet.IsSplit = mode + if client.wallet != nil { + client.wallet.IsSplit = mode + } } // splitKeyWallet parameter is valid only if SignatureScheme is "BLS0Chain" From 39ea5b98c2a74e05d819faebbdb972f1fe2da936 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Sun, 10 Nov 2024 14:43:47 +0530 Subject: [PATCH 316/319] use alloc txn --- zboxcore/sdk/allocation.go | 2 +- zboxcore/sdk/chunked_upload_form_builder.go | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 2fe5f8f1f..c67f63efb 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -967,7 +967,7 @@ func (a *Allocation) GetCurrentVersion() (bool, error) { wg.Add(1) go func(rb *RollbackBlobber) { defer wg.Done() - err := rb.processRollback(context.TODO(), a.ID) + err := rb.processRollback(context.TODO(), a.Tx) if err != nil { success = false } diff --git a/zboxcore/sdk/chunked_upload_form_builder.go b/zboxcore/sdk/chunked_upload_form_builder.go index 3a5422817..794fb5b68 100644 --- a/zboxcore/sdk/chunked_upload_form_builder.go +++ b/zboxcore/sdk/chunked_upload_form_builder.go @@ -13,7 +13,6 @@ import ( "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/encryption" - "github.com/0chain/gosdk/zboxcore/logger" "golang.org/x/crypto/sha3" ) @@ -183,14 +182,12 @@ func (b *chunkedUploadFormBuilder) Build( if err != nil { return res, err } - logger.Logger.Info("sign using private signing key: ", formData.SignatureVersion) formData.ActualFileHashSignature = hex.EncodeToString(sig) } else { sig, err := client.Sign(fileMeta.ActualHash) if err != nil { return res, err } - logger.Logger.Info("sign using client: ", formData.SignatureVersion) formData.ActualFileHashSignature = sig } hash := formData.ActualFileHashSignature + formData.ValidationRoot From 0a5c66040568f48118967fa64150697d614c1014 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Mon, 11 Nov 2024 00:32:54 +0530 Subject: [PATCH 317/319] set size for root --- zboxcore/sdk/allocation.go | 4 ++-- zboxcore/sdk/listworker.go | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index c67f63efb..19ab21241 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -1606,7 +1606,7 @@ func (a *Allocation) ListDirFromAuthTicket(authTicket string, lookupHash string, return nil, errors.New("invalid_path", "Invalid path for the list") } - listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}, storageVersion: a.StorageVersion} + listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}, storageVersion: a.StorageVersion, dataShards: a.DataShards} listReq.ClientId = a.Owner listReq.allocationID = a.ID listReq.allocationTx = a.Tx @@ -1647,7 +1647,7 @@ func (a *Allocation) ListDir(path string, opts ...ListRequestOptions) (*ListResu if !isabs { return nil, errors.New("invalid_path", "Path should be valid and absolute") } - listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}, storageVersion: a.StorageVersion} + listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}, storageVersion: a.StorageVersion, dataShards: a.DataShards} listReq.ClientId = a.Owner listReq.allocationID = a.ID listReq.allocationTx = a.Tx diff --git a/zboxcore/sdk/listworker.go b/zboxcore/sdk/listworker.go index f8664f7d7..f621b5f14 100644 --- a/zboxcore/sdk/listworker.go +++ b/zboxcore/sdk/listworker.go @@ -38,6 +38,7 @@ type ListRequest struct { offset int pageLimit int storageVersion int + dataShards int Consensus } @@ -269,7 +270,9 @@ func (req *ListRequest) GetListFromBlobbers() (*ListResult, error) { } result.Size += ti.ref.Size result.NumBlocks += ti.ref.NumBlocks - + if ti.ref.Path == "/" && result.ActualSize == 0 { + result.ActualSize = ti.ref.Size * int64(req.dataShards) + } if len(lR[i].ref.Children) > 0 { result.populateChildren(lR[i].ref.Children, childResultMap, selected, req) } From fbcf0c6b83a97abf597f1537688e65fc1c0083a0 Mon Sep 17 00:00:00 2001 From: hitenjain14 Date: Mon, 11 Nov 2024 01:28:15 +0530 Subject: [PATCH 318/319] remove v1 sig in upload request --- zboxcore/allocationchange/newfile.go | 14 -------------- zboxcore/allocationchange/updatefile.go | 14 -------------- zboxcore/zboxutil/http.go | 18 ++++++++---------- 3 files changed, 8 insertions(+), 38 deletions(-) diff --git a/zboxcore/allocationchange/newfile.go b/zboxcore/allocationchange/newfile.go index a2fef6b62..53e572a37 100644 --- a/zboxcore/allocationchange/newfile.go +++ b/zboxcore/allocationchange/newfile.go @@ -9,7 +9,6 @@ import ( zError "github.com/0chain/errors" "github.com/0chain/gosdk/core/common" "github.com/0chain/gosdk/core/pathutil" - "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/util" "github.com/0chain/gosdk/zboxcore/fileref" "github.com/0chain/gosdk/zboxcore/logger" @@ -40,19 +39,6 @@ func (ch *NewFileChange) ProcessChange(rootRef *fileref.Ref, fileIDMeta map[stri return } - fileHashSign, err := client.Sign(ch.File.ActualFileHash) - if err != nil { - return - } - - validationRootSign, err := client.Sign(fileHashSign + ch.File.ValidationRoot) - if err != nil { - return - } - - ch.File.ActualFileHashSignature = fileHashSign - ch.File.ValidationRootSignature = validationRootSign - rootRef.HashToBeComputed = true dirRef := rootRef for i := 0; i < len(fields); i++ { diff --git a/zboxcore/allocationchange/updatefile.go b/zboxcore/allocationchange/updatefile.go index e4399f0d4..4e3ad8ab8 100644 --- a/zboxcore/allocationchange/updatefile.go +++ b/zboxcore/allocationchange/updatefile.go @@ -4,7 +4,6 @@ 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/zboxcore/fileref" @@ -28,19 +27,6 @@ func (ch *UpdateFileChange) ProcessChange(rootRef *fileref.Ref, _ map[string]str return } - fileHashSign, err := client.Sign(ch.NewFile.ActualFileHash) - if err != nil { - return - } - - validationRootSign, err := client.Sign(fileHashSign + ch.NewFile.ValidationRoot) - if err != nil { - return - } - - ch.NewFile.ActualFileHashSignature = fileHashSign - ch.NewFile.ValidationRootSignature = validationRootSign - fields, err := common.GetPathFields(pathutil.Dir(ch.NewFile.Path)) if err != nil { diff --git a/zboxcore/zboxutil/http.go b/zboxcore/zboxutil/http.go index 8613a78c8..d8a474fe0 100644 --- a/zboxcore/zboxutil/http.go +++ b/zboxcore/zboxutil/http.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "github.com/hashicorp/golang-lru/v2/simplelru" "io" "net" "net/http" @@ -14,6 +13,8 @@ import ( "strconv" "time" + "github.com/hashicorp/golang-lru/v2/simplelru" + "github.com/0chain/errors" "github.com/0chain/gosdk/core/client" "github.com/0chain/gosdk/core/encryption" @@ -215,10 +216,10 @@ func setClientInfoWithSign(req *http.Request, sig, allocation, baseURL string, c if !ok { var err error sig2, err = client.Sign(encryption.Hash(hashData), clientID) - SignCache.Add(hashData+":"+clientID, sig2) if err != nil { return err } + SignCache.Add(hashData+":"+clientID, sig2) } req.Header.Set(CLIENT_SIGNATURE_HEADER_V2, sig2) return nil @@ -653,19 +654,16 @@ func setFastClientInfoWithSign(req *fasthttp.Request, allocation, baseURL string req.Header.Set("X-App-Client-ID", client.Id()) req.Header.Set("X-App-Client-Key", client.PublicKey()) - sign, err := client.Sign(encryption.Hash(allocation)) - if err != nil { - return err - } - req.Header.Set(CLIENT_SIGNATURE_HEADER, sign) hashData := allocation + baseURL - sig2, ok := SignCache.Get(hashData) + clientID := client.Id() + sig2, ok := SignCache.Get(hashData + ":" + clientID) if !ok { - sig2, err = client.Sign(encryption.Hash(hashData)) - SignCache.Add(hashData, sig2) + var err error + sig2, err = client.Sign(encryption.Hash(hashData), clientID) if err != nil { return err } + SignCache.Add(hashData+":"+clientID, sig2) } req.Header.Set(CLIENT_SIGNATURE_HEADER_V2, sig2) return nil From 4f364ded5d08e942e1833bec6977ddc3266b8b54 Mon Sep 17 00:00:00 2001 From: Hitenjain14 Date: Tue, 12 Nov 2024 19:04:20 +0530 Subject: [PATCH 319/319] update common pacakge --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 2fa0e6739..23e1cebfa 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/0chain/gosdk go 1.22.0 require ( - github.com/0chain/common v1.18.2 + github.com/0chain/common v1.18.3 github.com/0chain/errors v1.0.3 github.com/Luzifer/go-openssl/v3 v3.1.0 github.com/btcsuite/btcd v0.23.4 diff --git a/go.sum b/go.sum index 6cb76057d..91dc05e8c 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/0chain/common v1.18.2 h1:VGWfd3Xqio9xbmebPFnUbuk5QN0pK0xzvifaUggJF5g= -github.com/0chain/common v1.18.2/go.mod h1:Lapu2Tj7z5Sm4r+X141e7vsz4NDODTEypeElYAP3iSw= +github.com/0chain/common v1.18.3 h1:42dYOv2KyMTSanuS67iDtfv+ErbSRqR8NJ3MG72MwaI= +github.com/0chain/common v1.18.3/go.mod h1:Lapu2Tj7z5Sm4r+X141e7vsz4NDODTEypeElYAP3iSw= github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM= github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=