Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update node #165

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ func (c *chainClient) Reconnect() error {
return nil
}

func (c *chainClient) GetSdkName() string {
func (c *chainClient) GetSDKName() string {
return c.name
}

func (c *chainClient) GetCurrentRpcAddr() string {
return c.currentRpcAddr
}

func (c *chainClient) SetSdkName(name string) {
func (c *chainClient) SetSDKName(name string) {
c.name = name
}

Expand Down
36 changes: 36 additions & 0 deletions chain/deoss.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,42 @@ func (c *chainClient) QueryDeossInfo(pubkey []byte) (pattern.OssInfo, error) {
return data, nil
}

// QueryAllDeOSSInfo
func (c *chainClient) QueryAllDeOSSInfo() ([]pattern.OssInfo, error) {
defer func() {
if err := recover(); err != nil {
log.Println(utils.RecoverError(err))
}
}()
var result []pattern.OssInfo

if !c.GetChainState() {
return nil, pattern.ERR_RPC_CONNECTION
}

key := createPrefixedKey(pattern.OSS, pattern.OSS)
keys, err := c.api.RPC.State.GetKeysLatest(key)
if err != nil {
return nil, errors.Wrap(err, "[GetKeysLatest]")
}

set, err := c.api.RPC.State.QueryStorageAtLatest(keys)
if err != nil {
return nil, errors.Wrap(err, "[QueryStorageAtLatest]")
}

for _, elem := range set {
for _, change := range elem.Changes {
var data pattern.OssInfo
if err := codec.Decode(change.StorageData, &data); err != nil {
continue
}
result = append(result, data)
}
}
return result, nil
}

// QueryDeossPeerIdList
func (c *chainClient) QueryDeossPeerIdList() ([]string, error) {
defer func() {
Expand Down
1 change: 1 addition & 0 deletions chain/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ func (c *chainClient) RetrieveFile(url, fid, savepath string) error {
}

req.Header.Set("Operation", "download")
req.Header.Set("Account", c.GetSignatureAcc())

client := &http.Client{}
client.Transport = globalTransport
Expand Down
4 changes: 2 additions & 2 deletions chain/sminer.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ func (c *chainClient) RegisterSminer(peerId []byte, earnings string, pledge uint
return txhash, pattern.ERR_RPC_CONNECTION
}

c.SetSdkName(pattern.Name_Sminer)
c.SetSDKName(pattern.Name_Sminer)

var peerid pattern.PeerId
if len(peerid) != len(peerId) {
Expand Down Expand Up @@ -886,7 +886,7 @@ func (c *chainClient) RegisterSminerPOISKey(poisKey pattern.PoISKeyInfo, sign pa
return txhash, pattern.ERR_RPC_CONNECTION
}

c.SetSdkName(pattern.Name_Sminer)
c.SetSDKName(pattern.Name_Sminer)

key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion chain/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (c *chainClient) SyncState() (pattern.SysSyncState, error) {
return data, err
}

func (c *chainClient) SysVersion() (string, error) {
func (c *chainClient) ChainVersion() (string, error) {
defer func() {
if err := recover(); err != nil {
log.Println(utils.RecoverError(err))
Expand Down
16 changes: 8 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package config

import (
"context"
"fmt"
"time"

"github.com/CESSProject/cess-go-sdk/chain"
Expand All @@ -29,19 +28,20 @@ type Option func(cfg *Config) error

// default service name
const (
CharacterName_Client = "client"
CharacterName_Bucket = "bucket"
CharacterName_Deoss = "deoss"
CharacterName_Default = "sdk"
CharacterName_Client = "client"
CharacterName_Bucket = "bucket"
CharacterName_Deoss = "deoss"
)

// 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")
func (cfg *Config) NewSDK(ctx context.Context) (sdk.SDK, error) {
if cfg.Name == "" {
cfg.Name = CharacterName_Default
}
return chain.NewChainClient(ctx, serviceName, cfg.Rpc, cfg.Mnemonic, cfg.Timeout)
return chain.NewChainClient(ctx, cfg.Name, cfg.Rpc, cfg.Mnemonic, cfg.Timeout)
}

// Apply applies the given options to the config, returning the first error
Expand Down
8 changes: 5 additions & 3 deletions core/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ type SDK interface {
QuaryAuthorizedAccounts(puk []byte) ([]string, error)
// QueryDeossInfo queries deoss info.
QueryDeossInfo(pubkey []byte) (pattern.OssInfo, error)
// QueryAllDeOSSInfo queries all deoss info
QueryAllDeOSSInfo() ([]pattern.OssInfo, error)
// QueryDeossPeerIdList queries peerid of all deoss.
QueryDeossPeerIdList() ([]string, error)
// CheckSpaceUsageAuthorization checks if the puk is authorized to itself
Expand Down Expand Up @@ -218,7 +220,7 @@ type SDK interface {
// SyncState returns the system sync state.
SyncState() (pattern.SysSyncState, error)
// SysVersion returns the system version.
SysVersion() (string, error)
ChainVersion() (string, error)
// NetListening returns whether the current node is listening.
NetListening() (bool, error)
//
Expand Down Expand Up @@ -250,11 +252,11 @@ type SDK interface {
// SetChainState sets the state of the chain node.
SetChainState(state bool)
// GetSdkName return sdk name
GetSdkName() string
GetSDKName() string
// GetCurrentRpcAddr return current rpc address
GetCurrentRpcAddr() string
// SetSdkName set sdk name
SetSdkName(name string)
SetSDKName(name string)
// Reconnect for reconnecting chains.
Reconnect() error
// GetMetadata returns the metadata of the chain.
Expand Down
2 changes: 0 additions & 2 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"time"

cess "github.com/CESSProject/cess-go-sdk"
"github.com/CESSProject/cess-go-sdk/config"
"github.com/CESSProject/cess-go-sdk/core/sdk"
)

Expand Down Expand Up @@ -81,7 +80,6 @@ func main() {
func NewSDK() (sdk.SDK, error) {
return cess.New(
context.Background(),
config.CharacterName_Client,
cess.ConnectRpcAddrs(RPC_ADDRS),
cess.Mnemonic(MY_MNEMONIC),
cess.TransactionTimeout(time.Second*10),
Expand Down
31 changes: 31 additions & 0 deletions example/quic_init_sdk/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"context"

cess "github.com/CESSProject/cess-go-sdk"
)

// Substrate well-known mnemonic:
//
// https://github.com/substrate-developer-hub/substrate-developer-hub.github.io/issues/613
var MY_MNEMONIC = "bottom drive obey lake curtain smoke basket hold race lonely fit walk"

var RPC_ADDRS = []string{
"wss://testnet-rpc0.cess.cloud/ws/",
"wss://testnet-rpc1.cess.cloud/ws/",
"wss://testnet-rpc2.cess.cloud/ws/",
}

func main() {
sdk, err := cess.New(
context.Background(),
cess.ConnectRpcAddrs(RPC_ADDRS),
cess.Mnemonic(MY_MNEMONIC),
)
if err != nil {
panic(err)
}
// Upload file to oss
sdk.StoreFile("http://deoss-pub-gateway.cess.cloud/", "test.log", "test-bucket")
}
2 changes: 0 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/stretchr/testify/assert"

cess "github.com/CESSProject/cess-go-sdk"
"github.com/CESSProject/cess-go-sdk/config"
)

const DEFAULT_WAIT_TIME = time.Second * 15
Expand All @@ -36,7 +35,6 @@ func TestMain(m *testing.M) {
func TestNewClient(t *testing.T) {
_, err := cess.New(
context.Background(),
config.CharacterName_Client,
cess.ConnectRpcAddrs(strings.Split(os.Getenv("RPC_ADDRS"), " ")),
cess.Mnemonic(os.Getenv("MY_MNEMONIC")),
cess.TransactionTimeout(time.Duration(DEFAULT_WAIT_TIME)),
Expand Down
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ func TransactionTimeout(timeout time.Duration) Option {
return nil
}
}

// Name configuration sdk name
func Name(name string) Option {
return func(cfg *Config) error {
cfg.Name = name
return nil
}
}
8 changes: 4 additions & 4 deletions sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type Option = config.Option
// cess-bucket (cess storage service) must be set to bucket
// DeOSS (cess decentralized object storage service) must be set to deoss
// cess-cli (cess client) must be set to client
func New(ctx context.Context, serviceName string, opts ...Option) (sdk.SDK, error) {
return NewWithoutDefaults(ctx, serviceName, append(opts, FallbackDefaults)...)
func New(ctx context.Context, opts ...Option) (sdk.SDK, error) {
return NewWithoutDefaults(ctx, append(opts, FallbackDefaults)...)
}

// NewWithoutDefaults constructs a new client with the given options but
Expand All @@ -45,10 +45,10 @@ func New(ctx context.Context, serviceName string, opts ...Option) (sdk.SDK, erro
// Warning: This function should not be considered a stable interface. We may
// choose to add required services at any time and, by using this function, you
// opt-out of any defaults we may provide.
func NewWithoutDefaults(ctx context.Context, serviceName string, opts ...Option) (sdk.SDK, error) {
func NewWithoutDefaults(ctx context.Context, opts ...Option) (sdk.SDK, error) {
var cfg Config
if err := cfg.Apply(opts...); err != nil {
return nil, err
}
return cfg.NewSDK(ctx, serviceName)
return cfg.NewSDK(ctx)
}
11 changes: 11 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Copyright (C) CESS. All rights reserved.
Copyright (C) Cumulus Encrypted Storage System. All rights reserved.

SPDX-License-Identifier: Apache-2.0
*/

package sdkgo

// SDK Version
const Version = "0.4.0"