Skip to content

Commit

Permalink
Base portuse (#109)
Browse files Browse the repository at this point in the history
* update p2p to v0.1.5

* update DownloadFromGateway

* update DownloadFromGateway
  • Loading branch information
AstaFrode authored Jul 13, 2023
1 parent 39ee3dd commit c0af7b1
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
11 changes: 10 additions & 1 deletion chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package chain
import (
"context"
"errors"
"fmt"
"io"
"log"
"net/http"
Expand Down Expand Up @@ -77,6 +78,10 @@ func NewChainSDK(
}
)

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++ {
chainSDK.api, err = gsrpc.NewSubstrateAPI(rpcs[i])
Expand All @@ -85,10 +90,14 @@ func NewChainSDK(
}
}
log.SetOutput(os.Stdout)
if err != nil || chainSDK.api == nil {
if err != nil {
return nil, err
}

if chainSDK.api == nil {
return nil, pattern.ERR_RPC_CONNECTION
}

chainSDK.SetChainState(true)

chainSDK.metadata, err = chainSDK.api.RPC.State.GetMetadataLatest()
Expand Down
5 changes: 3 additions & 2 deletions chain/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ func (c *ChainSDK) UploadtoGateway(url, account, uploadfile, bucketName string)
return "", err
}

req.Header.Set("BucketName", bucketName)
req.Header.Set("Account", account)
req.Header.Set("Message", message)
req.Header.Set("Signature", base58.Encode(sig[:]))
Expand Down Expand Up @@ -380,7 +381,7 @@ func (c *ChainSDK) DownloadFromGateway(url, roothash, savepath string) error {
}
defer f.Close()

req, err := http.NewRequest(http.MethodGet, filepath.Join(url, roothash), nil)
req, err := http.NewRequest(http.MethodGet, url+roothash, nil)
if err != nil {
return err
}
Expand All @@ -399,7 +400,7 @@ func (c *ChainSDK) DownloadFromGateway(url, roothash, savepath string) error {
return errors.New("failed")
}

_, err = io.Copy(f, req.Body)
_, err = io.Copy(f, resp.Body)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion core/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ type SDK interface {
RetrieveFile(roothash, savepath string) error
// UploadtoGateway uploads files to deoss gateway.
UploadtoGateway(url, account, uploadfile, bucketName string) (string, error)
//
// DownloadFromGateway downloads files from deoss gateway.
DownloadFromGateway(url, roothash, savepath string) error
// EnabledP2P returns the p2p enable status
EnabledP2P() bool
Expand Down
13 changes: 8 additions & 5 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@ var MY_MNEMONIC = "bottom drive obey lake curtain smoke basket hold race lonely
var RPC_ADDRS = []string{
"wss://testnet-rpc0.cess.cloud/ws/",
"wss://testnet-rpc1.cess.cloud/ws/",
"wss://testnet-rpc2.cess.cloud/ws/",
}

var Workspace = "/cess"
var Port = 4001
var Bootstrap = []string{
"_dnsaddr.sjc-1.bootstrap-kldr.cess.cloud",
"_dnsaddr.boot-kldr-testnet.cess.cloud",
}

const File = "/home/test_download"
const UploadFile = "example.go"
const DownloadFile = "download_file"
const BucketName = "myBucket"
const FileHash = "c158d7008e94d3af61033b6861aa4f35a4c2b829c7e97224fcbb54618de55945"
const FileHash = "3ea772e68cf615260916dc94f501c43da78f6fdc15dc20e722e5284aca612a92"

func main() {
StoreFile()
RetrieveFile()
}

Expand All @@ -51,7 +54,7 @@ func RetrieveFile() {
panic(err)
}

fmt.Println(sdk.RetrieveFile(FileHash, File))
fmt.Println(sdk.RetrieveFile(FileHash, DownloadFile))
}

func StoreFile() {
Expand All @@ -70,7 +73,7 @@ func StoreFile() {
panic(err)
}

fmt.Println(sdk.StoreFile(File, BucketName))
fmt.Println(sdk.StoreFile(UploadFile, BucketName))
}

func CreateBucket() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/CESSProject/go-keyring v0.0.0-20220614131247-ee3a8da30fde
github.com/CESSProject/p2p-go v0.1.4
github.com/CESSProject/p2p-go v0.1.5
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.0.13
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
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.1.4 h1:K6NNDfp1dCLDQ5BmeYGf9Lq5ZHgBM4Yuyv2q6oZ2xgY=
github.com/CESSProject/p2p-go v0.1.4/go.mod h1:JSePQ8HZVxC/t8VpkvD/Gfu9XOqXkeHYXQZBRiGZNqo=
github.com/CESSProject/p2p-go v0.1.5 h1:TKmn4ArpUXVngowyYftQScz21X7kObQIr34CemuLn6Q=
github.com/CESSProject/p2p-go v0.1.5/go.mod h1:JSePQ8HZVxC/t8VpkvD/Gfu9XOqXkeHYXQZBRiGZNqo=
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=
Expand Down

0 comments on commit c0af7b1

Please sign in to comment.