diff --git a/chain/chain.go b/chain/chain.go index c6ba073..b7d13cf 100644 --- a/chain/chain.go +++ b/chain/chain.go @@ -9,13 +9,10 @@ package chain import ( "context" - "errors" - "fmt" "io" "log" "net/http" "os" - "path/filepath" "sync" "sync/atomic" "time" @@ -23,8 +20,6 @@ import ( "github.com/CESSProject/cess-go-sdk/core/pattern" "github.com/CESSProject/cess-go-sdk/core/sdk" "github.com/CESSProject/cess-go-sdk/core/utils" - p2pgo "github.com/CESSProject/p2p-go" - "github.com/CESSProject/p2p-go/core" gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4" "github.com/centrifuge/go-substrate-rpc-client/v4/registry/retriever" "github.com/centrifuge/go-substrate-rpc-client/v4/registry/state" @@ -34,7 +29,6 @@ import ( ) type chainClient struct { - *core.Node lock *sync.Mutex api *gsrpc.SubstrateAPI chainState *atomic.Bool @@ -51,7 +45,6 @@ type chainClient struct { networkEnv string signatureAcc string name string - enabledP2P bool } var _ sdk.SDK = (*chainClient)(nil) @@ -66,13 +59,8 @@ func NewChainClient( rpcs []string, mnemonic string, t time.Duration, - workspace string, - p2pPort int, - bootnodes []string, - protocolPrefix string, ) (*chainClient, error) { var ( - ok bool err error chainClient = &chainClient{ lock: new(sync.Mutex), @@ -83,10 +71,6 @@ func NewChainClient( } ) - if !core.FreeLocalPort(uint32(p2pPort)) { - return nil, fmt.Errorf("port [%d] is in use", p2pPort) - } - log.SetOutput(io.Discard) for i := 0; i < len(rpcs); i++ { chainClient.api, err = gsrpc.NewSubstrateAPI(rpcs[i]) @@ -147,24 +131,6 @@ func NewChainClient( return nil, err } - if workspace != "" && p2pPort > 0 { - p2p, err := p2pgo.New( - ctx, - p2pgo.ListenPort(p2pPort), - p2pgo.Workspace(filepath.Join(workspace, chainClient.GetSignatureAcc(), chainClient.GetSdkName())), - p2pgo.BootPeers(bootnodes), - p2pgo.ProtocolPrefix(protocolPrefix), - ) - if err != nil { - return nil, err - } - chainClient.Node, ok = p2p.(*core.Node) - if !ok { - return nil, errors.New("invalid p2p type") - } - chainClient.enabledP2P = true - } - return chainClient, nil } @@ -246,10 +212,6 @@ func (c *chainClient) Verify(msg []byte, sig []byte) (bool, error) { return signature.Verify(msg, sig, c.keyring.URI) } -func (c *chainClient) EnabledP2P() bool { - return c.enabledP2P -} - func reconnectChainSDK(rpcs []string) ( *gsrpc.SubstrateAPI, *types.Metadata, diff --git a/chain/event.go b/chain/event.go index 7d524b6..0d55332 100644 --- a/chain/event.go +++ b/chain/event.go @@ -1591,8 +1591,8 @@ func (c *chainClient) RetrieveAllEvent_FileBank_DeleteFile(blockhash types.Hash) if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) if vf.Len() > 0 { - allValue := fmt.Sprintf("%v", vf.Index(0)) if strings.Contains(v.Name, "AccountId32.operator") { + allValue := fmt.Sprintf("%v", vf.Index(0)) temp := strings.Split(allValue, "] ") puk := make([]byte, types.AccountIDLen) for _, v := range temp { @@ -1610,6 +1610,7 @@ func (c *chainClient) RetrieveAllEvent_FileBank_DeleteFile(blockhash types.Hash) } ele.Operator, _ = utils.EncodePublicKeyAsCessAccount(puk) } else if strings.Contains(v.Name, "AccountId32.owner") { + allValue := fmt.Sprintf("%v", vf.Index(0)) temp := strings.Split(allValue, "] ") puk := make([]byte, types.AccountIDLen) for _, v := range temp { @@ -1626,7 +1627,8 @@ func (c *chainClient) RetrieveAllEvent_FileBank_DeleteFile(blockhash types.Hash) } } ele.Owner, _ = utils.EncodePublicKeyAsCessAccount(puk) - } else if strings.Contains(v.Name, "deal_hash") { + } else if strings.Contains(v.Name, "file_hash") { + allValue := fmt.Sprintf("%v", vf.Index(0)) temp := strings.Split(allValue, "] ") for _, v := range temp { if strings.Count(v, " ") == (pattern.FileHashLen - 1) { diff --git a/chain/file.go b/chain/file.go index 55cd4d2..d8a4e58 100644 --- a/chain/file.go +++ b/chain/file.go @@ -16,7 +16,6 @@ import ( "os" "path/filepath" "strings" - "time" "github.com/CESSProject/cess-go-sdk/core/crypte" "github.com/CESSProject/cess-go-sdk/core/erasure" @@ -172,7 +171,7 @@ func cutfile(file string) ([]string, error) { if i+1 != segmentCount { return segment, errors.New("read file err") } - copy(buf[num:], []byte(utils.RandStr(pattern.SegmentSize-num))) + copy(buf[num:], make([]byte, pattern.SegmentSize-num)) } hash, err := utils.CalcSHA256(buf) @@ -229,7 +228,7 @@ func cutFileWithEncryption(file string) ([]string, error) { if i+1 != segmentCount { return segment, errors.New("read file err") } - copy(buf[num:], []byte(utils.RandStr(segmentSize-num))) + copy(buf[num:], make([]byte, segmentSize-num)) } hash, err := utils.CalcSHA256(buf) @@ -528,54 +527,54 @@ func (c *chainClient) RetrieveObject(url, fid string) (io.ReadCloser, error) { return resp.Body, nil } -func (c *chainClient) StorageData(roothash string, segment []pattern.SegmentDataInfo, minerTaskList []pattern.MinerTaskList) error { - if !c.enabledP2P { - return errors.New("P2P network not enabled") - } - - var err error - - // query all assigned miner multiaddr - peerids, err := c.QueryAssignedMinerPeerId(minerTaskList) - if err != nil { - return errors.Wrapf(err, "[QueryAssignedMinerPeerId]") - } - - basedir := filepath.Dir(segment[0].FragmentHash[0]) - for i := 0; i < len(peerids); i++ { - for j := 0; j < len(minerTaskList[i].Hash); j++ { - err = c.WriteFileAction(peerids[j], roothash, filepath.Join(basedir, string(minerTaskList[i].Hash[j][:]))) - if err != nil { - return errors.Wrapf(err, "[WriteFileAction]") - } - } - } - - return nil -} - -func (c *chainClient) FindPeers() map[string]peer.AddrInfo { - var peerMap = make(map[string]peer.AddrInfo, 100) - timeOut := time.NewTicker(time.Second * 10) - defer timeOut.Stop() - c.RouteTableFindPeers(0) - for { - select { - case peer, ok := <-c.GetDiscoveredPeers(): - if !ok { - return peerMap - } - if len(peer.Responses) == 0 { - break - } - for _, v := range peer.Responses { - peerMap[v.ID.Pretty()] = *v - } - case <-timeOut.C: - return peerMap - } - } -} +// func (c *chainClient) StorageData(roothash string, segment []pattern.SegmentDataInfo, minerTaskList []pattern.MinerTaskList) error { +// if !c.enabledP2P { +// return errors.New("P2P network not enabled") +// } + +// var err error + +// // query all assigned miner multiaddr +// peerids, err := c.QueryAssignedMinerPeerId(minerTaskList) +// if err != nil { +// return errors.Wrapf(err, "[QueryAssignedMinerPeerId]") +// } + +// basedir := filepath.Dir(segment[0].FragmentHash[0]) +// for i := 0; i < len(peerids); i++ { +// for j := 0; j < len(minerTaskList[i].Hash); j++ { +// err = c.WriteFileAction(peerids[j], roothash, filepath.Join(basedir, string(minerTaskList[i].Hash[j][:]))) +// if err != nil { +// return errors.Wrapf(err, "[WriteFileAction]") +// } +// } +// } + +// return nil +// } + +// func (c *chainClient) FindPeers() map[string]peer.AddrInfo { +// var peerMap = make(map[string]peer.AddrInfo, 100) +// timeOut := time.NewTicker(time.Second * 10) +// defer timeOut.Stop() +// c.RouteTableFindPeers(0) +// for { +// select { +// case peer, ok := <-c.GetDiscoveredPeers(): +// if !ok { +// return peerMap +// } +// if len(peer.Responses) == 0 { +// break +// } +// for _, v := range peer.Responses { +// peerMap[v.ID.Pretty()] = *v +// } +// case <-timeOut.C: +// return peerMap +// } +// } +// } func (c *chainClient) QueryAssignedMinerPeerId(minerTaskList []pattern.MinerTaskList) ([]peer.ID, error) { var peerids = make([]peer.ID, len(minerTaskList)) diff --git a/config/config.go b/config/config.go index d8bb70f..eb03956 100644 --- a/config/config.go +++ b/config/config.go @@ -18,14 +18,10 @@ import ( // Config describes a set of settings for a client type Config struct { - Rpc []string - Bootnodes []string - Mnemonic string - Name string - Workspace string - ProtocolPrefix string - P2pPort int - Timeout time.Duration + Rpc []string + Mnemonic string + Name string + Timeout time.Duration } // Option is a client config option that can be given to the client constructor @@ -38,13 +34,6 @@ const ( CharacterName_Deoss = "deoss" ) -// cess network protocol prefix -const ( - DevnetProtocolPrefix = "/kldr-devnet" - TestnetProtocolPrefix = "/kldr-testnet" - MainnetProtocolPrefix = "/kldr-mainnet" -) - // NewSDK constructs a new client from the Config. // // This function consumes the config. Do not reuse it (really!). @@ -52,7 +41,7 @@ func (cfg *Config) NewSDK(ctx context.Context, serviceName string) (sdk.SDK, err if serviceName == "" { return nil, fmt.Errorf("empty service name") } - return chain.NewChainClient(ctx, serviceName, cfg.Rpc, cfg.Mnemonic, cfg.Timeout, cfg.Workspace, cfg.P2pPort, cfg.Bootnodes, cfg.ProtocolPrefix) + return chain.NewChainClient(ctx, serviceName, cfg.Rpc, cfg.Mnemonic, cfg.Timeout) } // Apply applies the given options to the config, returning the first error diff --git a/core/pattern/pattern.go b/core/pattern/pattern.go index e35ceb3..64d07df 100644 --- a/core/pattern/pattern.go +++ b/core/pattern/pattern.go @@ -20,6 +20,8 @@ const DOT = "." // Unit precision of CESS token const TokenPrecision_CESS = "000000000000" +const StakingStakePerTiB = 4000 + // BlockInterval is the time interval for generating blocks, in seconds const BlockInterval = time.Second * time.Duration(6) @@ -397,13 +399,13 @@ type MinerSnapShot struct { type IdleProveInfo struct { TeeAcc types.AccountID IdleProve types.Bytes - VerifyResult types.OptionBool + VerifyResult types.Option[bool] } type ServiceProveInfo struct { TeeAcc types.AccountID ServiceProve types.Bytes - VerifyResult types.OptionBool + VerifyResult types.Option[bool] } type MinerSnapShot_V2 struct { diff --git a/core/sdk/sdk.go b/core/sdk/sdk.go index 6af918c..264b6e8 100644 --- a/core/sdk/sdk.go +++ b/core/sdk/sdk.go @@ -13,16 +13,12 @@ import ( "github.com/CESSProject/cess-go-sdk/core/event" "github.com/CESSProject/cess-go-sdk/core/pattern" - "github.com/CESSProject/p2p-go/core" gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4" "github.com/centrifuge/go-substrate-rpc-client/v4/types" ) // CESS Go SDK Interface Description type SDK interface { - // References libp2p: https://github.com/libp2p/go-libp2p - core.P2P - // Audit-State // Query the expired block height of the challenge. @@ -275,7 +271,7 @@ type SDK interface { // Verify the signature of the msg with the public key of the signing account. Verify(msg []byte, sig []byte) (bool, error) // EnabledP2P returns the p2p enable status - EnabledP2P() bool + // EnabledP2P() bool // Process the file according to CESS specifications. // diff --git a/example_test.go b/example_test.go index 1a08da0..716d72d 100644 --- a/example_test.go +++ b/example_test.go @@ -20,6 +20,7 @@ import ( cess "github.com/CESSProject/cess-go-sdk" "github.com/CESSProject/cess-go-sdk/config" + p2pgo "github.com/CESSProject/p2p-go" ) const DEFAULT_WAIT_TIME = time.Second * 15 @@ -52,15 +53,22 @@ func Example_register_deoss() { cess.ConnectRpcAddrs(strings.Split(os.Getenv("RPC_ADDRS"), " ")), cess.Mnemonic(os.Getenv("MY_MNEMONIC")), cess.TransactionTimeout(time.Duration(DEFAULT_WAIT_TIME)), - cess.Bootnodes([]string{os.Getenv("BOOTSTRAP_NODES")}), - cess.P2pPort(P2P_PORT), - cess.Workspace(TMP_DIR), ) if err != nil { log.Fatalf("err: %v", err.Error()) } - _, err = cli.RegisterDeoss(cli.GetPeerPublickey(), "") + p2p, err := p2pgo.New( + context.Background(), + p2pgo.ListenPort(P2P_PORT), + p2pgo.Workspace(TMP_DIR), + p2pgo.BootPeers([]string{os.Getenv("BOOTSTRAP_NODES")}), + ) + if err != nil { + log.Fatalf("err: %v", err.Error()) + } + + _, err = cli.RegisterDeoss(p2p.GetPeerPublickey(), "") if err != nil { log.Fatalf("err: %v", err.Error()) } @@ -73,15 +81,22 @@ func Example_register_storage_node() { cess.ConnectRpcAddrs(strings.Split(os.Getenv("RPC_ADDRS"), " ")), cess.Mnemonic(os.Getenv("MY_MNEMONIC")), cess.TransactionTimeout(time.Duration(DEFAULT_WAIT_TIME)), - cess.Bootnodes([]string{os.Getenv("BOOTSTRAP_NODES")}), - cess.P2pPort(P2P_PORT), - cess.Workspace(TMP_DIR), ) if err != nil { log.Fatalf("err: %v", err.Error()) } - _, _, err = cli.RegisterOrUpdateSminer(cli.GetPeerPublickey(), os.Getenv("MY_ADDR"), 0) + p2p, err := p2pgo.New( + context.Background(), + p2pgo.ListenPort(P2P_PORT), + p2pgo.Workspace(TMP_DIR), + p2pgo.BootPeers([]string{os.Getenv("BOOTSTRAP_NODES")}), + ) + if err != nil { + log.Fatalf("err: %v", err.Error()) + } + + _, _, err = cli.RegisterOrUpdateSminer(p2p.GetPeerPublickey(), os.Getenv("MY_ADDR"), 0) if err != nil { log.Fatalf("err: %v", err.Error()) } diff --git a/go.mod b/go.mod index c297beb..1b20ba1 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.20 require ( github.com/CESSProject/go-keyring v0.0.0-20220614131247-ee3a8da30fde - github.com/CESSProject/p2p-go v0.2.1 + github.com/CESSProject/p2p-go v0.2.2-0.20231023164634-85c11098b224 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.1.0 diff --git a/go.sum b/go.sum index 6f70a8a..4d31834 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,8 @@ git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGy github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/CESSProject/go-keyring v0.0.0-20220614131247-ee3a8da30fde h1:5MDRjjtg6PEhqyVjupwaapN96cOZiddOGAYwKQeaTu0= github.com/CESSProject/go-keyring v0.0.0-20220614131247-ee3a8da30fde/go.mod h1:RUXBd3ROP98MYepEEa0Y0l/T0vQlIKqFJxI/ocdnRLM= -github.com/CESSProject/p2p-go v0.2.1 h1:5NlMhelkneoNpNG7j6+rQCOUKcVheOZYwy4U1FyflQ8= -github.com/CESSProject/p2p-go v0.2.1/go.mod h1:smxUf83bVyoHeF9NPrL2p2WjwrjgN1SHKIeCh9ELVd0= +github.com/CESSProject/p2p-go v0.2.2-0.20231023164634-85c11098b224 h1:FWwDtqn2DW+ysgrJ7IC6o56DTLEmWyD9ZOisGwLq1iI= +github.com/CESSProject/p2p-go v0.2.2-0.20231023164634-85c11098b224/go.mod h1:W5ppY/TAYnIMGX5uYyO1k1prXXFd+b8Ag+GsRTmqrew= 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= diff --git a/options.go b/options.go index 2dc4e60..de2a676 100644 --- a/options.go +++ b/options.go @@ -40,35 +40,3 @@ func TransactionTimeout(timeout time.Duration) Option { return nil } } - -// Workspace configuration working directory -func Workspace(workspace string) Option { - return func(cfg *Config) error { - cfg.Workspace = workspace - return nil - } -} - -// P2pPort configuration p2p communication port -func P2pPort(port int) Option { - return func(cfg *Config) error { - cfg.P2pPort = port - return nil - } -} - -// P2pPort configuration boot node list -func Bootnodes(bootnodes []string) Option { - return func(cfg *Config) error { - cfg.Bootnodes = bootnodes - return nil - } -} - -// P2pPort configuration boot node list -func ProtocolPrefix(protocolPrefix string) Option { - return func(cfg *Config) error { - cfg.ProtocolPrefix = protocolPrefix - return nil - } -}