diff --git a/v3/client/go_client.go b/v3/client/go_client.go index 0648f79f..69937ed5 100644 --- a/v3/client/go_client.go +++ b/v3/client/go_client.go @@ -327,6 +327,10 @@ func (c *Client) SetPrivateKey(privateKey []byte) error { return c.conn.GetCSDK().SetPrivateKey(privateKey) } +func (c *Client) PrivateKeyBytes() []byte { + return c.conn.GetCSDK().PrivateKeyBytes() +} + // TransactionReceipt returns the receipt of a transaction by transaction hash. // Note that the receipt is not available for pending transactions. func (c *Client) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) { @@ -428,26 +432,29 @@ func (c *Client) GetPBFTView(ctx context.Context) ([]byte, error) { // Raft consensus } +type ConsensusNodeInfo struct { + ID string `json:"nodeID"` + Weight uint `json:"weight"` +} + // GetSealerList returns the list of consensus nodes' ID according to the groupID -func (c *Client) GetSealerList(ctx context.Context) ([]byte, error) { - var raw interface{} +func (c *Client) GetSealerList(ctx context.Context) ([]ConsensusNodeInfo, error) { + var raw []ConsensusNodeInfo err := c.conn.CallContext(ctx, &raw, "getSealerList") if err != nil { return nil, err } - js, err := json.MarshalIndent(raw, "", indent) - return js, err + return raw, err } // GetObserverList returns the list of observer nodes' ID according to the groupID -func (c *Client) GetObserverList(ctx context.Context) ([]byte, error) { - var raw interface{} +func (c *Client) GetObserverList(ctx context.Context) ([]string, error) { + var raw []string err := c.conn.CallContext(ctx, &raw, "getObserverList") if err != nil { return nil, err } - js, err := json.MarshalIndent(raw, "", indent) - return js, err + return raw, err } // GetConsensusStatus returns the status information about the consensus algorithm on a specific groupID @@ -625,14 +632,13 @@ func (c *Client) GetContractAddress(ctx context.Context, txHash common.Hash) (co } // GetPendingTxSize returns amount of the pending transactions -func (c *Client) GetPendingTxSize(ctx context.Context) ([]byte, error) { - var raw interface{} +func (c *Client) GetPendingTxSize(ctx context.Context) (int64, error) { + var raw int64 err := c.conn.CallContext(ctx, &raw, "getPendingTxSize") if err != nil { - return nil, err + return 0, err } - js, err := json.MarshalIndent(raw, "", indent) - return js, err + return raw, err } // GetCode returns the contract code according to the contract address diff --git a/v3/cmd/commandline/auth_manager.go b/v3/cmd/commandline/auth_manager.go index e0fcc52a..0a6c16f5 100644 --- a/v3/cmd/commandline/auth_manager.go +++ b/v3/cmd/commandline/auth_manager.go @@ -8,7 +8,9 @@ import ( "strconv" "github.com/FISCO-BCOS/go-sdk/v3/precompiled/auth" + "github.com/FISCO-BCOS/go-sdk/v3/smcrypto" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" "github.com/spf13/cobra" ) @@ -183,7 +185,7 @@ Arguments: For example: - [resetAdmin] [0x112fb844934c794a9e425dd6b4e57eff1b519f17][0x112fb844934c794a9e425dd6b4e57eff1b519f17] + resetAdmin 0x112fb844934c794a9e425dd6b4e57eff1b519f17 0x112fb844934c794a9e425dd6b4e57eff1b519f17 For more information please refer: @@ -200,10 +202,20 @@ For more information please refer: fmt.Printf("the format of contractAddr %v is invalid\n", contractAddr) return } - + var currentAddress string + if RPC.SMCrypto() { + currentAddress = smcrypto.SM2KeyToAddress(RPC.PrivateKeyBytes()).Hex() + } else { + private, err := crypto.ToECDSA(RPC.PrivateKeyBytes()) + if err != nil { + fmt.Printf("resetAdmin get current private failed, err:%v\n", err) + return + } + currentAddress = crypto.PubkeyToAddress(private.PublicKey).Hex() + } authManagerService, err := auth.NewAuthManagerService(RPC) if err != nil { - fmt.Printf("resetAdmin failed, err:%v\n", err) + fmt.Printf("resetAdmin failed, currentAccount: %s, err:%v\n", currentAddress, err) return } result, err := authManagerService.ResetAdmin(common.HexToAddress(newAdmin), common.HexToAddress(contractAddr)) diff --git a/v3/cmd/commandline/commands.go b/v3/cmd/commandline/commands.go index 1c2b5518..cb61f92f 100644 --- a/v3/cmd/commandline/commands.go +++ b/v3/cmd/commandline/commands.go @@ -116,7 +116,7 @@ var getSealerListCmd = &cobra.Command{ fmt.Printf("sealer list not found: %v\n", err) return } - fmt.Printf("Sealer List: \n%s\n", sealerList) + fmt.Printf("Sealer List: \n%+v\n", sealerList) }, } @@ -500,7 +500,7 @@ var getPendingTxSizeCmd = &cobra.Command{ fmt.Printf("transactions not found: %v\n", err) return } - fmt.Printf("Pending Transactions Count: \n hex: %s\n", tx) + fmt.Printf("Pending Transactions Count: %d\n", tx) }, } diff --git a/v3/cmd/commandline/root.go b/v3/cmd/commandline/root.go index 82d15b35..34146d5a 100644 --- a/v3/cmd/commandline/root.go +++ b/v3/cmd/commandline/root.go @@ -95,7 +95,13 @@ func initConfig() { ret := strings.Split(nodeEndpoint, ":") host := ret[0] port, _ := strconv.Atoi(ret[1]) - config := &client.Config{IsSMCrypto: smCrypto, GroupID: groupID, DisableSsl: disableSsl, - PrivateKey: privateKey, Host: host, Port: port, TLSCaFile: certPath + "/ca.crt", TLSKeyFile: certPath + "/sdk.key", TLSCertFile: certPath + "/sdk.crt"} + var config *client.Config + if !smCrypto { + config = &client.Config{IsSMCrypto: smCrypto, GroupID: groupID, DisableSsl: disableSsl, + PrivateKey: privateKey, Host: host, Port: port, TLSCaFile: certPath + "/ca.crt", TLSKeyFile: certPath + "/sdk.key", TLSCertFile: certPath + "/sdk.crt"} + } else { + config = &client.Config{IsSMCrypto: smCrypto, GroupID: groupID, DisableSsl: disableSsl, + PrivateKey: privateKey, Host: host, Port: port, TLSCaFile: certPath + "/sm_ca.crt", TLSKeyFile: certPath + "/sm_sdk.key", TLSCertFile: certPath + "/sm_sdk.crt", TLSSmEnKeyFile: certPath + "/sm_ensdk.key", TLSSmEnCertFile: certPath + "/sm_ensdk.crt"} + } RPC = getClient(config) } diff --git a/v3/examples/parallelok/main.go b/v3/examples/parallelok/main.go index 4f37640e..78da0d2c 100644 --- a/v3/examples/parallelok/main.go +++ b/v3/examples/parallelok/main.go @@ -185,7 +185,7 @@ func main() { wg2.Wait() // check balance - fmt.Println("check balance") + fmt.Println("check balance...") var wg3 sync.WaitGroup for i := 0; i < userCount; i++ { wg3.Add(1) diff --git a/v3/precompiled/consensus/consensus_service.go b/v3/precompiled/consensus/consensus_service.go index 2091caad..2c7c0864 100644 --- a/v3/precompiled/consensus/consensus_service.go +++ b/v3/precompiled/consensus/consensus_service.go @@ -19,11 +19,6 @@ const ( invalidNodeID int64 = -51100 ) -type nodeIdList struct { - nodeID string `json:"nodeID"` - weight uint `json:"weight"` -} - // getErrorMessage returns the message of error code func getErrorMessage(errorCode int64) string { var message string @@ -82,17 +77,11 @@ func (service *Service) AddObserver(nodeID string) (int64, error) { // return precompiled.DefaultErrorCode, fmt.Errorf("the node is not reachable") //} - observerRaw, err := service.client.GetObserverList(context.Background()) + nodeIDs, err := service.client.GetObserverList(context.Background()) if err != nil { return precompiled.DefaultErrorCode, fmt.Errorf("get the observer list failed: %v", err) } - var nodeIDs []string - err = json.Unmarshal(observerRaw, &nodeIDs) - if err != nil { - return precompiled.DefaultErrorCode, fmt.Errorf("unmarshal the observer list failed: %v", err) - } - for _, nID := range nodeIDs { if nID == nodeID { return precompiled.DefaultErrorCode, fmt.Errorf("the node is already in the observer list") @@ -115,19 +104,13 @@ func (service *Service) AddSealer(nodeID string, weight int64) (int64, error) { // return precompiled.DefaultErrorCode, fmt.Errorf("the node is not reachable") //} - sealerRaw, err := service.client.GetSealerList(context.Background()) + nodes, err := service.client.GetSealerList(context.Background()) if err != nil { return precompiled.DefaultErrorCode, fmt.Errorf("get the sealer list failed: %v", err) } - var nodeIDs []nodeIdList - err = json.Unmarshal(sealerRaw, &nodeIDs) - if err != nil { - return precompiled.DefaultErrorCode, fmt.Errorf("unmarshal the sealer list failed: %v", err) - } - - for _, nID := range nodeIDs { - if nID.nodeID == nodeID { + for _, node := range nodes { + if node.ID == nodeID { return precompiled.DefaultErrorCode, fmt.Errorf("the node is already in the sealer list") } } @@ -194,17 +177,21 @@ func (service *Service) isValidNodeID(nodeID string) (bool, error) { } func (service *Service) SetWeight(nodeID string, weight int64) (int64, error) { - sealerRaw, err := service.client.GetSealerList(context.Background()) + nodes, err := service.client.GetSealerList(context.Background()) if err != nil { return precompiled.DefaultErrorCode, fmt.Errorf("get the sealer list failed: %v", err) } - var nodeIDs []nodeIdList - err = json.Unmarshal(sealerRaw, &nodeIDs) - if err != nil { - return precompiled.DefaultErrorCode, fmt.Errorf("unmarshal the sealer list failed: %v", err) + find := false + for _, node := range nodes { + if node.ID == nodeID { + find = true + break + } + } + if !find { + return precompiled.DefaultErrorCode, fmt.Errorf("the node is not in the sealer list") } - _, _, receipt, err := service.consensus.SetWeight(service.consensusAuth, nodeID, big.NewInt(weight)) if err != nil { return precompiled.DefaultErrorCode, fmt.Errorf("ConsensusService setWeight failed: %+v", err) diff --git a/v3/types/receipt.go b/v3/types/receipt.go index a1c37c39..284d637a 100644 --- a/v3/types/receipt.go +++ b/v3/types/receipt.go @@ -197,7 +197,7 @@ func (r *Receipt) GetErrorMessage() string { return fmt.Sprintf("receipt error code: %v, receipt error message: %v", r.Status, errorMessage) } -// String returns the string representation of Receipt sturct. +// String returns the string representation of Receipt struct. func (r *Receipt) String() string { out, err := json.MarshalIndent(r, "", "\t") if err != nil {