Skip to content

Commit

Permalink
Base peer (#46)
Browse files Browse the repository at this point in the history
* update p2p to v0.0.19, update pubkey to peerid

* add ReportProof, update events

* add QueryUnverifyProof
  • Loading branch information
AstaFrode committed May 15, 2023
1 parent c3e0408 commit df5fa39
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 106 deletions.
123 changes: 123 additions & 0 deletions core/chain/audit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package chain

import (
"log"
"time"

"github.com/CESSProject/sdk-go/core/utils"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
"github.com/pkg/errors"
)

func (c *chainClient) QueryUnverifyProof() ([]AllProofInfo, error) {
var list []AllProofInfo
key := createPrefixedKey(AUDIT, UNVERIFYPROOF)
keys, err := c.api.RPC.State.GetKeysLatest(key)
if err != nil {
return list, errors.Wrap(err, "[GetKeysLatest]")
}
set, err := c.api.RPC.State.QueryStorageAtLatest(keys)
if err != nil {
return list, errors.Wrap(err, "[QueryStorageAtLatest]")
}
for _, elem := range set {
for _, change := range elem.Changes {
var data AllProofInfo
if err := codec.Decode(change.StorageData, &data); err != nil {
log.Println(err)
continue
}
list = append(list, data)
}
}
return list, nil
}

func (c *chainClient) ReportProof(idlesigma, servicesigma string) (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 !c.GetChainState() {
return txhash, ERR_RPC_CONNECTION
}

call, err := types.NewCall(c.metadata, TX_AUDIT_SUBMITPROOF, types.NewBytes([]byte(idlesigma)), types.NewBytes([]byte(servicesigma)))
if err != nil {
return txhash, errors.Wrap(err, "[NewCall]")
}

key, err := types.CreateStorageKey(c.metadata, SYSTEM, 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, 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 {
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 := 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.Audit_SubmitProof) > 0 {
return txhash, nil
}
return txhash, errors.New(ERR_Failed)
}
case err = <-sub.Err():
return txhash, errors.Wrap(err, "[sub]")
case <-timeout.C:
return txhash, ERR_RPC_TIMEOUT
}
}
}
6 changes: 6 additions & 0 deletions core/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ type Chain interface {
// QueryTeeInfoList queries the information of all tee workers.
QueryTeeInfoList() ([]TeeWorkerInfo, error)

//
QueryUnverifyProof() ([]AllProofInfo, error)

// Register is used to register OSS or BUCKET roles.
Register(role string, puk []byte, income string, pledge uint64) (string, error)

Expand Down Expand Up @@ -124,6 +127,9 @@ type Chain interface {
// Withdraw is used to withdraw staking
Withdraw() (string, error)

//
ReportProof(idlesigma, servicesigma string) (string, error)

// ExtractAccountPuk extracts the public key of the account,
// and returns its own public key if the account is empty.
ExtractAccountPuk(account string) ([]byte, error)
Expand Down
2 changes: 1 addition & 1 deletion core/chain/deoss.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (c *chainClient) QueryDeoss(pubkey []byte) ([]byte, error) {
log.Panicln(utils.RecoverError(err))
}
}()
var data PeerPuk
var data PeerId

if !c.GetChainState() {
return nil, ERR_RPC_CONNECTION
Expand Down
110 changes: 31 additions & 79 deletions core/chain/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,7 @@ import "github.com/centrifuge/go-substrate-rpc-client/v4/types"
// cess event type
// ******************************************************

// ------------------------SegmentBook-------------------
type Event_PPBNoOnTimeSubmit struct {
Phase types.Phase
Acc types.AccountID
SegmentId types.U64
Topics []types.Hash
}

type Event_PPDNoOnTimeSubmit struct {
Phase types.Phase
Acc types.AccountID
SegmentId types.U64
Topics []types.Hash
}

// ------------------------Audit-------------------
type Event_ChallengeProof struct {
Phase types.Phase
Miner types.AccountID
Expand All @@ -36,16 +22,20 @@ type Event_ChallengeProof struct {
}

type Event_VerifyProof struct {
Phase types.Phase
TeeWorker types.AccountID
Miner types.AccountID
Topics []types.Hash
}

type Event_SubmitProof struct {
Phase types.Phase
Miner types.AccountID
Fileid types.Bytes
Topics []types.Hash
}

type Event_OutstandingChallenges struct {
type Event_GenerateChallenge struct {
Phase types.Phase
Miner types.AccountID
Fileid types.Bytes
Topics []types.Hash
}

Expand All @@ -57,11 +47,6 @@ type Event_Registered struct {
Topics []types.Hash
}

type Event_TimedTask struct {
Phase types.Phase
Topics []types.Hash
}

type Event_DrawFaucetMoney struct {
Phase types.Phase
Topics []types.Hash
Expand Down Expand Up @@ -110,25 +95,6 @@ type Event_Deposit struct {
Topics []types.Hash
}

type Event_Redeemed struct {
Phase types.Phase
Acc types.AccountID
Deposit types.U128
Topics []types.Hash
}

type Event_Claimed struct {
Phase types.Phase
Acc types.AccountID
Deposit types.U128
Topics []types.Hash
}

type Event_TimingStorageSpace struct {
Phase types.Phase
Topics []types.Hash
}

type Event_UpdataBeneficiary struct {
Phase types.Phase
Acc types.AccountID
Expand All @@ -144,18 +110,6 @@ type Event_UpdataIp struct {
Topics []types.Hash
}

type Event_StartOfBufferPeriod struct {
Phase types.Phase
When types.U32
Topics []types.Hash
}

type Event_EndOfBufferPeriod struct {
Phase types.Phase
When types.U32
Topics []types.Hash
}

type Event_Receive struct {
Phase types.Phase
Acc types.AccountID
Expand Down Expand Up @@ -397,31 +351,25 @@ type Event_Balances_Withdraw struct {
// Events
type EventRecords struct {
// AUDIT
SegmentBook_PPBNoOnTimeSubmit []Event_PPBNoOnTimeSubmit
SegmentBook_PPDNoOnTimeSubmit []Event_PPDNoOnTimeSubmit
SegmentBook_ChallengeProof []Event_ChallengeProof
SegmentBook_VerifyProof []Event_VerifyProof
SegmentBook_OutstandingChallenges []Event_OutstandingChallenges
Audit_VerifyProof []Event_VerifyProof
Audit_SubmitProof []Event_SubmitProof
Audit_GenerateChallenge []Event_GenerateChallenge

// SMINER
Sminer_Registered []Event_Registered
Sminer_TimedTask []Event_TimedTask
Sminer_DrawFaucetMoney []Event_DrawFaucetMoney
Sminer_FaucetTopUpMoney []Event_FaucetTopUpMoney
Sminer_LessThan24Hours []Event_LessThan24Hours
Sminer_AlreadyFrozen []Event_AlreadyFrozen
Sminer_MinerExit []Event_MinerExit
Sminer_MinerClaim []Event_MinerClaim
Sminer_IncreaseCollateral []Event_IncreaseCollateral
Sminer_Deposit []Event_Deposit
Sminer_Redeemed []Event_Redeemed
Sminer_Claimed []Event_Claimed
Sminer_TimingStorageSpace []Event_TimingStorageSpace
Sminer_UpdataBeneficiary []Event_UpdataBeneficiary
Sminer_UpdataIp []Event_UpdataIp
Sminer_StartOfBufferPeriod []Event_StartOfBufferPeriod
Sminer_EndOfBufferPeriod []Event_EndOfBufferPeriod
Sminer_Receive []Event_Receive
Sminer_Withdraw []Event_Withdraw
Sminer_Registered []Event_Registered
Sminer_DrawFaucetMoney []Event_DrawFaucetMoney
Sminer_FaucetTopUpMoney []Event_FaucetTopUpMoney
Sminer_LessThan24Hours []Event_LessThan24Hours
Sminer_AlreadyFrozen []Event_AlreadyFrozen
Sminer_MinerExit []Event_MinerExit
Sminer_MinerClaim []Event_MinerClaim
Sminer_IncreaseCollateral []Event_IncreaseCollateral
Sminer_Deposit []Event_Deposit
Sminer_UpdataBeneficiary []Event_UpdataBeneficiary
Sminer_UpdataIp []Event_UpdataIp
Sminer_Receive []Event_Receive
Sminer_Withdraw []Event_Withdraw

// FILEBANK
FileBank_DeleteFile []Event_DeleteFile
FileBank_FileUpload []Event_FileUpload
Expand All @@ -440,19 +388,23 @@ type EventRecords struct {
FileBank_TransferReport []Event_TransferReport
FileBank_ReplaceFiller []Event_ReplaceFiller
FileBank_CalculateEnd []Event_CalculateEnd

// StorageHandler
StorageHandler_BuySpace []Event_BuySpace
StorageHandler_ExpansionSpace []Event_ExpansionSpace
StorageHandler_RenewalSpace []Event_RenewalSpace
StorageHandler_LeaseExpired []Event_LeaseExpired
StorageHandler_LeaseExpireIn24Hours []Event_LeaseExpireIn24Hours

// TeeWorker
TeeWorker_RegistrationScheduler []Event_RegistrationScheduler
TeeWorker_UpdateScheduler []Event_UpdateScheduler

// OSS
Oss_OssRegister []Event_OssRegister
Oss_OssUpdate []Event_OssUpdate
Oss_OssDestroy []Event_OssDestroy

// System
types.EventRecords
}
Loading

0 comments on commit df5fa39

Please sign in to comment.