Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions core/client/init_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ func Init(ctx context.Context, cfg conf.Config) error {
//init packages
conf.InitClientConfig(&cfg)

// If miners and sharders are provided in the config, use them directly.
// Otherwise, fetch them from the network.
if len(cfg.Miners) > 0 && len(cfg.Sharders) > 0 {
return initNode(ctx, cfg, cfg.Miners, cfg.Sharders)
}

network, err := GetNetwork(ctx)
if err != nil {
logging.Error("Failed to get network details ", zap.Error(err), zap.Any("block_worker", cfg.BlockWorker))
Expand Down Expand Up @@ -174,6 +180,26 @@ func Init(ctx context.Context, cfg conf.Config) error {
onlineMiners := filterOnlineNodes(network.Miners)
onlineSharders := filterOnlineNodes(network.Sharders)

return initNode(ctx, cfg, onlineMiners, onlineSharders)
}

func initNode(ctx context.Context, cfg conf.Config, onlineMiners, onlineSharders []string) error {
var network *conf.Network
var err error

// if we have miners and sharders, we can use them directly
if len(onlineMiners) > 0 && len(onlineSharders) > 0 {
network = &conf.Network{
Miners: onlineMiners,
Sharders: onlineSharders,
}
} else {
network, err = GetNetwork(ctx)
if err != nil {
return err
}
}

reqMiners := util.MaxInt(3, int(math.Ceil(float64(cfg.MinSubmit)*float64(len(network.Miners))/100)))
reqSharders := util.MinInt(len(network.Sharders), util.MaxInt(cfg.SharderConsensous, conf.DefaultSharderConsensous))

Expand Down
49 changes: 43 additions & 6 deletions core/client/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type InitSdkOptions struct {
SharderConsensous *int
ZboxHost string
ZboxAppType string

Miners []string
Sharders []string
}

func init() {
Expand Down Expand Up @@ -381,17 +384,51 @@ func InitSDK(walletJSON string,
}

func InitSDKWithWebApp(params InitSdkOptions) error {
if params.MinConfirmation != nil && params.MinSubmit != nil && params.ConfirmationChainLength != nil && params.SharderConsensous != nil {
err := InitSDK(params.WalletJSON, params.BlockWorker, params.ChainID, params.SignatureScheme, params.Nonce, params.AddWallet, *params.MinConfirmation, *params.MinSubmit, *params.ConfirmationChainLength, *params.SharderConsensous)
if params.AddWallet {
wallet := zcncrypto.Wallet{}
err := json.Unmarshal([]byte(params.WalletJSON), &wallet)
if err != nil {
return err
}
} else {
err := InitSDK(params.WalletJSON, params.BlockWorker, params.ChainID, params.SignatureScheme, params.Nonce, params.AddWallet)
if err != nil {
return err

SetWallet(wallet)
SetSignatureScheme(params.SignatureScheme)
SetNonce(params.Nonce)
if params.TxnFee != nil {
SetTxnFee(uint64(*params.TxnFee))
}
}

var minConfirmation, minSubmit, confirmationChainLength, sharderConsensous int
if params.MinConfirmation != nil {
minConfirmation = *params.MinConfirmation
}
if params.MinSubmit != nil {
minSubmit = *params.MinSubmit
}
if params.ConfirmationChainLength != nil {
confirmationChainLength = *params.ConfirmationChainLength
}
if params.SharderConsensous != nil {
sharderConsensous = *params.SharderConsensous
}

err := Init(context.Background(), conf.Config{
BlockWorker: params.BlockWorker,
SignatureScheme: params.SignatureScheme,
ChainID: params.ChainID,
MinConfirmation: minConfirmation,
MinSubmit: minSubmit,
ConfirmationChainLength: confirmationChainLength,
SharderConsensous: sharderConsensous,
Miners: params.Miners,
Sharders: params.Sharders,
})

if err != nil {
return err
}
SetSdkInitialized(true)
conf.SetZboxAppConfigs(params.ZboxHost, params.ZboxAppType)
SetIsAppFlow(true)
return nil
Expand Down
3 changes: 3 additions & 0 deletions core/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ type Config struct {
SharderConsensous int `json:"sharder_consensous"`
ZauthServer string `json:"zauth_server"`
V *viper.Viper `json:"-"`

Miners []string `json:"miners"`
Sharders []string `json:"sharders"`
}

// LoadConfigFile load and parse SDK Config from file
Expand Down
66 changes: 64 additions & 2 deletions wasmsdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (

"io"
"os"
"syscall/js"
)


var CreateObjectURL func(buf []byte, mimeType string) string

// initSDKs init sdk with its parameters
Expand All @@ -32,15 +34,21 @@ 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
// - 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 {
zboxHost, zboxAppType string, sharderConsensous int, refresh 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 }\n", chainID, blockWorker, signatureScheme, minConfirmation, minSubmit, confirmationChainLength, zboxHost, zboxAppType, sharderConsensous)
fmt.Printf("{ chainID: %s, blockWorker: %s, signatureScheme: %s, minConfirmation: %d, minSubmit: %d, confirmationChainLength: %d, zboxHost: %s, zboxAppType: %s, sharderConsensous: %d, refresh: %v }\n", chainID, blockWorker, signatureScheme, minConfirmation, minSubmit, confirmationChainLength, zboxHost, zboxAppType, sharderConsensous, refresh)

zboxApiClient.SetRequest(zboxHost, zboxAppType)

var miners, sharders []string
if !refresh {
miners, sharders = loadNetworkFromCache()
}

params := client.InitSdkOptions{
WalletJSON: "{}",
BlockWorker: blockWorker,
Expand All @@ -54,6 +62,8 @@ func initSDKs(chainID, blockWorker, signatureScheme string,
ConfirmationChainLength: &confirmationChainLength,
ZboxHost: zboxHost,
ZboxAppType: zboxAppType,
Miners: miners,
Sharders: sharders,
}

err := client.InitSDKWithWebApp(params)
Expand All @@ -62,10 +72,62 @@ func initSDKs(chainID, blockWorker, signatureScheme string,
return err
}

if refresh || len(miners) == 0 {
saveNetworkToCache()
}

sdk.SetWasm()
return nil
}

func loadNetworkFromCache() ([]string, []string) {
storage := js.Global().Get("localStorage")
if storage.IsNull() || storage.IsUndefined() {
return nil, nil
}
val := storage.Call("getItem", "zcn_network_info")
if val.IsNull() || val.IsUndefined() {
return nil, nil
}
jsonStr := val.String()
type networkInfo struct {
Miners []string `json:"miners"`
Sharders []string `json:"sharders"`
}
var info networkInfo
if err := json.Unmarshal([]byte(jsonStr), &info); err != nil {
return nil, nil
}
return info.Miners, info.Sharders
}

func saveNetworkToCache() {
node, err := client.GetNode()
if err != nil {
return
}
network := node.Network()
if network == nil {
return
}
type networkInfo struct {
Miners []string `json:"miners"`
Sharders []string `json:"sharders"`
}
info := networkInfo{
Miners: network.Miners,
Sharders: network.Sharders,
}
data, err := json.Marshal(info)
if err != nil {
return
}
storage := js.Global().Get("localStorage")
if !storage.IsNull() && !storage.IsUndefined() {
storage.Call("setItem", "zcn_network_info", string(data))
}
}

// getVersion retrieve the sdk version
func getVersion() string {
return sdk.GetVersion()
Expand Down
Loading