Skip to content

Commit

Permalink
Territory 1 (#221)
Browse files Browse the repository at this point in the history
* update PlaceStorageOrder

* update file process and example

* update QueryTerritory
  • Loading branch information
AstaFrode authored Jun 28, 2024
1 parent 33ba779 commit 2fa4941
Show file tree
Hide file tree
Showing 14 changed files with 236 additions and 291 deletions.
2 changes: 1 addition & 1 deletion chain/chainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Chainer interface {
QueryAllRestoralOrder(block int32) ([]RestoralOrderInfo, error)
QueryAllBucketName(accountID []byte, block int32) ([]string, error)
QueryAllUserFiles(accountID []byte, block int32) ([]string, error)
GenerateStorageOrder(fid string, segment []SegmentDataInfo, owner []byte, filename string, buckname string, filesize uint64) (string, error)
PlaceStorageOrder(fid, file_name, bucket_name, territory_name string, segment []SegmentDataInfo, owner []byte, file_size uint64) (string, error)
UploadDeclaration(fid string, segment []SegmentList, user UserBrief, filesize uint64) (string, error)
CreateBucket(owner []byte, bucketName string) (string, error)
DeleteBucket(owner []byte, bucketName string) (string, error)
Expand Down
16 changes: 9 additions & 7 deletions chain/file_bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,18 +571,19 @@ func (c *ChainClient) QueryUserHoldFileList(accountID []byte, block int32) ([]st
return value, nil
}

// GenerateStorageOrder generate a file storage order
// PlaceStorageOrder place an order for storage file
// - fid: file identification
// - file_name: file name
// - bucket_name: bucket name
// - territory_name: territory name
// - segment: segment info
// - owner: account of the file owner
// - filename: file name
// - buckname: bucket to store the file
// - filesize: file size
//
// Return:
// - string: block hash
// - error: error message
func (c *ChainClient) GenerateStorageOrder(fid string, segment []SegmentDataInfo, owner []byte, filename string, buckname string, filesize uint64) (string, error) {
func (c *ChainClient) PlaceStorageOrder(fid, file_name, bucket_name, territory_name string, segment []SegmentDataInfo, owner []byte, file_size uint64) (string, error) {
var err error
var segmentList = make([]SegmentList, len(segment))
var user UserBrief
Expand All @@ -606,9 +607,10 @@ func (c *ChainClient) GenerateStorageOrder(fid string, segment []SegmentDataInfo
return "", err
}
user.User = *acc
user.BucketName = types.NewBytes([]byte(buckname))
user.FileName = types.NewBytes([]byte(filename))
return c.UploadDeclaration(fid, segmentList, user, filesize)
user.BucketName = types.NewBytes([]byte(bucket_name))
user.FileName = types.NewBytes([]byte(file_name))
user.TerriortyName = types.NewBytes([]byte(territory_name))
return c.UploadDeclaration(fid, segmentList, user, file_size)
}

// GenerateStorageOrder generate a file storage order
Expand Down
8 changes: 1 addition & 7 deletions chain/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const (
TotalIdleSpace = "TotalIdleSpace"
TotalServiceSpace = "TotalServiceSpace"
PurchasedSpace = "PurchasedSpace"
TerritoryKey = "TerritoryKey"
Territory = "Territory"
PayOrder = "PayOrder"
Consignment = "Consignment"

Expand Down Expand Up @@ -431,12 +431,6 @@ type FragmentInfo struct {
}

type UserBrief struct {
User types.AccountID
FileName types.Bytes
BucketName types.Bytes
}

type UserBrief_T struct {
User types.AccountID
FileName types.Bytes
BucketName types.Bytes
Expand Down
10 changes: 5 additions & 5 deletions chain/storage_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,21 @@ func (c *ChainClient) QueryTerritory(accountId []byte, name string, block int32)
return data, ERR_RPC_CONNECTION
}

param1, err := codec.Encode(accountId)
param2, err := codec.Encode(types.NewBytes([]byte(name)))
if err != nil {
return data, errors.New("invalid account id")
}

key, err := types.CreateStorageKey(c.metadata, StorageHandler, TerritoryKey, param1, types.NewBytes([]byte(name)))
key, err := types.CreateStorageKey(c.metadata, StorageHandler, Territory, accountId, param2)
if err != nil {
err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), StorageHandler, TerritoryKey, err)
err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), StorageHandler, Territory, err)
return data, err
}

if block < 0 {
ok, err := c.api.RPC.State.GetStorageLatest(key, &data)
if err != nil {
err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), StorageHandler, TerritoryKey, err)
err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), StorageHandler, Territory, err)
c.SetRpcState(false)
return data, err
}
Expand All @@ -285,7 +285,7 @@ func (c *ChainClient) QueryTerritory(accountId []byte, name string, block int32)
}
ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash)
if err != nil {
err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), StorageHandler, TerritoryKey, err)
err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), StorageHandler, Territory, err)
c.SetRpcState(false)
return data, err
}
Expand Down
15 changes: 7 additions & 8 deletions core/erasure/rs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import (
// ReedSolomon uses reed-solomon algorithm to redundancy file
//
// Receive parameter:
// - path: files to process.
// - file: the file to be processed
// - saveDir: save directory
//
// Return parameter:
// - []string: Processed data fragmentation.
// - []string: processed data fragmentation
// - error: error message.
func ReedSolomon(path string) ([]string, error) {
func ReedSolomon(file string, saveDir string) ([]string, error) {
var shardspath = make([]string, 0)
fstat, err := os.Stat(path)
fstat, err := os.Stat(file)
if err != nil {
return nil, err
}
Expand All @@ -39,14 +40,12 @@ func ReedSolomon(path string) ([]string, error) {
return nil, errors.New("invalid size")
}

basedir := filepath.Dir(path)

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

b, err := os.ReadFile(path)
b, err := os.ReadFile(file)
if err != nil {
return shardspath, err
}
Expand All @@ -67,7 +66,7 @@ func ReedSolomon(path string) ([]string, error) {
if err != nil {
return shardspath, err
}
newpath := filepath.Join(basedir, hash)
newpath := filepath.Join(saveDir, hash)
_, err = os.Stat(newpath)
if err != nil {
err = os.WriteFile(newpath, shard, 0755)
Expand Down
22 changes: 13 additions & 9 deletions core/process/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ var globalTransport = &http.Transport{
// Receive parameter:
// - url: gateway url
// - file: stored file
// - bucket: bucket for storing file
// - bucket: bucket for storing file, it will be created automatically
// - territory: territory name
// - mnemonic: polkadot account mnemonic
//
// Return parameter:
Expand All @@ -49,7 +50,7 @@ var globalTransport = &http.Transport{
// - Account refers to the account where you configured mnemonic when creating an SDK.
// - CESS public gateway address: [http://deoss-pub-gateway.cess.cloud/]
// - CESS public gateway account: [cXhwBytXqrZLr1qM5NHJhCzEMckSTzNKw17ci2aHft6ETSQm9]
func StoreFile(url, file, bucket, mnemonic string) (string, error) {
func StoreFile(url, file, bucket, territory, mnemonic string) (string, error) {
fstat, err := os.Stat(file)
if err != nil {

Expand Down Expand Up @@ -113,7 +114,8 @@ func StoreFile(url, file, bucket, mnemonic string) (string, error) {
return "", err
}

req.Header.Set("BucketName", bucket)
req.Header.Set("Bucket", bucket)
req.Header.Set("Territory", territory)
req.Header.Set("Account", acc)
req.Header.Set("Message", message)
req.Header.Set("Signature", base58.Encode(sig[:]))
Expand Down Expand Up @@ -146,13 +148,14 @@ func StoreFile(url, file, bucket, mnemonic string) (string, error) {
//
// Receive parameter:
// - url: gateway url
// - bucket: the bucket for storing object, it will be created automatically.
// - bucket: the bucket for storing object, it will be created automatically
// - territory: territory name
// - mnemonic: polkadot account mnemonic
// - reader: strings, byte data, file streams, network streams, etc.
// - reader: strings, byte data, file streams, network streams, etc
//
// Return parameter:
// - string: [fid] unique identifier for the file.
// - error: error message.
// - string: [fid] unique identifier for the file
// - error: error message
//
// Preconditions:
// 1. Account requires purchasing space, refer to [BuySpace] interface.
Expand All @@ -164,7 +167,7 @@ func StoreFile(url, file, bucket, mnemonic string) (string, error) {
// - Account refers to the account where you configured mnemonic when creating an SDK.
// - CESS public gateway address: [http://deoss-pub-gateway.cess.cloud/]
// - CESS public gateway account: [cXhwBytXqrZLr1qM5NHJhCzEMckSTzNKw17ci2aHft6ETSQm9]
func StoreObject(url string, bucket, mnemonic string, reader io.Reader) (string, error) {
func StoreObject(url string, bucket, territory, mnemonic string, reader io.Reader) (string, error) {
if !utils.CheckBucketName(bucket) {
return "", errors.New("invalid bucket name")
}
Expand All @@ -191,7 +194,8 @@ func StoreObject(url string, bucket, mnemonic string, reader io.Reader) (string,
return "", err
}

req.Header.Set("BucketName", bucket)
req.Header.Set("Bucket", bucket)
req.Header.Set("Territory", territory)
req.Header.Set("Account", acc)
req.Header.Set("Message", message)
req.Header.Set("Signature", base58.Encode(sig[:]))
Expand Down
Loading

0 comments on commit 2fa4941

Please sign in to comment.