Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base reconnect #74

Merged
merged 2 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,10 @@ func (c *ChainSDK) Reconnect() error {
c.api.Client.Close()
}
c.api = nil
c.api, err = reconnectChainSDK(c.rpcAddr)
if err != nil {
return err
}
c.metadata, err = c.api.RPC.State.GetMetadataLatest()
c.metadata = nil
c.runtimeVersion = nil
c.keyEvents = nil
c.api, c.metadata, c.runtimeVersion, c.keyEvents, c.genesisHash, err = reconnectChainSDK(c.rpcAddr)
if err != nil {
return err
}
Expand Down Expand Up @@ -164,18 +163,51 @@ func (c *ChainSDK) Verify(msg []byte, sig []byte) (bool, error) {
return signature.Verify(msg, sig, c.keyring.URI)
}

func reconnectChainSDK(rpcAddr []string) (*gsrpc.SubstrateAPI, error) {
func reconnectChainSDK(rpcs []string) (
*gsrpc.SubstrateAPI,
*types.Metadata,
*types.RuntimeVersion,
types.StorageKey,
types.Hash,
error,
) {
var err error
var api *gsrpc.SubstrateAPI

defer log.SetOutput(os.Stdout)
log.SetOutput(io.Discard)
for i := 0; i < len(rpcAddr); i++ {
api, err = gsrpc.NewSubstrateAPI(rpcAddr[i])
if err == nil {
return api, nil
for i := 0; i < len(rpcs); i++ {
api, err = gsrpc.NewSubstrateAPI(rpcs[i])
if err != nil {
continue
}
}
return api, err
if api == nil {
return nil, nil, nil, nil, types.Hash{}, pattern.ERR_RPC_CONNECTION
}
var metadata *types.Metadata
var runtimeVer *types.RuntimeVersion
var keyEvents types.StorageKey
var genesisHash types.Hash

metadata, err = api.RPC.State.GetMetadataLatest()
if err != nil {
return nil, nil, nil, nil, types.Hash{}, pattern.ERR_RPC_CONNECTION
}
genesisHash, err = api.RPC.Chain.GetBlockHash(0)
if err != nil {
return nil, nil, nil, nil, types.Hash{}, pattern.ERR_RPC_CONNECTION
}
runtimeVer, err = api.RPC.State.GetRuntimeVersionLatest()
if err != nil {
return nil, nil, nil, nil, types.Hash{}, pattern.ERR_RPC_CONNECTION
}
keyEvents, err = types.CreateStorageKey(metadata, pattern.SYSTEM, pattern.EVENTS, nil)
if err != nil {
return nil, nil, nil, nil, types.Hash{}, pattern.ERR_RPC_CONNECTION
}

return api, metadata, runtimeVer, keyEvents, genesisHash, err
}

func createPrefixedKey(pallet, method string) []byte {
Expand Down
2 changes: 1 addition & 1 deletion chain/fileBank.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ func (c *ChainSDK) ReplaceIdleFiles(roothash []pattern.FileHash) (string, []patt
}
err = types.EventRecordsRaw(*h).DecodeEventRecords(c.metadata, &events)
if err != nil || len(events.FileBank_ReplaceFiller) > 0 {
return txhash, events.FileBank_ReplaceFiller[0].Filler_list, nil
return txhash, nil, nil
}
return txhash, nil, errors.New(pattern.ERR_Failed)
}
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (cfg *Config) NewSDK(roleName string) (sdk.SDK, error) {
if roleName != CharacterName_Bucket &&
roleName != CharacterName_Deoss &&
roleName != CharacterName_Client {
return nil, fmt.Errorf("invalid character name")
return nil, fmt.Errorf("invalid role name")
}
return chain.NewChainSDK(roleName, cfg.Rpc, cfg.Mnemonic, cfg.Timeout)
}
Expand Down
21 changes: 21 additions & 0 deletions core/utils/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,24 @@ func CalcMD5(data string) ([]byte, error) {
m := md5.Sum([]byte(data))
return m[:], nil
}

// CalcPathSHA256 is used to calculate the sha256 value
// of a file with a given path.
func CalcPathSHA256Bytes(fpath string) ([]byte, error) {
f, err := os.Open(fpath)
if err != nil {
return nil, err
}
defer f.Close()
return CalcFileSHA256Bytes(f)
}

// CalcFileSHA256 is used to calculate the sha256 value
// of the file type.
func CalcFileSHA256Bytes(f *os.File) ([]byte, error) {
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return nil, err
}
return h.Sum(nil), nil
}