Skip to content

Commit

Permalink
add SetChainState and Reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
AstaFrode committed May 12, 2023
1 parent c270e13 commit fe33e67
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions core/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ type Chain interface {
// GetChainState returns chain node state
GetChainState() bool

//
SetChainState(state bool)

//
Reconnect() error

//
GetMetadata() *types.Metadata

Expand Down Expand Up @@ -229,22 +235,21 @@ func NewChainClient(rpcAddr []string, secret string, t time.Duration) (Chain, er
return cli, nil
}

func (c *chainClient) IsChainClientOk() bool {
err := healthchek(c.api)
func (c *chainClient) Reconnect() error {
var err error
if c.api.Client != nil {
c.api.Client.Close()
}
c.api = nil
c.api, err = reconnectChainClient(c.rpcAddr)
if err != nil {
c.api = nil
cli, err := reconnectChainClient(c.rpcAddr)
if err != nil {
return false
}
c.api = cli
c.metadata, err = c.api.RPC.State.GetMetadataLatest()
if err != nil {
return false
}
return true
return err
}
c.metadata, err = c.api.RPC.State.GetMetadataLatest()
if err != nil {
return err
}
return true
return nil
}

func (c *chainClient) SetChainState(state bool) {
Expand Down Expand Up @@ -312,20 +317,6 @@ func reconnectChainClient(rpcAddr []string) (*gsrpc.SubstrateAPI, error) {
return api, err
}

func healthchek(a *gsrpc.SubstrateAPI) error {
defer func() { recover() }()
_, err := a.RPC.System.Health()
return err
}

func (c *chainClient) KeepConnect() {
tick := time.NewTicker(time.Second * 20)
select {
case <-tick.C:
healthchek(c.api)
}
}

func createPrefixedKey(method, prefix string) []byte {
return append(xxhash.New128([]byte(prefix)).Sum(nil), xxhash.New128([]byte(method)).Sum(nil)...)
}

0 comments on commit fe33e67

Please sign in to comment.