From 7751f91c3908478c7484c31b11381cf28db3674c Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Wed, 21 Aug 2024 14:49:31 +0800 Subject: [PATCH] update reconnect timing --- chain/audit.go | 113 ++++++++---- chain/babe.go | 12 +- chain/balances.go | 45 +++-- chain/deoss.go | 151 ++++++++++----- chain/evm.go | 21 ++- chain/file_bank.go | 358 ++++++++++++++++++++++++------------ chain/rpc_call.go | 97 +++++++--- chain/scheduler_credit.go | 12 +- chain/session.go | 12 +- chain/sminer.go | 377 +++++++++++++++++++++++++------------- chain/staking.go | 184 +++++++++++++------ chain/storage_handler.go | 224 ++++++++++++++-------- chain/system.go | 34 +++- chain/tee.go | 60 ++++-- chain/treasury.go | 48 +++-- 15 files changed, 1187 insertions(+), 561 deletions(-) diff --git a/chain/audit.go b/chain/audit.go index 72f1222..cbd0f0b 100755 --- a/chain/audit.go +++ b/chain/audit.go @@ -27,6 +27,14 @@ import ( // - ChallengeInfo: challenge snapshot data // - error: error message func (c *ChainClient) QueryChallengeSnapShot(accountID []byte, block int32) (bool, ChallengeInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Audit, ChallengeSnapShot, ERR_RPC_CONNECTION.Error()) + return false, ChallengeInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -35,10 +43,6 @@ func (c *ChainClient) QueryChallengeSnapShot(accountID []byte, block int32) (boo var data ChallengeInfo - if !c.GetRpcState() { - return false, data, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, Audit, ChallengeSnapShot, accountID) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Audit, ChallengeSnapShot, err) @@ -75,6 +79,14 @@ func (c *ChainClient) QueryChallengeSnapShot(accountID []byte, block int32) (boo // - uint8: cleanup count // - error: error message func (c *ChainClient) QueryCountedClear(accountID []byte, block int32) (uint8, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Audit, CountedClear, ERR_RPC_CONNECTION.Error()) + return 0, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -83,10 +95,6 @@ func (c *ChainClient) QueryCountedClear(accountID []byte, block int32) (uint8, e var data types.U8 - if !c.GetRpcState() { - return uint8(data), ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, Audit, CountedClear, accountID) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Audit, CountedClear, err) @@ -129,16 +137,21 @@ func (c *ChainClient) QueryCountedClear(accountID []byte, block int32) (uint8, e // - uint32: Is there a value // - error: error message func (c *ChainClient) QueryCountedServiceFailed(accountID []byte, block int32) (uint32, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Audit, CountedServiceFailed, ERR_RPC_CONNECTION.Error()) + return 0, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data types.U32 - if !c.GetRpcState() { - return uint32(data), ERR_RPC_CONNECTION - } + var data types.U32 key, err := types.CreateStorageKey(c.metadata, Audit, CountedServiceFailed, accountID) if err != nil { @@ -181,9 +194,11 @@ func (c *ChainClient) QueryCountedServiceFailed(accountID []byte, block int32) ( // - string: block hash // - error: error message func (c *ChainClient) SubmitIdleProof(idleProof []types.U8) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -198,10 +213,6 @@ func (c *ChainClient) SubmitIdleProof(idleProof []types.U8) (string, error) { return blockhash, ERR_IdleProofIsEmpty } - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_Audit_submit_idle_proof, idleProof) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_Audit_submit_idle_proof, err) @@ -245,6 +256,15 @@ func (c *ChainClient) SubmitIdleProof(idleProof []types.U8) (string, error) { <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Audit_submit_idle_proof, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -295,9 +315,11 @@ func (c *ChainClient) SubmitIdleProof(idleProof []types.U8) (string, error) { // - string: block hash // - error: error message func (c *ChainClient) SubmitServiceProof(serviceProof []types.U8) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -308,10 +330,6 @@ func (c *ChainClient) SubmitServiceProof(serviceProof []types.U8) (string, error accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_Audit_submit_service_proof, serviceProof) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_Audit_submit_service_proof, err) @@ -355,6 +373,15 @@ func (c *ChainClient) SubmitServiceProof(serviceProof []types.U8) (string, error <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Audit_submit_service_proof, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -411,9 +438,11 @@ func (c *ChainClient) SubmitServiceProof(serviceProof []types.U8) (string, error // - string: block hash // - error: error message func (c *ChainClient) SubmitVerifyIdleResult(totalProofHash []types.U8, front, rear types.U64, accumulator Accumulator, result types.Bool, sig types.Bytes, teePuk WorkerPublicKey) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -424,10 +453,6 @@ func (c *ChainClient) SubmitVerifyIdleResult(totalProofHash []types.U8, front, r accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_Audit_submit_verify_idle_result, totalProofHash, front, rear, accumulator, result, sig, teePuk) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_Audit_submit_verify_idle_result, err) @@ -471,6 +496,15 @@ func (c *ChainClient) SubmitVerifyIdleResult(totalProofHash []types.U8, front, r <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Audit_submit_verify_idle_result, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -524,9 +558,11 @@ func (c *ChainClient) SubmitVerifyIdleResult(totalProofHash []types.U8, front, r // - string: block hash // - error: error message func (c *ChainClient) SubmitVerifyServiceResult(result types.Bool, sign types.Bytes, bloomFilter BloomFilter, teePuk WorkerPublicKey) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -537,10 +573,6 @@ func (c *ChainClient) SubmitVerifyServiceResult(result types.Bool, sign types.By accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_Audit_submit_verify_service_result, result, sign, bloomFilter, teePuk) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_Audit_submit_verify_service_result, err) @@ -584,6 +616,15 @@ func (c *ChainClient) SubmitVerifyServiceResult(result types.Bool, sign types.By <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Audit_submit_verify_service_result, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { diff --git a/chain/babe.go b/chain/babe.go index 4dbe8c0..86eee52 100644 --- a/chain/babe.go +++ b/chain/babe.go @@ -22,6 +22,14 @@ import ( // - []ConsensusRrscAppPublic: all consensus rrsc public // - error: error message func (c *ChainClient) QueryAuthorities(block int32) ([]ConsensusRrscAppPublic, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Babe, Authorities, ERR_RPC_CONNECTION.Error()) + return []ConsensusRrscAppPublic{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -30,10 +38,6 @@ func (c *ChainClient) QueryAuthorities(block int32) ([]ConsensusRrscAppPublic, e var data []ConsensusRrscAppPublic - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, Babe, Authorities) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Babe, Authorities, err) diff --git a/chain/balances.go b/chain/balances.go index a9bb281..b8d204d 100644 --- a/chain/balances.go +++ b/chain/balances.go @@ -25,16 +25,21 @@ import ( // - string: the total amount of token issuance // - error: error message func (c *ChainClient) QueryTotalIssuance(block int32) (string, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Balances, TotalIssuance, ERR_RPC_CONNECTION.Error()) + return "", err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data types.U128 - if !c.GetRpcState() { - return "", ERR_RPC_CONNECTION - } + var data types.U128 key, err := types.CreateStorageKey(c.metadata, Balances, TotalIssuance) if err != nil { @@ -83,16 +88,21 @@ func (c *ChainClient) QueryTotalIssuance(block int32) (string, error) { // - string: the amount of inactive token issuance // - error: error message func (c *ChainClient) QueryInactiveIssuance(block int32) (string, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Balances, InactiveIssuance, ERR_RPC_CONNECTION.Error()) + return "", err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data types.U128 - if !c.GetRpcState() { - return "", ERR_RPC_CONNECTION - } + var data types.U128 key, err := types.CreateStorageKey(c.metadata, Balances, InactiveIssuance) if err != nil { @@ -143,9 +153,11 @@ func (c *ChainClient) QueryInactiveIssuance(block int32) (string, error) { // - string: block hash // - error: error message func (c *ChainClient) TransferToken(dest string, amount string) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -156,10 +168,6 @@ func (c *ChainClient) TransferToken(dest string, amount string) (string, error) accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - pubkey, err := utils.ParsingPublickey(dest) if err != nil { return blockhash, errors.Wrapf(err, "[ParsingPublickey]") @@ -218,6 +226,15 @@ func (c *ChainClient) TransferToken(dest string, amount string) (string, error) <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Balances_transferKeepAlive, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { diff --git a/chain/deoss.go b/chain/deoss.go index 4b2d9ff..746718e 100755 --- a/chain/deoss.go +++ b/chain/deoss.go @@ -29,16 +29,21 @@ import ( // - OssInfo: oss info // - error: error message func (c *ChainClient) QueryOss(accountID []byte, block int32) (OssInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Oss, Oss, ERR_RPC_CONNECTION.Error()) + return OssInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data OssInfo - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } + var data OssInfo key, err := types.CreateStorageKey(c.metadata, Oss, Oss, accountID) if err != nil { @@ -81,16 +86,21 @@ func (c *ChainClient) QueryOss(accountID []byte, block int32) (OssInfo, error) { // - []OssInfo: all oss info // - error: error message func (c *ChainClient) QueryAllOss(block int32) ([]OssInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Oss, Oss, ERR_RPC_CONNECTION.Error()) + return []OssInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var result []OssInfo - if !c.GetRpcState() { - return nil, ERR_RPC_CONNECTION - } + var result []OssInfo key := CreatePrefixedKey(Oss, Oss) keys, err := c.api.RPC.State.GetKeysLatest(key) @@ -139,16 +149,21 @@ func (c *ChainClient) QueryAllOss(block int32) ([]OssInfo, error) { // - []string: all oss's peer id // - error: error message func (c *ChainClient) QueryAllOssPeerId(block int32) ([]string, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Oss, Oss, ERR_RPC_CONNECTION.Error()) + return []string{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var result []string - if !c.GetRpcState() { - return nil, ERR_RPC_CONNECTION - } + var result []string key := CreatePrefixedKey(Oss, Oss) keys, err := c.api.RPC.State.GetKeysLatest(key) @@ -199,16 +214,21 @@ func (c *ChainClient) QueryAllOssPeerId(block int32) ([]string, error) { // - []types.AccountID: authorised all accounts // - error: error message func (c *ChainClient) QueryAuthorityList(accountID []byte, block int32) ([]types.AccountID, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Oss, AuthorityList, ERR_RPC_CONNECTION.Error()) + return []types.AccountID{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data []types.AccountID - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } + var data []types.AccountID key, err := types.CreateStorageKey(c.metadata, Oss, AuthorityList, accountID) if err != nil { @@ -254,9 +274,11 @@ func (c *ChainClient) QueryAuthorityList(accountID []byte, block int32) ([]types // Node: // - accountID should be oss account func (c *ChainClient) Authorize(accountID []byte) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -267,10 +289,6 @@ func (c *ChainClient) Authorize(accountID []byte) (string, error) { accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - acc, err := types.NewAccountID(accountID) if err != nil { return blockhash, errors.Wrap(err, "[NewAccountID]") @@ -319,6 +337,15 @@ func (c *ChainClient) Authorize(accountID []byte) (string, error) { <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Oss_authorize, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -369,9 +396,11 @@ func (c *ChainClient) Authorize(accountID []byte) (string, error) { // - string: block hash // - error: error message func (c *ChainClient) CancelAuthorize(accountID []byte) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -382,10 +411,6 @@ func (c *ChainClient) CancelAuthorize(accountID []byte) (string, error) { accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_Oss_cancel_authorize, accountID) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_Oss_cancel_authorize, err) @@ -429,6 +454,15 @@ func (c *ChainClient) CancelAuthorize(accountID []byte) (string, error) { <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Oss_cancel_authorize, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -480,9 +514,11 @@ func (c *ChainClient) CancelAuthorize(accountID []byte) (string, error) { // - string: block hash // - error: error message func (c *ChainClient) RegisterOss(peerId []byte, domain string) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -490,14 +526,10 @@ func (c *ChainClient) RegisterOss(peerId []byte, domain string) (string, error) var ( blockhash string + peerid PeerId accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - - var peerid PeerId if len(peerId) != PeerIdPublicKeyLen { return blockhash, errors.New("register oss: invalid peerid") } @@ -563,6 +595,15 @@ func (c *ChainClient) RegisterOss(peerId []byte, domain string) (string, error) <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Oss_register, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -614,9 +655,11 @@ func (c *ChainClient) RegisterOss(peerId []byte, domain string) (string, error) // - string: block hash // - error: error message func (c *ChainClient) UpdateOss(peerId string, domain string) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -624,14 +667,10 @@ func (c *ChainClient) UpdateOss(peerId string, domain string) (string, error) { var ( blockhash string + peerid PeerId accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - - var peerid PeerId if len(peerid) != len(peerId) { return blockhash, errors.New("update oss: invalid peerid") } @@ -691,6 +730,15 @@ func (c *ChainClient) UpdateOss(peerId string, domain string) (string, error) { <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Oss_update, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -738,9 +786,11 @@ func (c *ChainClient) UpdateOss(peerId string, domain string) (string, error) { // - string: block hash // - error: error message func (c *ChainClient) DestroyOss() (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -751,10 +801,6 @@ func (c *ChainClient) DestroyOss() (string, error) { accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_Oss_destroy) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_Oss_destroy, err) @@ -799,6 +845,15 @@ func (c *ChainClient) DestroyOss() (string, error) { <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Oss_destroy, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { diff --git a/chain/evm.go b/chain/evm.go index a4532a4..afd1998 100644 --- a/chain/evm.go +++ b/chain/evm.go @@ -19,9 +19,11 @@ import ( ) func (c *ChainClient) SendEvmCall(source types.H160, target types.H160, input types.Bytes, value types.U256, gasLimit types.U64, maxFeePerGas types.U256, accessList []AccessInfo) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", fmt.Errorf("chainSDK.UploadDeclaration(): GetRpcState(): %v", ERR_RPC_CONNECTION) + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -32,10 +34,6 @@ func (c *ChainClient) SendEvmCall(source types.H160, target types.H160, input ty accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, fmt.Errorf("chainSDK.UploadDeclaration(): GetRpcState(): %v", ERR_RPC_CONNECTION) - } - var nonce types.Option[types.U256] nonce.SetNone() @@ -88,6 +86,17 @@ func (c *ChainClient) SendEvmCall(source types.H160, target types.H160, input ty return blockhash, err } + <-c.txTicker.C + + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Evm_call, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { if strings.Contains(err.Error(), ERR_PriorityIsTooLow) { diff --git a/chain/file_bank.go b/chain/file_bank.go index a4d1db4..31978c0 100755 --- a/chain/file_bank.go +++ b/chain/file_bank.go @@ -31,6 +31,14 @@ import ( // - BucketInfo: bucket info // - error: error message func (c *ChainClient) QueryBucket(accountID []byte, bucketName string, block int32) (BucketInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), FileBank, Bucket, ERR_RPC_CONNECTION.Error()) + return BucketInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -39,10 +47,6 @@ func (c *ChainClient) QueryBucket(accountID []byte, bucketName string, block int var data BucketInfo - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } - acc, err := types.NewAccountID(accountID) if err != nil { return data, errors.Wrap(err, "[NewAccountID]") @@ -99,6 +103,14 @@ func (c *ChainClient) QueryBucket(accountID []byte, bucketName string, block int // - StorageOrder: file storage order // - error: error message func (c *ChainClient) QueryDealMap(fid string, block int32) (StorageOrder, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), FileBank, DealMap, ERR_RPC_CONNECTION.Error()) + return StorageOrder{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -123,10 +135,6 @@ func (c *ChainClient) QueryDealMap(fid string, block int32) (StorageOrder, error return data, errors.Wrap(err, "[Encode]") } - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, FileBank, DealMap, param_hash) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), FileBank, DealMap, err) @@ -168,16 +176,21 @@ func (c *ChainClient) QueryDealMap(fid string, block int32) (StorageOrder, error // - []StorageOrder: file storage order list // - error: error message func (c *ChainClient) QueryDealMapList(block int32) ([]StorageOrder, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), FileBank, DealMap, ERR_RPC_CONNECTION.Error()) + return []StorageOrder{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var result []StorageOrder - if !c.GetRpcState() { - return nil, ERR_RPC_CONNECTION - } + var result []StorageOrder key := CreatePrefixedKey(FileBank, DealMap) keys, err := c.api.RPC.State.GetKeysLatest(key) @@ -227,6 +240,14 @@ func (c *ChainClient) QueryDealMapList(block int32) ([]StorageOrder, error) { // - FileMetadata: file metadata // - error: error message func (c *ChainClient) QueryFile(fid string, block int32) (FileMetadata, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), FileBank, File, ERR_RPC_CONNECTION.Error()) + return FileMetadata{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -238,10 +259,6 @@ func (c *ChainClient) QueryFile(fid string, block int32) (FileMetadata, error) { hash FileHash ) - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } - if len(fid) != FileHashLen { return data, errors.New("invalid filehash") } @@ -299,6 +316,14 @@ func (c *ChainClient) QueryFile(fid string, block int32) (FileMetadata, error) { // - RestoralOrderInfo: restoral order info // - error: error message func (c *ChainClient) QueryRestoralOrder(fragmentHash string, block int32) (RestoralOrderInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), FileBank, RestoralOrder, ERR_RPC_CONNECTION.Error()) + return RestoralOrderInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -310,10 +335,6 @@ func (c *ChainClient) QueryRestoralOrder(fragmentHash string, block int32) (Rest hash FileHash ) - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } - if len(fragmentHash) != FileHashLen { return data, errors.New("invalid fragment hash") } @@ -368,16 +389,21 @@ func (c *ChainClient) QueryRestoralOrder(fragmentHash string, block int32) (Rest // - []RestoralOrderInfo: all restoral order info // - error: error message func (c *ChainClient) QueryAllRestoralOrder(block int32) ([]RestoralOrderInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), FileBank, RestoralOrder, ERR_RPC_CONNECTION.Error()) + return []RestoralOrderInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var result []RestoralOrderInfo - if !c.GetRpcState() { - return nil, ERR_RPC_CONNECTION - } + var result []RestoralOrderInfo key := CreatePrefixedKey(FileBank, RestoralOrder) keys, err := c.api.RPC.State.GetKeysLatest(key) @@ -427,6 +453,14 @@ func (c *ChainClient) QueryAllRestoralOrder(block int32) ([]RestoralOrderInfo, e // - []string: all bucket names // - error: error message func (c *ChainClient) QueryAllBucketName(accountID []byte, block int32) ([]string, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), FileBank, UserBucketList, ERR_RPC_CONNECTION.Error()) + return []string{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -435,10 +469,6 @@ func (c *ChainClient) QueryAllBucketName(accountID []byte, block int32) ([]strin var data []types.Bytes - if !c.GetRpcState() { - return nil, ERR_RPC_CONNECTION - } - acc, err := types.NewAccountID(accountID) if err != nil { return nil, errors.Wrap(err, "[NewAccountID]") @@ -498,6 +528,14 @@ func (c *ChainClient) QueryAllBucketName(accountID []byte, block int32) ([]strin // - []UserFileSliceInfo: file list // - error: error message func (c *ChainClient) QueryUserHoldFileList(accountID []byte, block int32) ([]UserFileSliceInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), FileBank, UserHoldFileList, ERR_RPC_CONNECTION.Error()) + return []UserFileSliceInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -505,9 +543,6 @@ func (c *ChainClient) QueryUserHoldFileList(accountID []byte, block int32) ([]Us }() var data []UserFileSliceInfo - if !c.GetRpcState() { - return nil, ERR_RPC_CONNECTION - } acc, err := types.NewAccountID(accountID) if err != nil { @@ -560,6 +595,14 @@ func (c *ChainClient) QueryUserHoldFileList(accountID []byte, block int32) ([]Us // - []string: all fid // - error: error message func (c *ChainClient) QueryUserFidList(accountID []byte, block int32) ([]string, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), FileBank, UserHoldFileList, ERR_RPC_CONNECTION.Error()) + return []string{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -568,10 +611,6 @@ func (c *ChainClient) QueryUserFidList(accountID []byte, block int32) ([]string, var data []UserFileSliceInfo - if !c.GetRpcState() { - return nil, ERR_RPC_CONNECTION - } - acc, err := types.NewAccountID(accountID) if err != nil { return nil, errors.Wrap(err, "[NewAccountID]") @@ -676,9 +715,11 @@ func (c *ChainClient) PlaceStorageOrder(fid, file_name, bucket_name, territory_n // - string: block hash // - error: error message func (c *ChainClient) UploadDeclaration(fid string, segment []SegmentList, user UserBrief, filesize uint64) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -699,10 +740,6 @@ func (c *ChainClient) UploadDeclaration(fid string, segment []SegmentList, user hash[i] = types.U8(fid[i]) } - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_FileBank_upload_declaration, hash, segment, user, types.NewU128(*new(big.Int).SetUint64(filesize))) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_FileBank_upload_declaration, err) @@ -751,6 +788,15 @@ func (c *ChainClient) UploadDeclaration(fid string, segment []SegmentList, user <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_FileBank_upload_declaration, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -806,9 +852,11 @@ func (c *ChainClient) UploadDeclaration(fid string, segment []SegmentList, user // - cannot create a bucket that already exists // - if you are not the owner, the owner account must be authorised to you func (c *ChainClient) CreateBucket(owner []byte, bucketName string) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -819,10 +867,6 @@ func (c *ChainClient) CreateBucket(owner []byte, bucketName string) (string, err accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - acc, err := types.NewAccountID(owner) if err != nil { return blockhash, errors.Wrap(err, "[NewAccountID]") @@ -871,6 +915,15 @@ func (c *ChainClient) CreateBucket(owner []byte, bucketName string) (string, err <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_FileBank_create_bucket, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -910,9 +963,11 @@ func (c *ChainClient) CreateBucket(owner []byte, bucketName string) (string, err // Note: // - if you are not the owner, the owner account must be authorised to you func (c *ChainClient) DeleteBucket(owner []byte, bucketName string) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -923,10 +978,6 @@ func (c *ChainClient) DeleteBucket(owner []byte, bucketName string) (string, err accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - acc, err := types.NewAccountID(owner) if err != nil { return blockhash, errors.Wrap(err, "[NewAccountID]") @@ -975,6 +1026,15 @@ func (c *ChainClient) DeleteBucket(owner []byte, bucketName string) (string, err <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_FileBank_delete_bucket, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1014,9 +1074,11 @@ func (c *ChainClient) DeleteBucket(owner []byte, bucketName string) (string, err // Note: // - if you are not the owner, the owner account must be authorised to you func (c *ChainClient) DeleteFile(owner []byte, fid string) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -1027,10 +1089,6 @@ func (c *ChainClient) DeleteFile(owner []byte, fid string) (string, error) { accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - if len(fid) != FileHashLen { return "", errors.New("invalid fid") } @@ -1088,6 +1146,15 @@ func (c *ChainClient) DeleteFile(owner []byte, fid string) (string, error) { <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_FileBank_delete_file, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1143,9 +1210,11 @@ func (c *ChainClient) DeleteFile(owner []byte, fid string) (string, error) { // Note: // - for storage miner use only func (c *ChainClient) TransferReport(index uint8, fid string) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -1156,10 +1225,6 @@ func (c *ChainClient) TransferReport(index uint8, fid string) (string, error) { accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - if index <= 0 || int(index) > (config.DataShards+config.ParShards) { return "", errors.New("invalid index") } @@ -1213,6 +1278,15 @@ func (c *ChainClient) TransferReport(index uint8, fid string) (string, error) { <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_FileBank_transfer_report, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1267,9 +1341,11 @@ func (c *ChainClient) TransferReport(index uint8, fid string) (string, error) { // Note: // - for storage miner use only func (c *ChainClient) GenerateRestoralOrder(fid, fragmentHash string) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -1280,10 +1356,6 @@ func (c *ChainClient) GenerateRestoralOrder(fid, fragmentHash string) (string, e accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - var rooth FileHash var fragh FileHash @@ -1346,6 +1418,15 @@ func (c *ChainClient) GenerateRestoralOrder(fid, fragmentHash string) (string, e <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_FileBank_generate_restoral_order, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1384,9 +1465,11 @@ func (c *ChainClient) GenerateRestoralOrder(fid, fragmentHash string) (string, e // Note: // - for storage miner use only func (c *ChainClient) ClaimRestoralOrder(fragmentHash string) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -1397,10 +1480,6 @@ func (c *ChainClient) ClaimRestoralOrder(fragmentHash string) (string, error) { accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - if len(fragmentHash) != FileHashLen { return blockhash, errors.New("invalid fragment hash") } @@ -1453,6 +1532,15 @@ func (c *ChainClient) ClaimRestoralOrder(fragmentHash string) (string, error) { <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_FileBank_claim_restoral_order, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1508,9 +1596,11 @@ func (c *ChainClient) ClaimRestoralOrder(fragmentHash string) (string, error) { // Note: // - for storage miner use only func (c *ChainClient) ClaimRestoralNoExistOrder(puk []byte, fid, fragmentHash string) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -1521,10 +1611,6 @@ func (c *ChainClient) ClaimRestoralNoExistOrder(puk []byte, fid, fragmentHash st accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - acc, err := types.NewAccountID(puk) if err != nil { return blockhash, errors.Wrap(err, "[NewAccountID]") @@ -1591,6 +1677,15 @@ func (c *ChainClient) ClaimRestoralNoExistOrder(puk []byte, fid, fragmentHash st <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_FileBank_claim_restoral_noexist_order, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1644,9 +1739,11 @@ func (c *ChainClient) ClaimRestoralNoExistOrder(puk []byte, fid, fragmentHash st // Note: // - for storage miner use only func (c *ChainClient) RestoralOrderComplete(fragmentHash string) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -1657,10 +1754,6 @@ func (c *ChainClient) RestoralOrderComplete(fragmentHash string) (string, error) accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - var fragh FileHash if len(fragmentHash) != FileHashLen { @@ -1712,6 +1805,17 @@ func (c *ChainClient) RestoralOrderComplete(fragmentHash string) (string, error) return blockhash, err } + <-c.txTicker.C + + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_FileBank_restoral_order_complete, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1767,9 +1871,11 @@ func (c *ChainClient) RestoralOrderComplete(fragmentHash string) (string, error) // Note: // - for storage miner use only func (c *ChainClient) CertIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk WorkerPublicKey) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -1780,10 +1886,6 @@ func (c *ChainClient) CertIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWithAc accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_FileBank_cert_idle_space, spaceProofInfo, teeSignWithAcc, teeSign, teePuk) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_FileBank_cert_idle_space, err) @@ -1827,6 +1929,15 @@ func (c *ChainClient) CertIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWithAc <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_FileBank_cert_idle_space, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1883,9 +1994,11 @@ func (c *ChainClient) CertIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWithAc // Note: // - for storage miner use only func (c *ChainClient) ReplaceIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk WorkerPublicKey) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -1896,10 +2009,6 @@ func (c *ChainClient) ReplaceIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWit accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_FileBank_replace_idle_space, spaceProofInfo, teeSignWithAcc, teeSign, teePuk) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_FileBank_replace_idle_space, err) @@ -1943,6 +2052,15 @@ func (c *ChainClient) ReplaceIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWit <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_FileBank_replace_idle_space, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1997,9 +2115,11 @@ func (c *ChainClient) ReplaceIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWit // Note: // - for storage miner use only func (c *ChainClient) CalculateReport(teeSig types.Bytes, tagSigInfo TagSigInfo) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -2010,10 +2130,6 @@ func (c *ChainClient) CalculateReport(teeSig types.Bytes, tagSigInfo TagSigInfo) accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_FileBank_calculate_report, teeSig, tagSigInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_FileBank_calculate_report, err) @@ -2057,6 +2173,15 @@ func (c *ChainClient) CalculateReport(teeSig types.Bytes, tagSigInfo TagSigInfo) <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_FileBank_calculate_report, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -2109,9 +2234,11 @@ func (c *ChainClient) CalculateReport(teeSig types.Bytes, tagSigInfo TagSigInfo) // - string: block hash // - error: error message func (c *ChainClient) TerritoryFileDelivery(user []byte, fid string, target_territory string) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -2122,10 +2249,6 @@ func (c *ChainClient) TerritoryFileDelivery(user []byte, fid string, target_terr accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - acc, err := types.NewAccountID(user) if err != nil { return blockhash, errors.Wrap(err, "[NewAccountID]") @@ -2174,6 +2297,15 @@ func (c *ChainClient) TerritoryFileDelivery(user []byte, fid string, target_terr <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_FileBank_territory_file_delivery, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { diff --git a/chain/rpc_call.go b/chain/rpc_call.go index 4ece481..fe4f3e8 100644 --- a/chain/rpc_call.go +++ b/chain/rpc_call.go @@ -8,6 +8,7 @@ package chain import ( + "fmt" "log" "github.com/CESSProject/cess-go-sdk/utils" @@ -20,15 +21,21 @@ import ( // - types.SignedBlock: SignedBlock info // - error: error message func (c *ChainClient) ChainGetBlock(hash types.Hash) (types.SignedBlock, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [rpc_call] [%s] %s", c.GetCurrentRpcAddr(), RPC_Chain_getBlock, ERR_RPC_CONNECTION.Error()) + return types.SignedBlock{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() + var data types.SignedBlock - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } err := c.api.Client.Call(&data, RPC_Chain_getBlock, hash) return data, err } @@ -39,15 +46,21 @@ func (c *ChainClient) ChainGetBlock(hash types.Hash) (types.SignedBlock, error) // - types.Hash: block hash // - error: error message func (c *ChainClient) ChainGetBlockHash(block uint32) (types.Hash, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [rpc_call] [%s] %s", c.GetCurrentRpcAddr(), RPC_Chain_getBlockHash, ERR_RPC_CONNECTION.Error()) + return types.Hash{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() + var data types.Hash - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } err := c.api.Client.Call(&data, RPC_Chain_getBlockHash, types.NewU32(block)) return data, err } @@ -58,15 +71,21 @@ func (c *ChainClient) ChainGetBlockHash(block uint32) (types.Hash, error) { // - types.Hash: block hash // - error: error message func (c *ChainClient) ChainGetFinalizedHead() (types.Hash, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [rpc_call] [%s] %s", c.GetCurrentRpcAddr(), RPC_Chain_getFinalizedHead, ERR_RPC_CONNECTION.Error()) + return types.Hash{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() + var data types.Hash - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } err := c.api.Client.Call(&data, RPC_Chain_getFinalizedHead) return data, err } @@ -77,15 +96,21 @@ func (c *ChainClient) ChainGetFinalizedHead() (types.Hash, error) { // - SysProperties: system properties // - error: error message func (c *ChainClient) SystemProperties() (SysProperties, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [rpc_call] [%s] %s", c.GetCurrentRpcAddr(), RPC_SYS_Properties, ERR_RPC_CONNECTION.Error()) + return SysProperties{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() + var data SysProperties - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } err := c.api.Client.Call(&data, RPC_SYS_Properties) return data, err } @@ -96,15 +121,21 @@ func (c *ChainClient) SystemProperties() (SysProperties, error) { // - string: system chain // - error: error message func (c *ChainClient) SystemChain() (string, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [rpc_call] [%s] %s", c.GetCurrentRpcAddr(), RPC_SYS_Chain, ERR_RPC_CONNECTION.Error()) + return "", err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() + var data types.Text - if !c.GetRpcState() { - return "", ERR_RPC_CONNECTION - } err := c.api.Client.Call(&data, RPC_SYS_Chain) return string(data), err } @@ -115,15 +146,21 @@ func (c *ChainClient) SystemChain() (string, error) { // - SysSyncState: system sync state // - error: error message func (c *ChainClient) SystemSyncState() (SysSyncState, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [rpc_call] [%s] %s", c.GetCurrentRpcAddr(), RPC_SYS_SyncState, ERR_RPC_CONNECTION.Error()) + return SysSyncState{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() + var data SysSyncState - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } err := c.api.Client.Call(&data, RPC_SYS_SyncState) return data, err } @@ -134,15 +171,21 @@ func (c *ChainClient) SystemSyncState() (SysSyncState, error) { // - string: system version // - error: error message func (c *ChainClient) SystemVersion() (string, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [rpc_call] [%s] %s", c.GetCurrentRpcAddr(), RPC_SYS_Version, ERR_RPC_CONNECTION.Error()) + return "", err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() + var data types.Text - if !c.GetRpcState() { - return "", ERR_RPC_CONNECTION - } err := c.api.Client.Call(&data, RPC_SYS_Version) return string(data), err } @@ -153,15 +196,21 @@ func (c *ChainClient) SystemVersion() (string, error) { // - bool: net listenning // - error: error message func (c *ChainClient) NetListening() (bool, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [rpc_call] [%s] %s", c.GetCurrentRpcAddr(), RPC_NET_Listening, ERR_RPC_CONNECTION.Error()) + return false, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() + var data types.Bool - if !c.GetRpcState() { - return false, ERR_RPC_CONNECTION - } err := c.api.Client.Call(&data, RPC_NET_Listening) return bool(data), err } diff --git a/chain/scheduler_credit.go b/chain/scheduler_credit.go index 4882a8f..77d17e2 100644 --- a/chain/scheduler_credit.go +++ b/chain/scheduler_credit.go @@ -23,6 +23,14 @@ import ( // - SchedulerCounterEntry: validator's credit score // - error: error message func (c *ChainClient) QueryCurrentCounters(accountId []byte, block int32) (SchedulerCounterEntry, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), SchedulerCredit, CurrentCounters, ERR_RPC_CONNECTION.Error()) + return SchedulerCounterEntry{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -31,10 +39,6 @@ func (c *ChainClient) QueryCurrentCounters(accountId []byte, block int32) (Sched var data SchedulerCounterEntry - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, SchedulerCredit, CurrentCounters, accountId) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), SchedulerCredit, CurrentCounters, err) diff --git a/chain/session.go b/chain/session.go index 02c3bdb..55acf82 100644 --- a/chain/session.go +++ b/chain/session.go @@ -22,6 +22,14 @@ import ( // - []types.AccountID: validators account // - error: error message func (c *ChainClient) QueryValidators(block int32) ([]types.AccountID, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Session, Validators, ERR_RPC_CONNECTION.Error()) + return []types.AccountID{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -30,10 +38,6 @@ func (c *ChainClient) QueryValidators(block int32) ([]types.AccountID, error) { var data []types.AccountID - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, Session, Validators) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Session, Validators, err) diff --git a/chain/sminer.go b/chain/sminer.go index 6c5b2fe..4adefaf 100755 --- a/chain/sminer.go +++ b/chain/sminer.go @@ -28,6 +28,14 @@ import ( // - ExpendersInfo: idle data specification // - error: error message func (c *ChainClient) QueryExpenders(block int32) (ExpendersInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Sminer, Expenders, ERR_RPC_CONNECTION.Error()) + return ExpendersInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -36,10 +44,6 @@ func (c *ChainClient) QueryExpenders(block int32) (ExpendersInfo, error) { var data ExpendersInfo - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, Sminer, Expenders) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Sminer, Expenders, err) @@ -82,6 +86,14 @@ func (c *ChainClient) QueryExpenders(block int32) (ExpendersInfo, error) { // - MinerInfo: storage miner info // - error: error message func (c *ChainClient) QueryMinerItems(accountID []byte, block int32) (MinerInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Sminer, MinerItems, ERR_RPC_CONNECTION.Error()) + return MinerInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -90,10 +102,6 @@ func (c *ChainClient) QueryMinerItems(accountID []byte, block int32) (MinerInfo, var data MinerInfo - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, Sminer, MinerItems, accountID) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Sminer, MinerItems, err) @@ -137,6 +145,14 @@ func (c *ChainClient) QueryMinerItems(accountID []byte, block int32) (MinerInfo, // - uint32: starting staking block // - error: error message func (c *ChainClient) QueryStakingStartBlock(accountID []byte, block int32) (uint32, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Sminer, StakingStartBlock, ERR_RPC_CONNECTION.Error()) + return 0, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -145,10 +161,6 @@ func (c *ChainClient) QueryStakingStartBlock(accountID []byte, block int32) (uin var data types.U32 - if !c.GetRpcState() { - return 0, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, Sminer, StakingStartBlock, accountID) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Sminer, StakingStartBlock, err) @@ -191,6 +203,14 @@ func (c *ChainClient) QueryStakingStartBlock(accountID []byte, block int32) (uin // - []types.AccountID: all storage miner accounts // - error: error message func (c *ChainClient) QueryAllMiner(block int32) ([]types.AccountID, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Sminer, AllMiner, ERR_RPC_CONNECTION.Error()) + return []types.AccountID{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -199,10 +219,6 @@ func (c *ChainClient) QueryAllMiner(block int32) ([]types.AccountID, error) { var data []types.AccountID - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, Sminer, AllMiner) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Sminer, AllMiner, err) @@ -244,6 +260,14 @@ func (c *ChainClient) QueryAllMiner(block int32) ([]types.AccountID, error) { // - uint32: all storage miner count // - error: error message func (c *ChainClient) QueryCounterForMinerItems(block int32) (uint32, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Sminer, CounterForMinerItems, ERR_RPC_CONNECTION.Error()) + return 0, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -252,10 +276,6 @@ func (c *ChainClient) QueryCounterForMinerItems(block int32) (uint32, error) { var data types.U32 - if !c.GetRpcState() { - return 0, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, Sminer, CounterForMinerItems) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Sminer, CounterForMinerItems, err) @@ -297,6 +317,14 @@ func (c *ChainClient) QueryCounterForMinerItems(block int32) (uint32, error) { // - MinerReward: all reward information // - error: error message func (c *ChainClient) QueryRewardMap(accountID []byte, block int32) (MinerReward, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Sminer, RewardMap, ERR_RPC_CONNECTION.Error()) + return MinerReward{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -305,10 +333,6 @@ func (c *ChainClient) QueryRewardMap(accountID []byte, block int32) (MinerReward var data MinerReward - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, Sminer, RewardMap, accountID) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Sminer, RewardMap, err) @@ -351,6 +375,14 @@ func (c *ChainClient) QueryRewardMap(accountID []byte, block int32) (MinerReward // - RestoralTargetInfo: the data recovery information // - error: error message func (c *ChainClient) QueryRestoralTarget(accountID []byte, block int32) (RestoralTargetInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Sminer, RestoralTarget, ERR_RPC_CONNECTION.Error()) + return RestoralTargetInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -359,10 +391,6 @@ func (c *ChainClient) QueryRestoralTarget(accountID []byte, block int32) (Restor var data RestoralTargetInfo - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } - acc, err := types.NewAccountID(accountID) if err != nil { return data, errors.Wrap(err, "[NewAccountID]") @@ -413,16 +441,21 @@ func (c *ChainClient) QueryRestoralTarget(accountID []byte, block int32) (Restor // - []RestoralTargetInfo: all the data recovery information // - error: error message func (c *ChainClient) QueryAllRestoralTarget(block int32) ([]RestoralTargetInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Sminer, RestoralTarget, ERR_RPC_CONNECTION.Error()) + return []RestoralTargetInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var result []RestoralTargetInfo - if !c.GetRpcState() { - return nil, ERR_RPC_CONNECTION - } + var result []RestoralTargetInfo key := CreatePrefixedKey(Sminer, RestoralTarget) keys, err := c.api.RPC.State.GetKeysLatest(key) @@ -471,6 +504,14 @@ func (c *ChainClient) QueryAllRestoralTarget(block int32) ([]RestoralTargetInfo, // - types.U128: the size of replaceable idle data // - error: error message func (c *ChainClient) QueryPendingReplacements(accountID []byte, block int32) (types.U128, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Sminer, PendingReplacements, ERR_RPC_CONNECTION.Error()) + return types.U128{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -489,10 +530,6 @@ func (c *ChainClient) QueryPendingReplacements(accountID []byte, block int32) (t return data, errors.Wrap(err, "[EncodeToBytes]") } - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, Sminer, PendingReplacements, account) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Sminer, PendingReplacements, err) @@ -536,6 +573,14 @@ func (c *ChainClient) QueryPendingReplacements(accountID []byte, block int32) (t // - uint64: all storage miners power in current era // - error: error message func (c *ChainClient) QueryCompleteSnapShot(era uint32, block int32) (uint32, uint64, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Sminer, CompleteSnapShot, ERR_RPC_CONNECTION.Error()) + return 0, 0, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -544,10 +589,6 @@ func (c *ChainClient) QueryCompleteSnapShot(era uint32, block int32) (uint32, ui var data CompleteSnapShotType - if !c.GetRpcState() { - return 0, 0, ERR_RPC_CONNECTION - } - param, err := codec.Encode(era) if err != nil { return 0, 0, err @@ -599,6 +640,14 @@ func (c *ChainClient) QueryCompleteSnapShot(era uint32, block int32) (uint32, ui // - MinerCompleteInfo: CompleteMinerSnapShot // - error: error message func (c *ChainClient) QueryCompleteMinerSnapShot(puk []byte, block int32) (MinerCompleteInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Sminer, CompleteMinerSnapShot, ERR_RPC_CONNECTION.Error()) + return MinerCompleteInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -607,10 +656,6 @@ func (c *ChainClient) QueryCompleteMinerSnapShot(puk []byte, block int32) (Miner var data MinerCompleteInfo - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } - param, err := codec.Encode(puk) if err != nil { return data, err @@ -666,9 +711,11 @@ func (c *ChainClient) QueryCompleteMinerSnapShot(puk []byte, block int32) (Miner // - The number of staking to be added is calculated in the smallest unit, // if you want to add 1CESS staking, you need to fill in "1000000000000000000" func (c *ChainClient) IncreaseCollateral(accountID []byte, token string) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -679,10 +726,6 @@ func (c *ChainClient) IncreaseCollateral(accountID []byte, token string) (string accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - tokens, ok := new(big.Int).SetString(token, 10) if !ok { return "", fmt.Errorf("[IncreaseCollateral] invalid token: %s", token) @@ -736,6 +779,15 @@ func (c *ChainClient) IncreaseCollateral(accountID []byte, token string) (string <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Sminer_increase_collateral, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -790,9 +842,11 @@ func (c *ChainClient) IncreaseCollateral(accountID []byte, token string) (string // - the size of the declared space cannot be reduced // - when the staking does not meet the declared space size, you will be frozen func (c *ChainClient) IncreaseDeclarationSpace(tibCount uint32) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -803,10 +857,6 @@ func (c *ChainClient) IncreaseDeclarationSpace(tibCount uint32) (string, error) accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_Sminer_increase_declaration_space, types.NewU32(tibCount)) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_increase_declaration_space, err) @@ -850,6 +900,15 @@ func (c *ChainClient) IncreaseDeclarationSpace(tibCount uint32) (string, error) <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Sminer_increase_declaration_space, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -903,9 +962,11 @@ func (c *ChainClient) IncreaseDeclarationSpace(tibCount uint32) (string, error) // - after pre-exit, you need to wait for one day before it will automatically exit // - cannot register as a storage miner again after pre-exit func (c *ChainClient) MinerExitPrep() (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -913,24 +974,20 @@ func (c *ChainClient) MinerExitPrep() (string, error) { var ( err error - txhash string + blockhash string call types.Call accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return txhash, ERR_RPC_CONNECTION - } - acc, err := types.NewAccountID(c.GetSignatureAccPulickey()) if err != nil { - return txhash, errors.Wrap(err, "[NewAccountID]") + return blockhash, errors.Wrap(err, "[NewAccountID]") } call, err = types.NewCall(c.metadata, ExtName_Sminer_miner_exit, *acc) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_miner_exit, err) - return txhash, err + return blockhash, err } ext := types.NewExtrinsic(call) @@ -938,18 +995,18 @@ func (c *ChainClient) MinerExitPrep() (string, error) { key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_miner_exit, err) - return txhash, err + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_miner_exit, err) c.SetRpcState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -966,11 +1023,20 @@ func (c *ChainClient) MinerExitPrep() (string, error) { err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_miner_exit, err) - return txhash, err + return blockhash, err } <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Sminer_miner_exit, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -978,19 +1044,19 @@ func (c *ChainClient) MinerExitPrep() (string, error) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_miner_exit, err) c.SetRpcState(false) - return txhash, err + return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_miner_exit, err) c.SetRpcState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -1002,14 +1068,14 @@ func (c *ChainClient) MinerExitPrep() (string, error) { select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() err = c.RetrieveEvent(status.AsInBlock, ExtName_Sminer_miner_exit, SminerMinerExitPrep, c.signatureAcc) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } @@ -1024,27 +1090,25 @@ func (c *ChainClient) MinerExitPrep() (string, error) { // - must be an exited miner to withdraw // - wait a day to withdraw after pre-exit func (c *ChainClient) MinerWithdraw() (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return txhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_Sminer_miner_withdraw) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_miner_withdraw, err) - return txhash, err + return blockhash, err } ext := types.NewExtrinsic(call) @@ -1052,17 +1116,17 @@ func (c *ChainClient) MinerWithdraw() (string, error) { key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_miner_withdraw, err) - return txhash, err + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_miner_withdraw, err) c.SetRpcState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -1079,11 +1143,20 @@ func (c *ChainClient) MinerWithdraw() (string, error) { err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_miner_withdraw, err) - return txhash, err + return blockhash, err } <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Sminer_miner_withdraw, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1091,19 +1164,19 @@ func (c *ChainClient) MinerWithdraw() (string, error) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_miner_withdraw, err) c.SetRpcState(false) - return txhash, err + return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_miner_withdraw, err) c.SetRpcState(false) - return txhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]") + return blockhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]") } } defer sub.Unsubscribe() @@ -1115,14 +1188,14 @@ func (c *ChainClient) MinerWithdraw() (string, error) { select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() err = c.RetrieveEvent(status.AsInBlock, ExtName_Sminer_miner_withdraw, SminerWithdraw, c.signatureAcc) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } @@ -1138,9 +1211,11 @@ func (c *ChainClient) MinerWithdraw() (string, error) { // - for storage miner only // - pass at least one idle and service challenge at the same time to get the reward func (c *ChainClient) ReceiveReward() (string, string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -1152,10 +1227,6 @@ func (c *ChainClient) ReceiveReward() (string, string, error) { accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, earningsAcc, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_Sminer_receive_reward) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_receive_reward, err) @@ -1199,6 +1270,15 @@ func (c *ChainClient) ReceiveReward() (string, string, error) { <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Sminer_receive_reward, ERR_RPC_CONNECTION.Error()) + return blockhash, "", err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1256,9 +1336,11 @@ func (c *ChainClient) ReceiveReward() (string, string, error) { // Note: // - storage miners must complete the first stage to register for the second stage func (c *ChainClient) RegisterPoisKey(poisKey PoISKeyInfo, teeSignWithAcc, teeSign types.Bytes, teePuk WorkerPublicKey) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -1269,10 +1351,6 @@ func (c *ChainClient) RegisterPoisKey(poisKey PoISKeyInfo, teeSignWithAcc, teeSi accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_Sminer_register_pois_key, poisKey, teeSignWithAcc, teeSign, teePuk) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_register_pois_key, err) @@ -1322,6 +1400,15 @@ func (c *ChainClient) RegisterPoisKey(poisKey PoISKeyInfo, teeSignWithAcc, teeSi <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Sminer_register_pois_key, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1377,9 +1464,11 @@ func (c *ChainClient) RegisterPoisKey(poisKey PoISKeyInfo, teeSignWithAcc, teeSi // - string: block hash // - error: error message func (c *ChainClient) RegnstkSminer(earnings string, peerId []byte, staking uint64, tibCount uint32) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -1390,10 +1479,6 @@ func (c *ChainClient) RegnstkSminer(earnings string, peerId []byte, staking uint accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - var peerid PeerId if len(peerId) != PeerIdPublicKeyLen { return blockhash, fmt.Errorf("[RegnstkSminer] invalid peerid: %v", peerId) @@ -1464,6 +1549,15 @@ func (c *ChainClient) RegnstkSminer(earnings string, peerId []byte, staking uint <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Sminer_regnstk, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1520,9 +1614,11 @@ func (c *ChainClient) RegnstkSminer(earnings string, peerId []byte, staking uint // - string: block hash // - error: error message func (c *ChainClient) RegnstkAssignStaking(earnings string, peerId []byte, stakingAcc string, tibCount uint32) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -1533,10 +1629,6 @@ func (c *ChainClient) RegnstkAssignStaking(earnings string, peerId []byte, staki accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - var peerid PeerId if len(peerId) != PeerIdPublicKeyLen { return blockhash, fmt.Errorf("invalid peerid: %v", peerId) @@ -1618,6 +1710,15 @@ func (c *ChainClient) RegnstkAssignStaking(earnings string, peerId []byte, staki <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Sminer_regnstk_assign_staking, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1669,9 +1770,11 @@ func (c *ChainClient) RegnstkAssignStaking(earnings string, peerId []byte, staki // - string: block hash // - error: error message func (c *ChainClient) UpdateBeneficiary(earnings string) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -1682,10 +1785,6 @@ func (c *ChainClient) UpdateBeneficiary(earnings string) (string, error) { accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - puk, err := utils.ParsingPublickey(earnings) if err != nil { return "", err @@ -1739,6 +1838,15 @@ func (c *ChainClient) UpdateBeneficiary(earnings string) (string, error) { <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Sminer_update_beneficiary, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1790,9 +1898,11 @@ func (c *ChainClient) UpdateBeneficiary(earnings string) (string, error) { // - string: block hash // - error: error message func (c *ChainClient) UpdateSminerPeerId(peerid PeerId) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -1803,10 +1913,6 @@ func (c *ChainClient) UpdateSminerPeerId(peerid PeerId) (string, error) { accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_Sminer_update_peer_id, peerid) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_update_peer_id, err) @@ -1850,6 +1956,15 @@ func (c *ChainClient) UpdateSminerPeerId(peerid PeerId) (string, error) { <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_Sminer_update_peer_id, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { diff --git a/chain/staking.go b/chain/staking.go index cbdcbeb..5d6bc5a 100755 --- a/chain/staking.go +++ b/chain/staking.go @@ -24,6 +24,14 @@ import ( // - uint32: validator number // - error: error message func (c *ChainClient) QueryCounterForValidators(block int32) (uint32, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Staking, CounterForValidators, ERR_RPC_CONNECTION.Error()) + return 0, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -32,10 +40,6 @@ func (c *ChainClient) QueryCounterForValidators(block int32) (uint32, error) { var data types.U32 - if !c.GetRpcState() { - return uint32(data), ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, Staking, CounterForValidators) if err != nil { return uint32(data), errors.Wrap(err, "[CreateStorageKey]") @@ -77,6 +81,14 @@ func (c *ChainClient) QueryCounterForValidators(block int32) (uint32, error) { // - uint32: validator number // - error: error message func (c *ChainClient) QueryValidatorsCount(block int32) (uint32, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Staking, ValidatorCount, ERR_RPC_CONNECTION.Error()) + return 0, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -85,10 +97,6 @@ func (c *ChainClient) QueryValidatorsCount(block int32) (uint32, error) { var data types.U32 - if !c.GetRpcState() { - return uint32(data), ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, Staking, ValidatorCount) if err != nil { return uint32(data), errors.Wrap(err, "[CreateStorageKey]") @@ -130,6 +138,14 @@ func (c *ChainClient) QueryValidatorsCount(block int32) (uint32, error) { // - uint32: nominator number // - error: error message func (c *ChainClient) QueryNominatorCount(block int32) (uint32, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Staking, CounterForNominators, ERR_RPC_CONNECTION.Error()) + return 0, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -138,10 +154,6 @@ func (c *ChainClient) QueryNominatorCount(block int32) (uint32, error) { var data types.U32 - if !c.GetRpcState() { - return uint32(data), ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, Staking, CounterForNominators) if err != nil { return uint32(data), errors.Wrap(err, "[CreateStorageKey]") @@ -184,6 +196,14 @@ func (c *ChainClient) QueryNominatorCount(block int32) (uint32, error) { // - string: the total number of staking // - error: error message func (c *ChainClient) QueryErasTotalStake(era uint32, block int32) (string, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Staking, ErasTotalStake, ERR_RPC_CONNECTION.Error()) + return "", err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -192,10 +212,6 @@ func (c *ChainClient) QueryErasTotalStake(era uint32, block int32) (string, erro var data types.U128 - if !c.GetRpcState() { - return "", ERR_RPC_CONNECTION - } - param, err := codec.Encode(era) if err != nil { return "", err @@ -243,6 +259,14 @@ func (c *ChainClient) QueryErasTotalStake(era uint32, block int32) (string, erro // - uint32: era id // - error: error message func (c *ChainClient) QueryCurrentEra(block int32) (uint32, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Staking, CurrentEra, ERR_RPC_CONNECTION.Error()) + return 0, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -251,10 +275,6 @@ func (c *ChainClient) QueryCurrentEra(block int32) (uint32, error) { var data types.U32 - if !c.GetRpcState() { - return 0, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, Staking, CurrentEra) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Staking, CurrentEra, err) @@ -298,16 +318,21 @@ func (c *ChainClient) QueryCurrentEra(block int32) (uint32, error) { // - StakingEraRewardPoints: the rewards of consensus nodes // - error: error message func (c *ChainClient) QueryErasRewardPoints(era uint32, block int32) (StakingEraRewardPoints, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Staking, ErasRewardPoints, ERR_RPC_CONNECTION.Error()) + return StakingEraRewardPoints{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var result StakingEraRewardPoints - if !c.GetRpcState() { - return result, ERR_RPC_CONNECTION - } + var result StakingEraRewardPoints param1, err := codec.Encode(types.NewU32(era)) if err != nil { @@ -353,6 +378,14 @@ func (c *ChainClient) QueryErasRewardPoints(era uint32, block int32) (StakingEra // - []StakingNominations: all nominators info // - error: error message func (c *ChainClient) QueryAllNominators(block int32) ([]StakingNominations, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Staking, Nominators, ERR_RPC_CONNECTION.Error()) + return []StakingNominations{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -361,10 +394,6 @@ func (c *ChainClient) QueryAllNominators(block int32) ([]StakingNominations, err var result []StakingNominations - if !c.GetRpcState() { - return nil, ERR_RPC_CONNECTION - } - key := CreatePrefixedKey(Staking, Nominators) keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { @@ -415,6 +444,14 @@ func (c *ChainClient) QueryAllNominators(block int32) ([]StakingNominations, err // - []types.AccountID: all consensus and nominators accounts // - error: error message func (c *ChainClient) QueryAllBonded(block int32) ([]types.AccountID, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Staking, Bonded, ERR_RPC_CONNECTION.Error()) + return []types.AccountID{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -423,10 +460,6 @@ func (c *ChainClient) QueryAllBonded(block int32) ([]types.AccountID, error) { var result []types.AccountID - if !c.GetRpcState() { - return nil, ERR_RPC_CONNECTION - } - key := CreatePrefixedKey(Staking, Bonded) keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { @@ -481,6 +514,14 @@ func (c *ChainClient) QueryAllBonded(block int32) ([]types.AccountID, error) { // - uint8: validator commission // - error: error message func (c *ChainClient) QueryValidatorCommission(accountID []byte, block int32) (uint8, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Staking, Validators, ERR_RPC_CONNECTION.Error()) + return 0, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -489,10 +530,6 @@ func (c *ChainClient) QueryValidatorCommission(accountID []byte, block int32) (u var result StakingValidatorPrefs - if !c.GetRpcState() { - return 0, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, Staking, Validators, accountID) if err != nil { return 0, err @@ -531,6 +568,14 @@ func (c *ChainClient) QueryValidatorCommission(accountID []byte, block int32) (u // - string: total rewards // - error: error message func (c *ChainClient) QueryEraValidatorReward(era uint32, block int32) (string, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Staking, ErasValidatorReward, ERR_RPC_CONNECTION.Error()) + return "", err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -539,10 +584,6 @@ func (c *ChainClient) QueryEraValidatorReward(era uint32, block int32) (string, var result types.U128 - if !c.GetRpcState() { - return "", ERR_RPC_CONNECTION - } - param, err := codec.Encode(types.NewU32(era)) if err != nil { return "", err @@ -585,6 +626,14 @@ func (c *ChainClient) QueryEraValidatorReward(era uint32, block int32) (string, // - StakingLedger: staking ledger // - error: error message func (c *ChainClient) QueryLedger(accountID []byte, block int32) (StakingLedger, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Staking, Ledger, ERR_RPC_CONNECTION.Error()) + return StakingLedger{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -593,10 +642,6 @@ func (c *ChainClient) QueryLedger(accountID []byte, block int32) (StakingLedger, var result StakingLedger - if !c.GetRpcState() { - return result, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, Staking, Ledger, accountID) if err != nil { return result, err @@ -634,16 +679,21 @@ func (c *ChainClient) QueryLedger(accountID []byte, block int32) (StakingLedger, // - StakingExposure: staking exposure // - error: error message func (c *ChainClient) QueryeErasStakers(era uint32, accountId []byte) (StakingExposure, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Staking, ErasStakers, ERR_RPC_CONNECTION.Error()) + return StakingExposure{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var result StakingExposure - if !c.GetRpcState() { - return result, ERR_RPC_CONNECTION - } + var result StakingExposure param1, err := codec.Encode(types.NewU32(era)) if err != nil { @@ -675,16 +725,21 @@ func (c *ChainClient) QueryeErasStakers(era uint32, accountId []byte) (StakingEx // - StakingNominations: nominator info // - error: error message func (c *ChainClient) QueryeNominators(accountId []byte, block int32) (StakingNominations, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Staking, Nominators, ERR_RPC_CONNECTION.Error()) + return StakingNominations{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var result StakingNominations - if !c.GetRpcState() { - return result, ERR_RPC_CONNECTION - } + var result StakingNominations key, err := types.CreateStorageKey(c.metadata, Staking, Nominators, accountId) if err != nil { @@ -727,16 +782,21 @@ func (c *ChainClient) QueryeNominators(accountId []byte, block int32) (StakingNo // - []QueryeErasStakersPaged: all staking exposure // - error: error message func (c *ChainClient) QueryeAllErasStakersPaged(era uint32, accountId []byte) ([]StakingExposurePaged, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Staking, ErasStakersPaged, ERR_RPC_CONNECTION.Error()) + return []StakingExposurePaged{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var result []StakingExposurePaged - if !c.GetRpcState() { - return result, ERR_RPC_CONNECTION - } + var result []StakingExposurePaged param1, err := codec.Encode(types.NewU32(era)) if err != nil { @@ -775,6 +835,14 @@ func (c *ChainClient) QueryeAllErasStakersPaged(era uint32, accountId []byte) ([ // - PagedExposureMetadata: PagedExposureMetadata // - error: error message func (c *ChainClient) QueryeErasStakersOverview(era uint32, accountId []byte) (PagedExposureMetadata, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), Staking, ErasStakersOverview, ERR_RPC_CONNECTION.Error()) + return PagedExposureMetadata{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -782,10 +850,6 @@ func (c *ChainClient) QueryeErasStakersOverview(era uint32, accountId []byte) (P }() var result PagedExposureMetadata - if !c.GetRpcState() { - return result, ERR_RPC_CONNECTION - } - param1, err := codec.Encode(types.NewU32(era)) if err != nil { return result, err diff --git a/chain/storage_handler.go b/chain/storage_handler.go index 5971423..a6216f2 100755 --- a/chain/storage_handler.go +++ b/chain/storage_handler.go @@ -26,6 +26,14 @@ import ( // - string: price per GiB space // - error: error message func (c *ChainClient) QueryUnitPrice(block int32) (string, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), StorageHandler, UnitPrice, ERR_RPC_CONNECTION.Error()) + return "", err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -34,10 +42,6 @@ func (c *ChainClient) QueryUnitPrice(block int32) (string, error) { var data types.U128 - if !c.GetRpcState() { - return "", ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, StorageHandler, UnitPrice) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), StorageHandler, UnitPrice, err) @@ -82,6 +86,14 @@ func (c *ChainClient) QueryUnitPrice(block int32) (string, error) { // - uint64: the size of all idle space // - error: error message func (c *ChainClient) QueryTotalIdleSpace(block int32) (uint64, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), StorageHandler, TotalIdleSpace, ERR_RPC_CONNECTION.Error()) + return 0, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -90,10 +102,6 @@ func (c *ChainClient) QueryTotalIdleSpace(block int32) (uint64, error) { var data types.U128 - if !c.GetRpcState() { - return 0, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, StorageHandler, TotalIdleSpace) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), StorageHandler, TotalIdleSpace, err) @@ -136,6 +144,14 @@ func (c *ChainClient) QueryTotalIdleSpace(block int32) (uint64, error) { // - uint64: the size of all service space // - error: error message func (c *ChainClient) QueryTotalServiceSpace(block int32) (uint64, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), StorageHandler, TotalServiceSpace, ERR_RPC_CONNECTION.Error()) + return 0, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -144,10 +160,6 @@ func (c *ChainClient) QueryTotalServiceSpace(block int32) (uint64, error) { var data types.U128 - if !c.GetRpcState() { - return 0, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, StorageHandler, TotalServiceSpace) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), StorageHandler, TotalServiceSpace, err) @@ -189,6 +201,14 @@ func (c *ChainClient) QueryTotalServiceSpace(block int32) (uint64, error) { // - uint64: all purchased space size // - error: error message func (c *ChainClient) QueryPurchasedSpace(block int32) (uint64, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), StorageHandler, PurchasedSpace, ERR_RPC_CONNECTION.Error()) + return 0, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -197,10 +217,6 @@ func (c *ChainClient) QueryPurchasedSpace(block int32) (uint64, error) { var data types.U128 - if !c.GetRpcState() { - return 0, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, StorageHandler, PurchasedSpace) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), StorageHandler, PurchasedSpace, err) @@ -244,6 +260,14 @@ func (c *ChainClient) QueryPurchasedSpace(block int32) (uint64, error) { // - TerritoryInfo: territory info // - error: error message func (c *ChainClient) QueryTerritory(accountId []byte, name string, block int32) (TerritoryInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), StorageHandler, Territory, ERR_RPC_CONNECTION.Error()) + return TerritoryInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -252,10 +276,6 @@ func (c *ChainClient) QueryTerritory(accountId []byte, name string, block int32) var data TerritoryInfo - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } - param2, err := codec.Encode(types.NewBytes([]byte(name))) if err != nil { return data, errors.New("invalid account id") @@ -303,6 +323,14 @@ func (c *ChainClient) QueryTerritory(accountId []byte, name string, block int32) // - ConsignmentInfo: consignment info // - error: error message func (c *ChainClient) QueryConsignment(token types.H256, block int32) (ConsignmentInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), StorageHandler, Consignment, ERR_RPC_CONNECTION.Error()) + return ConsignmentInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -311,10 +339,6 @@ func (c *ChainClient) QueryConsignment(token types.H256, block int32) (Consignme var data ConsignmentInfo - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } - param1, err := codec.Encode(token) if err != nil { return data, errors.New("invalid territory key") @@ -363,9 +387,11 @@ func (c *ChainClient) QueryConsignment(token types.H256, block int32) (Consignme // - string: block hash // - error: error message func (c *ChainClient) MintTerritory(gib_count uint32, territory_name string, days uint32) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -384,10 +410,6 @@ func (c *ChainClient) MintTerritory(gib_count uint32, territory_name string, day return "", errors.New("[MintTerritory] invalid days") } - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_StorageHandler_mint_territory, types.NewU32(gib_count), types.NewBytes([]byte(territory_name)), types.NewU32(days)) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_StorageHandler_mint_territory, err) @@ -431,6 +453,15 @@ func (c *ChainClient) MintTerritory(gib_count uint32, territory_name string, day <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_StorageHandler_mint_territory, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -482,9 +513,11 @@ func (c *ChainClient) MintTerritory(gib_count uint32, territory_name string, day // - string: block hash // - error: error message func (c *ChainClient) ExpandingTerritory(territory_name string, gib_count uint32) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -499,10 +532,6 @@ func (c *ChainClient) ExpandingTerritory(territory_name string, gib_count uint32 return "", errors.New("[ExpandingTerritory] invalid gib_count") } - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_StorageHandler_expanding_territory, types.NewBytes([]byte(territory_name)), types.NewU32(gib_count)) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_StorageHandler_expanding_territory, err) @@ -546,6 +575,15 @@ func (c *ChainClient) ExpandingTerritory(territory_name string, gib_count uint32 <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_StorageHandler_expanding_territory, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -597,9 +635,11 @@ func (c *ChainClient) ExpandingTerritory(territory_name string, gib_count uint32 // - string: block hash // - error: error message func (c *ChainClient) RenewalTerritory(territory_name string, days_count uint32) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -614,10 +654,6 @@ func (c *ChainClient) RenewalTerritory(territory_name string, days_count uint32) return "", errors.New("[RenewalTerritory] invalid days_count") } - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_StorageHandler_renewal_territory, types.NewBytes([]byte(territory_name)), types.NewU32(days_count)) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_StorageHandler_renewal_territory, err) @@ -661,6 +697,15 @@ func (c *ChainClient) RenewalTerritory(territory_name string, days_count uint32) <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_StorageHandler_renewal_territory, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -712,9 +757,11 @@ func (c *ChainClient) RenewalTerritory(territory_name string, days_count uint32) // - string: block hash // - error: error message func (c *ChainClient) ReactivateTerritory(territory_name string, days_count uint32) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -729,10 +776,6 @@ func (c *ChainClient) ReactivateTerritory(territory_name string, days_count uint return "", errors.New("[ReactivateTerritory] invalid days_count") } - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_StorageHandler_reactivate_territory, types.NewBytes([]byte(territory_name)), types.NewU32(days_count)) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_StorageHandler_reactivate_territory, err) @@ -776,6 +819,15 @@ func (c *ChainClient) ReactivateTerritory(territory_name string, days_count uint <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_StorageHandler_reactivate_territory, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -830,9 +882,11 @@ func (c *ChainClient) ReactivateTerritory(territory_name string, days_count uint // - The territory must be in an active state // - Remaining lease term greater than 1 day func (c *ChainClient) TerritoryConsignment(territory_name string) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -843,10 +897,6 @@ func (c *ChainClient) TerritoryConsignment(territory_name string) (string, error accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_StorageHandler_territory_consignment, types.NewBytes([]byte(territory_name))) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_StorageHandler_territory_consignment, err) @@ -890,6 +940,15 @@ func (c *ChainClient) TerritoryConsignment(territory_name string) (string, error <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_StorageHandler_territory_consignment, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -940,9 +999,11 @@ func (c *ChainClient) TerritoryConsignment(territory_name string) (string, error // - string: block hash // - error: error message func (c *ChainClient) CancelConsignment(territory_name string) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -953,10 +1014,6 @@ func (c *ChainClient) CancelConsignment(territory_name string) (string, error) { accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_StorageHandler_cancel_consignment, types.NewBytes([]byte(territory_name))) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_StorageHandler_cancel_consignment, err) @@ -1000,6 +1057,15 @@ func (c *ChainClient) CancelConsignment(territory_name string) (string, error) { <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_StorageHandler_cancel_consignment, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1051,9 +1117,11 @@ func (c *ChainClient) CancelConsignment(territory_name string) (string, error) { // - string: block hash // - error: error message func (c *ChainClient) BuyConsignment(token types.H256, territory_name string) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -1064,10 +1132,6 @@ func (c *ChainClient) BuyConsignment(token types.H256, territory_name string) (s accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - if len(territory_name) <= 0 { return blockhash, errors.New("territory name is empty") } @@ -1115,6 +1179,15 @@ func (c *ChainClient) BuyConsignment(token types.H256, territory_name string) (s <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_StorageHandler_buy_consignment, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1165,9 +1238,11 @@ func (c *ChainClient) BuyConsignment(token types.H256, territory_name string) (s // - string: block hash // - error: error message func (c *ChainClient) CancelPurchaseAction(token types.H256) (string, error) { - c.lock.Lock() + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + defer func() { - c.lock.Unlock() if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } @@ -1178,10 +1253,6 @@ func (c *ChainClient) CancelPurchaseAction(token types.H256) (string, error) { accountInfo types.AccountInfo ) - if !c.GetRpcState() { - return blockhash, ERR_RPC_CONNECTION - } - call, err := types.NewCall(c.metadata, ExtName_StorageHandler_cancel_purchase_action, token) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_StorageHandler_cancel_purchase_action, err) @@ -1225,6 +1296,15 @@ func (c *ChainClient) CancelPurchaseAction(token types.H256) (string, error) { <-c.txTicker.C + if !c.GetRpcState() { + err = c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] %s", c.GetCurrentRpcAddr(), ExtName_StorageHandler_cancel_purchase_action, ERR_RPC_CONNECTION.Error()) + return blockhash, err + } + <-c.txTicker.C + } + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { diff --git a/chain/system.go b/chain/system.go index 566bc20..b9e064a 100755 --- a/chain/system.go +++ b/chain/system.go @@ -24,6 +24,14 @@ import ( // - uint32: block number // - error: error message func (c *ChainClient) QueryBlockNumber(blockhash string) (uint32, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [GetBlockLatest] %s", c.GetCurrentRpcAddr(), ERR_RPC_CONNECTION.Error()) + return 0, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -73,17 +81,21 @@ func (c *ChainClient) QueryAccountInfo(account string, block int32) (types.Accou // - types.AccountInfo: account info // - error: error message func (c *ChainClient) QueryAccountInfoByAccountID(accountID []byte, block int32) (types.AccountInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), System, Account, ERR_RPC_CONNECTION.Error()) + return types.AccountInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data types.AccountInfo - - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } + var data types.AccountInfo acc, err := types.NewAccountID(accountID) if err != nil { return data, errors.Wrap(err, "[NewAccountID]") @@ -130,16 +142,20 @@ func (c *ChainClient) QueryAccountInfoByAccountID(accountID []byte, block int32) // - []types.AccountInfo: all account info // - error: error message func (c *ChainClient) QueryAllAccountInfo(block int32) ([]types.AccountInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), System, Account, ERR_RPC_CONNECTION.Error()) + return []types.AccountInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - if !c.GetRpcState() { - return nil, ERR_RPC_CONNECTION - } - var result []types.AccountInfo key := CreatePrefixedKey(System, Account) keys, err := c.api.RPC.State.GetKeysLatest(key) diff --git a/chain/tee.go b/chain/tee.go index 7104b6f..efb5fff 100755 --- a/chain/tee.go +++ b/chain/tee.go @@ -24,6 +24,14 @@ import ( // - []byte: master public key // - error: error message func (c *ChainClient) QueryMasterPubKey(block int32) ([]byte, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), TeeWorker, MasterPubkey, ERR_RPC_CONNECTION.Error()) + return []byte{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -32,10 +40,6 @@ func (c *ChainClient) QueryMasterPubKey(block int32) ([]byte, error) { var data MasterPublicKey - if !c.GetRpcState() { - return nil, ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, TeeWorker, MasterPubkey) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TeeWorker, MasterPubkey, err) @@ -78,6 +82,14 @@ func (c *ChainClient) QueryMasterPubKey(block int32) ([]byte, error) { // - WorkerInfo: tee worker info // - error: error message func (c *ChainClient) QueryWorkers(puk WorkerPublicKey, block int32) (WorkerInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), TeeWorker, Workers, ERR_RPC_CONNECTION.Error()) + return WorkerInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -86,10 +98,6 @@ func (c *ChainClient) QueryWorkers(puk WorkerPublicKey, block int32) (WorkerInfo var data WorkerInfo - if !c.GetRpcState() { - return data, ERR_RPC_CONNECTION - } - publickey, err := codec.Encode(puk) if err != nil { return data, errors.Wrap(err, "[EncodeToBytes]") @@ -136,6 +144,14 @@ func (c *ChainClient) QueryWorkers(puk WorkerPublicKey, block int32) (WorkerInfo // - []WorkerInfo: all tee worker info // - error: error message func (c *ChainClient) QueryAllWorkers(block int32) ([]WorkerInfo, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), TeeWorker, Workers, ERR_RPC_CONNECTION.Error()) + return []WorkerInfo{}, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -144,10 +160,6 @@ func (c *ChainClient) QueryAllWorkers(block int32) ([]WorkerInfo, error) { var list []WorkerInfo - if !c.GetRpcState() { - return list, ERR_RPC_CONNECTION - } - key := CreatePrefixedKey(TeeWorker, Workers) keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { @@ -194,6 +206,14 @@ func (c *ChainClient) QueryAllWorkers(block int32) ([]WorkerInfo, error) { // - string: tee's endpoint // - error: error message func (c *ChainClient) QueryEndpoints(puk WorkerPublicKey, block int32) (string, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), TeeWorker, Endpoints, ERR_RPC_CONNECTION.Error()) + return "", err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -202,10 +222,6 @@ func (c *ChainClient) QueryEndpoints(puk WorkerPublicKey, block int32) (string, var data types.Text - if !c.GetRpcState() { - return "", ERR_RPC_CONNECTION - } - val, err := codec.Encode(puk) if err != nil { return "", errors.Wrap(err, "[Encode]") @@ -251,6 +267,14 @@ func (c *ChainClient) QueryEndpoints(puk WorkerPublicKey, block int32) (string, // - uint32: tee work registered block // - error: error message func (c *ChainClient) QueryWorkerAddedAt(puk WorkerPublicKey, block int32) (uint32, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), TeeWorker, WorkerAddedAt, ERR_RPC_CONNECTION.Error()) + return 0, err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -259,10 +283,6 @@ func (c *ChainClient) QueryWorkerAddedAt(puk WorkerPublicKey, block int32) (uint var data types.U32 - if !c.GetRpcState() { - return uint32(data), ERR_RPC_CONNECTION - } - val, err := codec.Encode(puk) if err != nil { return uint32(data), errors.Wrap(err, "[Encode]") diff --git a/chain/treasury.go b/chain/treasury.go index eb98766..beee42a 100644 --- a/chain/treasury.go +++ b/chain/treasury.go @@ -23,6 +23,14 @@ import ( // - string: currency rewards // - error: error message func (c *ChainClient) QueryCurrencyReward(block int32) (string, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), CessTreasury, CurrencyReward, ERR_RPC_CONNECTION.Error()) + return "", err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -31,10 +39,6 @@ func (c *ChainClient) QueryCurrencyReward(block int32) (string, error) { var data types.U128 - if !c.GetRpcState() { - return "", ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, CessTreasury, CurrencyReward) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), CessTreasury, CurrencyReward, err) @@ -86,6 +90,14 @@ func (c *ChainClient) QueryCurrencyReward(block int32) (string, error) { // - string: rewards in era // - error: error message func (c *ChainClient) QueryEraReward(block int32) (string, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), CessTreasury, EraReward, ERR_RPC_CONNECTION.Error()) + return "", err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -94,10 +106,6 @@ func (c *ChainClient) QueryEraReward(block int32) (string, error) { var data types.U128 - if !c.GetRpcState() { - return "", ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, CessTreasury, EraReward) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), CessTreasury, EraReward, err) @@ -149,6 +157,14 @@ func (c *ChainClient) QueryEraReward(block int32) (string, error) { // - string: reserve rewards // - error: error message func (c *ChainClient) QueryReserveReward(block int32) (string, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), CessTreasury, ReserveReward, ERR_RPC_CONNECTION.Error()) + return "", err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -157,10 +173,6 @@ func (c *ChainClient) QueryReserveReward(block int32) (string, error) { var data types.U128 - if !c.GetRpcState() { - return "", ERR_RPC_CONNECTION - } - key, err := types.CreateStorageKey(c.metadata, CessTreasury, ReserveReward) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), CessTreasury, ReserveReward, err) @@ -213,6 +225,14 @@ func (c *ChainClient) QueryReserveReward(block int32) (string, error) { // - string: rewards in an era // - error: error message func (c *ChainClient) QueryRoundReward(era uint32, block int32) (string, error) { + if !c.GetRpcState() { + err := c.ReconnectRpc() + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] %s", c.GetCurrentRpcAddr(), CessTreasury, RoundReward, ERR_RPC_CONNECTION.Error()) + return "", err + } + } + defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -221,10 +241,6 @@ func (c *ChainClient) QueryRoundReward(era uint32, block int32) (string, error) var data RoundRewardType - if !c.GetRpcState() { - return "", ERR_RPC_CONNECTION - } - param, err := codec.Encode(era) if err != nil { return "", err