Skip to content

Commit

Permalink
Base option2 (#153)
Browse files Browse the repository at this point in the history
* update chall type

* update p2p

* update p2p

* update p2p

* update p2p

* update p2p

* update p2p

* update p2p

* update p2p

* update p2p

* update p2p

* remove p2p

* update RetrieveAllEvent_FileBank_DeleteFile

* update cut file
  • Loading branch information
AstaFrode authored Oct 26, 2023
1 parent 3a95dd3 commit 3ae9ae9
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 157 deletions.
38 changes: 0 additions & 38 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,17 @@ package chain

import (
"context"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"

"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"
Expand All @@ -34,7 +29,6 @@ import (
)

type chainClient struct {
*core.Node
lock *sync.Mutex
api *gsrpc.SubstrateAPI
chainState *atomic.Bool
Expand All @@ -51,7 +45,6 @@ type chainClient struct {
networkEnv string
signatureAcc string
name string
enabledP2P bool
}

var _ sdk.SDK = (*chainClient)(nil)
Expand All @@ -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),
Expand All @@ -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])
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions chain/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down
101 changes: 50 additions & 51 deletions chain/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
21 changes: 5 additions & 16 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,21 +34,14 @@ 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!).
func (cfg *Config) NewSDK(ctx context.Context, serviceName string) (sdk.SDK, error) {
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
Expand Down
6 changes: 4 additions & 2 deletions core/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 1 addition & 5 deletions core/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
//
Expand Down
Loading

0 comments on commit 3ae9ae9

Please sign in to comment.