Skip to content

Commit

Permalink
P2p to http (#243)
Browse files Browse the repository at this point in the history
* update some config

* update UpdateSminerPeerId to UpdateSminerAddr

* expose chainer

* add SubmitVerifyIdleResult and SubmitVerifyServiceResult

* update RegnstkSminer

* add StoreFileToMiners and RetrieveFileFromMiners

* add ExtName_Oss_evm_proxy_authorzie
  • Loading branch information
AstaFrode authored Sep 13, 2024
1 parent 119916c commit 9753395
Show file tree
Hide file tree
Showing 19 changed files with 682 additions and 126 deletions.
2 changes: 1 addition & 1 deletion cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Option func(cfg *Config) error
// 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) (*chain.ChainClient, error) {
func (cfg *Config) NewSDK(ctx context.Context) (chain.Chainer, error) {
if cfg.Name == "" {
cfg.Name = config.CharacterName_Default
}
Expand Down
4 changes: 2 additions & 2 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var _ Chainer = (*ChainClient)(nil)
// Return:
// - *ChainClient: chain client
// - error: error message
func NewChainClientUnconnectedRpc(ctx context.Context, name string, rpcs []string, mnemonic string, t time.Duration) (*ChainClient, error) {
func NewChainClientUnconnectedRpc(ctx context.Context, name string, rpcs []string, mnemonic string, t time.Duration) (Chainer, error) {
var err error
var chainClient = &ChainClient{
lock: new(sync.Mutex),
Expand Down Expand Up @@ -95,7 +95,7 @@ func NewChainClientUnconnectedRpc(ctx context.Context, name string, rpcs []strin
// Return:
// - *ChainClient: chain client
// - error: error message
func NewChainClient(ctx context.Context, name string, rpcs []string, mnemonic string, t time.Duration) (*ChainClient, error) {
func NewChainClient(ctx context.Context, name string, rpcs []string, mnemonic string, t time.Duration) (Chainer, error) {
var (
err error
chainClient = &ChainClient{
Expand Down
6 changes: 4 additions & 2 deletions chain/chainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Chainer interface {
QueryCountedServiceFailed(accountID []byte, block int32) (uint32, error)
SubmitIdleProof(idleProof []types.U8) (string, error)
SubmitServiceProof(serviceProof []types.U8) (string, error)
SubmitVerifyIdleResult(totalProofHash []types.U8, front, rear types.U64, accumulator Accumulator, result types.Bool, sig types.Bytes, teePuk WorkerPublicKey) (string, error)
SubmitVerifyServiceResult(result types.Bool, sign types.Bytes, bloomFilter BloomFilter, teePuk WorkerPublicKey) (string, error)

// Babe
QueryAuthorities(block int32) ([]ConsensusRrscAppPublic, error)
Expand Down Expand Up @@ -92,10 +94,10 @@ type Chainer interface {
MinerWithdraw() (string, error)
ReceiveReward() (string, string, error)
RegisterPoisKey(poisKey PoISKeyInfo, teeSignWithAcc, teeSign types.Bytes, teePuk WorkerPublicKey) (string, error)
RegnstkSminer(earnings string, peerId []byte, staking uint64, tibCount uint32) (string, error)
RegnstkSminer(earnings string, addr []byte, staking uint64, tibCount uint32) (string, error)
RegnstkAssignStaking(earnings string, peerId []byte, stakingAcc string, tibCount uint32) (string, error)
UpdateBeneficiary(earnings string) (string, error)
UpdateSminerPeerId(peerid PeerId) (string, error)
UpdateSminerAddr(addr []byte) (string, error)

// Staking
QueryCounterForValidators(block int32) (uint32, error)
Expand Down
9 changes: 4 additions & 5 deletions chain/deoss.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"strings"
"time"

"github.com/CESSProject/cess-go-sdk/config"
"github.com/CESSProject/cess-go-sdk/utils"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
Expand Down Expand Up @@ -523,8 +522,8 @@ func (c *ChainClient) RegisterOss(peerId []byte, domain string) (string, error)
peerid[i] = types.U8(peerId[i])
}

if len(domain) > config.MaxDomainNameLength {
return blockhash, fmt.Errorf("register deoss: Domain name length cannot exceed %v characters", config.MaxDomainNameLength)
if len(domain) > int(MaxDomainNameLength) {
return blockhash, fmt.Errorf("register deoss: Domain name length cannot exceed %v characters", MaxDomainNameLength)
}

err := utils.CheckDomain(domain)
Expand Down Expand Up @@ -658,8 +657,8 @@ func (c *ChainClient) UpdateOss(peerId string, domain string) (string, error) {
peerid[i] = types.U8(peerId[i])
}

if len(domain) > config.MaxDomainNameLength {
return blockhash, fmt.Errorf("update oss: domain name length cannot exceed %v", config.MaxDomainNameLength)
if len(domain) > int(MaxDomainNameLength) {
return blockhash, fmt.Errorf("update oss: domain name length cannot exceed %v", MaxDomainNameLength)
}

err := utils.CheckDomain(domain)
Expand Down
18 changes: 12 additions & 6 deletions chain/extrinsic_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,13 @@ const (
ExtName_Multisig_cancel_as_multi = "Multisig.cancel_as_multi"

// Oss
ExtName_Oss_authorize = "Oss.authorize"
ExtName_Oss_cancel_authorize = "Oss.cancel_authorize"
ExtName_Oss_destroy = "Oss.destroy"
ExtName_Oss_proxy_authorzie = "Oss.proxy_authorzie"
ExtName_Oss_register = "Oss.register"
ExtName_Oss_update = "Oss.update"
ExtName_Oss_authorize = "Oss.authorize"
ExtName_Oss_cancel_authorize = "Oss.cancel_authorize"
ExtName_Oss_destroy = "Oss.destroy"
ExtName_Oss_evm_proxy_authorzie = "Oss.evm_proxy_authorzie"
ExtName_Oss_proxy_authorzie = "Oss.proxy_authorzie"
ExtName_Oss_register = "Oss.register"
ExtName_Oss_update = "Oss.update"

// Parameters
ExtName_Parameters_set_parameter = "Parameters.set_parameter"
Expand Down Expand Up @@ -1213,6 +1214,11 @@ func (c *ChainClient) InitExtrinsicsName() error {
} else {
return err
}
if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_Oss_evm_proxy_authorzie); err == nil {
ExtrinsicsName[callIndex] = ExtName_Oss_evm_proxy_authorzie
} else {
return err
}
if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_Oss_proxy_authorzie); err == nil {
ExtrinsicsName[callIndex] = ExtName_Oss_proxy_authorzie
} else {
Expand Down
3 changes: 1 addition & 2 deletions chain/file_bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"strings"
"time"

"github.com/CESSProject/cess-go-sdk/config"
"github.com/CESSProject/cess-go-sdk/utils"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
Expand Down Expand Up @@ -1201,7 +1200,7 @@ func (c *ChainClient) TransferReport(index uint8, fid string) (string, error) {
accountInfo types.AccountInfo
)

if index <= 0 || int(index) > (config.DataShards+config.ParShards) {
if index <= 0 || int(index) > (DataShards+ParShards) {
return "", errors.New("invalid index")
}

Expand Down
22 changes: 22 additions & 0 deletions chain/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ const BlockInterval = time.Second * time.Duration(BlockIntervalSec)

const TreasuryAccount = "cXhT9Xh3DhrBMDmXcGeMPDmTzDm1J8vDxBtKvogV33pPshnWS"

const (
SIZE_1KiB = 1024
SIZE_1MiB = 1024 * SIZE_1KiB
SIZE_1GiB = 1024 * SIZE_1MiB
SIZE_1TiB = 1024 * SIZE_1GiB

NumberOfDataCopies = 3

SegmentSize = 32 * SIZE_1MiB
FragmentSize = 8 * SIZE_1MiB
DataShards = 4
ParShards = 8
)

const (
MinBucketNameLength uint8 = 3
MaxBucketNameLength uint8 = 63
MaxDomainNameLength uint8 = 100
)

const ZeroFileHash_8M string = "2daeb1f36095b44b318410b3f4e8b5d989dcc7bb023d1426c492dab0a3053e74"

// pallet names
const (
// Audit
Expand Down
34 changes: 22 additions & 12 deletions chain/sminer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1431,14 +1431,14 @@ func (c *ChainClient) RegisterPoisKey(poisKey PoISKeyInfo, teeSignWithAcc, teeSi
// which is the first stage of storage miner registration.
//
// - earnings: earnings account
// - peerId: peer id
// - addr: address
// - staking: number of staking, the unit is CESS
// - tibCount: the size of declaration space, in TiB
//
// Return:
// - string: block hash
// - error: error message
func (c *ChainClient) RegnstkSminer(earnings string, peerId []byte, staking uint64, tibCount uint32) (string, error) {
func (c *ChainClient) RegnstkSminer(earnings string, addr []byte, staking uint64, tibCount uint32) (string, error) {
<-c.tradeCh
defer func() {
c.tradeCh <- true
Expand All @@ -1452,13 +1452,13 @@ func (c *ChainClient) RegnstkSminer(earnings string, peerId []byte, staking uint
accountInfo types.AccountInfo
)

var peerid PeerId
if len(peerId) != PeerIdPublicKeyLen {
return blockhash, fmt.Errorf("[RegnstkSminer] invalid peerid: %v", peerId)
var pid PeerId
if len(addr) > PeerIdPublicKeyLen {
return blockhash, fmt.Errorf("[RegnstkSminer] addr exceeds %d bytes", PeerIdPublicKeyLen)
}

for i := 0; i < len(peerid); i++ {
peerid[i] = types.U8(peerId[i])
for i := 0; i < len(addr); i++ {
pid[i] = types.U8(addr[i])
}

pubkey, err := utils.ParsingPublickey(earnings)
Expand All @@ -1473,7 +1473,7 @@ func (c *ChainClient) RegnstkSminer(earnings string, peerId []byte, staking uint
if !ok {
return blockhash, errors.New("[big.Int.SetString]")
}
call, err := types.NewCall(c.metadata, ExtName_Sminer_regnstk, *acc, peerid, types.NewU128(*realTokens), types.U32(tibCount))
call, err := types.NewCall(c.metadata, ExtName_Sminer_regnstk, *acc, pid, types.NewU128(*realTokens), types.U32(tibCount))
if err != nil {
err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_regnstk, err)
return blockhash, err
Expand Down Expand Up @@ -1847,14 +1847,14 @@ func (c *ChainClient) UpdateBeneficiary(earnings string) (string, error) {
}
}

// UpdateSminerPeerId update peer id for storage miner
// UpdateSminerAddr update address for storage miner
//
// - peerid: peer id
// - addr: address
//
// Return:
// - string: block hash
// - error: error message
func (c *ChainClient) UpdateSminerPeerId(peerid PeerId) (string, error) {
func (c *ChainClient) UpdateSminerAddr(addr []byte) (string, error) {
<-c.tradeCh
defer func() {
c.tradeCh <- true
Expand All @@ -1868,7 +1868,17 @@ func (c *ChainClient) UpdateSminerPeerId(peerid PeerId) (string, error) {
accountInfo types.AccountInfo
)

call, err := types.NewCall(c.metadata, ExtName_Sminer_update_peer_id, peerid)
var pid PeerId

if len(addr) > PeerIdPublicKeyLen {
return blockhash, fmt.Errorf("addr exceeds %d bytes", PeerIdPublicKeyLen)
}

for i := 0; i < len(addr); i++ {
pid[i] = types.U8(addr[i])
}

call, err := types.NewCall(c.metadata, ExtName_Sminer_update_peer_id, pid)
if err != nil {
err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), ExtName_Sminer_update_peer_id, err)
return blockhash, err
Expand Down
41 changes: 41 additions & 0 deletions chain/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ package chain

import (
"encoding/hex"
"regexp"
"strings"

"github.com/CESSProject/cess-go-sdk/utils"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/ethereum/go-ethereum/log"
"github.com/pkg/errors"
Expand Down Expand Up @@ -103,3 +105,42 @@ func H160ToSS58(origin string, chain_id uint16) (string, error) {

return new_acc, nil
}

var re = regexp.MustCompile(`^[a-zA-Z0-9._-]+$`)

func CheckBucketName(name string) bool {
if len(name) < int(MinBucketNameLength) || len(name) > int(MaxBucketNameLength) {
return false
}

if !re.MatchString(name) {
return false
}

if strings.Contains(name, " ") {
return false
}

if strings.Count(name, ".") > 2 {
return false
}

if byte(name[0]) == byte('.') ||
byte(name[0]) == byte('-') ||
byte(name[0]) == byte('_') ||
byte(name[len(name)-1]) == byte('.') ||
byte(name[len(name)-1]) == byte('-') ||
byte(name[len(name)-1]) == byte('_') {
return false
}

if utils.IsIPv4(name) {
return false
}

if utils.IsIPv6(name) {
return false
}

return true
}
23 changes: 0 additions & 23 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,7 @@

package config

const (
SIZE_1KiB = 1024
SIZE_1MiB = 1024 * SIZE_1KiB
SIZE_1GiB = 1024 * SIZE_1MiB
SIZE_1TiB = 1024 * SIZE_1GiB

SegmentSize = 32 * SIZE_1MiB
FragmentSize = 8 * SIZE_1MiB
DataShards = 4
ParShards = 8
)

const (
MinBucketNameLength = 3
MaxBucketNameLength = 63
MaxDomainNameLength = 100
)

const (
// default name
CharacterName_Default = "cess-sdk-go"

// offcial gateway address
PublicGatewayAddr = "https://deoss-pub-gateway.cess.network/"
// offcial gateway account
PublicGatewayAccount = "cXhwBytXqrZLr1qM5NHJhCzEMckSTzNKw17ci2aHft6ETSQm9"
)
14 changes: 7 additions & 7 deletions core/erasure/rs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"os"
"path/filepath"

"github.com/CESSProject/cess-go-sdk/config"
"github.com/CESSProject/cess-go-sdk/chain"
"github.com/CESSProject/cess-go-sdk/utils"
"github.com/klauspost/reedsolomon"
)
Expand All @@ -36,11 +36,11 @@ func ReedSolomon(file string, saveDir string) ([]string, error) {
if fstat.IsDir() {
return nil, errors.New("not a file")
}
if fstat.Size() != config.SegmentSize {
if fstat.Size() != chain.SegmentSize {
return nil, errors.New("invalid size")
}

enc, err := reedsolomon.New(config.DataShards, config.ParShards)
enc, err := reedsolomon.New(chain.DataShards, chain.ParShards)
if err != nil {
return shardspath, err
}
Expand Down Expand Up @@ -93,12 +93,12 @@ func RSRestore(outpath string, shardspath []string) error {
return nil
}

enc, err := reedsolomon.New(config.DataShards, config.ParShards)
enc, err := reedsolomon.New(chain.DataShards, chain.ParShards)
if err != nil {
return err
}

shards := make([][]byte, config.DataShards+config.ParShards)
shards := make([][]byte, chain.DataShards+chain.ParShards)
for k, v := range shardspath {
shards[k], err = os.ReadFile(v)
if err != nil {
Expand Down Expand Up @@ -127,7 +127,7 @@ func RSRestore(outpath string, shardspath []string) error {
return err
}
defer f.Close()
err = enc.Join(f, shards, len(shards[0])*config.DataShards)
err = enc.Join(f, shards, len(shards[0])*chain.DataShards)
return err
}

Expand All @@ -145,7 +145,7 @@ func RSRestoreData(outpath string, sharddata [][]byte) error {
return nil
}

datashards, parshards := config.DataShards, config.ParShards
datashards, parshards := chain.DataShards, chain.ParShards

enc, err := reedsolomon.New(datashards, parshards)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions core/process/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
Copyright (C) CESS. All rights reserved.
Copyright (C) Cumulus Encrypted Storage System. All rights reserved.
SPDX-License-Identifier: Apache-2.0
*/

package process

type RespType struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data any `json:"data"`
}
Loading

0 comments on commit 9753395

Please sign in to comment.