Skip to content

Commit

Permalink
Base p2p (#95)
Browse files Browse the repository at this point in the history
* update p2p to v0.0.34

* update ChainSDK
  • Loading branch information
AstaFrode authored Jul 2, 2023
1 parent e2c4963 commit e2d7498
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
10 changes: 8 additions & 2 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package chain

import (
"context"
"errors"
"io"
"log"
"os"
Expand All @@ -29,7 +30,7 @@ import (
)

type ChainSDK struct {
core.P2P
*core.Node
lock *sync.Mutex
api *gsrpc.SubstrateAPI
chainState *atomic.Bool
Expand All @@ -50,6 +51,7 @@ var _ sdk.SDK = (*ChainSDK)(nil)

func NewChainSDK(name string, rpcs []string, mnemonic string, t time.Duration, workspace string, p2pPort int, bootnodes []string) (*ChainSDK, error) {
var (
ok bool
err error
chainSDK = &ChainSDK{}
)
Expand Down Expand Up @@ -109,7 +111,7 @@ func NewChainSDK(name string, rpcs []string, mnemonic string, t time.Duration, w
chainSDK.name = name

if workspace != "" && p2pPort > 0 {
chainSDK.P2P, err = p2pgo.New(
p2p, err := p2pgo.New(
ctx,
p2pgo.ListenPort(p2pPort),
p2pgo.Workspace(filepath.Join(workspace, chainSDK.GetSignatureAcc(), chainSDK.GetRoleName())),
Expand All @@ -118,6 +120,10 @@ func NewChainSDK(name string, rpcs []string, mnemonic string, t time.Duration, w
if err != nil {
return nil, err
}
chainSDK.Node, ok = p2p.(*core.Node)
if !ok {
return nil, errors.New("invalid p2p type")
}
chainSDK.enabledP2P = true
}

Expand Down
25 changes: 22 additions & 3 deletions chain/fileBank.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,13 @@ func (c *ChainSDK) DeleteFile(puk []byte, filehash []string) (string, []pattern.
return txhash, hashs, errors.Wrap(err, "[GetStorageRaw]")
}
err = types.EventRecordsRaw(*h).DecodeEventRecords(c.metadata, &events)
if err != nil || len(events.FileBank_DeleteFile) > 0 {
if err != nil {
if len(events.FileBank_DeleteFile) > 0 {
return txhash, events.FileBank_DeleteFile[0].Filehash, nil
}
return txhash, nil, nil
}
if len(events.FileBank_DeleteFile) > 0 {
return txhash, events.FileBank_DeleteFile[0].Filehash, nil
}
return txhash, hashs, errors.New(pattern.ERR_Failed)
Expand Down Expand Up @@ -982,7 +988,14 @@ func (c *ChainSDK) SubmitFileReport(roothash []pattern.FileHash) (string, []patt
return txhash, nil, errors.Wrap(err, "[GetStorageRaw]")
}
err = types.EventRecordsRaw(*h).DecodeEventRecords(c.metadata, &events)
if err != nil || len(events.FileBank_TransferReport) > 0 {
if err != nil {
if len(events.FileBank_TransferReport) > 0 {
return txhash, events.FileBank_TransferReport[0].Failed_list, nil
} else {
return txhash, nil, nil
}
}
if len(events.FileBank_TransferReport) > 0 {
return txhash, events.FileBank_TransferReport[0].Failed_list, nil
}
return txhash, nil, errors.New(pattern.ERR_Failed)
Expand Down Expand Up @@ -1086,9 +1099,15 @@ func (c *ChainSDK) ReplaceIdleFiles(roothash []pattern.FileHash) (string, []patt
return txhash, nil, errors.Wrap(err, "[GetStorageRaw]")
}
err = types.EventRecordsRaw(*h).DecodeEventRecords(c.metadata, &events)
if err != nil || len(events.FileBank_ReplaceFiller) > 0 {
if err != nil {
if len(events.FileBank_ReplaceFiller) > 0 {
return txhash, events.FileBank_ReplaceFiller[0].Filler_list, nil
}
return txhash, nil, nil
}
if len(events.FileBank_ReplaceFiller) > 0 {
return txhash, events.FileBank_ReplaceFiller[0].Filler_list, nil
}
return txhash, nil, errors.New(pattern.ERR_Failed)
}
case err = <-sub.Err():
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/CESSProject/cess-go-sdk
go 1.19

require (
github.com/CESSProject/p2p-go v0.0.33
github.com/CESSProject/p2p-go v0.0.34
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce
github.com/cbergoon/merkletree v0.2.0
github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.13
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/CESSProject/p2p-go v0.0.33 h1:2EXMEa1RAFHWfRTd5O6pyKQMBIbpsedIJvlYAFX/Qp0=
github.com/CESSProject/p2p-go v0.0.33/go.mod h1:MmuZ2UfXnEJMZMhOCC4Ec5QQUJCT7j2yg3Xk/oZ16yw=
github.com/CESSProject/p2p-go v0.0.34 h1:KgWA/kvJq8M4EtmZ1jcP33udx8OJeIbVKcJXIGmi6SE=
github.com/CESSProject/p2p-go v0.0.34/go.mod h1:MmuZ2UfXnEJMZMhOCC4Ec5QQUJCT7j2yg3Xk/oZ16yw=
github.com/ChainSafe/go-schnorrkel v1.0.0 h1:3aDA67lAykLaG1y3AOjs88dMxC88PgUuHRrLeDnvGIM=
github.com/ChainSafe/go-schnorrkel v1.0.0/go.mod h1:dpzHYVxLZcp8pjlV+O+UR8K0Hp/z7vcchBSbMBEhCw4=
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIOA6tDi6QXUemppXK3P9BI7mr2hd6gx8=
Expand Down

0 comments on commit e2d7498

Please sign in to comment.