diff --git a/core/client/init_node.go b/core/client/init_node.go index 01c577f24..fbb470197 100644 --- a/core/client/init_node.go +++ b/core/client/init_node.go @@ -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)) @@ -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)) diff --git a/core/client/set.go b/core/client/set.go index 113909e90..94cfeced3 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -51,6 +51,9 @@ type InitSdkOptions struct { SharderConsensous *int ZboxHost string ZboxAppType string + + Miners []string + Sharders []string } func init() { @@ -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 diff --git a/core/conf/config.go b/core/conf/config.go index 4fae43bec..5f13d8eea 100644 --- a/core/conf/config.go +++ b/core/conf/config.go @@ -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 diff --git a/wasmsdk/sdk.go b/wasmsdk/sdk.go index 07f9974ad..0a86fdfa8 100644 --- a/wasmsdk/sdk.go +++ b/wasmsdk/sdk.go @@ -18,8 +18,10 @@ import ( "io" "os" + "syscall/js" ) + var CreateObjectURL func(buf []byte, mimeType string) string // initSDKs init sdk with its parameters @@ -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, @@ -54,6 +62,8 @@ func initSDKs(chainID, blockWorker, signatureScheme string, ConfirmationChainLength: &confirmationChainLength, ZboxHost: zboxHost, ZboxAppType: zboxAppType, + Miners: miners, + Sharders: sharders, } err := client.InitSDKWithWebApp(params) @@ -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()