Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
smaulik13 committed Jan 16, 2025

Verified

This commit was signed with the committer’s verified signature.
rouault Even Rouault
1 parent 524b0ec commit c440472
Showing 74 changed files with 432 additions and 7,098 deletions.
1 change: 1 addition & 0 deletions core/block/block.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ package block

import (
"fmt"

"github.com/0chain/gosdk_common/core/common"
"github.com/0chain/gosdk_common/core/encryption"
"github.com/0chain/gosdk_common/core/transaction"
3 changes: 2 additions & 1 deletion core/client/cache.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package client

import (
"github.com/0chain/gosdk_common/core/logger"
"sync"

"github.com/0chain/gosdk_common/core/logger"
)

var Cache *NonceCache
13 changes: 7 additions & 6 deletions core/client/http.go
Original file line number Diff line number Diff line change
@@ -3,14 +3,15 @@ package client
import (
"encoding/json"
"fmt"
"github.com/0chain/errors"
"github.com/0chain/gosdk_common/core/conf"
"github.com/0chain/gosdk_common/core/util"
"github.com/shopspring/decimal"
"log"
"net/http"
"net/url"
"sync"

"github.com/0chain/errors"
"github.com/0chain/gosdk_common/core/conf"
"github.com/0chain/gosdk_common/core/util"
"github.com/shopspring/decimal"
)

// SCRestAPIHandler is a function type to handle the response from the SC Rest API
@@ -20,7 +21,7 @@ import (
// `err` - the error if any
type SCRestAPIHandler func(response map[string][]byte, numSharders int, err error)

func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string, restApiUrls ...string) ([]byte, error) {
func MakeSCRestAPICallToSharder(scAddress string, relativePath string, params map[string]string, restApiUrls ...string) ([]byte, error) {
const (
consensusThresh = float32(25.0)
ScRestApiUrl = "v1/screst/"
@@ -159,7 +160,7 @@ func GetBalance(clientIDs ...string) (*GetBalanceResponse, error) {
clientID = Id()
}

if res, err = MakeSCRestAPICall("", GetBalance, map[string]string{
if res, err = MakeSCRestAPICallToSharder("", GetBalance, map[string]string{
"client_id": clientID,
}, "v1/"); err != nil {
return nil, err
11 changes: 6 additions & 5 deletions core/client/init_node.go
Original file line number Diff line number Diff line change
@@ -18,14 +18,11 @@ import (
)

var (
logging logger.Logger
logging = logger.GetLogger()
nodeClient *Node
IsAppFlow = false
)

func init() {
logging.Init(logger.DEBUG, "0chain-core")
}

// 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.
@@ -38,6 +35,10 @@ type Node struct {
networkGuard sync.RWMutex
}

func SetIsAppFlow(val bool) {
IsAppFlow = true
}

// 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 {
27 changes: 27 additions & 0 deletions core/client/set.go
Original file line number Diff line number Diff line change
@@ -36,6 +36,23 @@ type Client struct {
sign SignFunc

Check failure on line 36 in core/client/set.go

GitHub Actions / lint

field `sign` is unused (unused)
}

type InitSdkOptions struct {
WalletJSON string
BlockWorker string
ChainID string
SignatureScheme string
Nonce int64
IsSplitWallet bool
AddWallet bool
TxnFee *int
MinConfirmation *int
MinSubmit *int
ConfirmationChainLength *int
SharderConsensous *int
ZboxHost string
ZboxAppType string
}

func init() {
sys.Sign = signHash
sys.SignWithAuth = signHash
@@ -326,6 +343,16 @@ func InitSDK(walletJSON string,
return nil
}

func InitSDKWithWebApp(params InitSdkOptions) error {
err := InitSDK(params.WalletJSON, params.BlockWorker, params.ChainID, params.SignatureScheme, params.Nonce, params.AddWallet, *params.MinConfirmation, *params.MinSubmit, *params.ConfirmationChainLength, *params.SharderConsensous)
if err != nil {
return err
}
conf.SetZboxAppConfigs(params.ZboxHost, params.ZboxAppType)
SetIsAppFlow(true)
return nil
}

func IsSDKInitialized() bool {
return sdkInitialized
}
3 changes: 0 additions & 3 deletions core/conf/config.go
Original file line number Diff line number Diff line change
@@ -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"`
@@ -169,7 +167,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
2 changes: 1 addition & 1 deletion core/conf/network.go
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ type Network struct {

func NewNetwork(miners, sharders []string) (*Network, error) {
n := &Network{
Miners: miners,
Miners: miners,
Sharders: sharders,
}
if !n.IsValid() {
5 changes: 5 additions & 0 deletions core/conf/vars.go
Original file line number Diff line number Diff line change
@@ -39,6 +39,11 @@ func GetClientConfig() (*Config, error) {
return cfg, nil
}

func SetZboxAppConfigs(zboxHost, zboxAppType string) {
cfg.ZboxHost = zboxHost
cfg.ZboxAppType = zboxAppType
}

// InitClientConfig set global client config
func InitClientConfig(c *Config) {
onceCfg.Do(func() {
39 changes: 39 additions & 0 deletions core/logger/logger.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,9 @@ import (
"io"
"log"
"os"

"github.com/0chain/gosdk_common/core/version"
"gopkg.in/natefinch/lumberjack.v2"
)

const (
@@ -142,3 +145,39 @@ func (l *Logger) Close() {
}
}
}

// Initialize common logger
var logging Logger

func GetLogger() *Logger {
return &logging
}

func CloseLog() {
logging.Close()
}

func init() {
logging.Init(DEBUG, "0chain-core-sdk")
}

// SetLogLevel set the log level.
// lvl - 0 disabled; higher number (upto 4) more verbosity
func SetLogLevel(lvl int) {
logging.SetLevel(lvl)
}

// SetLogFile - sets file path to write log
// verbose - true - console output; false - no console output
func SetLogFile(logFile string, verbose bool) {
ioWriter := &lumberjack.Logger{
Filename: logFile,
MaxSize: 100, // MB
MaxBackups: 5, // number of backups
MaxAge: 28, //days
LocalTime: false,
Compress: false, // disabled by default
}
logging.SetLogFile(ioWriter, verbose)
logging.Info("******* Wallet SDK Version:", version.VERSIONSTR, " ******* (SetLogFile)")
}
53 changes: 0 additions & 53 deletions core/node/cache.go

This file was deleted.

Loading

0 comments on commit c440472

Please sign in to comment.