Skip to content

Commit

Permalink
update reconnect timing
Browse files Browse the repository at this point in the history
  • Loading branch information
AstaFrode committed Aug 21, 2024
1 parent b5ae200 commit 7751f91
Show file tree
Hide file tree
Showing 15 changed files with 1,187 additions and 561 deletions.
113 changes: 77 additions & 36 deletions chain/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 8 additions & 4 deletions chain/babe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand Down
45 changes: 31 additions & 14 deletions chain/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}
Expand All @@ -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]")
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 7751f91

Please sign in to comment.