Skip to content

Commit

Permalink
Base p2p4 (#99)
Browse files Browse the repository at this point in the history
* update p2p to v0.1

* add BuySpace,ExpansionSpace,RenewalSpace
  • Loading branch information
AstaFrode authored Jul 6, 2023
1 parent 633bcca commit 0bd49af
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 4 deletions.
281 changes: 281 additions & 0 deletions chain/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ package chain
import (
"fmt"
"log"
"time"

"github.com/CESSProject/cess-go-sdk/core/event"
"github.com/CESSProject/cess-go-sdk/core/pattern"
"github.com/CESSProject/cess-go-sdk/core/utils"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
Expand Down Expand Up @@ -100,3 +102,282 @@ func (c *ChainSDK) QueryUserSpaceSt(puk []byte) (pattern.UserSpaceSt, error) {
userSpaceSt.State = string(spaceinfo.State)
return userSpaceSt, nil
}

func (c *ChainSDK) BuySpace(count uint32) (string, error) {
c.lock.Lock()
defer func() {
c.lock.Unlock()
if err := recover(); err != nil {
log.Println(utils.RecoverError(err))
}
}()

var (
txhash string
accountInfo types.AccountInfo
)

if count == 0 {
return "", errors.New("[BuySpace] invalid count")
}

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

call, err := types.NewCall(c.metadata, pattern.TX_STORAGE_BUYSPACE, types.NewU32(count))
if err != nil {
return txhash, errors.Wrap(err, "[NewCall]")
}

key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey)
if err != nil {
return txhash, errors.Wrap(err, "[CreateStorageKey]")
}

ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo)
if err != nil {
return txhash, errors.Wrap(err, "[GetStorageLatest]")
}
if !ok {
return txhash, pattern.ERR_RPC_EMPTY_VALUE
}

o := types.SignatureOptions{
BlockHash: c.genesisHash,
Era: types.ExtrinsicEra{IsMortalEra: false},
GenesisHash: c.genesisHash,
Nonce: types.NewUCompactFromUInt(uint64(accountInfo.Nonce)),
SpecVersion: c.runtimeVersion.SpecVersion,
Tip: types.NewUCompactFromUInt(0),
TransactionVersion: c.runtimeVersion.TransactionVersion,
}

ext := types.NewExtrinsic(call)

// Sign the transaction
err = ext.Sign(c.keyring, o)
if err != nil {
return txhash, errors.Wrap(err, "[Sign]")
}

// Do the transfer and track the actual status
sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext)
if err != nil {
c.SetChainState(false)
return txhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]")
}
defer sub.Unsubscribe()

timeout := time.NewTimer(c.timeForBlockOut)
defer timeout.Stop()

for {
select {
case status := <-sub.Chan():
if status.IsInBlock {
events := event.EventRecords{}
txhash, _ = codec.EncodeToHex(status.AsInBlock)
h, err := c.api.RPC.State.GetStorageRaw(c.keyEvents, status.AsInBlock)
if err != nil {
return txhash, errors.Wrap(err, "[GetStorageRaw]")
}
err = types.EventRecordsRaw(*h).DecodeEventRecords(c.metadata, &events)
if err != nil || len(events.StorageHandler_BuySpace) > 0 {
return txhash, nil
}
return txhash, errors.New(pattern.ERR_Failed)
}
case err = <-sub.Err():
return txhash, errors.Wrap(err, "[sub]")
case <-timeout.C:
return txhash, pattern.ERR_RPC_TIMEOUT
}
}
}

func (c *ChainSDK) ExpansionSpace(count uint32) (string, error) {
c.lock.Lock()
defer func() {
c.lock.Unlock()
if err := recover(); err != nil {
log.Println(utils.RecoverError(err))
}
}()

var (
txhash string
accountInfo types.AccountInfo
)

if count == 0 {
return "", errors.New("[ExpansionSpace] invalid count")
}

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

call, err := types.NewCall(c.metadata, pattern.TX_STORAGE_EXPANSIONSPACE, types.NewU32(count))
if err != nil {
return txhash, errors.Wrap(err, "[NewCall]")
}

key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey)
if err != nil {
return txhash, errors.Wrap(err, "[CreateStorageKey]")
}

ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo)
if err != nil {
return txhash, errors.Wrap(err, "[GetStorageLatest]")
}
if !ok {
return txhash, pattern.ERR_RPC_EMPTY_VALUE
}

o := types.SignatureOptions{
BlockHash: c.genesisHash,
Era: types.ExtrinsicEra{IsMortalEra: false},
GenesisHash: c.genesisHash,
Nonce: types.NewUCompactFromUInt(uint64(accountInfo.Nonce)),
SpecVersion: c.runtimeVersion.SpecVersion,
Tip: types.NewUCompactFromUInt(0),
TransactionVersion: c.runtimeVersion.TransactionVersion,
}

ext := types.NewExtrinsic(call)

// Sign the transaction
err = ext.Sign(c.keyring, o)
if err != nil {
return txhash, errors.Wrap(err, "[Sign]")
}

// Do the transfer and track the actual status
sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext)
if err != nil {
c.SetChainState(false)
return txhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]")
}
defer sub.Unsubscribe()

timeout := time.NewTimer(c.timeForBlockOut)
defer timeout.Stop()

for {
select {
case status := <-sub.Chan():
if status.IsInBlock {
events := event.EventRecords{}
txhash, _ = codec.EncodeToHex(status.AsInBlock)
h, err := c.api.RPC.State.GetStorageRaw(c.keyEvents, status.AsInBlock)
if err != nil {
return txhash, errors.Wrap(err, "[GetStorageRaw]")
}
err = types.EventRecordsRaw(*h).DecodeEventRecords(c.metadata, &events)
if err != nil || len(events.StorageHandler_ExpansionSpace) > 0 {
return txhash, nil
}
return txhash, errors.New(pattern.ERR_Failed)
}
case err = <-sub.Err():
return txhash, errors.Wrap(err, "[sub]")
case <-timeout.C:
return txhash, pattern.ERR_RPC_TIMEOUT
}
}
}

func (c *ChainSDK) RenewalSpace(days uint32) (string, error) {
c.lock.Lock()
defer func() {
c.lock.Unlock()
if err := recover(); err != nil {
log.Println(utils.RecoverError(err))
}
}()

var (
txhash string
accountInfo types.AccountInfo
)

if days == 0 {
return "", errors.New("[RenewalSpace] invalid days")
}

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

call, err := types.NewCall(c.metadata, pattern.TX_STORAGE_RENEWALSPACE, types.NewU32(days))
if err != nil {
return txhash, errors.Wrap(err, "[NewCall]")
}

key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey)
if err != nil {
return txhash, errors.Wrap(err, "[CreateStorageKey]")
}

ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo)
if err != nil {
return txhash, errors.Wrap(err, "[GetStorageLatest]")
}
if !ok {
return txhash, pattern.ERR_RPC_EMPTY_VALUE
}

o := types.SignatureOptions{
BlockHash: c.genesisHash,
Era: types.ExtrinsicEra{IsMortalEra: false},
GenesisHash: c.genesisHash,
Nonce: types.NewUCompactFromUInt(uint64(accountInfo.Nonce)),
SpecVersion: c.runtimeVersion.SpecVersion,
Tip: types.NewUCompactFromUInt(0),
TransactionVersion: c.runtimeVersion.TransactionVersion,
}

ext := types.NewExtrinsic(call)

// Sign the transaction
err = ext.Sign(c.keyring, o)
if err != nil {
return txhash, errors.Wrap(err, "[Sign]")
}

// Do the transfer and track the actual status
sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext)
if err != nil {
c.SetChainState(false)
return txhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]")
}
defer sub.Unsubscribe()

timeout := time.NewTimer(c.timeForBlockOut)
defer timeout.Stop()

for {
select {
case status := <-sub.Chan():
if status.IsInBlock {
events := event.EventRecords{}
txhash, _ = codec.EncodeToHex(status.AsInBlock)
h, err := c.api.RPC.State.GetStorageRaw(c.keyEvents, status.AsInBlock)
if err != nil {
return txhash, errors.Wrap(err, "[GetStorageRaw]")
}
err = types.EventRecordsRaw(*h).DecodeEventRecords(c.metadata, &events)
if err != nil || len(events.StorageHandler_RenewalSpace) > 0 {
return txhash, nil
}
return txhash, errors.New(pattern.ERR_Failed)
}
case err = <-sub.Err():
return txhash, errors.Wrap(err, "[sub]")
case <-timeout.C:
return txhash, pattern.ERR_RPC_TIMEOUT
}
}
}
7 changes: 6 additions & 1 deletion core/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const (
RESTORALORDER = "RestoralOrder"
RESTORALTARGETINFO = "RestoralTarget"

// STORAGEHANDLER
// STORAGE_HANDLER
USERSPACEINFO = "UserOwnedSpace"
UNITPRICE = "UnitPrice"

Expand Down Expand Up @@ -115,6 +115,11 @@ const (
TX_FILEBANK_CLAIMRESTOREORDER = FILEBANK + DOT + "claim_restoral_order"
TX_FILEBANK_CLAIMNOEXISTORDER = FILEBANK + DOT + "claim_restoral_noexist_order"
TX_FILEBANK_RESTORALCOMPLETE = FILEBANK + DOT + "restoral_order_complete"

// STORAGE_HANDLER
TX_STORAGE_BUYSPACE = STORAGEHANDLER + DOT + "buy_space"
TX_STORAGE_EXPANSIONSPACE = STORAGEHANDLER + DOT + "expansion_space"
TX_STORAGE_RENEWALSPACE = STORAGEHANDLER + DOT + "renewal_space"
)

// RPC Call
Expand Down
9 changes: 9 additions & 0 deletions core/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,13 @@ type SDK interface {

//
EnabledP2P() bool

//
BuySpace(count uint32) (string, error)

//
ExpansionSpace(count uint32) (string, error)

//
RenewalSpace(days uint32) (string, error)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/CESSProject/cess-go-sdk
go 1.19

require (
github.com/CESSProject/p2p-go v0.0.37
github.com/CESSProject/p2p-go v0.1.0
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 @@ -40,8 +40,8 @@ dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/CESSProject/p2p-go v0.0.37 h1:FYlTPWAKMmRW/HxzXH1FD1ueGIT8kkvGCJQK0YRlBms=
github.com/CESSProject/p2p-go v0.0.37/go.mod h1:MmuZ2UfXnEJMZMhOCC4Ec5QQUJCT7j2yg3Xk/oZ16yw=
github.com/CESSProject/p2p-go v0.1.0 h1:0rOjG8hshY+X3f4cs/HDJnYEtDtFIjceLtAHkUjz4yU=
github.com/CESSProject/p2p-go v0.1.0/go.mod h1:MmuZ2UfXnEJMZMhOCC4Ec5QQUJCT7j2yg3Xk/oZ16yw=
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 0bd49af

Please sign in to comment.