From e7915227b9e2ba2abea36fd3759f54fca0f40de1 Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Tue, 7 May 2024 10:51:04 +0800 Subject: [PATCH 01/18] update delete file and tee extrinsic --- chain/extrinsic_name.go | 57 ++++++++++++++++++++++++++--------------- chain/fileBank.go | 45 ++++++++++++++++---------------- config/config.go | 5 +--- core/sdk/sdk.go | 7 ++--- example/new/sdk.go | 2 +- sdk.go | 5 ---- 6 files changed, 64 insertions(+), 57 deletions(-) diff --git a/chain/extrinsic_name.go b/chain/extrinsic_name.go index 01df6e1..3cf03db 100644 --- a/chain/extrinsic_name.go +++ b/chain/extrinsic_name.go @@ -321,16 +321,21 @@ const ( ExtName_TechnicalMembership_swap_member = "TechnicalMembership.swap_member" // TeeWorker - ExtName_TeeWorker_add_ceseal = "TeeWorker.add_ceseal" - ExtName_TeeWorker_force_register_worker = "TeeWorker.force_register_worker" - ExtName_TeeWorker_register_keyfairy = "TeeWorker.register_keyfairy" - ExtName_TeeWorker_register_worker = "TeeWorker.register_worker" - ExtName_TeeWorker_register_worker_v2 = "TeeWorker.register_worker_v2" - ExtName_TeeWorker_remove_ceseal = "TeeWorker.remove_ceseal" - ExtName_TeeWorker_rotate_master_key = "TeeWorker.rotate_master_key" + ExtName_TeeWorker_add_ceseal = "TeeWorker.add_ceseal" + ExtName_TeeWorker_apply_master_key = "TeeWorker.apply_master_key" + ExtName_TeeWorker_force_register_worker = "TeeWorker.force_register_worker" + //ExtName_TeeWorker_register_keyfairy = "TeeWorker.register_keyfairy" + ExtName_TeeWorker_launch_master_key = "TeeWorker.launch_master_key" + ExtName_TeeWorker_migration_last_work = "TeeWorker.migration_last_work" + ExtName_TeeWorker_patch_clear_invalid_tee = "TeeWorker.patch_clear_invalid_tee" + ExtName_TeeWorker_patch_clear_not_work_tee = "TeeWorker.patch_clear_not_work_tee" + ExtName_TeeWorker_register_worker = "TeeWorker.register_worker" + ExtName_TeeWorker_register_worker_v2 = "TeeWorker.register_worker_v2" + ExtName_TeeWorker_remove_ceseal = "TeeWorker.remove_ceseal" + //ExtName_TeeWorker_rotate_master_key = "TeeWorker.rotate_master_key" ExtName_TeeWorker_set_minimum_ceseal_version = "TeeWorker.set_minimum_ceseal_version" - ExtName_TeeWorker_unregister_keyfairy = "TeeWorker.unregister_keyfairy" - ExtName_TeeWorker_update_worker_endpoint = "TeeWorker.update_worker_endpoint" + //ExtName_TeeWorker_unregister_keyfairy = "TeeWorker.unregister_keyfairy" + ExtName_TeeWorker_update_worker_endpoint = "TeeWorker.update_worker_endpoint" // Timestamp ExtName_Timestamp_set = "Timestamp.set" @@ -1627,13 +1632,33 @@ func (c *ChainClient) InitExtrinsicsName() error { } else { return err } + if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_TeeWorker_apply_master_key); err == nil { + ExtrinsicsName[callIndex] = ExtName_TeeWorker_apply_master_key + } else { + return err + } if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_TeeWorker_force_register_worker); err == nil { ExtrinsicsName[callIndex] = ExtName_TeeWorker_force_register_worker } else { return err } - if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_TeeWorker_register_keyfairy); err == nil { - ExtrinsicsName[callIndex] = ExtName_TeeWorker_register_keyfairy + if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_TeeWorker_launch_master_key); err == nil { + ExtrinsicsName[callIndex] = ExtName_TeeWorker_launch_master_key + } else { + return err + } + if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_TeeWorker_migration_last_work); err == nil { + ExtrinsicsName[callIndex] = ExtName_TeeWorker_migration_last_work + } else { + return err + } + if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_TeeWorker_patch_clear_invalid_tee); err == nil { + ExtrinsicsName[callIndex] = ExtName_TeeWorker_patch_clear_invalid_tee + } else { + return err + } + if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_TeeWorker_patch_clear_not_work_tee); err == nil { + ExtrinsicsName[callIndex] = ExtName_TeeWorker_patch_clear_not_work_tee } else { return err } @@ -1652,21 +1677,11 @@ func (c *ChainClient) InitExtrinsicsName() error { } else { return err } - if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_TeeWorker_rotate_master_key); err == nil { - ExtrinsicsName[callIndex] = ExtName_TeeWorker_rotate_master_key - } else { - return err - } if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_TeeWorker_set_minimum_ceseal_version); err == nil { ExtrinsicsName[callIndex] = ExtName_TeeWorker_set_minimum_ceseal_version } else { return err } - if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_TeeWorker_unregister_keyfairy); err == nil { - ExtrinsicsName[callIndex] = ExtName_TeeWorker_unregister_keyfairy - } else { - return err - } if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_TeeWorker_update_worker_endpoint); err == nil { ExtrinsicsName[callIndex] = ExtName_TeeWorker_update_worker_endpoint } else { diff --git a/chain/fileBank.go b/chain/fileBank.go index fd1e1ab..c67cc10 100755 --- a/chain/fileBank.go +++ b/chain/fileBank.go @@ -727,7 +727,7 @@ func (c *ChainClient) DeleteBucket(owner_pkey []byte, name string) (string, erro } } -func (c *ChainClient) DeleteFile(puk []byte, filehash []string) (string, []pattern.FileHash, error) { +func (c *ChainClient) DeleteFile(puk []byte, filehash string) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -739,32 +739,31 @@ func (c *ChainClient) DeleteFile(puk []byte, filehash []string) (string, []patte var ( txhash string accountInfo types.AccountInfo - hashs = make([]pattern.FileHash, len(filehash)) ) if !c.GetChainState() { - return txhash, hashs, pattern.ERR_RPC_CONNECTION + return txhash, pattern.ERR_RPC_CONNECTION } - for j := 0; j < len(filehash); j++ { - if len(filehash[j]) != len(hashs[j]) { - return txhash, hashs, errors.New("invalid filehash") - } - for i := 0; i < len(hashs[j]); i++ { - hashs[j][i] = types.U8(filehash[j][i]) - } + if len(filehash) != pattern.FileHashLen { + return "", errors.New("invalid fid") } acc, err := types.NewAccountID(puk) if err != nil { - return txhash, hashs, errors.Wrap(err, "[NewAccountID]") + return txhash, errors.Wrap(err, "[NewAccountID]") } - call, err := types.NewCall(c.metadata, pattern.TX_FILEBANK_DELFILE, *acc, hashs) + var fhash pattern.FileHash + for i := 0; i < len(filehash); i++ { + fhash[i] = types.U8(filehash[i]) + } + + call, err := types.NewCall(c.metadata, pattern.TX_FILEBANK_DELFILE, *acc, fhash) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_DELFILE, err) c.SetChainState(false) - return txhash, hashs, err + return txhash, err } ext := types.NewExtrinsic(call) @@ -773,17 +772,17 @@ func (c *ChainClient) DeleteFile(puk []byte, filehash []string) (string, []patte if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_DELFILE, err) c.SetChainState(false) - return txhash, hashs, err + return txhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_DELFILE, err) c.SetChainState(false) - return txhash, hashs, err + return txhash, err } if !ok { - return txhash, hashs, pattern.ERR_RPC_EMPTY_VALUE + return txhash, pattern.ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -801,7 +800,7 @@ func (c *ChainClient) DeleteFile(puk []byte, filehash []string) (string, []patte if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_DELFILE, err) c.SetChainState(false) - return txhash, hashs, err + return txhash, err } <-c.txTicker.C @@ -813,18 +812,18 @@ func (c *ChainClient) DeleteFile(puk []byte, filehash []string) (string, []patte o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, hashs, errors.Wrap(err, "[Sign]") + return txhash, errors.Wrap(err, "[Sign]") } sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_DELFILE, err) c.SetChainState(false) - return txhash, hashs, err + return txhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_DELFILE, err) c.SetChainState(false) - return txhash, hashs, err + return txhash, err } } @@ -839,12 +838,12 @@ func (c *ChainClient) DeleteFile(puk []byte, filehash []string) (string, []patte if status.IsInBlock { txhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_FileBank_DeleteFile(status.AsInBlock) - return txhash, nil, err + return txhash, err } case err = <-sub.Err(): - return txhash, hashs, errors.Wrap(err, "[sub]") + return txhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, hashs, pattern.ERR_RPC_TIMEOUT + return txhash, pattern.ERR_RPC_TIMEOUT } } } diff --git a/config/config.go b/config/config.go index a51525a..949db90 100755 --- a/config/config.go +++ b/config/config.go @@ -27,10 +27,7 @@ type Option func(cfg *Config) error // default service name const ( - CharacterName_Default = "sdk" - CharacterName_Client = "client" - CharacterName_Bucket = "bucket" - CharacterName_Deoss = "deoss" + CharacterName_Default = "cess-sdk-go" ) // NewSDK constructs a new client from the Config. diff --git a/core/sdk/sdk.go b/core/sdk/sdk.go index 83effa8..657f19f 100755 --- a/core/sdk/sdk.go +++ b/core/sdk/sdk.go @@ -24,7 +24,8 @@ type SDK interface { // QueryChallengeVerifyExpiration Query Challenge Expiration Block High QueryChallengeVerifyExpiration() (uint32, error) // QueryChallengeInfo queries accountID's challenge information - // Tip: accountID can only be a storage node account + // - accountID: signature account of the storage node + // - block: block number, less than 0 indicates the latest block QueryChallengeInfo(accountID []byte, block int32) (bool, pattern.ChallengeInfo, error) // Audit-Extrinsics @@ -68,13 +69,13 @@ type SDK interface { ClaimRestoralOrder(fragmentHash string) (string, error) // CreateBucket creates a bucket for accountID // For details on bucket naming rules, see: - // https://app.gitbook.com/o/kiTNX10jBU59sjnYZbiH/s/G1ekWsjn9OlGH381wiK2/get-started/deoss-gateway/step-1-create-a-bucket#naming-conventions-for-a-bucket + // https://docs.cess.cloud/deoss/get-started/deoss-gateway/step-1-create-a-bucket#naming-conventions-for-a-bucket CreateBucket(accountID []byte, bucketName string) (string, error) // DeleteBucket deletes buckets for accountID // Tip: Only empty buckets can be deleted DeleteBucket(accountID []byte, bucketName string) (string, error) // DeleteFile deletes files for accountID - DeleteFile(accountID []byte, fid []string) (string, []pattern.FileHash, error) + DeleteFile(puk []byte, filehash string) (string, error) // GenerateRestoralOrder generates data for restoration orders. GenerateRestoralOrder(fid, fragmentHash string) (string, error) // RestoralComplete reports order recovery completion. diff --git a/example/new/sdk.go b/example/new/sdk.go index 487e30e..1e97fec 100755 --- a/example/new/sdk.go +++ b/example/new/sdk.go @@ -50,7 +50,7 @@ func main() { fmt.Println(sdk.InitExtrinsicsName()) fmt.Println(sdk.GetCurrentRpcAddr()) // - fmt.Println(sdk.QueryRoundReward(0, -1)) + //fmt.Println(sdk.QueryRoundReward(0, -1)) return blockhash, err := sdk.GetSubstrateAPI().RPC.Chain.GetBlockHash(180) diff --git a/sdk.go b/sdk.go index 0469a16..b08c21b 100755 --- a/sdk.go +++ b/sdk.go @@ -30,11 +30,6 @@ type Option = config.Option // timeout: time.Duration(time.Second * 6) // // - The serviceName is used to specify the name of your service -// Warning: -// -// 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, opts ...Option) (*chain.ChainClient, error) { return NewWithoutDefaults(ctx, append(opts, FallbackDefaults)...) } From 9352de1eeefcf113a00fcdc4ea7e5ff5b95bff4b Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Tue, 7 May 2024 17:42:26 +0800 Subject: [PATCH 02/18] update Audit --- chain/audit.go | 384 ++++++++----- chain/chain.go | 3 +- chain/pattern.go | 611 ++++++++++++++++++++ core/pattern/pattern.go | 1212 +++++++++++++++++++-------------------- 4 files changed, 1470 insertions(+), 740 deletions(-) create mode 100644 chain/pattern.go diff --git a/chain/audit.go b/chain/audit.go index 3454ca1..b2a42a0 100755 --- a/chain/audit.go +++ b/chain/audit.go @@ -13,86 +13,174 @@ import ( "strings" "time" - "github.com/CESSProject/cess-go-sdk/core/pattern" "github.com/CESSProject/cess-go-sdk/utils" "github.com/centrifuge/go-substrate-rpc-client/v4/types" "github.com/pkg/errors" ) -func (c *ChainClient) QueryChallengeVerifyExpiration() (uint32, error) { +// QueryChallengeSnapShot query challenge snapshot data +// - accountID: signature account of the storage miner +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - bool: is there a value +// - ChallengeInfo: challenge snapshot data +// - error: error message +func (c *ChainClient) QueryChallengeSnapShot(accountID []byte, block int32) (bool, ChallengeInfo, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data types.U32 + + var data ChallengeInfo if !c.GetChainState() { - return 0, pattern.ERR_RPC_CONNECTION + return false, data, ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.AUDIT, pattern.CHALLENGEVERIFYDURATION) + key, err := types.CreateStorageKey(c.metadata, Audit, ChallengeSnapShot, accountID) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.AUDIT, pattern.CHALLENGEVERIFYDURATION, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Audit, ChallengeSnapShot, err) + return false, 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(), Audit, ChallengeSnapShot, err) + c.SetChainState(false) + return false, data, err + } + return ok, data, nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + return false, data, err + } + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Audit, ChallengeSnapShot, err) c.SetChainState(false) - return 0, err + return false, data, err + } + return ok, data, nil +} + +// QueryCounterdClear query the number of times to clear the challenge failure count +// - accountID: signature account of the storage miner +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - uint8: cleanup count +// - error: error message +func (c *ChainClient) QueryCountedClear(accountID []byte, block int32) (uint8, error) { + defer func() { + if err := recover(); err != nil { + log.Println(utils.RecoverError(err)) + } + }() + + var data types.U8 + + if !c.GetChainState() { + return uint8(data), ERR_RPC_CONNECTION } - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + key, err := types.CreateStorageKey(c.metadata, Audit, CountedClear, accountID) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.AUDIT, pattern.CHALLENGEVERIFYDURATION, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Audit, CountedClear, err) + return uint8(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(), Audit, CountedClear, err) + c.SetChainState(false) + return uint8(data), err + } + if !ok { + return uint8(data), ERR_RPC_EMPTY_VALUE + } + return uint8(data), nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + return uint8(data), err + } + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Audit, CountedClear, err) c.SetChainState(false) - return 0, err + return uint8(data), err } if !ok { - return 0, pattern.ERR_RPC_EMPTY_VALUE + return uint8(data), ERR_RPC_EMPTY_VALUE } - return uint32(data), nil + return uint8(data), nil } -func (c *ChainClient) QueryChallengeInfo(accountID []byte, block int32) (bool, pattern.ChallengeInfo, error) { +// QueryCountedServiceFailed query the number of failed service data challenge +// - accountID: signature account of the storage miner +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - uint32: Is there a value +// - error: error message +func (c *ChainClient) QueryCountedServiceFailed(accountID []byte, block int32) (uint32, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - - var data pattern.ChallengeInfo + var data types.U32 if !c.GetChainState() { - return false, data, pattern.ERR_RPC_CONNECTION + return uint32(data), ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.AUDIT, pattern.CHALLENGESNAPSHOT, accountID) + key, err := types.CreateStorageKey(c.metadata, Audit, CountedServiceFailed, accountID) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.AUDIT, pattern.CHALLENGESNAPSHOT, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Audit, CountedServiceFailed, err) c.SetChainState(false) - return false, data, err + return uint32(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(), pattern.AUDIT, pattern.CHALLENGESNAPSHOT, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Audit, CountedServiceFailed, err) c.SetChainState(false) - return false, data, err + return 0, err } - return ok, data, nil + if !ok { + return 0, ERR_RPC_EMPTY_VALUE + } + return uint32(data), nil } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - return false, data, err + return uint32(data), err } - ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.AUDIT, pattern.CHALLENGESNAPSHOT, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), Audit, CountedServiceFailed, err) c.SetChainState(false) - return false, data, err + return uint32(data), err } - return ok, data, nil + if !ok { + return uint32(data), ERR_RPC_EMPTY_VALUE + } + return uint32(data), nil } +// SubmitIdleProof submit idle data proof to the chain +// - idleProof: idle data proof +// +// Return: +// - string: block hash +// - error: error message func (c *ChainClient) SubmitIdleProof(idleProof []types.U8) (string, error) { c.lock.Lock() defer func() { @@ -103,38 +191,40 @@ func (c *ChainClient) SubmitIdleProof(idleProof []types.U8) (string, error) { }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) + if len(idleProof) == 0 { + return blockhash, ERR_IdleProofIsEmpty + } + if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - call, err := types.NewCall(c.metadata, pattern.TX_AUDIT_SUBMITIDLEPROOF, idleProof) + call, err := types.NewCall(c.metadata, TX_Audit_SubmitIdleProof, idleProof) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITIDLEPROOF, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitIdleProof, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITIDLEPROOF, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitIdleProof, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITIDLEPROOF, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitIdleProof, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -150,30 +240,33 @@ func (c *ChainClient) SubmitIdleProof(idleProof []types.U8) (string, error) { // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITIDLEPROOF, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitIdleProof, err) c.SetChainState(false) - return txhash, err + return blockhash, err } + <-c.txTicker.C + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITIDLEPROOF, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitIdleProof, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITIDLEPROOF, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitIdleProof, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -185,18 +278,24 @@ func (c *ChainClient) SubmitIdleProof(idleProof []types.U8) (string, error) { select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_Audit_SubmitIdleProof(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } +// SubmitServiceProof submit service data proof to the chain +// - serviceProof: service data proof +// +// Return: +// - string: block hash +// - error: error message func (c *ChainClient) SubmitServiceProof(serviceProof []types.U8) (string, error) { c.lock.Lock() defer func() { @@ -207,38 +306,36 @@ func (c *ChainClient) SubmitServiceProof(serviceProof []types.U8) (string, error }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - call, err := types.NewCall(c.metadata, pattern.TX_AUDIT_SUBMITSERVICEPROOF, serviceProof) + call, err := types.NewCall(c.metadata, TX_Audit_SubmitServiceProof, serviceProof) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITSERVICEPROOF, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitServiceProof, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITSERVICEPROOF, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitServiceProof, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITSERVICEPROOF, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitServiceProof, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -254,30 +351,32 @@ func (c *ChainClient) SubmitServiceProof(serviceProof []types.U8) (string, error // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITSERVICEPROOF, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitServiceProof, err) + return blockhash, err } + <-c.txTicker.C + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITSERVICEPROOF, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitServiceProof, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITSERVICEPROOF, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitServiceProof, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -289,19 +388,31 @@ func (c *ChainClient) SubmitServiceProof(serviceProof []types.U8) (string, error select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_Audit_SubmitServiceProof(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) SubmitIdleProofResult(totalProofHash []types.U8, front, rear types.U64, accumulator pattern.Accumulator, result types.Bool, sig types.Bytes, teePuk pattern.WorkerPublicKey) (string, error) { +// SubmitVerifyIdleResult submit validation result of idle data proof to the chain +// - totalProofHash: total idle data proof hash value +// - front: idle data pre-offset +// - rear: back offset of idle data +// - accumulator: accumulator value +// - result: validation result of idle data proof +// - sig: signature from tee +// - teePuk: tee's work public key +// +// Return: +// - string: block hash +// - error: error message +func (c *ChainClient) SubmitVerifyIdleResult(totalProofHash []types.U8, front, rear types.U64, accumulator Accumulator, result types.Bool, sig types.Bytes, teePuk WorkerPublicKey) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -311,38 +422,36 @@ func (c *ChainClient) SubmitIdleProofResult(totalProofHash []types.U8, front, re }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - call, err := types.NewCall(c.metadata, pattern.TX_AUDIT_SUBMITIDLEPROOFRESULT, totalProofHash, front, rear, accumulator, result, sig, teePuk) + call, err := types.NewCall(c.metadata, TX_Audit_SubmitVerifyIdleResult, totalProofHash, front, rear, accumulator, result, sig, teePuk) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITIDLEPROOFRESULT, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyIdleResult, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITIDLEPROOFRESULT, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyIdleResult, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITIDLEPROOFRESULT, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyIdleResult, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -358,30 +467,32 @@ func (c *ChainClient) SubmitIdleProofResult(totalProofHash []types.U8, front, re // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITIDLEPROOFRESULT, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyIdleResult, err) + return blockhash, err } + <-c.txTicker.C + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITIDLEPROOFRESULT, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyIdleResult, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITIDLEPROOFRESULT, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyIdleResult, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -393,19 +504,28 @@ func (c *ChainClient) SubmitIdleProofResult(totalProofHash []types.U8, front, re select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_Audit_SubmitIdleVerifyResult(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) SubmitServiceProofResult(result types.Bool, sign types.Bytes, bloomFilter pattern.BloomFilter, teePuk pattern.WorkerPublicKey) (string, error) { +// SubmitVerifyServiceResult submit validation result of service data proof to the chain +// - result: validation result of idle data proof +// - sig: signature from tee +// - bloomFilter: bloom filter value +// - teePuk: tee's work public key +// +// Return: +// - string: block hash +// - error: error message +func (c *ChainClient) SubmitVerifyServiceResult(result types.Bool, sign types.Bytes, bloomFilter BloomFilter, teePuk WorkerPublicKey) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -415,38 +535,36 @@ func (c *ChainClient) SubmitServiceProofResult(result types.Bool, sign types.Byt }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - call, err := types.NewCall(c.metadata, pattern.TX_AUDIT_SUBMITSERVICEPROOFRESULT, result, sign, bloomFilter, teePuk) + call, err := types.NewCall(c.metadata, TX_Audit_SubmitVerifyServiceResult, result, sign, bloomFilter, teePuk) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITSERVICEPROOFRESULT, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyServiceResult, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITSERVICEPROOFRESULT, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyServiceResult, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITSERVICEPROOFRESULT, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyServiceResult, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -462,30 +580,32 @@ func (c *ChainClient) SubmitServiceProofResult(result types.Bool, sign types.Byt // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITSERVICEPROOFRESULT, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyServiceResult, err) + return blockhash, err } + <-c.txTicker.C + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITSERVICEPROOFRESULT, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyServiceResult, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_AUDIT_SUBMITSERVICEPROOFRESULT, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyServiceResult, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -497,14 +617,14 @@ func (c *ChainClient) SubmitServiceProofResult(result types.Bool, sign types.Byt select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_Audit_SubmitServiceVerifyResult(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } diff --git a/chain/chain.go b/chain/chain.go index ba11e27..7e2b1b4 100755 --- a/chain/chain.go +++ b/chain/chain.go @@ -20,7 +20,6 @@ import ( "time" "github.com/CESSProject/cess-go-sdk/core/pattern" - "github.com/CESSProject/cess-go-sdk/core/sdk" "github.com/CESSProject/cess-go-sdk/utils" gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4" "github.com/centrifuge/go-substrate-rpc-client/v4/registry/retriever" @@ -55,7 +54,7 @@ type ChainClient struct { chainState bool } -var _ sdk.SDK = (*ChainClient)(nil) +// var _ sdk.SDK = (*ChainClient)(nil) var globalTransport = &http.Transport{ DisableKeepAlives: true, diff --git a/chain/pattern.go b/chain/pattern.go new file mode 100644 index 0000000..0970495 --- /dev/null +++ b/chain/pattern.go @@ -0,0 +1,611 @@ +/* + Copyright (C) CESS. All rights reserved. + Copyright (C) Cumulus Encrypted Storage System. All rights reserved. + + SPDX-License-Identifier: Apache-2.0 +*/ + +package chain + +import ( + "time" + + "github.com/centrifuge/go-substrate-rpc-client/v4/types" + "github.com/pkg/errors" +) + +// DOT is "." character +const DOT = "." + +// Unit precision of CESS token +const TokenPrecision_CESS = "000000000000000000" + +const StakingStakePerTiB = 4000 + +const BlockIntervalSec = 6 + +// BlockInterval is the time interval for generating blocks, in seconds +const BlockInterval = time.Second * time.Duration(BlockIntervalSec) + +const MaxSubmitedIdleFileMeta = 30 + +const PublicDeoss = "http://deoss-pub-gateway.cess.cloud/" +const PublicDeossAccount = "cXhwBytXqrZLr1qM5NHJhCzEMckSTzNKw17ci2aHft6ETSQm9" + +// Pallets +const ( + // + Audit = "Audit" + // OSS is a module about DeOSS + OSS = "Oss" + // FILEBANK is a module about data metadata, bucket info, etc. + FILEBANK = "FileBank" + // TEEWOEKER is a module about TEE + TEEWORKER = "TeeWorker" + // SMINER is a module about storage miners + SMINER = "Sminer" + // STAKING is a module about staking + STAKING = "Staking" + // SMINER is a module about storage miners + STORAGEHANDLER = "StorageHandler" + // BALANCES is a module about the balances + BALANCES = "Balances" + // SYSTEM is a module about the system + System = "System" + // EVM is a module about the evm contract + EVM = "EVM" + // + CessTreasury = "CessTreasury" +) + +// Chain state +const ( + //AUDIT + ChallengeSlip = "ChallengeSlip" + ChallengeSnapShot = "ChallengeSnapShot" + CountedClear = "CountedClear" + CountedServiceFailed = "CountedServiceFailed" + VerifySlip = "VerifySlip" + + // OSS + // OSS + AUTHORITYLIST = "AuthorityList" + + // SMINER + ALLMINER = "AllMiner" + MINERITEMS = "MinerItems" + REWARDMAP = "RewardMap" + EXPENDERS = "Expenders" + RESTORALTARGETINFO = "RestoralTarget" + STAKINGSTARTBLOCK = "StakingStartBlock" + CompleteSnapShot = "CompleteSnapShot" + + // TEEWORKER + TEEWorkers = "Workers" + TEEMasterPubkey = "MasterPubkey" + TEEEndpoints = "Endpoints" + TEEWorkerAddedAt = "WorkerAddedAt" + + // FILEBANK + FILE = "File" + BUCKET = "Bucket" + BUCKETLIST = "UserBucketList" + DEALMAP = "DealMap" + FILLERMAP = "FillerMap" + PENDINGREPLACE = "PendingReplacements" + RESTORALORDER = "RestoralOrder" + + // STAKING + COUNTERFORVALIDATORS = "CounterForValidators" + CounterForNominators = "CounterForNominators" + ErasTotalStake = "ErasTotalStake" + CurrentEra = "CurrentEra" + ErasStakers = "ErasStakers" + ErasRewardPoints = "ErasRewardPoints" + Nominators = "Nominators" + Bonded = "Bonded" + Validators = "Validators" + ErasValidatorReward = "ErasValidatorReward" + ValidatorCount = "ValidatorCount" + + // STORAGE_HANDLER + USERSPACEINFO = "UserOwnedSpace" + UNITPRICE = "UnitPrice" + TOTALIDLESPACE = "TotalIdleSpace" + TOTALSERVICESPACE = "TotalServiceSpace" + PURCHASEDSPACE = "PurchasedSpace" + + // BALANCES + TOTALISSUANCE = "TotalIssuance" + + // SYSTEM + Account = "Account" + EVENTS = "Events" + + // CessTreasury + RoundReward = "RoundReward" +) + +// Extrinsics +const ( + //AUDIT + TX_Audit_SubmitIdleProof = Audit + DOT + "submit_idle_proof" + TX_Audit_SubmitServiceProof = Audit + DOT + "submit_service_proof" + TX_Audit_SubmitVerifyIdleResult = Audit + DOT + "submit_verify_idle_result" + TX_Audit_SubmitVerifyServiceResult = Audit + DOT + "submit_verify_service_result" + + // OSS + TX_OSS_REGISTER = OSS + DOT + "register" + TX_OSS_UPDATE = OSS + DOT + "update" + TX_OSS_DESTROY = OSS + DOT + "destroy" + TX_OSS_AUTHORIZE = OSS + DOT + "authorize" + TX_OSS_UNAUTHORIZE = OSS + DOT + "cancel_authorize" + + // SMINER + TX_SMINER_REGISTER = SMINER + DOT + "regnstk" + TX_SMINER_REGISTERASSIGNSTAKING = SMINER + DOT + "regnstk_assign_staking" + TX_SMINER_INCREASESTAKES = SMINER + DOT + "increase_collateral" + TX_SMINER_UPDATEPEERID = SMINER + DOT + "update_peer_id" + TX_SMINER_UPDATEINCOME = SMINER + DOT + "update_beneficiary" + TX_SMINER_CLAIMREWARD = SMINER + DOT + "receive_reward" + TX_SMINER_MINEREXITPREP = SMINER + DOT + "miner_exit_prep" + TX_SMINER_WITHDRAW = SMINER + DOT + "miner_withdraw" + TX_SMINER_REGISTERPOISKEY = SMINER + DOT + "register_pois_key" + TX_SMINER_INCREASEDECSPACE = SMINER + DOT + "increase_declaration_space" + + // FILEBANK + TX_FILEBANK_PUTBUCKET = FILEBANK + DOT + "create_bucket" + TX_FILEBANK_DELBUCKET = FILEBANK + DOT + "delete_bucket" + TX_FILEBANK_DELFILE = FILEBANK + DOT + "delete_file" + TX_FILEBANK_UPLOADDEC = FILEBANK + DOT + "upload_declaration" + TX_FILEBANK_FILEREPORT = FILEBANK + DOT + "transfer_report" + TX_FILEBANK_GENRESTOREORDER = FILEBANK + DOT + "generate_restoral_order" + 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" + TX_FILEBANK_CERTIDLESPACE = FILEBANK + DOT + "cert_idle_space" + TX_FILEBANK_REPLACEIDLESPACE = FILEBANK + DOT + "replace_idle_space" + TX_FILEBANK_CALCULATEREPORT = FILEBANK + DOT + "calculate_report" + + // STORAGE_HANDLER + TX_STORAGE_BUYSPACE = STORAGEHANDLER + DOT + "buy_space" + TX_STORAGE_EXPANSIONSPACE = STORAGEHANDLER + DOT + "expansion_space" + TX_STORAGE_RENEWALSPACE = STORAGEHANDLER + DOT + "renewal_space" + + // BALANCES + TX_BALANCES_FORCETRANSFER = "Balances" + DOT + "transfer" + + // EVM + TX_EVM_CALL = EVM + DOT + "call" +) + +// RPC Call +const ( + // System + RPC_SYS_Properties = "system_properties" + RPC_SYS_SyncState = "system_syncState" + RPC_SYS_Version = "system_version" + RPC_SYS_Chain = "system_chain" + + //Net + RPC_NET_Listening = "net_listening" +) + +const ( + Active = iota + Calculate + Missing + Recovery +) + +const ( + MINER_STATE_POSITIVE = "positive" + MINER_STATE_FROZEN = "frozen" + MINER_STATE_EXIT = "exit" + MINER_STATE_LOCK = "lock" + MINER_STATE_OFFLINE = "offline" +) + +// 0:Full 1:Verifier 2:Marker +const ( + TeeType_Full uint8 = 0 + TeeType_Verifier uint8 = 1 + TeeType_Marker uint8 = 2 +) + +const ( + ERR_Failed = "failed" + ERR_Timeout = "timeout" + ERR_Empty = "empty" +) + +const ( + MinBucketNameLength = 3 + MaxBucketNameLength = 63 + MaxDomainNameLength = 50 +) + +// byte size +const ( + SIZE_1KiB = 1024 + SIZE_1MiB = 1024 * SIZE_1KiB + SIZE_1GiB = 1024 * SIZE_1MiB + SIZE_1TiB = 1024 * SIZE_1GiB +) + +const ( + SegmentSize = 32 * SIZE_1MiB + FragmentSize = 8 * SIZE_1MiB + DataShards = 4 + ParShards = 8 +) + +var ( + ERR_RPC_CONNECTION = errors.New("rpc err: connection failed") + ERR_RPC_IP_FORMAT = errors.New("unsupported ip format") + ERR_RPC_TIMEOUT = errors.New("timeout") + ERR_RPC_EMPTY_VALUE = errors.New("empty") + ERR_RPC_PRIORITYTOOLOW = "Priority is too low" + + ERR_IdleProofIsEmpty = errors.New("idle data proof is empty") +) + +const ( + FileHashLen = 64 + RandomLen = 20 + PeerIdPublicKeyLen = 38 + PoISKeyLen = 256 + TeeSignatureLen = 256 + AccumulatorLen = 256 + SpaceChallengeParamLen = 8 + BloomFilterLen = 256 + MaxSegmentNum = 1000 + WorkerPublicKeyLen = 32 + MasterPublicKeyLen = 32 + EcdhPublicKeyLen = 32 + TeeSigLen = 64 +) + +type FileHash [FileHashLen]types.U8 +type Random [RandomLen]types.U8 +type PeerId [PeerIdPublicKeyLen]types.U8 +type PoISKey_G [PoISKeyLen]types.U8 +type PoISKey_N [PoISKeyLen]types.U8 +type TeeSignature [TeeSignatureLen]types.U8 +type Accumulator [AccumulatorLen]types.U8 +type SpaceChallengeParam [SpaceChallengeParamLen]types.U64 +type BloomFilter [BloomFilterLen]types.U64 +type WorkerPublicKey [WorkerPublicKeyLen]types.U8 +type MasterPublicKey [MasterPublicKeyLen]types.U8 +type EcdhPublicKey [EcdhPublicKeyLen]types.U8 +type TeeSig [TeeSigLen]types.U8 + +// Audit +type ChallengeInfo struct { + MinerSnapshot MinerSnapShot + ChallengeElement ChallengeElement + ProveInfo ProveInfo +} + +type SysProperties struct { + Ss58Format types.Bytes + TokenDecimals types.U8 + TokenSymbol types.Text + SS58Prefix types.U32 +} + +type SysSyncState struct { + StartingBlock types.U32 + CurrentBlock types.U32 + HighestBlock types.U32 +} + +type OssInfo struct { + Peerid PeerId + Domain types.Bytes +} + +type BucketInfo struct { + ObjectsList []FileHash + Authority []types.AccountID +} + +type MinerInfo struct { + BeneficiaryAccount types.AccountID + StakingAccount types.AccountID + PeerId PeerId + Collaterals types.U128 + Debt types.U128 + State types.Bytes // positive, exit, frozen, lock + DeclarationSpace types.U128 + IdleSpace types.U128 + ServiceSpace types.U128 + LockSpace types.U128 + SpaceProofInfo types.Option[SpaceProofInfo] + ServiceBloomFilter BloomFilter + TeeSig TeeSig +} + +type SpaceProofInfo struct { + Miner types.AccountID + Front types.U64 + Rear types.U64 + PoisKey PoISKeyInfo + Accumulator Accumulator +} + +type MinerReward struct { + TotalReward types.U128 + RewardIssued types.U128 + OrderList []RewardOrder +} + +type RewardOrder struct { + ReceiveCount types.U8 + MaxCount types.U8 + Atonce types.Bool + OrderReward types.U128 + EachAmount types.U128 + LastReceiveBlock types.U32 +} + +type FileMetadata struct { + SegmentList []SegmentInfo + Owner []UserBrief + FileSize types.U128 + Completion types.U32 + State types.U8 +} + +type SegmentInfo struct { + Hash FileHash + FragmentList []FragmentInfo +} + +type UserBrief struct { + User types.AccountID + FileName types.Bytes + BucketName types.Bytes +} + +type FragmentInfo struct { + Hash FileHash + Avail types.Bool + Tag types.Option[types.U32] + Miner types.AccountID +} + +type StorageOrder struct { + FileSize types.U128 + SegmentList []SegmentList + User UserBrief + CompleteList []CompleteInfo +} + +type SegmentList struct { + SegmentHash FileHash + FragmentHash []FileHash +} + +type CompleteInfo struct { + Index types.U8 + Miner types.AccountID +} + +type UserSpaceInfo struct { + TotalSpace types.U128 + UsedSpace types.U128 + LockedSpace types.U128 + RemainingSpace types.U128 + Start types.U32 + Deadline types.U32 + State types.Bytes +} + +type ProveInfo struct { + Assign types.U8 + IdleProve types.Option[IdleProveInfo] + ServiceProve types.Option[ServiceProveInfo] +} + +type ChallengeElement struct { + Start types.U32 + IdleSlip types.U32 + ServiceSlip types.U32 + VerifySlip types.U32 + SpaceParam SpaceChallengeParam + ServiceParam QElement +} + +type QElement struct { + Index []types.U32 + Value []Random +} + +type MinerSnapShot struct { + IdleSpace types.U128 + ServiceSpace types.U128 + ServiceBloomFilter BloomFilter + SpaceProofInfo SpaceProofInfo + TeeSig TeeSig +} + +type IdleProveInfo struct { + TeePubkey WorkerPublicKey + IdleProve types.Bytes + VerifyResult types.Option[bool] +} + +type ServiceProveInfo struct { + TeePubkey WorkerPublicKey + ServiceProve types.Bytes + VerifyResult types.Option[bool] +} + +type TeeWorkerInfo struct { + Pubkey WorkerPublicKey + EcdhPubkey EcdhPublicKey + Version types.U32 + LastUpdated types.U64 + StashAccount types.Option[types.AccountID] + AttestationProvider types.Option[types.U8] + ConfidenceLevel types.U8 + Features []types.U32 + Role types.U8 // 0:Full 1:Verifier 2:Marker +} + +type RestoralOrderInfo struct { + Count types.U32 + Miner types.AccountID + OriginMiner types.AccountID + FragmentHash FileHash + FileHash FileHash + GenBlock types.U32 + Deadline types.U32 +} + +type RestoralTargetInfo struct { + Miner types.AccountID + ServiceSpace types.U128 + RestoredSpace types.U128 + CoolingBlock types.U32 +} + +type ExpendersInfo struct { + K types.U64 + N types.U64 + D types.U64 +} + +type PoISKeyInfo struct { + G PoISKey_G + N PoISKey_N +} + +type IdleSignInfo struct { + Miner types.AccountID + Rear types.U64 + Front types.U64 + Accumulator Accumulator + LastOperationBlock types.U32 + PoisKey PoISKeyInfo +} +type TagSigInfo struct { + Miner types.AccountID + Digest []DigestInfo + Filehash FileHash +} + +type DigestInfo struct { + Fragment FileHash + TeePubkey WorkerPublicKey +} + +type StakingExposure struct { + Total types.U128 + Own types.U128 + Others []OtherStakingExposure +} + +type OtherStakingExposure struct { + Who types.AccountID + Value types.U128 +} + +type StakingEraRewardPoints struct { + Total types.U32 + Individual []Individual +} + +type Individual struct { + Acc types.AccountID + Reward types.U32 +} + +type StakingNominations struct { + Targets []types.AccountID + SubmittedIn types.U32 + Suppressed types.Bool +} + +type StakingValidatorPrefs struct { + Commission types.U32 + Blocked types.Bool +} + +type CompleteSnapShotType struct { + MinerCount types.U32 + TotalPower types.U128 +} + +type RoundRewardType struct { + TotalReward types.U128 + OtherReward types.U128 +} + +// --------------------customer----------------- +type IdleFileMeta struct { + BlockNum uint32 + MinerAcc []byte + Hash string +} + +type UserSpaceSt struct { + TotalSpace string + UsedSpace string + LockedSpace string + RemainingSpace string + State string + Start uint32 + Deadline uint32 +} + +type NetSnapshot struct { + Start uint32 + Life uint32 + Total_reward string + Total_idle_space string + Total_service_space string + Random_index_list []uint32 + Random [][]byte +} + +type MinerSnapshot struct { + Miner string + Idle_space string + Service_space string +} + +type TeeInfo struct { + Pubkey string + EcdhPubkey string + Version uint32 + LastUpdated uint64 + StashAccount string + AttestationProvider uint8 + ConfidenceLevel uint8 + Features []uint32 + WorkerRole uint8 // 0:Full 1:Verifier 2:Marker +} + +type RewardsType struct { + Total string + Claimed string +} + +type SegmentDataInfo struct { + SegmentHash string + FragmentHash []string +} + +type UserInfo struct { + UserAccount string + FileName string + BucketName string + FileSize uint64 +} + +type AccessInfo struct { + r types.H160 + c []types.H160 +} diff --git a/core/pattern/pattern.go b/core/pattern/pattern.go index e88ea17..87bc325 100755 --- a/core/pattern/pattern.go +++ b/core/pattern/pattern.go @@ -1,610 +1,610 @@ -/* - Copyright (C) CESS. All rights reserved. - Copyright (C) Cumulus Encrypted Storage System. All rights reserved. +// /* +// Copyright (C) CESS. All rights reserved. +// Copyright (C) Cumulus Encrypted Storage System. All rights reserved. - SPDX-License-Identifier: Apache-2.0 -*/ +// SPDX-License-Identifier: Apache-2.0 +// */ package pattern -import ( - "time" - - "github.com/centrifuge/go-substrate-rpc-client/v4/types" - "github.com/pkg/errors" -) - -// DOT is "." character -const DOT = "." - -// Unit precision of CESS token -const TokenPrecision_CESS = "000000000000000000" - -const StakingStakePerTiB = 4000 - -const BlockIntervalSec = 6 - -// BlockInterval is the time interval for generating blocks, in seconds -const BlockInterval = time.Second * time.Duration(BlockIntervalSec) - -const MaxSubmitedIdleFileMeta = 30 - -const PublicDeoss = "http://deoss-pub-gateway.cess.cloud/" -const PublicDeossAccount = "cXhwBytXqrZLr1qM5NHJhCzEMckSTzNKw17ci2aHft6ETSQm9" - -// Pallets -const ( - // - AUDIT = "Audit" - // OSS is a module about DeOSS - OSS = "Oss" - // FILEBANK is a module about data metadata, bucket info, etc. - FILEBANK = "FileBank" - // TEEWOEKER is a module about TEE - TEEWORKER = "TeeWorker" - // SMINER is a module about storage miners - SMINER = "Sminer" - // STAKING is a module about staking - STAKING = "Staking" - // SMINER is a module about storage miners - STORAGEHANDLER = "StorageHandler" - // BALANCES is a module about the balances - BALANCES = "Balances" - // SYSTEM is a module about the system - SYSTEM = "System" - // EVM is a module about the evm contract - EVM = "EVM" - // - CessTreasury = "CessTreasury" -) - -// Chain state -const ( - //AUDIT - UNVERIFYPROOF = "UnverifyProof" - CHALLENGEDURATION = "ChallengeDuration" - CHALLENGEVERIFYDURATION = "VerifyDuration" - CHALLENGESNAPSHOT = "ChallengeSnapShot" - UNVERIFYIDLEPROOF = "UnverifyIdleProof" - UNVERIFYSERVICEPROOF = "UnverifyServiceProof" - - // OSS - // OSS - AUTHORITYLIST = "AuthorityList" - - // SMINER - ALLMINER = "AllMiner" - MINERITEMS = "MinerItems" - REWARDMAP = "RewardMap" - EXPENDERS = "Expenders" - RESTORALTARGETINFO = "RestoralTarget" - STAKINGSTARTBLOCK = "StakingStartBlock" - CompleteSnapShot = "CompleteSnapShot" - - // TEEWORKER - TEEWorkers = "Workers" - TEEMasterPubkey = "MasterPubkey" - TEEEndpoints = "Endpoints" - TEEWorkerAddedAt = "WorkerAddedAt" - - // FILEBANK - FILE = "File" - BUCKET = "Bucket" - BUCKETLIST = "UserBucketList" - DEALMAP = "DealMap" - FILLERMAP = "FillerMap" - PENDINGREPLACE = "PendingReplacements" - RESTORALORDER = "RestoralOrder" - - // STAKING - COUNTERFORVALIDATORS = "CounterForValidators" - CounterForNominators = "CounterForNominators" - ErasTotalStake = "ErasTotalStake" - CurrentEra = "CurrentEra" - ErasStakers = "ErasStakers" - ErasRewardPoints = "ErasRewardPoints" - Nominators = "Nominators" - Bonded = "Bonded" - Validators = "Validators" - ErasValidatorReward = "ErasValidatorReward" - ValidatorCount = "ValidatorCount" - - // STORAGE_HANDLER - USERSPACEINFO = "UserOwnedSpace" - UNITPRICE = "UnitPrice" - TOTALIDLESPACE = "TotalIdleSpace" - TOTALSERVICESPACE = "TotalServiceSpace" - PURCHASEDSPACE = "PurchasedSpace" - - // BALANCES - TOTALISSUANCE = "TotalIssuance" - - // SYSTEM - ACCOUNT = "Account" - EVENTS = "Events" - - // CessTreasury - RoundReward = "RoundReward" -) - -// Extrinsics -const ( - //AUDIT - TX_AUDIT_SUBMITPROOF = AUDIT + DOT + "submit_proof" - TX_AUDIT_SUBMITIDLEPROOF = AUDIT + DOT + "submit_idle_proof" - TX_AUDIT_SUBMITSERVICEPROOF = AUDIT + DOT + "submit_service_proof" - TX_AUDIT_SUBMITIDLEPROOFRESULT = AUDIT + DOT + "submit_verify_idle_result" - TX_AUDIT_SUBMITSERVICEPROOFRESULT = AUDIT + DOT + "submit_verify_service_result" - - // OSS - TX_OSS_REGISTER = OSS + DOT + "register" - TX_OSS_UPDATE = OSS + DOT + "update" - TX_OSS_DESTROY = OSS + DOT + "destroy" - TX_OSS_AUTHORIZE = OSS + DOT + "authorize" - TX_OSS_UNAUTHORIZE = OSS + DOT + "cancel_authorize" - - // SMINER - TX_SMINER_REGISTER = SMINER + DOT + "regnstk" - TX_SMINER_REGISTERASSIGNSTAKING = SMINER + DOT + "regnstk_assign_staking" - TX_SMINER_INCREASESTAKES = SMINER + DOT + "increase_collateral" - TX_SMINER_UPDATEPEERID = SMINER + DOT + "update_peer_id" - TX_SMINER_UPDATEINCOME = SMINER + DOT + "update_beneficiary" - TX_SMINER_CLAIMREWARD = SMINER + DOT + "receive_reward" - TX_SMINER_MINEREXITPREP = SMINER + DOT + "miner_exit_prep" - TX_SMINER_WITHDRAW = SMINER + DOT + "miner_withdraw" - TX_SMINER_REGISTERPOISKEY = SMINER + DOT + "register_pois_key" - TX_SMINER_INCREASEDECSPACE = SMINER + DOT + "increase_declaration_space" - - // FILEBANK - TX_FILEBANK_PUTBUCKET = FILEBANK + DOT + "create_bucket" - TX_FILEBANK_DELBUCKET = FILEBANK + DOT + "delete_bucket" - TX_FILEBANK_DELFILE = FILEBANK + DOT + "delete_file" - TX_FILEBANK_UPLOADDEC = FILEBANK + DOT + "upload_declaration" - TX_FILEBANK_FILEREPORT = FILEBANK + DOT + "transfer_report" - TX_FILEBANK_GENRESTOREORDER = FILEBANK + DOT + "generate_restoral_order" - 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" - TX_FILEBANK_CERTIDLESPACE = FILEBANK + DOT + "cert_idle_space" - TX_FILEBANK_REPLACEIDLESPACE = FILEBANK + DOT + "replace_idle_space" - TX_FILEBANK_CALCULATEREPORT = FILEBANK + DOT + "calculate_report" - - // STORAGE_HANDLER - TX_STORAGE_BUYSPACE = STORAGEHANDLER + DOT + "buy_space" - TX_STORAGE_EXPANSIONSPACE = STORAGEHANDLER + DOT + "expansion_space" - TX_STORAGE_RENEWALSPACE = STORAGEHANDLER + DOT + "renewal_space" - - // BALANCES - TX_BALANCES_FORCETRANSFER = "Balances" + DOT + "transfer" - - // EVM - TX_EVM_CALL = EVM + DOT + "call" -) - -// RPC Call -const ( - // System - RPC_SYS_Properties = "system_properties" - RPC_SYS_SyncState = "system_syncState" - RPC_SYS_Version = "system_version" - RPC_SYS_Chain = "system_chain" - - //Net - RPC_NET_Listening = "net_listening" -) - -const ( - Active = iota - Calculate - Missing - Recovery -) - -const ( - MINER_STATE_POSITIVE = "positive" - MINER_STATE_FROZEN = "frozen" - MINER_STATE_EXIT = "exit" - MINER_STATE_LOCK = "lock" - MINER_STATE_OFFLINE = "offline" -) - -// 0:Full 1:Verifier 2:Marker -const ( - TeeType_Full uint8 = 0 - TeeType_Verifier uint8 = 1 - TeeType_Marker uint8 = 2 -) - -const ( - ERR_Failed = "failed" - ERR_Timeout = "timeout" - ERR_Empty = "empty" -) - -const ( - MinBucketNameLength = 3 - MaxBucketNameLength = 63 - MaxDomainNameLength = 50 -) - -// byte size -const ( - SIZE_1KiB = 1024 - SIZE_1MiB = 1024 * SIZE_1KiB - SIZE_1GiB = 1024 * SIZE_1MiB - SIZE_1TiB = 1024 * SIZE_1GiB -) - -const ( - SegmentSize = 32 * SIZE_1MiB - FragmentSize = 8 * SIZE_1MiB - DataShards = 4 - ParShards = 8 -) - -var ( - ERR_RPC_CONNECTION = errors.New("rpc err: connection failed") - ERR_RPC_IP_FORMAT = errors.New("unsupported ip format") - ERR_RPC_TIMEOUT = errors.New("timeout") - ERR_RPC_EMPTY_VALUE = errors.New("empty") - ERR_RPC_PRIORITYTOOLOW = "Priority is too low" -) - -const ( - FileHashLen = 64 - RandomLen = 20 - PeerIdPublicKeyLen = 38 - PoISKeyLen = 256 - TeeSignatureLen = 256 - AccumulatorLen = 256 - SpaceChallengeParamLen = 8 - BloomFilterLen = 256 - MaxSegmentNum = 1000 - WorkerPublicKeyLen = 32 - MasterPublicKeyLen = 32 - EcdhPublicKeyLen = 32 - TeeSigLen = 64 -) - -type FileHash [FileHashLen]types.U8 -type Random [RandomLen]types.U8 -type PeerId [PeerIdPublicKeyLen]types.U8 -type PoISKey_G [PoISKeyLen]types.U8 -type PoISKey_N [PoISKeyLen]types.U8 -type TeeSignature [TeeSignatureLen]types.U8 -type Accumulator [AccumulatorLen]types.U8 -type SpaceChallengeParam [SpaceChallengeParamLen]types.U64 -type BloomFilter [BloomFilterLen]types.U64 -type WorkerPublicKey [WorkerPublicKeyLen]types.U8 -type MasterPublicKey [MasterPublicKeyLen]types.U8 -type EcdhPublicKey [EcdhPublicKeyLen]types.U8 -type TeeSig [TeeSigLen]types.U8 - -type SysProperties struct { - Ss58Format types.Bytes - TokenDecimals types.U8 - TokenSymbol types.Text - SS58Prefix types.U32 -} - -type SysSyncState struct { - StartingBlock types.U32 - CurrentBlock types.U32 - HighestBlock types.U32 -} - -type OssInfo struct { - Peerid PeerId - Domain types.Bytes -} - -type BucketInfo struct { - ObjectsList []FileHash - Authority []types.AccountID -} - -type MinerInfo struct { - BeneficiaryAccount types.AccountID - StakingAccount types.AccountID - PeerId PeerId - Collaterals types.U128 - Debt types.U128 - State types.Bytes // positive, exit, frozen, lock - DeclarationSpace types.U128 - IdleSpace types.U128 - ServiceSpace types.U128 - LockSpace types.U128 - SpaceProofInfo types.Option[SpaceProofInfo] - ServiceBloomFilter BloomFilter - TeeSig TeeSig -} - -type SpaceProofInfo struct { - Miner types.AccountID - Front types.U64 - Rear types.U64 - PoisKey PoISKeyInfo - Accumulator Accumulator -} - -type MinerReward struct { - TotalReward types.U128 - RewardIssued types.U128 - OrderList []RewardOrder -} - -type RewardOrder struct { - ReceiveCount types.U8 - MaxCount types.U8 - Atonce types.Bool - OrderReward types.U128 - EachAmount types.U128 - LastReceiveBlock types.U32 -} - -type FileMetadata struct { - SegmentList []SegmentInfo - Owner []UserBrief - FileSize types.U128 - Completion types.U32 - State types.U8 -} - -type SegmentInfo struct { - Hash FileHash - FragmentList []FragmentInfo -} - -type UserBrief struct { - User types.AccountID - FileName types.Bytes - BucketName types.Bytes -} - -type FragmentInfo struct { - Hash FileHash - Avail types.Bool - Tag types.Option[types.U32] - Miner types.AccountID -} - -type StorageOrder struct { - FileSize types.U128 - SegmentList []SegmentList - User UserBrief - CompleteList []CompleteInfo -} - -type SegmentList struct { - SegmentHash FileHash - FragmentHash []FileHash -} - -type CompleteInfo struct { - Index types.U8 - Miner types.AccountID -} - -type UserSpaceInfo struct { - TotalSpace types.U128 - UsedSpace types.U128 - LockedSpace types.U128 - RemainingSpace types.U128 - Start types.U32 - Deadline types.U32 - State types.Bytes -} - -type ChallengeInfo struct { - MinerSnapshot MinerSnapShot - ChallengeElement ChallengeElement - ProveInfo ProveInfo -} - -type ProveInfo struct { - Assign types.U8 - IdleProve types.Option[IdleProveInfo] - ServiceProve types.Option[ServiceProveInfo] -} - -type ChallengeElement struct { - Start types.U32 - IdleSlip types.U32 - ServiceSlip types.U32 - VerifySlip types.U32 - SpaceParam SpaceChallengeParam - ServiceParam QElement -} - -type QElement struct { - Index []types.U32 - Value []Random -} - -type MinerSnapShot struct { - IdleSpace types.U128 - ServiceSpace types.U128 - ServiceBloomFilter BloomFilter - SpaceProofInfo SpaceProofInfo - TeeSig TeeSig -} - -type IdleProveInfo struct { - TeePubkey WorkerPublicKey - IdleProve types.Bytes - VerifyResult types.Option[bool] -} - -type ServiceProveInfo struct { - TeePubkey WorkerPublicKey - ServiceProve types.Bytes - VerifyResult types.Option[bool] -} - -type TeeWorkerInfo struct { - Pubkey WorkerPublicKey - EcdhPubkey EcdhPublicKey - Version types.U32 - LastUpdated types.U64 - StashAccount types.Option[types.AccountID] - AttestationProvider types.Option[types.U8] - ConfidenceLevel types.U8 - Features []types.U32 - Role types.U8 // 0:Full 1:Verifier 2:Marker -} - -type RestoralOrderInfo struct { - Count types.U32 - Miner types.AccountID - OriginMiner types.AccountID - FragmentHash FileHash - FileHash FileHash - GenBlock types.U32 - Deadline types.U32 -} - -type RestoralTargetInfo struct { - Miner types.AccountID - ServiceSpace types.U128 - RestoredSpace types.U128 - CoolingBlock types.U32 -} - -type ExpendersInfo struct { - K types.U64 - N types.U64 - D types.U64 -} - -type PoISKeyInfo struct { - G PoISKey_G - N PoISKey_N -} - -type IdleSignInfo struct { - Miner types.AccountID - Rear types.U64 - Front types.U64 - Accumulator Accumulator - LastOperationBlock types.U32 - PoisKey PoISKeyInfo -} -type TagSigInfo struct { - Miner types.AccountID - Digest []DigestInfo - Filehash FileHash -} - -type DigestInfo struct { - Fragment FileHash - TeePubkey WorkerPublicKey -} - -type StakingExposure struct { - Total types.U128 - Own types.U128 - Others []OtherStakingExposure -} - -type OtherStakingExposure struct { - Who types.AccountID - Value types.U128 -} - -type StakingEraRewardPoints struct { - Total types.U32 - Individual []Individual -} - -type Individual struct { - Acc types.AccountID - Reward types.U32 -} - -type StakingNominations struct { - Targets []types.AccountID - SubmittedIn types.U32 - Suppressed types.Bool -} - -type StakingValidatorPrefs struct { - Commission types.U32 - Blocked types.Bool -} - -type CompleteSnapShotType struct { - MinerCount types.U32 - TotalPower types.U128 -} - -type RoundRewardType struct { - TotalReward types.U128 - OtherReward types.U128 -} - -// --------------------customer----------------- -type IdleFileMeta struct { - BlockNum uint32 - MinerAcc []byte - Hash string -} - -type UserSpaceSt struct { - TotalSpace string - UsedSpace string - LockedSpace string - RemainingSpace string - State string - Start uint32 - Deadline uint32 -} - -type NetSnapshot struct { - Start uint32 - Life uint32 - Total_reward string - Total_idle_space string - Total_service_space string - Random_index_list []uint32 - Random [][]byte -} - -type MinerSnapshot struct { - Miner string - Idle_space string - Service_space string -} - -type TeeInfo struct { - Pubkey string - EcdhPubkey string - Version uint32 - LastUpdated uint64 - StashAccount string - AttestationProvider uint8 - ConfidenceLevel uint8 - Features []uint32 - WorkerRole uint8 // 0:Full 1:Verifier 2:Marker -} - -type RewardsType struct { - Total string - Claimed string -} - -type SegmentDataInfo struct { - SegmentHash string - FragmentHash []string -} - -type UserInfo struct { - UserAccount string - FileName string - BucketName string - FileSize uint64 -} - -type AccessInfo struct { - r types.H160 - c []types.H160 -} +// import ( +// "time" + +// "github.com/centrifuge/go-substrate-rpc-client/v4/types" +// "github.com/pkg/errors" +// ) + +// // DOT is "." character +// const DOT = "." + +// // Unit precision of CESS token +// const TokenPrecision_CESS = "000000000000000000" + +// const StakingStakePerTiB = 4000 + +// const BlockIntervalSec = 6 + +// // BlockInterval is the time interval for generating blocks, in seconds +// const BlockInterval = time.Second * time.Duration(BlockIntervalSec) + +// const MaxSubmitedIdleFileMeta = 30 + +// const PublicDeoss = "http://deoss-pub-gateway.cess.cloud/" +// const PublicDeossAccount = "cXhwBytXqrZLr1qM5NHJhCzEMckSTzNKw17ci2aHft6ETSQm9" + +// // Pallets +// const ( +// // +// AUDIT = "Audit" +// // OSS is a module about DeOSS +// OSS = "Oss" +// // FILEBANK is a module about data metadata, bucket info, etc. +// FILEBANK = "FileBank" +// // TEEWOEKER is a module about TEE +// TEEWORKER = "TeeWorker" +// // SMINER is a module about storage miners +// SMINER = "Sminer" +// // STAKING is a module about staking +// STAKING = "Staking" +// // SMINER is a module about storage miners +// STORAGEHANDLER = "StorageHandler" +// // BALANCES is a module about the balances +// BALANCES = "Balances" +// // SYSTEM is a module about the system +// SYSTEM = "System" +// // EVM is a module about the evm contract +// EVM = "EVM" +// // +// CessTreasury = "CessTreasury" +// ) + +// // Chain state +// const ( +// //AUDIT +// UNVERIFYPROOF = "UnverifyProof" +// CHALLENGEDURATION = "ChallengeDuration" +// CHALLENGEVERIFYDURATION = "VerifyDuration" +// CHALLENGESNAPSHOT = "ChallengeSnapShot" +// UNVERIFYIDLEPROOF = "UnverifyIdleProof" +// UNVERIFYSERVICEPROOF = "UnverifyServiceProof" + +// // OSS +// // OSS +// AUTHORITYLIST = "AuthorityList" + +// // SMINER +// ALLMINER = "AllMiner" +// MINERITEMS = "MinerItems" +// REWARDMAP = "RewardMap" +// EXPENDERS = "Expenders" +// RESTORALTARGETINFO = "RestoralTarget" +// STAKINGSTARTBLOCK = "StakingStartBlock" +// CompleteSnapShot = "CompleteSnapShot" + +// // TEEWORKER +// TEEWorkers = "Workers" +// TEEMasterPubkey = "MasterPubkey" +// TEEEndpoints = "Endpoints" +// TEEWorkerAddedAt = "WorkerAddedAt" + +// // FILEBANK +// FILE = "File" +// BUCKET = "Bucket" +// BUCKETLIST = "UserBucketList" +// DEALMAP = "DealMap" +// FILLERMAP = "FillerMap" +// PENDINGREPLACE = "PendingReplacements" +// RESTORALORDER = "RestoralOrder" + +// // STAKING +// COUNTERFORVALIDATORS = "CounterForValidators" +// CounterForNominators = "CounterForNominators" +// ErasTotalStake = "ErasTotalStake" +// CurrentEra = "CurrentEra" +// ErasStakers = "ErasStakers" +// ErasRewardPoints = "ErasRewardPoints" +// Nominators = "Nominators" +// Bonded = "Bonded" +// Validators = "Validators" +// ErasValidatorReward = "ErasValidatorReward" +// ValidatorCount = "ValidatorCount" + +// // STORAGE_HANDLER +// USERSPACEINFO = "UserOwnedSpace" +// UNITPRICE = "UnitPrice" +// TOTALIDLESPACE = "TotalIdleSpace" +// TOTALSERVICESPACE = "TotalServiceSpace" +// PURCHASEDSPACE = "PurchasedSpace" + +// // BALANCES +// TOTALISSUANCE = "TotalIssuance" + +// // SYSTEM +// ACCOUNT = "Account" +// EVENTS = "Events" + +// // CessTreasury +// RoundReward = "RoundReward" +// ) + +// // Extrinsics +// const ( +// //AUDIT +// TX_AUDIT_SUBMITPROOF = AUDIT + DOT + "submit_proof" +// TX_AUDIT_SUBMITIDLEPROOF = AUDIT + DOT + "submit_idle_proof" +// TX_AUDIT_SUBMITSERVICEPROOF = AUDIT + DOT + "submit_service_proof" +// TX_AUDIT_SUBMITIDLEPROOFRESULT = AUDIT + DOT + "submit_verify_idle_result" +// TX_AUDIT_SUBMITSERVICEPROOFRESULT = AUDIT + DOT + "submit_verify_service_result" + +// // OSS +// TX_OSS_REGISTER = OSS + DOT + "register" +// TX_OSS_UPDATE = OSS + DOT + "update" +// TX_OSS_DESTROY = OSS + DOT + "destroy" +// TX_OSS_AUTHORIZE = OSS + DOT + "authorize" +// TX_OSS_UNAUTHORIZE = OSS + DOT + "cancel_authorize" + +// // SMINER +// TX_SMINER_REGISTER = SMINER + DOT + "regnstk" +// TX_SMINER_REGISTERASSIGNSTAKING = SMINER + DOT + "regnstk_assign_staking" +// TX_SMINER_INCREASESTAKES = SMINER + DOT + "increase_collateral" +// TX_SMINER_UPDATEPEERID = SMINER + DOT + "update_peer_id" +// TX_SMINER_UPDATEINCOME = SMINER + DOT + "update_beneficiary" +// TX_SMINER_CLAIMREWARD = SMINER + DOT + "receive_reward" +// TX_SMINER_MINEREXITPREP = SMINER + DOT + "miner_exit_prep" +// TX_SMINER_WITHDRAW = SMINER + DOT + "miner_withdraw" +// TX_SMINER_REGISTERPOISKEY = SMINER + DOT + "register_pois_key" +// TX_SMINER_INCREASEDECSPACE = SMINER + DOT + "increase_declaration_space" + +// // FILEBANK +// TX_FILEBANK_PUTBUCKET = FILEBANK + DOT + "create_bucket" +// TX_FILEBANK_DELBUCKET = FILEBANK + DOT + "delete_bucket" +// TX_FILEBANK_DELFILE = FILEBANK + DOT + "delete_file" +// TX_FILEBANK_UPLOADDEC = FILEBANK + DOT + "upload_declaration" +// TX_FILEBANK_FILEREPORT = FILEBANK + DOT + "transfer_report" +// TX_FILEBANK_GENRESTOREORDER = FILEBANK + DOT + "generate_restoral_order" +// 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" +// TX_FILEBANK_CERTIDLESPACE = FILEBANK + DOT + "cert_idle_space" +// TX_FILEBANK_REPLACEIDLESPACE = FILEBANK + DOT + "replace_idle_space" +// TX_FILEBANK_CALCULATEREPORT = FILEBANK + DOT + "calculate_report" + +// // STORAGE_HANDLER +// TX_STORAGE_BUYSPACE = STORAGEHANDLER + DOT + "buy_space" +// TX_STORAGE_EXPANSIONSPACE = STORAGEHANDLER + DOT + "expansion_space" +// TX_STORAGE_RENEWALSPACE = STORAGEHANDLER + DOT + "renewal_space" + +// // BALANCES +// TX_BALANCES_FORCETRANSFER = "Balances" + DOT + "transfer" + +// // EVM +// TX_EVM_CALL = EVM + DOT + "call" +// ) + +// // RPC Call +// const ( +// // System +// RPC_SYS_Properties = "system_properties" +// RPC_SYS_SyncState = "system_syncState" +// RPC_SYS_Version = "system_version" +// RPC_SYS_Chain = "system_chain" + +// //Net +// RPC_NET_Listening = "net_listening" +// ) + +// const ( +// Active = iota +// Calculate +// Missing +// Recovery +// ) + +// const ( +// MINER_STATE_POSITIVE = "positive" +// MINER_STATE_FROZEN = "frozen" +// MINER_STATE_EXIT = "exit" +// MINER_STATE_LOCK = "lock" +// MINER_STATE_OFFLINE = "offline" +// ) + +// // 0:Full 1:Verifier 2:Marker +// const ( +// TeeType_Full uint8 = 0 +// TeeType_Verifier uint8 = 1 +// TeeType_Marker uint8 = 2 +// ) + +// const ( +// ERR_Failed = "failed" +// ERR_Timeout = "timeout" +// ERR_Empty = "empty" +// ) + +// const ( +// MinBucketNameLength = 3 +// MaxBucketNameLength = 63 +// MaxDomainNameLength = 50 +// ) + +// // byte size +// const ( +// SIZE_1KiB = 1024 +// SIZE_1MiB = 1024 * SIZE_1KiB +// SIZE_1GiB = 1024 * SIZE_1MiB +// SIZE_1TiB = 1024 * SIZE_1GiB +// ) + +// const ( +// SegmentSize = 32 * SIZE_1MiB +// FragmentSize = 8 * SIZE_1MiB +// DataShards = 4 +// ParShards = 8 +// ) + +// var ( +// ERR_RPC_CONNECTION = errors.New("rpc err: connection failed") +// ERR_RPC_IP_FORMAT = errors.New("unsupported ip format") +// ERR_RPC_TIMEOUT = errors.New("timeout") +// ERR_RPC_EMPTY_VALUE = errors.New("empty") +// ERR_RPC_PRIORITYTOOLOW = "Priority is too low" +// ) + +// const ( +// FileHashLen = 64 +// RandomLen = 20 +// PeerIdPublicKeyLen = 38 +// PoISKeyLen = 256 +// TeeSignatureLen = 256 +// AccumulatorLen = 256 +// SpaceChallengeParamLen = 8 +// BloomFilterLen = 256 +// MaxSegmentNum = 1000 +// WorkerPublicKeyLen = 32 +// MasterPublicKeyLen = 32 +// EcdhPublicKeyLen = 32 +// TeeSigLen = 64 +// ) + +// type FileHash [FileHashLen]types.U8 +// type Random [RandomLen]types.U8 +// type PeerId [PeerIdPublicKeyLen]types.U8 +// type PoISKey_G [PoISKeyLen]types.U8 +// type PoISKey_N [PoISKeyLen]types.U8 +// type TeeSignature [TeeSignatureLen]types.U8 +// type Accumulator [AccumulatorLen]types.U8 +// type SpaceChallengeParam [SpaceChallengeParamLen]types.U64 +// type BloomFilter [BloomFilterLen]types.U64 +// type WorkerPublicKey [WorkerPublicKeyLen]types.U8 +// type MasterPublicKey [MasterPublicKeyLen]types.U8 +// type EcdhPublicKey [EcdhPublicKeyLen]types.U8 +// type TeeSig [TeeSigLen]types.U8 + +// type SysProperties struct { +// Ss58Format types.Bytes +// TokenDecimals types.U8 +// TokenSymbol types.Text +// SS58Prefix types.U32 +// } + +// type SysSyncState struct { +// StartingBlock types.U32 +// CurrentBlock types.U32 +// HighestBlock types.U32 +// } + +// type OssInfo struct { +// Peerid PeerId +// Domain types.Bytes +// } + +// type BucketInfo struct { +// ObjectsList []FileHash +// Authority []types.AccountID +// } + +// type MinerInfo struct { +// BeneficiaryAccount types.AccountID +// StakingAccount types.AccountID +// PeerId PeerId +// Collaterals types.U128 +// Debt types.U128 +// State types.Bytes // positive, exit, frozen, lock +// DeclarationSpace types.U128 +// IdleSpace types.U128 +// ServiceSpace types.U128 +// LockSpace types.U128 +// SpaceProofInfo types.Option[SpaceProofInfo] +// ServiceBloomFilter BloomFilter +// TeeSig TeeSig +// } + +// type SpaceProofInfo struct { +// Miner types.AccountID +// Front types.U64 +// Rear types.U64 +// PoisKey PoISKeyInfo +// Accumulator Accumulator +// } + +// type MinerReward struct { +// TotalReward types.U128 +// RewardIssued types.U128 +// OrderList []RewardOrder +// } + +// type RewardOrder struct { +// ReceiveCount types.U8 +// MaxCount types.U8 +// Atonce types.Bool +// OrderReward types.U128 +// EachAmount types.U128 +// LastReceiveBlock types.U32 +// } + +// type FileMetadata struct { +// SegmentList []SegmentInfo +// Owner []UserBrief +// FileSize types.U128 +// Completion types.U32 +// State types.U8 +// } + +// type SegmentInfo struct { +// Hash FileHash +// FragmentList []FragmentInfo +// } + +// type UserBrief struct { +// User types.AccountID +// FileName types.Bytes +// BucketName types.Bytes +// } + +// type FragmentInfo struct { +// Hash FileHash +// Avail types.Bool +// Tag types.Option[types.U32] +// Miner types.AccountID +// } + +// type StorageOrder struct { +// FileSize types.U128 +// SegmentList []SegmentList +// User UserBrief +// CompleteList []CompleteInfo +// } + +// type SegmentList struct { +// SegmentHash FileHash +// FragmentHash []FileHash +// } + +// type CompleteInfo struct { +// Index types.U8 +// Miner types.AccountID +// } + +// type UserSpaceInfo struct { +// TotalSpace types.U128 +// UsedSpace types.U128 +// LockedSpace types.U128 +// RemainingSpace types.U128 +// Start types.U32 +// Deadline types.U32 +// State types.Bytes +// } + +// type ChallengeSnapShot struct { +// MinerSnapshot MinerSnapShot +// ChallengeElement ChallengeElement +// ProveInfo ProveInfo +// } + +// type ProveInfo struct { +// Assign types.U8 +// IdleProve types.Option[IdleProveInfo] +// ServiceProve types.Option[ServiceProveInfo] +// } + +// type ChallengeElement struct { +// Start types.U32 +// IdleSlip types.U32 +// ServiceSlip types.U32 +// VerifySlip types.U32 +// SpaceParam SpaceChallengeParam +// ServiceParam QElement +// } + +// type QElement struct { +// Index []types.U32 +// Value []Random +// } + +// type MinerSnapShot struct { +// IdleSpace types.U128 +// ServiceSpace types.U128 +// ServiceBloomFilter BloomFilter +// SpaceProofInfo SpaceProofInfo +// TeeSig TeeSig +// } + +// type IdleProveInfo struct { +// TeePubkey WorkerPublicKey +// IdleProve types.Bytes +// VerifyResult types.Option[bool] +// } + +// type ServiceProveInfo struct { +// TeePubkey WorkerPublicKey +// ServiceProve types.Bytes +// VerifyResult types.Option[bool] +// } + +// type TeeWorkerInfo struct { +// Pubkey WorkerPublicKey +// EcdhPubkey EcdhPublicKey +// Version types.U32 +// LastUpdated types.U64 +// StashAccount types.Option[types.AccountID] +// AttestationProvider types.Option[types.U8] +// ConfidenceLevel types.U8 +// Features []types.U32 +// Role types.U8 // 0:Full 1:Verifier 2:Marker +// } + +// type RestoralOrderInfo struct { +// Count types.U32 +// Miner types.AccountID +// OriginMiner types.AccountID +// FragmentHash FileHash +// FileHash FileHash +// GenBlock types.U32 +// Deadline types.U32 +// } + +// type RestoralTargetInfo struct { +// Miner types.AccountID +// ServiceSpace types.U128 +// RestoredSpace types.U128 +// CoolingBlock types.U32 +// } + +// type ExpendersInfo struct { +// K types.U64 +// N types.U64 +// D types.U64 +// } + +// type PoISKeyInfo struct { +// G PoISKey_G +// N PoISKey_N +// } + +// type IdleSignInfo struct { +// Miner types.AccountID +// Rear types.U64 +// Front types.U64 +// Accumulator Accumulator +// LastOperationBlock types.U32 +// PoisKey PoISKeyInfo +// } +// type TagSigInfo struct { +// Miner types.AccountID +// Digest []DigestInfo +// Filehash FileHash +// } + +// type DigestInfo struct { +// Fragment FileHash +// TeePubkey WorkerPublicKey +// } + +// type StakingExposure struct { +// Total types.U128 +// Own types.U128 +// Others []OtherStakingExposure +// } + +// type OtherStakingExposure struct { +// Who types.AccountID +// Value types.U128 +// } + +// type StakingEraRewardPoints struct { +// Total types.U32 +// Individual []Individual +// } + +// type Individual struct { +// Acc types.AccountID +// Reward types.U32 +// } + +// type StakingNominations struct { +// Targets []types.AccountID +// SubmittedIn types.U32 +// Suppressed types.Bool +// } + +// type StakingValidatorPrefs struct { +// Commission types.U32 +// Blocked types.Bool +// } + +// type CompleteSnapShotType struct { +// MinerCount types.U32 +// TotalPower types.U128 +// } + +// type RoundRewardType struct { +// TotalReward types.U128 +// OtherReward types.U128 +// } + +// // --------------------customer----------------- +// type IdleFileMeta struct { +// BlockNum uint32 +// MinerAcc []byte +// Hash string +// } + +// type UserSpaceSt struct { +// TotalSpace string +// UsedSpace string +// LockedSpace string +// RemainingSpace string +// State string +// Start uint32 +// Deadline uint32 +// } + +// type NetSnapshot struct { +// Start uint32 +// Life uint32 +// Total_reward string +// Total_idle_space string +// Total_service_space string +// Random_index_list []uint32 +// Random [][]byte +// } + +// type MinerSnapshot struct { +// Miner string +// Idle_space string +// Service_space string +// } + +// type TeeInfo struct { +// Pubkey string +// EcdhPubkey string +// Version uint32 +// LastUpdated uint64 +// StashAccount string +// AttestationProvider uint8 +// ConfidenceLevel uint8 +// Features []uint32 +// WorkerRole uint8 // 0:Full 1:Verifier 2:Marker +// } + +// type RewardsType struct { +// Total string +// Claimed string +// } + +// type SegmentDataInfo struct { +// SegmentHash string +// FragmentHash []string +// } + +// type UserInfo struct { +// UserAccount string +// FileName string +// BucketName string +// FileSize uint64 +// } + +// type AccessInfo struct { +// r types.H160 +// c []types.H160 +// } From 99f8ce73455427076ec5c64e1ddacd77d9ae6ee8 Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Wed, 8 May 2024 10:12:57 +0800 Subject: [PATCH 03/18] update balance --- chain/balances.go | 134 ++++++++++++++++++++++++++++++++++++++++++++++ chain/chain.go | 21 ++++---- chain/pattern.go | 53 +++++++++--------- chain/system.go | 52 ------------------ 4 files changed, 171 insertions(+), 89 deletions(-) create mode 100644 chain/balances.go diff --git a/chain/balances.go b/chain/balances.go new file mode 100644 index 0000000..197daa0 --- /dev/null +++ b/chain/balances.go @@ -0,0 +1,134 @@ +/* + Copyright (C) CESS. All rights reserved. + Copyright (C) Cumulus Encrypted Storage System. All rights reserved. + + SPDX-License-Identifier: Apache-2.0 +*/ + +package chain + +import ( + "fmt" + "log" + + "github.com/CESSProject/cess-go-sdk/utils" + "github.com/centrifuge/go-substrate-rpc-client/v4/types" +) + +// QueryTotalIssuance query the total amount of token issuance +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - string: the total amount of token issuance +// - error: error message +func (c *ChainClient) QueryTotalIssuance(block int) (string, error) { + defer func() { + if err := recover(); err != nil { + log.Println(utils.RecoverError(err)) + } + }() + var data types.U128 + + if !c.GetChainState() { + return "", ERR_RPC_CONNECTION + } + + key, err := types.CreateStorageKey(c.metadata, Balances, TotalIssuance) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Balances, TotalIssuance, err) + return "", 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(), Balances, TotalIssuance, err) + c.SetChainState(false) + return "", err + } + if !ok { + return "", ERR_RPC_EMPTY_VALUE + } + return data.String(), nil + } + + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Balances, TotalIssuance, err) + c.SetChainState(false) + return "", err + } + + 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(), Balances, TotalIssuance, err) + c.SetChainState(false) + return "", err + } + if !ok { + return "0", nil + } + if data.String() == "" { + return "0", nil + } + return data.String(), nil +} + +// QueryInactiveIssuance query the amount of inactive token issuance +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - string: the amount of inactive token issuance +// - error: error message +func (c *ChainClient) QueryInactiveIssuance(block int) (string, error) { + defer func() { + if err := recover(); err != nil { + log.Println(utils.RecoverError(err)) + } + }() + var data types.U128 + + if !c.GetChainState() { + return "", ERR_RPC_CONNECTION + } + + key, err := types.CreateStorageKey(c.metadata, Balances, InactiveIssuance) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Balances, InactiveIssuance, err) + return "", 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(), Balances, InactiveIssuance, err) + c.SetChainState(false) + return "", err + } + if !ok { + return "", ERR_RPC_EMPTY_VALUE + } + return data.String(), nil + } + + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Balances, InactiveIssuance, err) + c.SetChainState(false) + return "", err + } + + 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(), Balances, InactiveIssuance, err) + c.SetChainState(false) + return "", err + } + if !ok { + return "0", nil + } + if data.String() == "" { + return "0", nil + } + return data.String(), nil +} diff --git a/chain/chain.go b/chain/chain.go index 7e2b1b4..be8aa3d 100755 --- a/chain/chain.go +++ b/chain/chain.go @@ -19,7 +19,6 @@ import ( "sync" "time" - "github.com/CESSProject/cess-go-sdk/core/pattern" "github.com/CESSProject/cess-go-sdk/utils" gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4" "github.com/centrifuge/go-substrate-rpc-client/v4/registry/retriever" @@ -76,7 +75,7 @@ func NewChainClient( chainClient = &ChainClient{ lock: new(sync.Mutex), chainStLock: new(sync.Mutex), - txTicker: time.NewTicker(pattern.BlockInterval), + txTicker: time.NewTicker(BlockInterval), rpcAddr: rpcs, packingTime: t, name: serviceName, @@ -97,7 +96,7 @@ func NewChainClient( } if chainClient.api == nil { - return nil, pattern.ERR_RPC_CONNECTION + return nil, ERR_RPC_CONNECTION } chainClient.SetChainState(true) @@ -114,7 +113,7 @@ func NewChainClient( if err != nil { return nil, err } - chainClient.keyEvents, err = types.CreateStorageKey(chainClient.metadata, pattern.SYSTEM, pattern.EVENTS, nil) + chainClient.keyEvents, err = types.CreateStorageKey(chainClient.metadata, System, Events, nil) if err != nil { return nil, err } @@ -285,7 +284,7 @@ func reconnectChainSDK(oldRpc string, rpcs []string) ( rpcAddr = rpcaddrs[i] } if api == nil { - return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, pattern.ERR_RPC_CONNECTION + return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, ERR_RPC_CONNECTION } var metadata *types.Metadata var runtimeVer *types.RuntimeVersion @@ -295,23 +294,23 @@ func reconnectChainSDK(oldRpc string, rpcs []string) ( metadata, err = api.RPC.State.GetMetadataLatest() if err != nil { - return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, pattern.ERR_RPC_CONNECTION + return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, ERR_RPC_CONNECTION } genesisHash, err = api.RPC.Chain.GetBlockHash(0) if err != nil { - return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, pattern.ERR_RPC_CONNECTION + return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, ERR_RPC_CONNECTION } runtimeVer, err = api.RPC.State.GetRuntimeVersionLatest() if err != nil { - return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, pattern.ERR_RPC_CONNECTION + return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, ERR_RPC_CONNECTION } - keyEvents, err = types.CreateStorageKey(metadata, pattern.SYSTEM, pattern.EVENTS, nil) + keyEvents, err = types.CreateStorageKey(metadata, System, Events, nil) if err != nil { - return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, pattern.ERR_RPC_CONNECTION + return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, ERR_RPC_CONNECTION } eventRetriever, err = retriever.NewDefaultEventRetriever(state.NewEventProvider(api.RPC.State), api.RPC.State) if err != nil { - return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, pattern.ERR_RPC_CONNECTION + return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, ERR_RPC_CONNECTION } return api, metadata, runtimeVer, keyEvents, eventRetriever, genesisHash, rpcAddr, err } diff --git a/chain/pattern.go b/chain/pattern.go index 0970495..3ea0663 100644 --- a/chain/pattern.go +++ b/chain/pattern.go @@ -32,24 +32,24 @@ const MaxSubmitedIdleFileMeta = 30 const PublicDeoss = "http://deoss-pub-gateway.cess.cloud/" const PublicDeossAccount = "cXhwBytXqrZLr1qM5NHJhCzEMckSTzNKw17ci2aHft6ETSQm9" -// Pallets +// pallet names const ( - // + // Audit Audit = "Audit" // OSS is a module about DeOSS - OSS = "Oss" + Oss = "Oss" // FILEBANK is a module about data metadata, bucket info, etc. - FILEBANK = "FileBank" + FileBank = "FileBank" // TEEWOEKER is a module about TEE - TEEWORKER = "TeeWorker" + TeeWorker = "TeeWorker" // SMINER is a module about storage miners - SMINER = "Sminer" + Sminer = "Sminer" // STAKING is a module about staking - STAKING = "Staking" + Staking = "Staking" // SMINER is a module about storage miners - STORAGEHANDLER = "StorageHandler" + StorageHandler = "StorageHandler" // BALANCES is a module about the balances - BALANCES = "Balances" + Balances = "Balances" // SYSTEM is a module about the system System = "System" // EVM is a module about the evm contract @@ -58,9 +58,9 @@ const ( CessTreasury = "CessTreasury" ) -// Chain state +// chain state const ( - //AUDIT + // Audit ChallengeSlip = "ChallengeSlip" ChallengeSnapShot = "ChallengeSnapShot" CountedClear = "CountedClear" @@ -69,16 +69,16 @@ const ( // OSS // OSS - AUTHORITYLIST = "AuthorityList" + AuthorityList = "AuthorityList" // SMINER - ALLMINER = "AllMiner" - MINERITEMS = "MinerItems" - REWARDMAP = "RewardMap" - EXPENDERS = "Expenders" - RESTORALTARGETINFO = "RestoralTarget" - STAKINGSTARTBLOCK = "StakingStartBlock" - CompleteSnapShot = "CompleteSnapShot" + AllMiner = "AllMiner" + MinerItems = "MinerItems" + RewardMap = "RewardMap" + Expenders = "Expenders" + RestoralTarget = "RestoralTarget" + StakingStartBlock = "StakingStartBlock" + CompleteSnapShot = "CompleteSnapShot" // TEEWORKER TEEWorkers = "Workers" @@ -109,18 +109,19 @@ const ( ValidatorCount = "ValidatorCount" // STORAGE_HANDLER - USERSPACEINFO = "UserOwnedSpace" - UNITPRICE = "UnitPrice" - TOTALIDLESPACE = "TotalIdleSpace" - TOTALSERVICESPACE = "TotalServiceSpace" - PURCHASEDSPACE = "PurchasedSpace" + UserOwnedSpace = "UserOwnedSpace" + UnitPrice = "UnitPrice" + TotalIdleSpace = "TotalIdleSpace" + TotalServiceSpace = "TotalServiceSpace" + PurchasedSpace = "PurchasedSpace" // BALANCES - TOTALISSUANCE = "TotalIssuance" + TotalIssuance = "TotalIssuance" + InactiveIssuance = "InactiveIssuance" // SYSTEM Account = "Account" - EVENTS = "Events" + Events = "Events" // CessTreasury RoundReward = "RoundReward" diff --git a/chain/system.go b/chain/system.go index 97755fc..35b55b5 100755 --- a/chain/system.go +++ b/chain/system.go @@ -202,58 +202,6 @@ func (c *ChainClient) QueryAllAccountInfoFromBlock(block int) ([]types.AccountIn return data, nil } -// QueryTotalIssuance -func (c *ChainClient) QueryTotalIssuance(block int) (string, error) { - defer func() { - if err := recover(); err != nil { - log.Println(utils.RecoverError(err)) - } - }() - var data types.U128 - - if !c.GetChainState() { - return "", pattern.ERR_RPC_CONNECTION - } - - key, err := types.CreateStorageKey(c.metadata, pattern.BALANCES, pattern.TOTALISSUANCE) - if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.BALANCES, pattern.TOTALISSUANCE, err) - c.SetChainState(false) - return "", 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(), pattern.BALANCES, pattern.TOTALISSUANCE, err) - c.SetChainState(false) - return "", err - } - if !ok { - return "", pattern.ERR_RPC_EMPTY_VALUE - } - return data.String(), nil - } - - blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) - if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), pattern.BALANCES, pattern.TOTALISSUANCE, err) - c.SetChainState(false) - return "", err - } - - 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(), pattern.BALANCES, pattern.TOTALISSUANCE, err) - c.SetChainState(false) - return "", err - } - if !ok { - return "", pattern.ERR_RPC_EMPTY_VALUE - } - return data.String(), nil -} - func (c *ChainClient) SysProperties() (pattern.SysProperties, error) { defer func() { if err := recover(); err != nil { From 50d11a33479e307fcd8829380d0984e49f1afe52 Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Wed, 8 May 2024 11:45:58 +0800 Subject: [PATCH 04/18] update treasury --- chain/balances.go | 109 ++++ chain/pattern.go | 9 +- chain/system.go | 92 --- chain/treasury.go | 222 ++++++- core/pattern/pattern.go | 1212 +++++++++++++++++++-------------------- 5 files changed, 932 insertions(+), 712 deletions(-) diff --git a/chain/balances.go b/chain/balances.go index 197daa0..49e7f0b 100644 --- a/chain/balances.go +++ b/chain/balances.go @@ -10,9 +10,11 @@ package chain import ( "fmt" "log" + "time" "github.com/CESSProject/cess-go-sdk/utils" "github.com/centrifuge/go-substrate-rpc-client/v4/types" + "github.com/pkg/errors" ) // QueryTotalIssuance query the total amount of token issuance @@ -132,3 +134,110 @@ func (c *ChainClient) QueryInactiveIssuance(block int) (string, error) { } return data.String(), nil } + +// TransferToken transfers to other accounts +// - dest: target account +// - amount: transfer amount +// +// Return: +// - string: block hash +// - string: target account +// - error: error message +func (c *ChainClient) TransferToken(dest string, amount uint64) (string, string, error) { + c.lock.Lock() + defer func() { + c.lock.Unlock() + if err := recover(); err != nil { + log.Println(utils.RecoverError(err)) + } + }() + + var ( + blockhash string + accountInfo types.AccountInfo + ) + + if !c.GetChainState() { + return blockhash, "", ERR_RPC_CONNECTION + } + + pubkey, err := utils.ParsingPublickey(dest) + if err != nil { + return blockhash, "", errors.Wrapf(err, "[ParsingPublickey]") + } + + address, err := types.NewMultiAddressFromAccountID(pubkey) + if err != nil { + return blockhash, "", errors.Wrapf(err, "[NewMultiAddressFromAccountID]") + } + + call, err := types.NewCall(c.metadata, TX_Balances_Transfer, address, types.NewUCompactFromUInt(amount)) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Balances_Transfer, err) + return blockhash, "", err + } + + ext := types.NewExtrinsic(call) + + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Balances_Transfer, err) + return blockhash, "", err + } + + ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Balances_Transfer, err) + c.SetChainState(false) + return blockhash, "", err + } + if !ok { + return blockhash, "", 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, + } + + // Sign the transaction + err = ext.Sign(c.keyring, o) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Balances_Transfer, err) + return blockhash, "", err + } + + <-c.txTicker.C + + // Do the transfer and track the actual status + sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Balances_Transfer, err) + c.SetChainState(false) + return blockhash, "", err + } + defer sub.Unsubscribe() + + timeout := time.NewTimer(c.packingTime) + defer timeout.Stop() + + for { + select { + case status := <-sub.Chan(): + if status.IsInBlock { + blockhash = status.AsInBlock.Hex() + _, err = c.RetrieveEvent_Balances_Transfer(status.AsInBlock) + return blockhash, dest, err + } + case err = <-sub.Err(): + return blockhash, "", errors.Wrap(err, "[sub]") + case <-timeout.C: + return blockhash, "", ERR_RPC_TIMEOUT + } + } +} diff --git a/chain/pattern.go b/chain/pattern.go index 3ea0663..557c872 100644 --- a/chain/pattern.go +++ b/chain/pattern.go @@ -115,7 +115,7 @@ const ( TotalServiceSpace = "TotalServiceSpace" PurchasedSpace = "PurchasedSpace" - // BALANCES + // Balances TotalIssuance = "TotalIssuance" InactiveIssuance = "InactiveIssuance" @@ -124,7 +124,10 @@ const ( Events = "Events" // CessTreasury - RoundReward = "RoundReward" + CurrencyReward = "CurrencyReward" + EraReward = "EraReward" + ReserveReward = "ReserveReward" + RoundReward = "RoundReward" ) // Extrinsics @@ -174,7 +177,7 @@ const ( TX_STORAGE_RENEWALSPACE = STORAGEHANDLER + DOT + "renewal_space" // BALANCES - TX_BALANCES_FORCETRANSFER = "Balances" + DOT + "transfer" + TX_Balances_Transfer = "Balances" + DOT + "transfer" // EVM TX_EVM_CALL = EVM + DOT + "call" diff --git a/chain/system.go b/chain/system.go index 35b55b5..396ff4d 100755 --- a/chain/system.go +++ b/chain/system.go @@ -10,7 +10,6 @@ package chain import ( "fmt" "log" - "time" "github.com/CESSProject/cess-go-sdk/core/pattern" "github.com/CESSProject/cess-go-sdk/utils" @@ -271,94 +270,3 @@ func (c *ChainClient) NetListening() (bool, error) { err := c.api.Client.Call(&data, pattern.RPC_NET_Listening) return bool(data), err } - -func (c *ChainClient) TransferToken(dest string, amount uint64) (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, "", pattern.ERR_RPC_CONNECTION - } - - pubkey, err := utils.ParsingPublickey(dest) - if err != nil { - return "", "", errors.Wrapf(err, "[ParsingPublickey]") - } - - address, err := types.NewMultiAddressFromAccountID(pubkey) - if err != nil { - return "", "", errors.Wrapf(err, "[NewAddressFromAccountID]") - } - - call, err := types.NewCall(c.metadata, pattern.TX_BALANCES_FORCETRANSFER, address, types.NewUCompactFromUInt(amount)) - 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.packingTime) - defer timeout.Stop() - - for { - select { - case status := <-sub.Chan(): - if status.IsInBlock { - txhash = status.AsInBlock.Hex() - _, err = c.RetrieveEvent_Balances_Transfer(status.AsInBlock) - return txhash, dest, err - } - case err = <-sub.Err(): - return txhash, "", errors.Wrap(err, "[sub]") - case <-timeout.C: - return txhash, "", pattern.ERR_RPC_TIMEOUT - } - } -} diff --git a/chain/treasury.go b/chain/treasury.go index 1391614..daa9332 100644 --- a/chain/treasury.go +++ b/chain/treasury.go @@ -11,12 +11,210 @@ import ( "fmt" "log" - "github.com/CESSProject/cess-go-sdk/core/pattern" "github.com/CESSProject/cess-go-sdk/utils" "github.com/centrifuge/go-substrate-rpc-client/v4/types" "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" ) +// QueryCurrencyReward query the currency rewards +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - string: currency rewards +// - error: error message +func (c *ChainClient) QueryCurrencyReward(block int32) (string, error) { + defer func() { + if err := recover(); err != nil { + log.Println(utils.RecoverError(err)) + } + }() + + var data types.U128 + + if !c.GetChainState() { + return "", ERR_RPC_CONNECTION + } + + key, err := types.CreateStorageKey(c.metadata, CessTreasury, CurrencyReward) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), CessTreasury, CurrencyReward, err) + return "", 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(), CessTreasury, CurrencyReward, err) + c.SetChainState(false) + return "", err + } + if !ok { + return "0", ERR_RPC_EMPTY_VALUE + } + if data.String() == "" { + return "0", nil + } + return data.String(), nil + } + + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), CessTreasury, CurrencyReward, err) + c.SetChainState(false) + return "", err + } + + 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(), CessTreasury, CurrencyReward, err) + c.SetChainState(false) + return "", err + } + if !ok { + return "0", ERR_RPC_EMPTY_VALUE + } + + if data.String() == "" { + return "0", nil + } + return data.String(), nil +} + +// QueryEraReward query the rewards in era +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - string: rewards in era +// - error: error message +func (c *ChainClient) QueryEraReward(block int32) (string, error) { + defer func() { + if err := recover(); err != nil { + log.Println(utils.RecoverError(err)) + } + }() + + var data types.U128 + + if !c.GetChainState() { + return "", ERR_RPC_CONNECTION + } + + key, err := types.CreateStorageKey(c.metadata, CessTreasury, EraReward) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), CessTreasury, EraReward, err) + return "", 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(), CessTreasury, EraReward, err) + c.SetChainState(false) + return "", err + } + if !ok { + return "0", ERR_RPC_EMPTY_VALUE + } + if data.String() == "" { + return "0", nil + } + return data.String(), nil + } + + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), CessTreasury, EraReward, err) + c.SetChainState(false) + return "", err + } + + 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(), CessTreasury, EraReward, err) + c.SetChainState(false) + return "", err + } + if !ok { + return "0", ERR_RPC_EMPTY_VALUE + } + + if data.String() == "" { + return "0", nil + } + return data.String(), nil +} + +// QueryReserveReward query the reserve rewards +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - string: reserve rewards +// - error: error message +func (c *ChainClient) QueryReserveReward(block int32) (string, error) { + defer func() { + if err := recover(); err != nil { + log.Println(utils.RecoverError(err)) + } + }() + + var data types.U128 + + if !c.GetChainState() { + return "", ERR_RPC_CONNECTION + } + + key, err := types.CreateStorageKey(c.metadata, CessTreasury, ReserveReward) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), CessTreasury, ReserveReward, err) + return "", 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(), CessTreasury, ReserveReward, err) + c.SetChainState(false) + return "", err + } + if !ok { + return "0", ERR_RPC_EMPTY_VALUE + } + if data.String() == "" { + return "0", nil + } + return data.String(), nil + } + + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), CessTreasury, ReserveReward, err) + c.SetChainState(false) + return "", err + } + + 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(), CessTreasury, ReserveReward, err) + c.SetChainState(false) + return "", err + } + if !ok { + return "0", ERR_RPC_EMPTY_VALUE + } + + if data.String() == "" { + return "0", nil + } + return data.String(), nil +} + +// QueryRoundReward querie the rewards in each era +// - era: era id +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - string: rewards in an era +// - error: error message func (c *ChainClient) QueryRoundReward(era uint32, block int32) (string, error) { defer func() { if err := recover(); err != nil { @@ -24,10 +222,10 @@ func (c *ChainClient) QueryRoundReward(era uint32, block int32) (string, error) } }() - var data pattern.RoundRewardType + var data RoundRewardType if !c.GetChainState() { - return "", pattern.ERR_RPC_CONNECTION + return "", ERR_RPC_CONNECTION } param, err := codec.Encode(era) @@ -35,42 +233,44 @@ func (c *ChainClient) QueryRoundReward(era uint32, block int32) (string, error) return "", err } - key, err := types.CreateStorageKey(c.metadata, pattern.CessTreasury, pattern.RoundReward, param) + key, err := types.CreateStorageKey(c.metadata, CessTreasury, RoundReward, param) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.CessTreasury, pattern.RoundReward, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), CessTreasury, RoundReward, err) return "", 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(), pattern.CessTreasury, pattern.RoundReward, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), CessTreasury, RoundReward, err) c.SetChainState(false) return "", err } if !ok { - return "", pattern.ERR_RPC_EMPTY_VALUE + return "0", ERR_RPC_EMPTY_VALUE } return data.TotalReward.String(), nil } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), pattern.CessTreasury, pattern.RoundReward, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), CessTreasury, RoundReward, err) c.SetChainState(false) return "", err } 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(), pattern.CessTreasury, pattern.RoundReward, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), CessTreasury, RoundReward, err) c.SetChainState(false) return "", err } if !ok { - return "", pattern.ERR_RPC_EMPTY_VALUE + return "0", ERR_RPC_EMPTY_VALUE } + if data.TotalReward.String() == "" { + return "0", nil + } return data.TotalReward.String(), nil } diff --git a/core/pattern/pattern.go b/core/pattern/pattern.go index 87bc325..b9afffd 100755 --- a/core/pattern/pattern.go +++ b/core/pattern/pattern.go @@ -1,610 +1,610 @@ -// /* -// Copyright (C) CESS. All rights reserved. -// Copyright (C) Cumulus Encrypted Storage System. All rights reserved. +/* + Copyright (C) CESS. All rights reserved. + Copyright (C) Cumulus Encrypted Storage System. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 -// */ + SPDX-License-Identifier: Apache-2.0 +*/ package pattern -// import ( -// "time" - -// "github.com/centrifuge/go-substrate-rpc-client/v4/types" -// "github.com/pkg/errors" -// ) - -// // DOT is "." character -// const DOT = "." - -// // Unit precision of CESS token -// const TokenPrecision_CESS = "000000000000000000" - -// const StakingStakePerTiB = 4000 - -// const BlockIntervalSec = 6 - -// // BlockInterval is the time interval for generating blocks, in seconds -// const BlockInterval = time.Second * time.Duration(BlockIntervalSec) - -// const MaxSubmitedIdleFileMeta = 30 - -// const PublicDeoss = "http://deoss-pub-gateway.cess.cloud/" -// const PublicDeossAccount = "cXhwBytXqrZLr1qM5NHJhCzEMckSTzNKw17ci2aHft6ETSQm9" - -// // Pallets -// const ( -// // -// AUDIT = "Audit" -// // OSS is a module about DeOSS -// OSS = "Oss" -// // FILEBANK is a module about data metadata, bucket info, etc. -// FILEBANK = "FileBank" -// // TEEWOEKER is a module about TEE -// TEEWORKER = "TeeWorker" -// // SMINER is a module about storage miners -// SMINER = "Sminer" -// // STAKING is a module about staking -// STAKING = "Staking" -// // SMINER is a module about storage miners -// STORAGEHANDLER = "StorageHandler" -// // BALANCES is a module about the balances -// BALANCES = "Balances" -// // SYSTEM is a module about the system -// SYSTEM = "System" -// // EVM is a module about the evm contract -// EVM = "EVM" -// // -// CessTreasury = "CessTreasury" -// ) - -// // Chain state -// const ( -// //AUDIT -// UNVERIFYPROOF = "UnverifyProof" -// CHALLENGEDURATION = "ChallengeDuration" -// CHALLENGEVERIFYDURATION = "VerifyDuration" -// CHALLENGESNAPSHOT = "ChallengeSnapShot" -// UNVERIFYIDLEPROOF = "UnverifyIdleProof" -// UNVERIFYSERVICEPROOF = "UnverifyServiceProof" - -// // OSS -// // OSS -// AUTHORITYLIST = "AuthorityList" - -// // SMINER -// ALLMINER = "AllMiner" -// MINERITEMS = "MinerItems" -// REWARDMAP = "RewardMap" -// EXPENDERS = "Expenders" -// RESTORALTARGETINFO = "RestoralTarget" -// STAKINGSTARTBLOCK = "StakingStartBlock" -// CompleteSnapShot = "CompleteSnapShot" - -// // TEEWORKER -// TEEWorkers = "Workers" -// TEEMasterPubkey = "MasterPubkey" -// TEEEndpoints = "Endpoints" -// TEEWorkerAddedAt = "WorkerAddedAt" - -// // FILEBANK -// FILE = "File" -// BUCKET = "Bucket" -// BUCKETLIST = "UserBucketList" -// DEALMAP = "DealMap" -// FILLERMAP = "FillerMap" -// PENDINGREPLACE = "PendingReplacements" -// RESTORALORDER = "RestoralOrder" - -// // STAKING -// COUNTERFORVALIDATORS = "CounterForValidators" -// CounterForNominators = "CounterForNominators" -// ErasTotalStake = "ErasTotalStake" -// CurrentEra = "CurrentEra" -// ErasStakers = "ErasStakers" -// ErasRewardPoints = "ErasRewardPoints" -// Nominators = "Nominators" -// Bonded = "Bonded" -// Validators = "Validators" -// ErasValidatorReward = "ErasValidatorReward" -// ValidatorCount = "ValidatorCount" - -// // STORAGE_HANDLER -// USERSPACEINFO = "UserOwnedSpace" -// UNITPRICE = "UnitPrice" -// TOTALIDLESPACE = "TotalIdleSpace" -// TOTALSERVICESPACE = "TotalServiceSpace" -// PURCHASEDSPACE = "PurchasedSpace" - -// // BALANCES -// TOTALISSUANCE = "TotalIssuance" - -// // SYSTEM -// ACCOUNT = "Account" -// EVENTS = "Events" - -// // CessTreasury -// RoundReward = "RoundReward" -// ) - -// // Extrinsics -// const ( -// //AUDIT -// TX_AUDIT_SUBMITPROOF = AUDIT + DOT + "submit_proof" -// TX_AUDIT_SUBMITIDLEPROOF = AUDIT + DOT + "submit_idle_proof" -// TX_AUDIT_SUBMITSERVICEPROOF = AUDIT + DOT + "submit_service_proof" -// TX_AUDIT_SUBMITIDLEPROOFRESULT = AUDIT + DOT + "submit_verify_idle_result" -// TX_AUDIT_SUBMITSERVICEPROOFRESULT = AUDIT + DOT + "submit_verify_service_result" - -// // OSS -// TX_OSS_REGISTER = OSS + DOT + "register" -// TX_OSS_UPDATE = OSS + DOT + "update" -// TX_OSS_DESTROY = OSS + DOT + "destroy" -// TX_OSS_AUTHORIZE = OSS + DOT + "authorize" -// TX_OSS_UNAUTHORIZE = OSS + DOT + "cancel_authorize" - -// // SMINER -// TX_SMINER_REGISTER = SMINER + DOT + "regnstk" -// TX_SMINER_REGISTERASSIGNSTAKING = SMINER + DOT + "regnstk_assign_staking" -// TX_SMINER_INCREASESTAKES = SMINER + DOT + "increase_collateral" -// TX_SMINER_UPDATEPEERID = SMINER + DOT + "update_peer_id" -// TX_SMINER_UPDATEINCOME = SMINER + DOT + "update_beneficiary" -// TX_SMINER_CLAIMREWARD = SMINER + DOT + "receive_reward" -// TX_SMINER_MINEREXITPREP = SMINER + DOT + "miner_exit_prep" -// TX_SMINER_WITHDRAW = SMINER + DOT + "miner_withdraw" -// TX_SMINER_REGISTERPOISKEY = SMINER + DOT + "register_pois_key" -// TX_SMINER_INCREASEDECSPACE = SMINER + DOT + "increase_declaration_space" - -// // FILEBANK -// TX_FILEBANK_PUTBUCKET = FILEBANK + DOT + "create_bucket" -// TX_FILEBANK_DELBUCKET = FILEBANK + DOT + "delete_bucket" -// TX_FILEBANK_DELFILE = FILEBANK + DOT + "delete_file" -// TX_FILEBANK_UPLOADDEC = FILEBANK + DOT + "upload_declaration" -// TX_FILEBANK_FILEREPORT = FILEBANK + DOT + "transfer_report" -// TX_FILEBANK_GENRESTOREORDER = FILEBANK + DOT + "generate_restoral_order" -// 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" -// TX_FILEBANK_CERTIDLESPACE = FILEBANK + DOT + "cert_idle_space" -// TX_FILEBANK_REPLACEIDLESPACE = FILEBANK + DOT + "replace_idle_space" -// TX_FILEBANK_CALCULATEREPORT = FILEBANK + DOT + "calculate_report" - -// // STORAGE_HANDLER -// TX_STORAGE_BUYSPACE = STORAGEHANDLER + DOT + "buy_space" -// TX_STORAGE_EXPANSIONSPACE = STORAGEHANDLER + DOT + "expansion_space" -// TX_STORAGE_RENEWALSPACE = STORAGEHANDLER + DOT + "renewal_space" - -// // BALANCES -// TX_BALANCES_FORCETRANSFER = "Balances" + DOT + "transfer" - -// // EVM -// TX_EVM_CALL = EVM + DOT + "call" -// ) - -// // RPC Call -// const ( -// // System -// RPC_SYS_Properties = "system_properties" -// RPC_SYS_SyncState = "system_syncState" -// RPC_SYS_Version = "system_version" -// RPC_SYS_Chain = "system_chain" - -// //Net -// RPC_NET_Listening = "net_listening" -// ) - -// const ( -// Active = iota -// Calculate -// Missing -// Recovery -// ) - -// const ( -// MINER_STATE_POSITIVE = "positive" -// MINER_STATE_FROZEN = "frozen" -// MINER_STATE_EXIT = "exit" -// MINER_STATE_LOCK = "lock" -// MINER_STATE_OFFLINE = "offline" -// ) - -// // 0:Full 1:Verifier 2:Marker -// const ( -// TeeType_Full uint8 = 0 -// TeeType_Verifier uint8 = 1 -// TeeType_Marker uint8 = 2 -// ) - -// const ( -// ERR_Failed = "failed" -// ERR_Timeout = "timeout" -// ERR_Empty = "empty" -// ) - -// const ( -// MinBucketNameLength = 3 -// MaxBucketNameLength = 63 -// MaxDomainNameLength = 50 -// ) - -// // byte size -// const ( -// SIZE_1KiB = 1024 -// SIZE_1MiB = 1024 * SIZE_1KiB -// SIZE_1GiB = 1024 * SIZE_1MiB -// SIZE_1TiB = 1024 * SIZE_1GiB -// ) - -// const ( -// SegmentSize = 32 * SIZE_1MiB -// FragmentSize = 8 * SIZE_1MiB -// DataShards = 4 -// ParShards = 8 -// ) - -// var ( -// ERR_RPC_CONNECTION = errors.New("rpc err: connection failed") -// ERR_RPC_IP_FORMAT = errors.New("unsupported ip format") -// ERR_RPC_TIMEOUT = errors.New("timeout") -// ERR_RPC_EMPTY_VALUE = errors.New("empty") -// ERR_RPC_PRIORITYTOOLOW = "Priority is too low" -// ) - -// const ( -// FileHashLen = 64 -// RandomLen = 20 -// PeerIdPublicKeyLen = 38 -// PoISKeyLen = 256 -// TeeSignatureLen = 256 -// AccumulatorLen = 256 -// SpaceChallengeParamLen = 8 -// BloomFilterLen = 256 -// MaxSegmentNum = 1000 -// WorkerPublicKeyLen = 32 -// MasterPublicKeyLen = 32 -// EcdhPublicKeyLen = 32 -// TeeSigLen = 64 -// ) - -// type FileHash [FileHashLen]types.U8 -// type Random [RandomLen]types.U8 -// type PeerId [PeerIdPublicKeyLen]types.U8 -// type PoISKey_G [PoISKeyLen]types.U8 -// type PoISKey_N [PoISKeyLen]types.U8 -// type TeeSignature [TeeSignatureLen]types.U8 -// type Accumulator [AccumulatorLen]types.U8 -// type SpaceChallengeParam [SpaceChallengeParamLen]types.U64 -// type BloomFilter [BloomFilterLen]types.U64 -// type WorkerPublicKey [WorkerPublicKeyLen]types.U8 -// type MasterPublicKey [MasterPublicKeyLen]types.U8 -// type EcdhPublicKey [EcdhPublicKeyLen]types.U8 -// type TeeSig [TeeSigLen]types.U8 - -// type SysProperties struct { -// Ss58Format types.Bytes -// TokenDecimals types.U8 -// TokenSymbol types.Text -// SS58Prefix types.U32 -// } - -// type SysSyncState struct { -// StartingBlock types.U32 -// CurrentBlock types.U32 -// HighestBlock types.U32 -// } - -// type OssInfo struct { -// Peerid PeerId -// Domain types.Bytes -// } - -// type BucketInfo struct { -// ObjectsList []FileHash -// Authority []types.AccountID -// } - -// type MinerInfo struct { -// BeneficiaryAccount types.AccountID -// StakingAccount types.AccountID -// PeerId PeerId -// Collaterals types.U128 -// Debt types.U128 -// State types.Bytes // positive, exit, frozen, lock -// DeclarationSpace types.U128 -// IdleSpace types.U128 -// ServiceSpace types.U128 -// LockSpace types.U128 -// SpaceProofInfo types.Option[SpaceProofInfo] -// ServiceBloomFilter BloomFilter -// TeeSig TeeSig -// } - -// type SpaceProofInfo struct { -// Miner types.AccountID -// Front types.U64 -// Rear types.U64 -// PoisKey PoISKeyInfo -// Accumulator Accumulator -// } - -// type MinerReward struct { -// TotalReward types.U128 -// RewardIssued types.U128 -// OrderList []RewardOrder -// } - -// type RewardOrder struct { -// ReceiveCount types.U8 -// MaxCount types.U8 -// Atonce types.Bool -// OrderReward types.U128 -// EachAmount types.U128 -// LastReceiveBlock types.U32 -// } - -// type FileMetadata struct { -// SegmentList []SegmentInfo -// Owner []UserBrief -// FileSize types.U128 -// Completion types.U32 -// State types.U8 -// } - -// type SegmentInfo struct { -// Hash FileHash -// FragmentList []FragmentInfo -// } - -// type UserBrief struct { -// User types.AccountID -// FileName types.Bytes -// BucketName types.Bytes -// } - -// type FragmentInfo struct { -// Hash FileHash -// Avail types.Bool -// Tag types.Option[types.U32] -// Miner types.AccountID -// } - -// type StorageOrder struct { -// FileSize types.U128 -// SegmentList []SegmentList -// User UserBrief -// CompleteList []CompleteInfo -// } - -// type SegmentList struct { -// SegmentHash FileHash -// FragmentHash []FileHash -// } - -// type CompleteInfo struct { -// Index types.U8 -// Miner types.AccountID -// } - -// type UserSpaceInfo struct { -// TotalSpace types.U128 -// UsedSpace types.U128 -// LockedSpace types.U128 -// RemainingSpace types.U128 -// Start types.U32 -// Deadline types.U32 -// State types.Bytes -// } - -// type ChallengeSnapShot struct { -// MinerSnapshot MinerSnapShot -// ChallengeElement ChallengeElement -// ProveInfo ProveInfo -// } - -// type ProveInfo struct { -// Assign types.U8 -// IdleProve types.Option[IdleProveInfo] -// ServiceProve types.Option[ServiceProveInfo] -// } - -// type ChallengeElement struct { -// Start types.U32 -// IdleSlip types.U32 -// ServiceSlip types.U32 -// VerifySlip types.U32 -// SpaceParam SpaceChallengeParam -// ServiceParam QElement -// } - -// type QElement struct { -// Index []types.U32 -// Value []Random -// } - -// type MinerSnapShot struct { -// IdleSpace types.U128 -// ServiceSpace types.U128 -// ServiceBloomFilter BloomFilter -// SpaceProofInfo SpaceProofInfo -// TeeSig TeeSig -// } - -// type IdleProveInfo struct { -// TeePubkey WorkerPublicKey -// IdleProve types.Bytes -// VerifyResult types.Option[bool] -// } - -// type ServiceProveInfo struct { -// TeePubkey WorkerPublicKey -// ServiceProve types.Bytes -// VerifyResult types.Option[bool] -// } - -// type TeeWorkerInfo struct { -// Pubkey WorkerPublicKey -// EcdhPubkey EcdhPublicKey -// Version types.U32 -// LastUpdated types.U64 -// StashAccount types.Option[types.AccountID] -// AttestationProvider types.Option[types.U8] -// ConfidenceLevel types.U8 -// Features []types.U32 -// Role types.U8 // 0:Full 1:Verifier 2:Marker -// } - -// type RestoralOrderInfo struct { -// Count types.U32 -// Miner types.AccountID -// OriginMiner types.AccountID -// FragmentHash FileHash -// FileHash FileHash -// GenBlock types.U32 -// Deadline types.U32 -// } - -// type RestoralTargetInfo struct { -// Miner types.AccountID -// ServiceSpace types.U128 -// RestoredSpace types.U128 -// CoolingBlock types.U32 -// } - -// type ExpendersInfo struct { -// K types.U64 -// N types.U64 -// D types.U64 -// } - -// type PoISKeyInfo struct { -// G PoISKey_G -// N PoISKey_N -// } - -// type IdleSignInfo struct { -// Miner types.AccountID -// Rear types.U64 -// Front types.U64 -// Accumulator Accumulator -// LastOperationBlock types.U32 -// PoisKey PoISKeyInfo -// } -// type TagSigInfo struct { -// Miner types.AccountID -// Digest []DigestInfo -// Filehash FileHash -// } - -// type DigestInfo struct { -// Fragment FileHash -// TeePubkey WorkerPublicKey -// } - -// type StakingExposure struct { -// Total types.U128 -// Own types.U128 -// Others []OtherStakingExposure -// } - -// type OtherStakingExposure struct { -// Who types.AccountID -// Value types.U128 -// } - -// type StakingEraRewardPoints struct { -// Total types.U32 -// Individual []Individual -// } - -// type Individual struct { -// Acc types.AccountID -// Reward types.U32 -// } - -// type StakingNominations struct { -// Targets []types.AccountID -// SubmittedIn types.U32 -// Suppressed types.Bool -// } - -// type StakingValidatorPrefs struct { -// Commission types.U32 -// Blocked types.Bool -// } - -// type CompleteSnapShotType struct { -// MinerCount types.U32 -// TotalPower types.U128 -// } - -// type RoundRewardType struct { -// TotalReward types.U128 -// OtherReward types.U128 -// } - -// // --------------------customer----------------- -// type IdleFileMeta struct { -// BlockNum uint32 -// MinerAcc []byte -// Hash string -// } - -// type UserSpaceSt struct { -// TotalSpace string -// UsedSpace string -// LockedSpace string -// RemainingSpace string -// State string -// Start uint32 -// Deadline uint32 -// } - -// type NetSnapshot struct { -// Start uint32 -// Life uint32 -// Total_reward string -// Total_idle_space string -// Total_service_space string -// Random_index_list []uint32 -// Random [][]byte -// } - -// type MinerSnapshot struct { -// Miner string -// Idle_space string -// Service_space string -// } - -// type TeeInfo struct { -// Pubkey string -// EcdhPubkey string -// Version uint32 -// LastUpdated uint64 -// StashAccount string -// AttestationProvider uint8 -// ConfidenceLevel uint8 -// Features []uint32 -// WorkerRole uint8 // 0:Full 1:Verifier 2:Marker -// } - -// type RewardsType struct { -// Total string -// Claimed string -// } - -// type SegmentDataInfo struct { -// SegmentHash string -// FragmentHash []string -// } - -// type UserInfo struct { -// UserAccount string -// FileName string -// BucketName string -// FileSize uint64 -// } - -// type AccessInfo struct { -// r types.H160 -// c []types.H160 -// } +import ( + "time" + + "github.com/centrifuge/go-substrate-rpc-client/v4/types" + "github.com/pkg/errors" +) + +// DOT is "." character +const DOT = "." + +// Unit precision of CESS token +const TokenPrecision_CESS = "000000000000000000" + +const StakingStakePerTiB = 4000 + +const BlockIntervalSec = 6 + +// BlockInterval is the time interval for generating blocks, in seconds +const BlockInterval = time.Second * time.Duration(BlockIntervalSec) + +const MaxSubmitedIdleFileMeta = 30 + +const PublicDeoss = "http://deoss-pub-gateway.cess.cloud/" +const PublicDeossAccount = "cXhwBytXqrZLr1qM5NHJhCzEMckSTzNKw17ci2aHft6ETSQm9" + +// Pallets +const ( + // + AUDIT = "Audit" + // OSS is a module about DeOSS + OSS = "Oss" + // FILEBANK is a module about data metadata, bucket info, etc. + FILEBANK = "FileBank" + // TEEWOEKER is a module about TEE + TEEWORKER = "TeeWorker" + // SMINER is a module about storage miners + SMINER = "Sminer" + // STAKING is a module about staking + STAKING = "Staking" + // SMINER is a module about storage miners + STORAGEHANDLER = "StorageHandler" + // BALANCES is a module about the balances + BALANCES = "Balances" + // SYSTEM is a module about the system + SYSTEM = "System" + // EVM is a module about the evm contract + EVM = "EVM" + // + CessTreasury = "CessTreasury" +) + +// Chain state +const ( + //AUDIT + UNVERIFYPROOF = "UnverifyProof" + CHALLENGEDURATION = "ChallengeDuration" + CHALLENGEVERIFYDURATION = "VerifyDuration" + CHALLENGESNAPSHOT = "ChallengeSnapShot" + UNVERIFYIDLEPROOF = "UnverifyIdleProof" + UNVERIFYSERVICEPROOF = "UnverifyServiceProof" + + // OSS + // OSS + AUTHORITYLIST = "AuthorityList" + + // SMINER + ALLMINER = "AllMiner" + MINERITEMS = "MinerItems" + REWARDMAP = "RewardMap" + EXPENDERS = "Expenders" + RESTORALTARGETINFO = "RestoralTarget" + STAKINGSTARTBLOCK = "StakingStartBlock" + CompleteSnapShot = "CompleteSnapShot" + + // TEEWORKER + TEEWorkers = "Workers" + TEEMasterPubkey = "MasterPubkey" + TEEEndpoints = "Endpoints" + TEEWorkerAddedAt = "WorkerAddedAt" + + // FILEBANK + FILE = "File" + BUCKET = "Bucket" + BUCKETLIST = "UserBucketList" + DEALMAP = "DealMap" + FILLERMAP = "FillerMap" + PENDINGREPLACE = "PendingReplacements" + RESTORALORDER = "RestoralOrder" + + // STAKING + COUNTERFORVALIDATORS = "CounterForValidators" + CounterForNominators = "CounterForNominators" + ErasTotalStake = "ErasTotalStake" + CurrentEra = "CurrentEra" + ErasStakers = "ErasStakers" + ErasRewardPoints = "ErasRewardPoints" + Nominators = "Nominators" + Bonded = "Bonded" + Validators = "Validators" + ErasValidatorReward = "ErasValidatorReward" + ValidatorCount = "ValidatorCount" + + // STORAGE_HANDLER + USERSPACEINFO = "UserOwnedSpace" + UNITPRICE = "UnitPrice" + TOTALIDLESPACE = "TotalIdleSpace" + TOTALSERVICESPACE = "TotalServiceSpace" + PURCHASEDSPACE = "PurchasedSpace" + + // BALANCES + TOTALISSUANCE = "TotalIssuance" + + // SYSTEM + ACCOUNT = "Account" + EVENTS = "Events" + + // CessTreasury + RoundReward = "RoundReward" +) + +// Extrinsics +const ( + //AUDIT + TX_AUDIT_SUBMITPROOF = AUDIT + DOT + "submit_proof" + TX_AUDIT_SUBMITIDLEPROOF = AUDIT + DOT + "submit_idle_proof" + TX_AUDIT_SUBMITSERVICEPROOF = AUDIT + DOT + "submit_service_proof" + TX_AUDIT_SUBMITIDLEPROOFRESULT = AUDIT + DOT + "submit_verify_idle_result" + TX_AUDIT_SUBMITSERVICEPROOFRESULT = AUDIT + DOT + "submit_verify_service_result" + + // OSS + TX_OSS_REGISTER = OSS + DOT + "register" + TX_OSS_UPDATE = OSS + DOT + "update" + TX_OSS_DESTROY = OSS + DOT + "destroy" + TX_OSS_AUTHORIZE = OSS + DOT + "authorize" + TX_OSS_UNAUTHORIZE = OSS + DOT + "cancel_authorize" + + // SMINER + TX_SMINER_REGISTER = SMINER + DOT + "regnstk" + TX_SMINER_REGISTERASSIGNSTAKING = SMINER + DOT + "regnstk_assign_staking" + TX_SMINER_INCREASESTAKES = SMINER + DOT + "increase_collateral" + TX_SMINER_UPDATEPEERID = SMINER + DOT + "update_peer_id" + TX_SMINER_UPDATEINCOME = SMINER + DOT + "update_beneficiary" + TX_SMINER_CLAIMREWARD = SMINER + DOT + "receive_reward" + TX_SMINER_MINEREXITPREP = SMINER + DOT + "miner_exit_prep" + TX_SMINER_WITHDRAW = SMINER + DOT + "miner_withdraw" + TX_SMINER_REGISTERPOISKEY = SMINER + DOT + "register_pois_key" + TX_SMINER_INCREASEDECSPACE = SMINER + DOT + "increase_declaration_space" + + // FILEBANK + TX_FILEBANK_PUTBUCKET = FILEBANK + DOT + "create_bucket" + TX_FILEBANK_DELBUCKET = FILEBANK + DOT + "delete_bucket" + TX_FILEBANK_DELFILE = FILEBANK + DOT + "delete_file" + TX_FILEBANK_UPLOADDEC = FILEBANK + DOT + "upload_declaration" + TX_FILEBANK_FILEREPORT = FILEBANK + DOT + "transfer_report" + TX_FILEBANK_GENRESTOREORDER = FILEBANK + DOT + "generate_restoral_order" + 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" + TX_FILEBANK_CERTIDLESPACE = FILEBANK + DOT + "cert_idle_space" + TX_FILEBANK_REPLACEIDLESPACE = FILEBANK + DOT + "replace_idle_space" + TX_FILEBANK_CALCULATEREPORT = FILEBANK + DOT + "calculate_report" + + // STORAGE_HANDLER + TX_STORAGE_BUYSPACE = STORAGEHANDLER + DOT + "buy_space" + TX_STORAGE_EXPANSIONSPACE = STORAGEHANDLER + DOT + "expansion_space" + TX_STORAGE_RENEWALSPACE = STORAGEHANDLER + DOT + "renewal_space" + + // BALANCES + TX_BALANCES_FORCETRANSFER = "Balances" + DOT + "transfer" + + // EVM + TX_EVM_CALL = EVM + DOT + "call" +) + +// RPC Call +const ( + // System + RPC_SYS_Properties = "system_properties" + RPC_SYS_SyncState = "system_syncState" + RPC_SYS_Version = "system_version" + RPC_SYS_Chain = "system_chain" + + //Net + RPC_NET_Listening = "net_listening" +) + +const ( + Active = iota + Calculate + Missing + Recovery +) + +const ( + MINER_STATE_POSITIVE = "positive" + MINER_STATE_FROZEN = "frozen" + MINER_STATE_EXIT = "exit" + MINER_STATE_LOCK = "lock" + MINER_STATE_OFFLINE = "offline" +) + +// 0:Full 1:Verifier 2:Marker +const ( + TeeType_Full uint8 = 0 + TeeType_Verifier uint8 = 1 + TeeType_Marker uint8 = 2 +) + +const ( + ERR_Failed = "failed" + ERR_Timeout = "timeout" + ERR_Empty = "empty" +) + +const ( + MinBucketNameLength = 3 + MaxBucketNameLength = 63 + MaxDomainNameLength = 50 +) + +// byte size +const ( + SIZE_1KiB = 1024 + SIZE_1MiB = 1024 * SIZE_1KiB + SIZE_1GiB = 1024 * SIZE_1MiB + SIZE_1TiB = 1024 * SIZE_1GiB +) + +const ( + SegmentSize = 32 * SIZE_1MiB + FragmentSize = 8 * SIZE_1MiB + DataShards = 4 + ParShards = 8 +) + +var ( + ERR_RPC_CONNECTION = errors.New("rpc err: connection failed") + ERR_RPC_IP_FORMAT = errors.New("unsupported ip format") + ERR_RPC_TIMEOUT = errors.New("timeout") + ERR_RPC_EMPTY_VALUE = errors.New("empty") + ERR_RPC_PRIORITYTOOLOW = "Priority is too low" +) + +const ( + FileHashLen = 64 + RandomLen = 20 + PeerIdPublicKeyLen = 38 + PoISKeyLen = 256 + TeeSignatureLen = 256 + AccumulatorLen = 256 + SpaceChallengeParamLen = 8 + BloomFilterLen = 256 + MaxSegmentNum = 1000 + WorkerPublicKeyLen = 32 + MasterPublicKeyLen = 32 + EcdhPublicKeyLen = 32 + TeeSigLen = 64 +) + +type FileHash [FileHashLen]types.U8 +type Random [RandomLen]types.U8 +type PeerId [PeerIdPublicKeyLen]types.U8 +type PoISKey_G [PoISKeyLen]types.U8 +type PoISKey_N [PoISKeyLen]types.U8 +type TeeSignature [TeeSignatureLen]types.U8 +type Accumulator [AccumulatorLen]types.U8 +type SpaceChallengeParam [SpaceChallengeParamLen]types.U64 +type BloomFilter [BloomFilterLen]types.U64 +type WorkerPublicKey [WorkerPublicKeyLen]types.U8 +type MasterPublicKey [MasterPublicKeyLen]types.U8 +type EcdhPublicKey [EcdhPublicKeyLen]types.U8 +type TeeSig [TeeSigLen]types.U8 + +type SysProperties struct { + Ss58Format types.Bytes + TokenDecimals types.U8 + TokenSymbol types.Text + SS58Prefix types.U32 +} + +type SysSyncState struct { + StartingBlock types.U32 + CurrentBlock types.U32 + HighestBlock types.U32 +} + +type OssInfo struct { + Peerid PeerId + Domain types.Bytes +} + +type BucketInfo struct { + ObjectsList []FileHash + Authority []types.AccountID +} + +type MinerInfo struct { + BeneficiaryAccount types.AccountID + StakingAccount types.AccountID + PeerId PeerId + Collaterals types.U128 + Debt types.U128 + State types.Bytes // positive, exit, frozen, lock + DeclarationSpace types.U128 + IdleSpace types.U128 + ServiceSpace types.U128 + LockSpace types.U128 + SpaceProofInfo types.Option[SpaceProofInfo] + ServiceBloomFilter BloomFilter + TeeSig TeeSig +} + +type SpaceProofInfo struct { + Miner types.AccountID + Front types.U64 + Rear types.U64 + PoisKey PoISKeyInfo + Accumulator Accumulator +} + +type MinerReward struct { + TotalReward types.U128 + RewardIssued types.U128 + OrderList []RewardOrder +} + +type RewardOrder struct { + ReceiveCount types.U8 + MaxCount types.U8 + Atonce types.Bool + OrderReward types.U128 + EachAmount types.U128 + LastReceiveBlock types.U32 +} + +type FileMetadata struct { + SegmentList []SegmentInfo + Owner []UserBrief + FileSize types.U128 + Completion types.U32 + State types.U8 +} + +type SegmentInfo struct { + Hash FileHash + FragmentList []FragmentInfo +} + +type UserBrief struct { + User types.AccountID + FileName types.Bytes + BucketName types.Bytes +} + +type FragmentInfo struct { + Hash FileHash + Avail types.Bool + Tag types.Option[types.U32] + Miner types.AccountID +} + +type StorageOrder struct { + FileSize types.U128 + SegmentList []SegmentList + User UserBrief + CompleteList []CompleteInfo +} + +type SegmentList struct { + SegmentHash FileHash + FragmentHash []FileHash +} + +type CompleteInfo struct { + Index types.U8 + Miner types.AccountID +} + +type UserSpaceInfo struct { + TotalSpace types.U128 + UsedSpace types.U128 + LockedSpace types.U128 + RemainingSpace types.U128 + Start types.U32 + Deadline types.U32 + State types.Bytes +} + +type ChallengeSnapShot struct { + MinerSnapshot MinerSnapShot + ChallengeElement ChallengeElement + ProveInfo ProveInfo +} + +type ProveInfo struct { + Assign types.U8 + IdleProve types.Option[IdleProveInfo] + ServiceProve types.Option[ServiceProveInfo] +} + +type ChallengeElement struct { + Start types.U32 + IdleSlip types.U32 + ServiceSlip types.U32 + VerifySlip types.U32 + SpaceParam SpaceChallengeParam + ServiceParam QElement +} + +type QElement struct { + Index []types.U32 + Value []Random +} + +type MinerSnapShot struct { + IdleSpace types.U128 + ServiceSpace types.U128 + ServiceBloomFilter BloomFilter + SpaceProofInfo SpaceProofInfo + TeeSig TeeSig +} + +type IdleProveInfo struct { + TeePubkey WorkerPublicKey + IdleProve types.Bytes + VerifyResult types.Option[bool] +} + +type ServiceProveInfo struct { + TeePubkey WorkerPublicKey + ServiceProve types.Bytes + VerifyResult types.Option[bool] +} + +type TeeWorkerInfo struct { + Pubkey WorkerPublicKey + EcdhPubkey EcdhPublicKey + Version types.U32 + LastUpdated types.U64 + StashAccount types.Option[types.AccountID] + AttestationProvider types.Option[types.U8] + ConfidenceLevel types.U8 + Features []types.U32 + Role types.U8 // 0:Full 1:Verifier 2:Marker +} + +type RestoralOrderInfo struct { + Count types.U32 + Miner types.AccountID + OriginMiner types.AccountID + FragmentHash FileHash + FileHash FileHash + GenBlock types.U32 + Deadline types.U32 +} + +type RestoralTargetInfo struct { + Miner types.AccountID + ServiceSpace types.U128 + RestoredSpace types.U128 + CoolingBlock types.U32 +} + +type ExpendersInfo struct { + K types.U64 + N types.U64 + D types.U64 +} + +type PoISKeyInfo struct { + G PoISKey_G + N PoISKey_N +} + +type IdleSignInfo struct { + Miner types.AccountID + Rear types.U64 + Front types.U64 + Accumulator Accumulator + LastOperationBlock types.U32 + PoisKey PoISKeyInfo +} +type TagSigInfo struct { + Miner types.AccountID + Digest []DigestInfo + Filehash FileHash +} + +type DigestInfo struct { + Fragment FileHash + TeePubkey WorkerPublicKey +} + +type StakingExposure struct { + Total types.U128 + Own types.U128 + Others []OtherStakingExposure +} + +type OtherStakingExposure struct { + Who types.AccountID + Value types.U128 +} + +type StakingEraRewardPoints struct { + Total types.U32 + Individual []Individual +} + +type Individual struct { + Acc types.AccountID + Reward types.U32 +} + +type StakingNominations struct { + Targets []types.AccountID + SubmittedIn types.U32 + Suppressed types.Bool +} + +type StakingValidatorPrefs struct { + Commission types.U32 + Blocked types.Bool +} + +type CompleteSnapShotType struct { + MinerCount types.U32 + TotalPower types.U128 +} + +type RoundRewardType struct { + TotalReward types.U128 + OtherReward types.U128 +} + +// --------------------customer----------------- +type IdleFileMeta struct { + BlockNum uint32 + MinerAcc []byte + Hash string +} + +type UserSpaceSt struct { + TotalSpace string + UsedSpace string + LockedSpace string + RemainingSpace string + State string + Start uint32 + Deadline uint32 +} + +type NetSnapshot struct { + Start uint32 + Life uint32 + Total_reward string + Total_idle_space string + Total_service_space string + Random_index_list []uint32 + Random [][]byte +} + +type MinerSnapshot struct { + Miner string + Idle_space string + Service_space string +} + +type TeeInfo struct { + Pubkey string + EcdhPubkey string + Version uint32 + LastUpdated uint64 + StashAccount string + AttestationProvider uint8 + ConfidenceLevel uint8 + Features []uint32 + WorkerRole uint8 // 0:Full 1:Verifier 2:Marker +} + +type RewardsType struct { + Total string + Claimed string +} + +type SegmentDataInfo struct { + SegmentHash string + FragmentHash []string +} + +type UserInfo struct { + UserAccount string + FileName string + BucketName string + FileSize uint64 +} + +type AccessInfo struct { + r types.H160 + c []types.H160 +} From 32009e585cc98bf78c3f9b5becd3cc0f7a5f6200 Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Wed, 8 May 2024 20:47:45 +0800 Subject: [PATCH 05/18] update filebank --- chain/evm.go | 61 +- chain/fileBank.go | 1278 +++++++++++++++++++++++---------------- chain/pattern.go | 140 +++-- chain/treasury.go | 3 + core/pattern/pattern.go | 1212 ++++++++++++++++++------------------- 5 files changed, 1471 insertions(+), 1223 deletions(-) diff --git a/chain/evm.go b/chain/evm.go index 671b81d..c38515c 100644 --- a/chain/evm.go +++ b/chain/evm.go @@ -1,3 +1,10 @@ +/* + Copyright (C) CESS. All rights reserved. + Copyright (C) Cumulus Encrypted Storage System. All rights reserved. + + SPDX-License-Identifier: Apache-2.0 +*/ + package chain import ( @@ -6,13 +13,12 @@ import ( "strings" "time" - "github.com/CESSProject/cess-go-sdk/core/pattern" "github.com/CESSProject/cess-go-sdk/utils" "github.com/centrifuge/go-substrate-rpc-client/v4/types" "github.com/pkg/errors" ) -func (c *ChainClient) SendEvmCall(source types.H160, target types.H160, input types.Bytes, value types.U256, gasLimit types.U64, maxFeePerGas types.U256, accessList []pattern.AccessInfo) (string, error) { +func (c *ChainClient) SendEvmCall(source types.H160, target types.H160, input types.Bytes, value types.U256, gasLimit types.U64, maxFeePerGas types.U256, accessList []AccessInfo) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -22,12 +28,12 @@ func (c *ChainClient) SendEvmCall(source types.H160, target types.H160, input ty }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, fmt.Errorf("chainSDK.UploadDeclaration(): GetChainState(): %v", pattern.ERR_RPC_CONNECTION) + return blockhash, fmt.Errorf("chainSDK.UploadDeclaration(): GetChainState(): %v", ERR_RPC_CONNECTION) } var nonce types.Option[types.U256] @@ -36,35 +42,33 @@ func (c *ChainClient) SendEvmCall(source types.H160, target types.H160, input ty var maxPriorityFeePerGas types.Option[types.U256] maxPriorityFeePerGas.SetNone() - call, err := types.NewCall(c.metadata, pattern.TX_EVM_CALL, source, target, input, value, gasLimit, maxFeePerGas, maxPriorityFeePerGas, nonce, accessList) + call, err := types.NewCall(c.metadata, TX_EVM_Call, source, target, input, value, gasLimit, maxFeePerGas, maxPriorityFeePerGas, nonce, accessList) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.EVM, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_EVM_Call, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.EVM, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_EVM_Call, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.EVM, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_EVM_Call, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { keyStr, _ := utils.NumsToByteStr(key, map[string]bool{}) - return txhash, fmt.Errorf( + return blockhash, fmt.Errorf( "chain rpc.state.GetStorageLatest[%v]: %v", keyStr, - pattern.ERR_RPC_EMPTY_VALUE, + ERR_RPC_EMPTY_VALUE, ) } @@ -80,29 +84,28 @@ func (c *ChainClient) SendEvmCall(source types.H160, target types.H160, input ty err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.EVM, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_EVM_Call, err) + return blockhash, err } sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.EVM, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_EVM_Call, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.EVM, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_EVM_Call, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -114,13 +117,13 @@ func (c *ChainClient) SendEvmCall(source types.H160, target types.H160, input ty select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() - return txhash, err + blockhash = status.AsInBlock.Hex() + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } diff --git a/chain/fileBank.go b/chain/fileBank.go index c67cc10..8cbd415 100755 --- a/chain/fileBank.go +++ b/chain/fileBank.go @@ -15,24 +15,31 @@ import ( "strings" "time" - "github.com/CESSProject/cess-go-sdk/core/pattern" "github.com/CESSProject/cess-go-sdk/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) QueryBucketInfo(accountID []byte, bucketname string) (pattern.BucketInfo, error) { +// QueryBucket query user's bucket information +// - accountID: user account +// - bucketName: bucket name +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - BucketInfo: bucket info +// - error: error message +func (c *ChainClient) QueryBucket(accountID []byte, bucketName string, block int32) (BucketInfo, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data pattern.BucketInfo + var data BucketInfo if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + return data, ERR_RPC_CONNECTION } acc, err := types.NewAccountID(accountID) @@ -45,87 +52,123 @@ func (c *ChainClient) QueryBucketInfo(accountID []byte, bucketname string) (patt return data, errors.Wrap(err, "[EncodeToBytes]") } - name, err := codec.Encode(bucketname) + name, err := codec.Encode(bucketName) if err != nil { return data, errors.Wrap(err, "[Encode]") } - key, err := types.CreateStorageKey(c.metadata, pattern.FILEBANK, pattern.BUCKET, owner, name) + key, err := types.CreateStorageKey(c.metadata, FileBank, Bucket, owner, name) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.FILEBANK, pattern.BUCKET, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), FileBank, Bucket, err) return data, err } - - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + 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(), FileBank, Bucket, err) + c.SetChainState(false) + return data, err + } + if !ok { + return data, ERR_RPC_EMPTY_VALUE + } + return data, nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + return data, err + } + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.FILEBANK, pattern.BUCKET, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), FileBank, Bucket, err) c.SetChainState(false) return data, err } if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + return data, ERR_RPC_EMPTY_VALUE } return data, nil } -func (c *ChainClient) QueryAllBucket(accountID []byte) ([]types.Bytes, error) { +// QueryDealMap query file storage order +// - fid: file identification +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - StorageOrder: file storage order +// - error: error message +func (c *ChainClient) QueryDealMap(fid string, block int32) (StorageOrder, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data []types.Bytes + var ( + data StorageOrder + hash FileHash + ) - if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + if len(fid) != FileHashLen { + return data, errors.New("invalid filehash") } - acc, err := types.NewAccountID(accountID) - if err != nil { - return data, errors.Wrap(err, "[NewAccountID]") + for i := 0; i < len(hash); i++ { + hash[i] = types.U8(fid[i]) } - owner, err := codec.Encode(*acc) + param_hash, err := codec.Encode(hash) if err != nil { - return data, errors.Wrap(err, "[EncodeToBytes]") + return data, errors.Wrap(err, "[Encode]") + } + + if !c.GetChainState() { + return data, ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.FILEBANK, pattern.BUCKETLIST, owner) + key, err := types.CreateStorageKey(c.metadata, FileBank, DealMap, param_hash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.FILEBANK, pattern.BUCKETLIST, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), FileBank, DealMap, 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(), FileBank, DealMap, err) + c.SetChainState(false) + return data, err + } + if !ok { + return data, ERR_RPC_EMPTY_VALUE + } + return data, nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), FileBank, DealMap, err) c.SetChainState(false) return data, err } - - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.FILEBANK, pattern.BUCKETLIST, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), FileBank, DealMap, err) c.SetChainState(false) return data, err } if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + return data, ERR_RPC_EMPTY_VALUE } return data, nil } -func (c *ChainClient) QueryAllBucketString(accountID []byte) ([]string, error) { - bucketlist, err := c.QueryAllBucket(accountID) - if err != nil { - if err.Error() != pattern.ERR_Empty { - return nil, err - } - } - var buckets = make([]string, len(bucketlist)) - for i := 0; i < len(bucketlist); i++ { - buckets[i] = string(bucketlist[i]) - } - return buckets, nil -} - -func (c *ChainClient) QueryFileMetadata(fid string) (pattern.FileMetadata, error) { +// QueryFile query file metadata +// - fid: file identification +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - FileMetadata: file metadata +// - error: error message +func (c *ChainClient) QueryFile(fid string, block int32) (FileMetadata, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -133,15 +176,15 @@ func (c *ChainClient) QueryFileMetadata(fid string) (pattern.FileMetadata, error }() var ( - data pattern.FileMetadata - hash pattern.FileHash + data FileMetadata + hash FileHash ) if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + return data, ERR_RPC_CONNECTION } - if len(hash) != len(fid) { + if len(fid) != FileHashLen { return data, errors.New("invalid filehash") } @@ -149,31 +192,56 @@ func (c *ChainClient) QueryFileMetadata(fid string) (pattern.FileMetadata, error hash[i] = types.U8(fid[i]) } - b, err := codec.Encode(hash) + param_hash, err := codec.Encode(hash) if err != nil { return data, errors.Wrap(err, "[Encode]") } - key, err := types.CreateStorageKey(c.metadata, pattern.FILEBANK, pattern.FILE, b) + key, err := types.CreateStorageKey(c.metadata, FileBank, File, param_hash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.FILEBANK, pattern.FILE, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), FileBank, File, err) return data, err } - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + 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(), FileBank, File, err) + c.SetChainState(false) + return data, err + } + if !ok { + return data, ERR_RPC_EMPTY_VALUE + } + return data, nil + } + + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), FileBank, File, err) + c.SetChainState(false) + return data, err + } + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.FILEBANK, pattern.FILE, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), FileBank, File, err) c.SetChainState(false) return data, err } if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + return data, ERR_RPC_EMPTY_VALUE } return data, nil } -func (c *ChainClient) QueryFileMetadataByBlock(fid string, block uint64) (pattern.FileMetadata, error) { +// QueryRestoralOrder query file restoral order +// - fragmentHash: fragment hash +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - RestoralOrderInfo: restoral order info +// - error: error message +func (c *ChainClient) QueryRestoralOrder(fragmentHash string, block int32) (RestoralOrderInfo, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -181,206 +249,284 @@ func (c *ChainClient) QueryFileMetadataByBlock(fid string, block uint64) (patter }() var ( - data pattern.FileMetadata - hash pattern.FileHash + data RestoralOrderInfo + hash FileHash ) if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + return data, ERR_RPC_CONNECTION } - if len(hash) != len(fid) { - return data, errors.New("invalid filehash") + if len(fragmentHash) != FileHashLen { + return data, errors.New("invalid fragment hash") } for i := 0; i < len(hash); i++ { - hash[i] = types.U8(fid[i]) + hash[i] = types.U8(fragmentHash[i]) } - b, err := codec.Encode(hash) + param_hash, err := codec.Encode(hash) if err != nil { return data, errors.Wrap(err, "[Encode]") } - key, err := types.CreateStorageKey(c.metadata, pattern.FILEBANK, pattern.FILE, b) + key, err := types.CreateStorageKey(c.metadata, FileBank, RestoralOrder, param_hash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.FILEBANK, pattern.FILE, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), FileBank, RestoralOrder, err) return data, err } - blockhash, err := c.api.RPC.Chain.GetBlockHash(block) + 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(), FileBank, RestoralOrder, err) + c.SetChainState(false) + return data, err + } + if !ok { + return data, ERR_RPC_EMPTY_VALUE + } + return data, nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), pattern.FILEBANK, pattern.FILE, err) c.SetChainState(false) return data, err } - 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(), pattern.FILEBANK, pattern.FILE, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), FileBank, RestoralOrder, err) c.SetChainState(false) return data, err } if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + return data, ERR_RPC_EMPTY_VALUE } return data, nil } -func (c *ChainClient) QueryStorageOrder(fid string) (pattern.StorageOrder, error) { +// QueryAllRestoralOrder query all file restoral order +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - []RestoralOrderInfo: all restoral order info +// - error: error message +func (c *ChainClient) QueryAllRestoralOrder(block int32) ([]RestoralOrderInfo, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - - var ( - data pattern.StorageOrder - hash pattern.FileHash - ) - - if len(hash) != len(fid) { - return data, errors.New("invalid filehash") - } - - for i := 0; i < len(hash); i++ { - hash[i] = types.U8(fid[i]) - } - - b, err := codec.Encode(hash) - if err != nil { - return data, errors.Wrap(err, "[Encode]") - } + var result []RestoralOrderInfo if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + return nil, ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.FILEBANK, pattern.DEALMAP, b) + key := createPrefixedKey(FileBank, RestoralOrder) + keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.FILEBANK, pattern.DEALMAP, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), FileBank, RestoralOrder, err) c.SetChainState(false) - return data, err + return nil, err } - - 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(), pattern.FILEBANK, pattern.DEALMAP, err) - c.SetChainState(false) - return data, err + var set []types.StorageChangeSet + if block < 0 { + set, err = c.api.RPC.State.QueryStorageAtLatest(keys) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), FileBank, RestoralOrder, err) + c.SetChainState(false) + return nil, err + } + } else { + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + return nil, err + } + set, err = c.api.RPC.State.QueryStorageAt(keys, blockhash) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAt: %v", c.GetCurrentRpcAddr(), FileBank, RestoralOrder, err) + c.SetChainState(false) + return nil, err + } } - if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + + for _, elem := range set { + for _, change := range elem.Changes { + var data RestoralOrderInfo + if err := codec.Decode(change.StorageData, &data); err != nil { + continue + } + result = append(result, data) + } } - return data, nil + return result, nil } -func (c *ChainClient) QueryRestoralOrder(fragmentHash string) (pattern.RestoralOrderInfo, error) { +// QueryAllBucketName query user's all bucket names +// - accountID: user account +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - []string: all bucket names +// - error: error message +func (c *ChainClient) QueryAllBucketName(accountID []byte, block int32) ([]string, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var ( - data pattern.RestoralOrderInfo - hash pattern.FileHash - ) + var data []types.Bytes + var value []string if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + return nil, ERR_RPC_CONNECTION } - if len(hash) != len(fragmentHash) { - return data, errors.New("invalid root hash") + acc, err := types.NewAccountID(accountID) + if err != nil { + return nil, errors.Wrap(err, "[NewAccountID]") } - for i := 0; i < len(hash); i++ { - hash[i] = types.U8(fragmentHash[i]) + owner, err := codec.Encode(*acc) + if err != nil { + return nil, errors.Wrap(err, "[EncodeToBytes]") } - b, err := codec.Encode(hash) + key, err := types.CreateStorageKey(c.metadata, FileBank, UserBucketList, owner) if err != nil { - return data, errors.Wrap(err, "[Encode]") + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), FileBank, UserBucketList, err) + return nil, err } - - key, err := types.CreateStorageKey(c.metadata, pattern.FILEBANK, pattern.RESTORALORDER, b) + 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(), FileBank, UserBucketList, err) + c.SetChainState(false) + return nil, err + } + if !ok { + return []string{}, ERR_RPC_EMPTY_VALUE + } + for i := 0; i < len(data); i++ { + value[i] = string(data[i]) + } + return value, nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.FILEBANK, pattern.RESTORALORDER, err) c.SetChainState(false) - return data, err + return nil, err } - - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.FILEBANK, pattern.RESTORALORDER, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), FileBank, UserBucketList, err) c.SetChainState(false) - return data, err + return nil, err } if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + return []string{}, ERR_RPC_EMPTY_VALUE } - return data, nil + for i := 0; i < len(data); i++ { + value[i] = string(data[i]) + } + return value, nil } -func (c *ChainClient) QueryRestoralOrderList() ([]pattern.RestoralOrderInfo, error) { +// QueryAllUserFiles query user's all files +// - accountID: user account +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - []string: all file identification +// - error: error message +func (c *ChainClient) QueryAllUserFiles(accountID []byte, block int32) ([]string, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var result []pattern.RestoralOrderInfo + + var data []UserFileSliceInfo + var value []string if !c.GetChainState() { - return nil, pattern.ERR_RPC_CONNECTION + return nil, ERR_RPC_CONNECTION } - key := createPrefixedKey(pattern.FILEBANK, pattern.RESTORALORDER) - keys, err := c.api.RPC.State.GetKeysLatest(key) + acc, err := types.NewAccountID(accountID) + if err != nil { + return nil, errors.Wrap(err, "[NewAccountID]") + } + + owner, err := codec.Encode(*acc) + if err != nil { + return nil, errors.Wrap(err, "[EncodeToBytes]") + } + + key, err := types.CreateStorageKey(c.metadata, FileBank, UserHoldFileList, owner) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), FileBank, UserHoldFileList, err) + return nil, 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(), FileBank, UserHoldFileList, err) + c.SetChainState(false) + return nil, err + } + if !ok { + return []string{}, ERR_RPC_EMPTY_VALUE + } + for i := 0; i < len(data); i++ { + value[i] = string(data[i].Filehash[:]) + } + return value, nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), pattern.FILEBANK, pattern.RESTORALORDER, err) c.SetChainState(false) return nil, err } - - set, err := c.api.RPC.State.QueryStorageAtLatest(keys) + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), pattern.FILEBANK, pattern.RESTORALORDER, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), FileBank, UserHoldFileList, err) c.SetChainState(false) return nil, err } - - for _, elem := range set { - for _, change := range elem.Changes { - var data pattern.RestoralOrderInfo - if err := codec.Decode(change.StorageData, &data); err != nil { - continue - } - result = append(result, data) - } + if !ok { + return []string{}, ERR_RPC_EMPTY_VALUE } - return result, nil + for i := 0; i < len(data); i++ { + value[i] = string(data[i].Filehash[:]) + } + return value, nil } -func (c *ChainClient) GenerateStorageOrder( - roothash string, - segment []pattern.SegmentDataInfo, - owner []byte, - filename string, - buckname string, - filesize uint64, -) (string, error) { +// GenerateStorageOrder generate a file storage order +// - fid: file identification +// - 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) { var err error - var segmentList = make([]pattern.SegmentList, len(segment)) - var user pattern.UserBrief + var segmentList = make([]SegmentList, len(segment)) + var user UserBrief for i := 0; i < len(segment); i++ { hash := filepath.Base(segment[i].SegmentHash) for k := 0; k < len(hash); k++ { segmentList[i].SegmentHash[k] = types.U8(hash[k]) } - segmentList[i].FragmentHash = make([]pattern.FileHash, len(segment[i].FragmentHash)) + segmentList[i].FragmentHash = make([]FileHash, len(segment[i].FragmentHash)) for j := 0; j < len(segment[i].FragmentHash); j++ { hash := filepath.Base(segment[i].FragmentHash[j]) for k := 0; k < len(hash); k++ { @@ -396,10 +542,20 @@ func (c *ChainClient) GenerateStorageOrder( user.User = *acc user.BucketName = types.NewBytes([]byte(buckname)) user.FileName = types.NewBytes([]byte(filename)) - return c.UploadDeclaration(roothash, segmentList, user, filesize) + return c.UploadDeclaration(fid, segmentList, user, filesize) } -func (c *ChainClient) UploadDeclaration(filehash string, dealinfo []pattern.SegmentList, user pattern.UserBrief, filesize uint64) (string, error) { +// GenerateStorageOrder generate a file storage order +// - fid: file identification +// - segment: segment info +// - user: UserBrief +// - filename: file name +// - filesize: file size +// +// Return: +// - string: block hash +// - error: error message +func (c *ChainClient) UploadDeclaration(fid string, segment []SegmentList, user UserBrief, filesize uint64) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -409,52 +565,50 @@ func (c *ChainClient) UploadDeclaration(filehash string, dealinfo []pattern.Segm }() var ( - txhash string - hash pattern.FileHash + blockhash string + hash FileHash accountInfo types.AccountInfo ) - if len(filehash) != len(hash) { - return txhash, errors.New("invalid filehash") + if len(fid) != FileHashLen { + return blockhash, errors.New("invalid filehash") } if filesize <= 0 { - return txhash, errors.New("invalid filesize") + return blockhash, errors.New("invalid filesize") } for i := 0; i < len(hash); i++ { - hash[i] = types.U8(filehash[i]) + hash[i] = types.U8(fid[i]) } if !c.GetChainState() { - return txhash, fmt.Errorf("chainSDK.UploadDeclaration(): GetChainState(): %v", pattern.ERR_RPC_CONNECTION) + return blockhash, ERR_RPC_CONNECTION } - call, err := types.NewCall(c.metadata, pattern.TX_FILEBANK_UPLOADDEC, hash, dealinfo, user, types.NewU128(*new(big.Int).SetUint64(filesize))) + call, err := types.NewCall(c.metadata, TX_FileBank_UploadDeclaration, hash, segment, user, types.NewU128(*new(big.Int).SetUint64(filesize))) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_UPLOADDEC, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_FileBank_UploadDeclaration, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_UPLOADDEC, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_FileBank_UploadDeclaration, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_UPLOADDEC, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_UploadDeclaration, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { keyStr, _ := utils.NumsToByteStr(key, map[string]bool{}) - return txhash, fmt.Errorf( + return blockhash, fmt.Errorf( "chain rpc.state.GetStorageLatest[%v]: %v", keyStr, - pattern.ERR_RPC_EMPTY_VALUE, + ERR_RPC_EMPTY_VALUE, ) } @@ -471,9 +625,9 @@ func (c *ChainClient) UploadDeclaration(filehash string, dealinfo []pattern.Segm // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_UPLOADDEC, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_UploadDeclaration, err) c.SetChainState(false) - return txhash, err + return blockhash, err } <-c.txTicker.C @@ -481,22 +635,23 @@ func (c *ChainClient) UploadDeclaration(filehash string, dealinfo []pattern.Segm // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_UPLOADDEC, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_UploadDeclaration, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_UPLOADDEC, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_UploadDeclaration, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -508,19 +663,30 @@ func (c *ChainClient) UploadDeclaration(filehash string, dealinfo []pattern.Segm select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_FileBank_UploadDeclaration(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) CreateBucket(owner_pkey []byte, name string) (string, error) { +// CreateBucket create a bucket for owner +// - owner: bucket owner account +// - bucketName: bucket name +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - cannot create a bucket that already exists +// - if you are not the owner, the owner account must be authorised to you +func (c *ChainClient) CreateBucket(owner []byte, bucketName string) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -530,56 +696,41 @@ func (c *ChainClient) CreateBucket(owner_pkey []byte, name string) (string, erro }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - buckets, err := c.QueryAllBucket(owner_pkey) - if err != nil { - if err.Error() != pattern.ERR_Empty { - return txhash, errors.Wrap(err, "[QueryBucketList]") - } - } else { - for _, v := range buckets { - if utils.CompareSlice(v, []byte(name)) { - return "", nil - } - } - } - - acc, err := types.NewAccountID(owner_pkey) + acc, err := types.NewAccountID(owner) if err != nil { - return txhash, errors.Wrap(err, "[NewAccountID]") + return blockhash, errors.Wrap(err, "[NewAccountID]") } - call, err := types.NewCall(c.metadata, pattern.TX_FILEBANK_PUTBUCKET, *acc, types.NewBytes([]byte(name))) + call, err := types.NewCall(c.metadata, TX_FileBank_CreateBucket, *acc, types.NewBytes([]byte(bucketName))) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_PUTBUCKET, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_FileBank_CreateBucket, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_PUTBUCKET, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_FileBank_CreateBucket, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_PUTBUCKET, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_CreateBucket, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -595,9 +746,9 @@ func (c *ChainClient) CreateBucket(owner_pkey []byte, name string) (string, erro // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_PUTBUCKET, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_CreateBucket, err) c.SetChainState(false) - return txhash, err + return blockhash, err } <-c.txTicker.C @@ -605,9 +756,9 @@ func (c *ChainClient) CreateBucket(owner_pkey []byte, name string) (string, erro // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_PUTBUCKET, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_CreateBucket, err) c.SetChainState(false) - return txhash, err + return blockhash, err } defer sub.Unsubscribe() @@ -618,19 +769,29 @@ func (c *ChainClient) CreateBucket(owner_pkey []byte, name string) (string, erro select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_FileBank_CreateBucket(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) DeleteBucket(owner_pkey []byte, name string) (string, error) { +// DeleteBucket delete a bucket for owner +// - owner: bucket owner account +// - bucketName: bucket name +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - if you are not the owner, the owner account must be authorised to you +func (c *ChainClient) DeleteBucket(owner []byte, bucketName string) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -640,43 +801,41 @@ func (c *ChainClient) DeleteBucket(owner_pkey []byte, name string) (string, erro }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - acc, err := types.NewAccountID(owner_pkey) + acc, err := types.NewAccountID(owner) if err != nil { - return txhash, errors.Wrap(err, "[NewAccountID]") + return blockhash, errors.Wrap(err, "[NewAccountID]") } - call, err := types.NewCall(c.metadata, pattern.TX_FILEBANK_DELBUCKET, *acc, types.NewBytes([]byte(name))) + call, err := types.NewCall(c.metadata, TX_FileBank_DeleteBucket, *acc, types.NewBytes([]byte(bucketName))) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_DELBUCKET, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteBucket, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_DELBUCKET, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteBucket, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_DELBUCKET, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteBucket, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -692,9 +851,9 @@ func (c *ChainClient) DeleteBucket(owner_pkey []byte, name string) (string, erro // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_DELBUCKET, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteBucket, err) c.SetChainState(false) - return txhash, err + return blockhash, err } <-c.txTicker.C @@ -702,9 +861,9 @@ func (c *ChainClient) DeleteBucket(owner_pkey []byte, name string) (string, erro // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_DELBUCKET, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteBucket, err) c.SetChainState(false) - return txhash, err + return blockhash, err } defer sub.Unsubscribe() @@ -715,19 +874,29 @@ func (c *ChainClient) DeleteBucket(owner_pkey []byte, name string) (string, erro select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_FileBank_DeleteBucket(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) DeleteFile(puk []byte, filehash string) (string, error) { +// DeleteFile delete a bucket for owner +// - owner: file owner account +// - fid: file identification +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - if you are not the owner, the owner account must be authorised to you +func (c *ChainClient) DeleteFile(owner []byte, fid string) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -737,52 +906,50 @@ func (c *ChainClient) DeleteFile(puk []byte, filehash string) (string, error) { }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - if len(filehash) != pattern.FileHashLen { + if len(fid) != FileHashLen { return "", errors.New("invalid fid") } - acc, err := types.NewAccountID(puk) + acc, err := types.NewAccountID(owner) if err != nil { - return txhash, errors.Wrap(err, "[NewAccountID]") + return blockhash, errors.Wrap(err, "[NewAccountID]") } - var fhash pattern.FileHash - for i := 0; i < len(filehash); i++ { - fhash[i] = types.U8(filehash[i]) + var fhash FileHash + for i := 0; i < len(fid); i++ { + fhash[i] = types.U8(fid[i]) } - call, err := types.NewCall(c.metadata, pattern.TX_FILEBANK_DELFILE, *acc, fhash) + call, err := types.NewCall(c.metadata, TX_FileBank_DeleteFile, *acc, fhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_DELFILE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteFile, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_DELFILE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteFile, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_DELFILE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteFile, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -798,9 +965,9 @@ func (c *ChainClient) DeleteFile(puk []byte, filehash string) (string, error) { // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_DELFILE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteFile, err) c.SetChainState(false) - return txhash, err + return blockhash, err } <-c.txTicker.C @@ -808,22 +975,23 @@ func (c *ChainClient) DeleteFile(puk []byte, filehash string) (string, error) { // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_DELFILE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteFile, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_DELFILE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteFile, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } @@ -836,28 +1004,29 @@ func (c *ChainClient) DeleteFile(puk []byte, filehash string) (string, error) { select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_FileBank_DeleteFile(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) ReportFile(index uint8, roothash string) (string, error) { - var hashs pattern.FileHash - - for j := 0; j < len(roothash); j++ { - hashs[j] = types.U8(roothash[j]) - } - return c.SubmitFileReport(types.U8(index), hashs) -} - -func (c *ChainClient) SubmitFileReport(index types.U8, roothash pattern.FileHash) (string, error) { +// TransferReport is used by miners to report that a file has been transferred +// - index: index of the file fragment +// - fid: file identification +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - for storage miner use only +func (c *ChainClient) TransferReport(index uint8, fid string) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -867,42 +1036,46 @@ func (c *ChainClient) SubmitFileReport(index types.U8, roothash pattern.FileHash }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) - if index == 0 || int(index) > (pattern.DataShards+pattern.ParShards) { + if !c.GetChainState() { + return blockhash, ERR_RPC_CONNECTION + } + + if index <= 0 || int(index) > (DataShards+ParShards) { return "", errors.New("invalid index") } - if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + var fhash FileHash + + for j := 0; j < len(fid); j++ { + fhash[j] = types.U8(fid[j]) } - call, err := types.NewCall(c.metadata, pattern.TX_FILEBANK_FILEREPORT, index, roothash) + call, err := types.NewCall(c.metadata, TX_FileBank_TransferReport, types.NewU8(index), fhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_FILEREPORT, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_FileBank_TransferReport, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_FILEREPORT, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_FileBank_TransferReport, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_FILEREPORT, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_TransferReport, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -918,9 +1091,9 @@ func (c *ChainClient) SubmitFileReport(index types.U8, roothash pattern.FileHash // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_FILEREPORT, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_TransferReport, err) c.SetChainState(false) - return txhash, err + return blockhash, err } <-c.txTicker.C @@ -928,22 +1101,23 @@ func (c *ChainClient) SubmitFileReport(index types.U8, roothash pattern.FileHash // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_FILEREPORT, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_TransferReport, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_FILEREPORT, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_TransferReport, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -955,19 +1129,29 @@ func (c *ChainClient) SubmitFileReport(index types.U8, roothash pattern.FileHash select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_FileBank_TransferReport(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) GenerateRestoralOrder(rootHash, fragmentHash string) (string, error) { +// GenerateRestoralOrder generate restoral orders for file fragment +// - fid: file identification +// - fragmentHash: fragment hash +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - for storage miner use only +func (c *ChainClient) GenerateRestoralOrder(fid, fragmentHash string) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -977,57 +1161,55 @@ func (c *ChainClient) GenerateRestoralOrder(rootHash, fragmentHash string) (stri }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - var rooth pattern.FileHash - var fragh pattern.FileHash + var rooth FileHash + var fragh FileHash - if len(rootHash) != len(rooth) { - return txhash, errors.New("invalid root hash") + if len(fid) != FileHashLen { + return blockhash, errors.New("invalid file hash") } - if len(fragmentHash) != len(fragh) { - return txhash, errors.New("invalid fragment hash") + if len(fragmentHash) != FileHashLen { + return blockhash, errors.New("invalid fragment hash") } - for i := 0; i < len(rootHash); i++ { - rooth[i] = types.U8(rootHash[i]) + for i := 0; i < len(fid); i++ { + rooth[i] = types.U8(fid[i]) } for i := 0; i < len(fragmentHash); i++ { fragh[i] = types.U8(fragmentHash[i]) } - call, err := types.NewCall(c.metadata, pattern.TX_FILEBANK_GENRESTOREORDER, rooth, fragh) + call, err := types.NewCall(c.metadata, TX_FileBank_GenerateRestoralOrder, rooth, fragh) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_GENRESTOREORDER, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_FileBank_GenerateRestoralOrder, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_GENRESTOREORDER, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_FileBank_GenerateRestoralOrder, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_GENRESTOREORDER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_GenerateRestoralOrder, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -1043,9 +1225,9 @@ func (c *ChainClient) GenerateRestoralOrder(rootHash, fragmentHash string) (stri // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_GENRESTOREORDER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_GenerateRestoralOrder, err) c.SetChainState(false) - return txhash, err + return blockhash, err } <-c.txTicker.C @@ -1053,9 +1235,9 @@ func (c *ChainClient) GenerateRestoralOrder(rootHash, fragmentHash string) (stri // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_GENRESTOREORDER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_GenerateRestoralOrder, err) c.SetChainState(false) - return txhash, err + return blockhash, err } defer sub.Unsubscribe() @@ -1066,18 +1248,27 @@ func (c *ChainClient) GenerateRestoralOrder(rootHash, fragmentHash string) (stri select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_FileBank_GenRestoralOrder(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } +// ClaimRestoralOrder claim a restoral order +// - fragmentHash: fragment hash +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - for storage miner use only func (c *ChainClient) ClaimRestoralOrder(fragmentHash string) (string, error) { c.lock.Lock() defer func() { @@ -1088,48 +1279,45 @@ func (c *ChainClient) ClaimRestoralOrder(fragmentHash string) (string, error) { }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - var fragh pattern.FileHash - - if len(fragmentHash) != len(fragh) { - return txhash, errors.New("invalid fragment hash") + if len(fragmentHash) != FileHashLen { + return blockhash, errors.New("invalid fragment hash") } + var fragh FileHash for i := 0; i < len(fragmentHash); i++ { fragh[i] = types.U8(fragmentHash[i]) } - call, err := types.NewCall(c.metadata, pattern.TX_FILEBANK_CLAIMRESTOREORDER, fragh) + call, err := types.NewCall(c.metadata, TX_FileBank_ClaimRestoralOrder, fragh) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CLAIMRESTOREORDER, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralOrder, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CLAIMRESTOREORDER, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralOrder, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CLAIMRESTOREORDER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralOrder, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -1145,9 +1333,9 @@ func (c *ChainClient) ClaimRestoralOrder(fragmentHash string) (string, error) { // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CLAIMRESTOREORDER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralOrder, err) c.SetChainState(false) - return txhash, err + return blockhash, err } <-c.txTicker.C @@ -1155,22 +1343,23 @@ func (c *ChainClient) ClaimRestoralOrder(fragmentHash string) (string, error) { // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CLAIMRESTOREORDER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralOrder, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CLAIMRESTOREORDER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralOrder, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -1182,19 +1371,30 @@ func (c *ChainClient) ClaimRestoralOrder(fragmentHash string) (string, error) { select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_FileBank_ClaimRestoralOrder(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) ClaimRestoralNoExistOrder(puk []byte, rootHash, restoralFragmentHash string) (string, error) { +// ClaimRestoralNoExistOrder claim the restoral order of an exited storage miner +// - puk: storage miner account +// - fid: file identification +// - fragmentHash: fragment hash +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - for storage miner use only +func (c *ChainClient) ClaimRestoralNoExistOrder(puk []byte, fid, fragmentHash string) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -1204,62 +1404,60 @@ func (c *ChainClient) ClaimRestoralNoExistOrder(puk []byte, rootHash, restoralFr }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } acc, err := types.NewAccountID(puk) if err != nil { - return txhash, errors.Wrap(err, "[NewAccountID]") + return blockhash, errors.Wrap(err, "[NewAccountID]") } - var rooth pattern.FileHash - var fragh pattern.FileHash + var rooth FileHash + var fragh FileHash - if len(rootHash) != len(rooth) { - return txhash, errors.New("invalid root hash") + if len(fid) != FileHashLen { + return blockhash, errors.New("invalid file hash") } - if len(restoralFragmentHash) != len(fragh) { - return txhash, errors.New("invalid fragment hash") + if len(fragmentHash) != FileHashLen { + return blockhash, errors.New("invalid fragment hash") } - for i := 0; i < len(rootHash); i++ { - rooth[i] = types.U8(rootHash[i]) + for i := 0; i < len(fid); i++ { + rooth[i] = types.U8(fid[i]) } - for i := 0; i < len(restoralFragmentHash); i++ { - fragh[i] = types.U8(restoralFragmentHash[i]) + for i := 0; i < len(fragmentHash); i++ { + fragh[i] = types.U8(fragmentHash[i]) } - call, err := types.NewCall(c.metadata, pattern.TX_FILEBANK_CLAIMNOEXISTORDER, *acc, rooth, fragh) + call, err := types.NewCall(c.metadata, TX_FileBank_ClaimRestoralNoexistOrder, *acc, rooth, fragh) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CLAIMNOEXISTORDER, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralNoexistOrder, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CLAIMNOEXISTORDER, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralNoexistOrder, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CLAIMNOEXISTORDER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralNoexistOrder, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -1275,7 +1473,7 @@ func (c *ChainClient) ClaimRestoralNoExistOrder(puk []byte, rootHash, restoralFr // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } <-c.txTicker.C @@ -1283,22 +1481,23 @@ func (c *ChainClient) ClaimRestoralNoExistOrder(puk []byte, rootHash, restoralFr // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CLAIMNOEXISTORDER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralNoexistOrder, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CLAIMNOEXISTORDER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralNoexistOrder, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -1310,19 +1509,28 @@ func (c *ChainClient) ClaimRestoralNoExistOrder(puk []byte, rootHash, restoralFr select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_FileBank_ClaimRestoralOrder(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) RestoralComplete(restoralFragmentHash string) (string, error) { +// RestoralOrderComplete submits the restored completed order +// - fragmentHash: fragment hash +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - for storage miner use only +func (c *ChainClient) RestoralOrderComplete(fragmentHash string) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -1332,46 +1540,44 @@ func (c *ChainClient) RestoralComplete(restoralFragmentHash string) (string, err }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - var fragh pattern.FileHash + var fragh FileHash - if len(restoralFragmentHash) != len(fragh) { - return txhash, errors.New("invalid fragment hash") + if len(fragmentHash) != FileHashLen { + return blockhash, errors.New("invalid fragment hash") } - for i := 0; i < len(restoralFragmentHash); i++ { - fragh[i] = types.U8(restoralFragmentHash[i]) + for i := 0; i < len(fragmentHash); i++ { + fragh[i] = types.U8(fragmentHash[i]) } - call, err := types.NewCall(c.metadata, pattern.TX_FILEBANK_RESTORALCOMPLETE, fragh) + call, err := types.NewCall(c.metadata, TX_FileBank_RestoralOrderComplete, fragh) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_RESTORALCOMPLETE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_FileBank_RestoralOrderComplete, err) + return blockhash, err } - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_RESTORALCOMPLETE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_FileBank_RestoralOrderComplete, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_RESTORALCOMPLETE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_RestoralOrderComplete, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -1389,30 +1595,30 @@ func (c *ChainClient) RestoralComplete(restoralFragmentHash string) (string, err // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_RESTORALCOMPLETE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_RestoralOrderComplete, err) c.SetChainState(false) - return txhash, err + return blockhash, err } // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_RESTORALCOMPLETE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_RestoralOrderComplete, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_RESTORALCOMPLETE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_RestoralOrderComplete, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -1424,19 +1630,31 @@ func (c *ChainClient) RestoralComplete(restoralFragmentHash string) (string, err select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_FileBank_RecoveryCompleted(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) CertIdleSpace(idleSignInfo pattern.SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk pattern.WorkerPublicKey) (string, error) { +// CertIdleSpace authenticates idle file to the chain +// - spaceProofInfo: space proof info +// - teeSignWithAcc: tee sign with account +// - teeSign: tee sign +// - teePuk: tee work public key +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - for storage miner use only +func (c *ChainClient) CertIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk WorkerPublicKey) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -1446,36 +1664,34 @@ func (c *ChainClient) CertIdleSpace(idleSignInfo pattern.SpaceProofInfo, teeSign }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - call, err := types.NewCall(c.metadata, pattern.TX_FILEBANK_CERTIDLESPACE, idleSignInfo, teeSignWithAcc, teeSign, teePuk) + call, err := types.NewCall(c.metadata, TX_FileBank_CertIdleSpace, spaceProofInfo, teeSignWithAcc, teeSign, teePuk) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CERTIDLESPACE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_FileBank_CertIdleSpace, err) + return blockhash, err } - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CERTIDLESPACE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_FileBank_CertIdleSpace, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CERTIDLESPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_CertIdleSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -1493,30 +1709,30 @@ func (c *ChainClient) CertIdleSpace(idleSignInfo pattern.SpaceProofInfo, teeSign // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CERTIDLESPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_CertIdleSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CERTIDLESPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_CertIdleSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CERTIDLESPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_CertIdleSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -1528,19 +1744,31 @@ func (c *ChainClient) CertIdleSpace(idleSignInfo pattern.SpaceProofInfo, teeSign select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_FileBank_IdleSpaceCert(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) ReplaceIdleSpace(idleSignInfo pattern.SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk pattern.WorkerPublicKey) (string, error) { +// ReplaceIdleSpace replaces idle files with service files +// - spaceProofInfo: space proof info +// - teeSignWithAcc: tee sign with account +// - teeSign: tee sign +// - teePuk: tee work public key +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - for storage miner use only +func (c *ChainClient) ReplaceIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk WorkerPublicKey) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -1550,36 +1778,34 @@ func (c *ChainClient) ReplaceIdleSpace(idleSignInfo pattern.SpaceProofInfo, teeS }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - call, err := types.NewCall(c.metadata, pattern.TX_FILEBANK_REPLACEIDLESPACE, idleSignInfo, teeSignWithAcc, teeSign, teePuk) + call, err := types.NewCall(c.metadata, TX_FileBank_ReplaceIdleSpace, spaceProofInfo, teeSignWithAcc, teeSign, teePuk) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_REPLACEIDLESPACE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_FileBank_ReplaceIdleSpace, err) + return blockhash, err } - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_REPLACEIDLESPACE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_FileBank_ReplaceIdleSpace, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_REPLACEIDLESPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_ReplaceIdleSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -1597,30 +1823,30 @@ func (c *ChainClient) ReplaceIdleSpace(idleSignInfo pattern.SpaceProofInfo, teeS // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_REPLACEIDLESPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_ReplaceIdleSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_REPLACEIDLESPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_ReplaceIdleSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_REPLACEIDLESPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_ReplaceIdleSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -1632,19 +1858,29 @@ func (c *ChainClient) ReplaceIdleSpace(idleSignInfo pattern.SpaceProofInfo, teeS select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_FileBank_ReplaceIdleSpace(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) ReportTagCalculated(teeSig types.Bytes, tagSigInfo pattern.TagSigInfo) (string, error) { +// CalculateReport report file tag calculation completed +// - teeSig: tee sign +// - tagSigInfo: tag sig info +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - for storage miner use only +func (c *ChainClient) CalculateReport(teeSig types.Bytes, tagSigInfo TagSigInfo) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -1654,38 +1890,36 @@ func (c *ChainClient) ReportTagCalculated(teeSig types.Bytes, tagSigInfo pattern }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - call, err := types.NewCall(c.metadata, pattern.TX_FILEBANK_CALCULATEREPORT, teeSig, tagSigInfo) + call, err := types.NewCall(c.metadata, TX_FileBank_CalculateReport, teeSig, tagSigInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CALCULATEREPORT, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_FileBank_CalculateReport, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CALCULATEREPORT, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_FileBank_CalculateReport, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CALCULATEREPORT, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_CalculateReport, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -1701,9 +1935,9 @@ func (c *ChainClient) ReportTagCalculated(teeSig types.Bytes, tagSigInfo pattern // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CALCULATEREPORT, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_CalculateReport, err) c.SetChainState(false) - return txhash, err + return blockhash, err } <-c.txTicker.C @@ -1711,22 +1945,23 @@ func (c *ChainClient) ReportTagCalculated(teeSig types.Bytes, tagSigInfo pattern // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CALCULATEREPORT, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_CalculateReport, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_FILEBANK_CALCULATEREPORT, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_CalculateReport, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -1738,15 +1973,14 @@ func (c *ChainClient) ReportTagCalculated(teeSig types.Bytes, tagSigInfo pattern select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() - blockhash := status.AsInBlock - _, err = c.RetrieveEvent_FileBank_CalculateReport(blockhash) - return txhash, err + blockhash = status.AsInBlock.Hex() + _, err = c.RetrieveEvent_FileBank_CalculateReport(status.AsInBlock) + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } diff --git a/chain/pattern.go b/chain/pattern.go index 557c872..7efe9d7 100644 --- a/chain/pattern.go +++ b/chain/pattern.go @@ -87,13 +87,14 @@ const ( TEEWorkerAddedAt = "WorkerAddedAt" // FILEBANK - FILE = "File" - BUCKET = "Bucket" - BUCKETLIST = "UserBucketList" - DEALMAP = "DealMap" - FILLERMAP = "FillerMap" - PENDINGREPLACE = "PendingReplacements" - RESTORALORDER = "RestoralOrder" + File = "File" + Bucket = "Bucket" + DealMap = "DealMap" + FillerMap = "FillerMap" + PendingReplacements = "PendingReplacements" + RestoralOrder = "RestoralOrder" + UserBucketList = "UserBucketList" + UserHoldFileList = "UserHoldFileList" // STAKING COUNTERFORVALIDATORS = "CounterForValidators" @@ -157,19 +158,19 @@ const ( TX_SMINER_REGISTERPOISKEY = SMINER + DOT + "register_pois_key" TX_SMINER_INCREASEDECSPACE = SMINER + DOT + "increase_declaration_space" - // FILEBANK - TX_FILEBANK_PUTBUCKET = FILEBANK + DOT + "create_bucket" - TX_FILEBANK_DELBUCKET = FILEBANK + DOT + "delete_bucket" - TX_FILEBANK_DELFILE = FILEBANK + DOT + "delete_file" - TX_FILEBANK_UPLOADDEC = FILEBANK + DOT + "upload_declaration" - TX_FILEBANK_FILEREPORT = FILEBANK + DOT + "transfer_report" - TX_FILEBANK_GENRESTOREORDER = FILEBANK + DOT + "generate_restoral_order" - 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" - TX_FILEBANK_CERTIDLESPACE = FILEBANK + DOT + "cert_idle_space" - TX_FILEBANK_REPLACEIDLESPACE = FILEBANK + DOT + "replace_idle_space" - TX_FILEBANK_CALCULATEREPORT = FILEBANK + DOT + "calculate_report" + // FileBank + TX_FileBank_CreateBucket = FileBank + DOT + "create_bucket" + TX_FileBank_DeleteBucket = FileBank + DOT + "delete_bucket" + TX_FileBank_DeleteFile = FileBank + DOT + "delete_file" + TX_FileBank_UploadDeclaration = FileBank + DOT + "upload_declaration" + TX_FileBank_TransferReport = FileBank + DOT + "transfer_report" + TX_FileBank_GenerateRestoralOrder = FileBank + DOT + "generate_restoral_order" + TX_FileBank_ClaimRestoralOrder = FileBank + DOT + "claim_restoral_order" + TX_FileBank_ClaimRestoralNoexistOrder = FileBank + DOT + "claim_restoral_noexist_order" + TX_FileBank_RestoralOrderComplete = FileBank + DOT + "restoral_order_complete" + TX_FileBank_CertIdleSpace = FileBank + DOT + "cert_idle_space" + TX_FileBank_ReplaceIdleSpace = FileBank + DOT + "replace_idle_space" + TX_FileBank_CalculateReport = FileBank + DOT + "calculate_report" // STORAGE_HANDLER TX_STORAGE_BUYSPACE = STORAGEHANDLER + DOT + "buy_space" @@ -180,7 +181,7 @@ const ( TX_Balances_Transfer = "Balances" + DOT + "transfer" // EVM - TX_EVM_CALL = EVM + DOT + "call" + TX_EVM_Call = EVM + DOT + "call" ) // RPC Call @@ -291,6 +292,58 @@ type ChallengeInfo struct { ProveInfo ProveInfo } +// Oss +type BucketInfo struct { + FileList []FileHash + Authority []types.AccountID +} + +// FileBank +type StorageOrder struct { + FileSize types.U128 + SegmentList []SegmentList + User UserBrief + CompleteList []CompleteInfo +} + +type FileMetadata struct { + SegmentList []SegmentInfo + Owner []UserBrief + FileSize types.U128 + Completion types.U32 + State types.U8 +} + +type RestoralOrderInfo struct { + Count types.U32 + Miner types.AccountID + OriginMiner types.AccountID + FragmentHash FileHash + FileHash FileHash + GenBlock types.U32 + Deadline types.U32 +} + +type RestoralTargetInfo struct { + Miner types.AccountID + ServiceSpace types.U128 + RestoredSpace types.U128 + CoolingBlock types.U32 +} + +type UserFileSliceInfo struct { + Filehash FileHash + Filesize types.U128 +} + +type SpaceProofInfo struct { + Miner types.AccountID + Front types.U64 + Rear types.U64 + PoisKey PoISKeyInfo + Accumulator Accumulator +} + type SysProperties struct { Ss58Format types.Bytes TokenDecimals types.U8 @@ -309,11 +362,6 @@ type OssInfo struct { Domain types.Bytes } -type BucketInfo struct { - ObjectsList []FileHash - Authority []types.AccountID -} - type MinerInfo struct { BeneficiaryAccount types.AccountID StakingAccount types.AccountID @@ -330,14 +378,6 @@ type MinerInfo struct { TeeSig TeeSig } -type SpaceProofInfo struct { - Miner types.AccountID - Front types.U64 - Rear types.U64 - PoisKey PoISKeyInfo - Accumulator Accumulator -} - type MinerReward struct { TotalReward types.U128 RewardIssued types.U128 @@ -353,14 +393,6 @@ type RewardOrder struct { LastReceiveBlock types.U32 } -type FileMetadata struct { - SegmentList []SegmentInfo - Owner []UserBrief - FileSize types.U128 - Completion types.U32 - State types.U8 -} - type SegmentInfo struct { Hash FileHash FragmentList []FragmentInfo @@ -379,13 +411,6 @@ type FragmentInfo struct { Miner types.AccountID } -type StorageOrder struct { - FileSize types.U128 - SegmentList []SegmentList - User UserBrief - CompleteList []CompleteInfo -} - type SegmentList struct { SegmentHash FileHash FragmentHash []FileHash @@ -458,23 +483,6 @@ type TeeWorkerInfo struct { Role types.U8 // 0:Full 1:Verifier 2:Marker } -type RestoralOrderInfo struct { - Count types.U32 - Miner types.AccountID - OriginMiner types.AccountID - FragmentHash FileHash - FileHash FileHash - GenBlock types.U32 - Deadline types.U32 -} - -type RestoralTargetInfo struct { - Miner types.AccountID - ServiceSpace types.U128 - RestoredSpace types.U128 - CoolingBlock types.U32 -} - type ExpendersInfo struct { K types.U64 N types.U64 diff --git a/chain/treasury.go b/chain/treasury.go index daa9332..39887f5 100644 --- a/chain/treasury.go +++ b/chain/treasury.go @@ -249,6 +249,9 @@ func (c *ChainClient) QueryRoundReward(era uint32, block int32) (string, error) if !ok { return "0", ERR_RPC_EMPTY_VALUE } + if data.TotalReward.String() == "" { + return "0", nil + } return data.TotalReward.String(), nil } diff --git a/core/pattern/pattern.go b/core/pattern/pattern.go index b9afffd..87bc325 100755 --- a/core/pattern/pattern.go +++ b/core/pattern/pattern.go @@ -1,610 +1,610 @@ -/* - Copyright (C) CESS. All rights reserved. - Copyright (C) Cumulus Encrypted Storage System. All rights reserved. +// /* +// Copyright (C) CESS. All rights reserved. +// Copyright (C) Cumulus Encrypted Storage System. All rights reserved. - SPDX-License-Identifier: Apache-2.0 -*/ +// SPDX-License-Identifier: Apache-2.0 +// */ package pattern -import ( - "time" - - "github.com/centrifuge/go-substrate-rpc-client/v4/types" - "github.com/pkg/errors" -) - -// DOT is "." character -const DOT = "." - -// Unit precision of CESS token -const TokenPrecision_CESS = "000000000000000000" - -const StakingStakePerTiB = 4000 - -const BlockIntervalSec = 6 - -// BlockInterval is the time interval for generating blocks, in seconds -const BlockInterval = time.Second * time.Duration(BlockIntervalSec) - -const MaxSubmitedIdleFileMeta = 30 - -const PublicDeoss = "http://deoss-pub-gateway.cess.cloud/" -const PublicDeossAccount = "cXhwBytXqrZLr1qM5NHJhCzEMckSTzNKw17ci2aHft6ETSQm9" - -// Pallets -const ( - // - AUDIT = "Audit" - // OSS is a module about DeOSS - OSS = "Oss" - // FILEBANK is a module about data metadata, bucket info, etc. - FILEBANK = "FileBank" - // TEEWOEKER is a module about TEE - TEEWORKER = "TeeWorker" - // SMINER is a module about storage miners - SMINER = "Sminer" - // STAKING is a module about staking - STAKING = "Staking" - // SMINER is a module about storage miners - STORAGEHANDLER = "StorageHandler" - // BALANCES is a module about the balances - BALANCES = "Balances" - // SYSTEM is a module about the system - SYSTEM = "System" - // EVM is a module about the evm contract - EVM = "EVM" - // - CessTreasury = "CessTreasury" -) - -// Chain state -const ( - //AUDIT - UNVERIFYPROOF = "UnverifyProof" - CHALLENGEDURATION = "ChallengeDuration" - CHALLENGEVERIFYDURATION = "VerifyDuration" - CHALLENGESNAPSHOT = "ChallengeSnapShot" - UNVERIFYIDLEPROOF = "UnverifyIdleProof" - UNVERIFYSERVICEPROOF = "UnverifyServiceProof" - - // OSS - // OSS - AUTHORITYLIST = "AuthorityList" - - // SMINER - ALLMINER = "AllMiner" - MINERITEMS = "MinerItems" - REWARDMAP = "RewardMap" - EXPENDERS = "Expenders" - RESTORALTARGETINFO = "RestoralTarget" - STAKINGSTARTBLOCK = "StakingStartBlock" - CompleteSnapShot = "CompleteSnapShot" - - // TEEWORKER - TEEWorkers = "Workers" - TEEMasterPubkey = "MasterPubkey" - TEEEndpoints = "Endpoints" - TEEWorkerAddedAt = "WorkerAddedAt" - - // FILEBANK - FILE = "File" - BUCKET = "Bucket" - BUCKETLIST = "UserBucketList" - DEALMAP = "DealMap" - FILLERMAP = "FillerMap" - PENDINGREPLACE = "PendingReplacements" - RESTORALORDER = "RestoralOrder" - - // STAKING - COUNTERFORVALIDATORS = "CounterForValidators" - CounterForNominators = "CounterForNominators" - ErasTotalStake = "ErasTotalStake" - CurrentEra = "CurrentEra" - ErasStakers = "ErasStakers" - ErasRewardPoints = "ErasRewardPoints" - Nominators = "Nominators" - Bonded = "Bonded" - Validators = "Validators" - ErasValidatorReward = "ErasValidatorReward" - ValidatorCount = "ValidatorCount" - - // STORAGE_HANDLER - USERSPACEINFO = "UserOwnedSpace" - UNITPRICE = "UnitPrice" - TOTALIDLESPACE = "TotalIdleSpace" - TOTALSERVICESPACE = "TotalServiceSpace" - PURCHASEDSPACE = "PurchasedSpace" - - // BALANCES - TOTALISSUANCE = "TotalIssuance" - - // SYSTEM - ACCOUNT = "Account" - EVENTS = "Events" - - // CessTreasury - RoundReward = "RoundReward" -) - -// Extrinsics -const ( - //AUDIT - TX_AUDIT_SUBMITPROOF = AUDIT + DOT + "submit_proof" - TX_AUDIT_SUBMITIDLEPROOF = AUDIT + DOT + "submit_idle_proof" - TX_AUDIT_SUBMITSERVICEPROOF = AUDIT + DOT + "submit_service_proof" - TX_AUDIT_SUBMITIDLEPROOFRESULT = AUDIT + DOT + "submit_verify_idle_result" - TX_AUDIT_SUBMITSERVICEPROOFRESULT = AUDIT + DOT + "submit_verify_service_result" - - // OSS - TX_OSS_REGISTER = OSS + DOT + "register" - TX_OSS_UPDATE = OSS + DOT + "update" - TX_OSS_DESTROY = OSS + DOT + "destroy" - TX_OSS_AUTHORIZE = OSS + DOT + "authorize" - TX_OSS_UNAUTHORIZE = OSS + DOT + "cancel_authorize" - - // SMINER - TX_SMINER_REGISTER = SMINER + DOT + "regnstk" - TX_SMINER_REGISTERASSIGNSTAKING = SMINER + DOT + "regnstk_assign_staking" - TX_SMINER_INCREASESTAKES = SMINER + DOT + "increase_collateral" - TX_SMINER_UPDATEPEERID = SMINER + DOT + "update_peer_id" - TX_SMINER_UPDATEINCOME = SMINER + DOT + "update_beneficiary" - TX_SMINER_CLAIMREWARD = SMINER + DOT + "receive_reward" - TX_SMINER_MINEREXITPREP = SMINER + DOT + "miner_exit_prep" - TX_SMINER_WITHDRAW = SMINER + DOT + "miner_withdraw" - TX_SMINER_REGISTERPOISKEY = SMINER + DOT + "register_pois_key" - TX_SMINER_INCREASEDECSPACE = SMINER + DOT + "increase_declaration_space" - - // FILEBANK - TX_FILEBANK_PUTBUCKET = FILEBANK + DOT + "create_bucket" - TX_FILEBANK_DELBUCKET = FILEBANK + DOT + "delete_bucket" - TX_FILEBANK_DELFILE = FILEBANK + DOT + "delete_file" - TX_FILEBANK_UPLOADDEC = FILEBANK + DOT + "upload_declaration" - TX_FILEBANK_FILEREPORT = FILEBANK + DOT + "transfer_report" - TX_FILEBANK_GENRESTOREORDER = FILEBANK + DOT + "generate_restoral_order" - 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" - TX_FILEBANK_CERTIDLESPACE = FILEBANK + DOT + "cert_idle_space" - TX_FILEBANK_REPLACEIDLESPACE = FILEBANK + DOT + "replace_idle_space" - TX_FILEBANK_CALCULATEREPORT = FILEBANK + DOT + "calculate_report" - - // STORAGE_HANDLER - TX_STORAGE_BUYSPACE = STORAGEHANDLER + DOT + "buy_space" - TX_STORAGE_EXPANSIONSPACE = STORAGEHANDLER + DOT + "expansion_space" - TX_STORAGE_RENEWALSPACE = STORAGEHANDLER + DOT + "renewal_space" - - // BALANCES - TX_BALANCES_FORCETRANSFER = "Balances" + DOT + "transfer" - - // EVM - TX_EVM_CALL = EVM + DOT + "call" -) - -// RPC Call -const ( - // System - RPC_SYS_Properties = "system_properties" - RPC_SYS_SyncState = "system_syncState" - RPC_SYS_Version = "system_version" - RPC_SYS_Chain = "system_chain" - - //Net - RPC_NET_Listening = "net_listening" -) - -const ( - Active = iota - Calculate - Missing - Recovery -) - -const ( - MINER_STATE_POSITIVE = "positive" - MINER_STATE_FROZEN = "frozen" - MINER_STATE_EXIT = "exit" - MINER_STATE_LOCK = "lock" - MINER_STATE_OFFLINE = "offline" -) - -// 0:Full 1:Verifier 2:Marker -const ( - TeeType_Full uint8 = 0 - TeeType_Verifier uint8 = 1 - TeeType_Marker uint8 = 2 -) - -const ( - ERR_Failed = "failed" - ERR_Timeout = "timeout" - ERR_Empty = "empty" -) - -const ( - MinBucketNameLength = 3 - MaxBucketNameLength = 63 - MaxDomainNameLength = 50 -) - -// byte size -const ( - SIZE_1KiB = 1024 - SIZE_1MiB = 1024 * SIZE_1KiB - SIZE_1GiB = 1024 * SIZE_1MiB - SIZE_1TiB = 1024 * SIZE_1GiB -) - -const ( - SegmentSize = 32 * SIZE_1MiB - FragmentSize = 8 * SIZE_1MiB - DataShards = 4 - ParShards = 8 -) - -var ( - ERR_RPC_CONNECTION = errors.New("rpc err: connection failed") - ERR_RPC_IP_FORMAT = errors.New("unsupported ip format") - ERR_RPC_TIMEOUT = errors.New("timeout") - ERR_RPC_EMPTY_VALUE = errors.New("empty") - ERR_RPC_PRIORITYTOOLOW = "Priority is too low" -) - -const ( - FileHashLen = 64 - RandomLen = 20 - PeerIdPublicKeyLen = 38 - PoISKeyLen = 256 - TeeSignatureLen = 256 - AccumulatorLen = 256 - SpaceChallengeParamLen = 8 - BloomFilterLen = 256 - MaxSegmentNum = 1000 - WorkerPublicKeyLen = 32 - MasterPublicKeyLen = 32 - EcdhPublicKeyLen = 32 - TeeSigLen = 64 -) - -type FileHash [FileHashLen]types.U8 -type Random [RandomLen]types.U8 -type PeerId [PeerIdPublicKeyLen]types.U8 -type PoISKey_G [PoISKeyLen]types.U8 -type PoISKey_N [PoISKeyLen]types.U8 -type TeeSignature [TeeSignatureLen]types.U8 -type Accumulator [AccumulatorLen]types.U8 -type SpaceChallengeParam [SpaceChallengeParamLen]types.U64 -type BloomFilter [BloomFilterLen]types.U64 -type WorkerPublicKey [WorkerPublicKeyLen]types.U8 -type MasterPublicKey [MasterPublicKeyLen]types.U8 -type EcdhPublicKey [EcdhPublicKeyLen]types.U8 -type TeeSig [TeeSigLen]types.U8 - -type SysProperties struct { - Ss58Format types.Bytes - TokenDecimals types.U8 - TokenSymbol types.Text - SS58Prefix types.U32 -} - -type SysSyncState struct { - StartingBlock types.U32 - CurrentBlock types.U32 - HighestBlock types.U32 -} - -type OssInfo struct { - Peerid PeerId - Domain types.Bytes -} - -type BucketInfo struct { - ObjectsList []FileHash - Authority []types.AccountID -} - -type MinerInfo struct { - BeneficiaryAccount types.AccountID - StakingAccount types.AccountID - PeerId PeerId - Collaterals types.U128 - Debt types.U128 - State types.Bytes // positive, exit, frozen, lock - DeclarationSpace types.U128 - IdleSpace types.U128 - ServiceSpace types.U128 - LockSpace types.U128 - SpaceProofInfo types.Option[SpaceProofInfo] - ServiceBloomFilter BloomFilter - TeeSig TeeSig -} - -type SpaceProofInfo struct { - Miner types.AccountID - Front types.U64 - Rear types.U64 - PoisKey PoISKeyInfo - Accumulator Accumulator -} - -type MinerReward struct { - TotalReward types.U128 - RewardIssued types.U128 - OrderList []RewardOrder -} - -type RewardOrder struct { - ReceiveCount types.U8 - MaxCount types.U8 - Atonce types.Bool - OrderReward types.U128 - EachAmount types.U128 - LastReceiveBlock types.U32 -} - -type FileMetadata struct { - SegmentList []SegmentInfo - Owner []UserBrief - FileSize types.U128 - Completion types.U32 - State types.U8 -} - -type SegmentInfo struct { - Hash FileHash - FragmentList []FragmentInfo -} - -type UserBrief struct { - User types.AccountID - FileName types.Bytes - BucketName types.Bytes -} - -type FragmentInfo struct { - Hash FileHash - Avail types.Bool - Tag types.Option[types.U32] - Miner types.AccountID -} - -type StorageOrder struct { - FileSize types.U128 - SegmentList []SegmentList - User UserBrief - CompleteList []CompleteInfo -} - -type SegmentList struct { - SegmentHash FileHash - FragmentHash []FileHash -} - -type CompleteInfo struct { - Index types.U8 - Miner types.AccountID -} - -type UserSpaceInfo struct { - TotalSpace types.U128 - UsedSpace types.U128 - LockedSpace types.U128 - RemainingSpace types.U128 - Start types.U32 - Deadline types.U32 - State types.Bytes -} - -type ChallengeSnapShot struct { - MinerSnapshot MinerSnapShot - ChallengeElement ChallengeElement - ProveInfo ProveInfo -} - -type ProveInfo struct { - Assign types.U8 - IdleProve types.Option[IdleProveInfo] - ServiceProve types.Option[ServiceProveInfo] -} - -type ChallengeElement struct { - Start types.U32 - IdleSlip types.U32 - ServiceSlip types.U32 - VerifySlip types.U32 - SpaceParam SpaceChallengeParam - ServiceParam QElement -} - -type QElement struct { - Index []types.U32 - Value []Random -} - -type MinerSnapShot struct { - IdleSpace types.U128 - ServiceSpace types.U128 - ServiceBloomFilter BloomFilter - SpaceProofInfo SpaceProofInfo - TeeSig TeeSig -} - -type IdleProveInfo struct { - TeePubkey WorkerPublicKey - IdleProve types.Bytes - VerifyResult types.Option[bool] -} - -type ServiceProveInfo struct { - TeePubkey WorkerPublicKey - ServiceProve types.Bytes - VerifyResult types.Option[bool] -} - -type TeeWorkerInfo struct { - Pubkey WorkerPublicKey - EcdhPubkey EcdhPublicKey - Version types.U32 - LastUpdated types.U64 - StashAccount types.Option[types.AccountID] - AttestationProvider types.Option[types.U8] - ConfidenceLevel types.U8 - Features []types.U32 - Role types.U8 // 0:Full 1:Verifier 2:Marker -} - -type RestoralOrderInfo struct { - Count types.U32 - Miner types.AccountID - OriginMiner types.AccountID - FragmentHash FileHash - FileHash FileHash - GenBlock types.U32 - Deadline types.U32 -} - -type RestoralTargetInfo struct { - Miner types.AccountID - ServiceSpace types.U128 - RestoredSpace types.U128 - CoolingBlock types.U32 -} - -type ExpendersInfo struct { - K types.U64 - N types.U64 - D types.U64 -} - -type PoISKeyInfo struct { - G PoISKey_G - N PoISKey_N -} - -type IdleSignInfo struct { - Miner types.AccountID - Rear types.U64 - Front types.U64 - Accumulator Accumulator - LastOperationBlock types.U32 - PoisKey PoISKeyInfo -} -type TagSigInfo struct { - Miner types.AccountID - Digest []DigestInfo - Filehash FileHash -} - -type DigestInfo struct { - Fragment FileHash - TeePubkey WorkerPublicKey -} - -type StakingExposure struct { - Total types.U128 - Own types.U128 - Others []OtherStakingExposure -} - -type OtherStakingExposure struct { - Who types.AccountID - Value types.U128 -} - -type StakingEraRewardPoints struct { - Total types.U32 - Individual []Individual -} - -type Individual struct { - Acc types.AccountID - Reward types.U32 -} - -type StakingNominations struct { - Targets []types.AccountID - SubmittedIn types.U32 - Suppressed types.Bool -} - -type StakingValidatorPrefs struct { - Commission types.U32 - Blocked types.Bool -} - -type CompleteSnapShotType struct { - MinerCount types.U32 - TotalPower types.U128 -} - -type RoundRewardType struct { - TotalReward types.U128 - OtherReward types.U128 -} - -// --------------------customer----------------- -type IdleFileMeta struct { - BlockNum uint32 - MinerAcc []byte - Hash string -} - -type UserSpaceSt struct { - TotalSpace string - UsedSpace string - LockedSpace string - RemainingSpace string - State string - Start uint32 - Deadline uint32 -} - -type NetSnapshot struct { - Start uint32 - Life uint32 - Total_reward string - Total_idle_space string - Total_service_space string - Random_index_list []uint32 - Random [][]byte -} - -type MinerSnapshot struct { - Miner string - Idle_space string - Service_space string -} - -type TeeInfo struct { - Pubkey string - EcdhPubkey string - Version uint32 - LastUpdated uint64 - StashAccount string - AttestationProvider uint8 - ConfidenceLevel uint8 - Features []uint32 - WorkerRole uint8 // 0:Full 1:Verifier 2:Marker -} - -type RewardsType struct { - Total string - Claimed string -} - -type SegmentDataInfo struct { - SegmentHash string - FragmentHash []string -} - -type UserInfo struct { - UserAccount string - FileName string - BucketName string - FileSize uint64 -} - -type AccessInfo struct { - r types.H160 - c []types.H160 -} +// import ( +// "time" + +// "github.com/centrifuge/go-substrate-rpc-client/v4/types" +// "github.com/pkg/errors" +// ) + +// // DOT is "." character +// const DOT = "." + +// // Unit precision of CESS token +// const TokenPrecision_CESS = "000000000000000000" + +// const StakingStakePerTiB = 4000 + +// const BlockIntervalSec = 6 + +// // BlockInterval is the time interval for generating blocks, in seconds +// const BlockInterval = time.Second * time.Duration(BlockIntervalSec) + +// const MaxSubmitedIdleFileMeta = 30 + +// const PublicDeoss = "http://deoss-pub-gateway.cess.cloud/" +// const PublicDeossAccount = "cXhwBytXqrZLr1qM5NHJhCzEMckSTzNKw17ci2aHft6ETSQm9" + +// // Pallets +// const ( +// // +// AUDIT = "Audit" +// // OSS is a module about DeOSS +// OSS = "Oss" +// // FILEBANK is a module about data metadata, bucket info, etc. +// FILEBANK = "FileBank" +// // TEEWOEKER is a module about TEE +// TEEWORKER = "TeeWorker" +// // SMINER is a module about storage miners +// SMINER = "Sminer" +// // STAKING is a module about staking +// STAKING = "Staking" +// // SMINER is a module about storage miners +// STORAGEHANDLER = "StorageHandler" +// // BALANCES is a module about the balances +// BALANCES = "Balances" +// // SYSTEM is a module about the system +// SYSTEM = "System" +// // EVM is a module about the evm contract +// EVM = "EVM" +// // +// CessTreasury = "CessTreasury" +// ) + +// // Chain state +// const ( +// //AUDIT +// UNVERIFYPROOF = "UnverifyProof" +// CHALLENGEDURATION = "ChallengeDuration" +// CHALLENGEVERIFYDURATION = "VerifyDuration" +// CHALLENGESNAPSHOT = "ChallengeSnapShot" +// UNVERIFYIDLEPROOF = "UnverifyIdleProof" +// UNVERIFYSERVICEPROOF = "UnverifyServiceProof" + +// // OSS +// // OSS +// AUTHORITYLIST = "AuthorityList" + +// // SMINER +// ALLMINER = "AllMiner" +// MINERITEMS = "MinerItems" +// REWARDMAP = "RewardMap" +// EXPENDERS = "Expenders" +// RESTORALTARGETINFO = "RestoralTarget" +// STAKINGSTARTBLOCK = "StakingStartBlock" +// CompleteSnapShot = "CompleteSnapShot" + +// // TEEWORKER +// TEEWorkers = "Workers" +// TEEMasterPubkey = "MasterPubkey" +// TEEEndpoints = "Endpoints" +// TEEWorkerAddedAt = "WorkerAddedAt" + +// // FILEBANK +// FILE = "File" +// BUCKET = "Bucket" +// BUCKETLIST = "UserBucketList" +// DEALMAP = "DealMap" +// FILLERMAP = "FillerMap" +// PENDINGREPLACE = "PendingReplacements" +// RESTORALORDER = "RestoralOrder" + +// // STAKING +// COUNTERFORVALIDATORS = "CounterForValidators" +// CounterForNominators = "CounterForNominators" +// ErasTotalStake = "ErasTotalStake" +// CurrentEra = "CurrentEra" +// ErasStakers = "ErasStakers" +// ErasRewardPoints = "ErasRewardPoints" +// Nominators = "Nominators" +// Bonded = "Bonded" +// Validators = "Validators" +// ErasValidatorReward = "ErasValidatorReward" +// ValidatorCount = "ValidatorCount" + +// // STORAGE_HANDLER +// USERSPACEINFO = "UserOwnedSpace" +// UNITPRICE = "UnitPrice" +// TOTALIDLESPACE = "TotalIdleSpace" +// TOTALSERVICESPACE = "TotalServiceSpace" +// PURCHASEDSPACE = "PurchasedSpace" + +// // BALANCES +// TOTALISSUANCE = "TotalIssuance" + +// // SYSTEM +// ACCOUNT = "Account" +// EVENTS = "Events" + +// // CessTreasury +// RoundReward = "RoundReward" +// ) + +// // Extrinsics +// const ( +// //AUDIT +// TX_AUDIT_SUBMITPROOF = AUDIT + DOT + "submit_proof" +// TX_AUDIT_SUBMITIDLEPROOF = AUDIT + DOT + "submit_idle_proof" +// TX_AUDIT_SUBMITSERVICEPROOF = AUDIT + DOT + "submit_service_proof" +// TX_AUDIT_SUBMITIDLEPROOFRESULT = AUDIT + DOT + "submit_verify_idle_result" +// TX_AUDIT_SUBMITSERVICEPROOFRESULT = AUDIT + DOT + "submit_verify_service_result" + +// // OSS +// TX_OSS_REGISTER = OSS + DOT + "register" +// TX_OSS_UPDATE = OSS + DOT + "update" +// TX_OSS_DESTROY = OSS + DOT + "destroy" +// TX_OSS_AUTHORIZE = OSS + DOT + "authorize" +// TX_OSS_UNAUTHORIZE = OSS + DOT + "cancel_authorize" + +// // SMINER +// TX_SMINER_REGISTER = SMINER + DOT + "regnstk" +// TX_SMINER_REGISTERASSIGNSTAKING = SMINER + DOT + "regnstk_assign_staking" +// TX_SMINER_INCREASESTAKES = SMINER + DOT + "increase_collateral" +// TX_SMINER_UPDATEPEERID = SMINER + DOT + "update_peer_id" +// TX_SMINER_UPDATEINCOME = SMINER + DOT + "update_beneficiary" +// TX_SMINER_CLAIMREWARD = SMINER + DOT + "receive_reward" +// TX_SMINER_MINEREXITPREP = SMINER + DOT + "miner_exit_prep" +// TX_SMINER_WITHDRAW = SMINER + DOT + "miner_withdraw" +// TX_SMINER_REGISTERPOISKEY = SMINER + DOT + "register_pois_key" +// TX_SMINER_INCREASEDECSPACE = SMINER + DOT + "increase_declaration_space" + +// // FILEBANK +// TX_FILEBANK_PUTBUCKET = FILEBANK + DOT + "create_bucket" +// TX_FILEBANK_DELBUCKET = FILEBANK + DOT + "delete_bucket" +// TX_FILEBANK_DELFILE = FILEBANK + DOT + "delete_file" +// TX_FILEBANK_UPLOADDEC = FILEBANK + DOT + "upload_declaration" +// TX_FILEBANK_FILEREPORT = FILEBANK + DOT + "transfer_report" +// TX_FILEBANK_GENRESTOREORDER = FILEBANK + DOT + "generate_restoral_order" +// 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" +// TX_FILEBANK_CERTIDLESPACE = FILEBANK + DOT + "cert_idle_space" +// TX_FILEBANK_REPLACEIDLESPACE = FILEBANK + DOT + "replace_idle_space" +// TX_FILEBANK_CALCULATEREPORT = FILEBANK + DOT + "calculate_report" + +// // STORAGE_HANDLER +// TX_STORAGE_BUYSPACE = STORAGEHANDLER + DOT + "buy_space" +// TX_STORAGE_EXPANSIONSPACE = STORAGEHANDLER + DOT + "expansion_space" +// TX_STORAGE_RENEWALSPACE = STORAGEHANDLER + DOT + "renewal_space" + +// // BALANCES +// TX_BALANCES_FORCETRANSFER = "Balances" + DOT + "transfer" + +// // EVM +// TX_EVM_CALL = EVM + DOT + "call" +// ) + +// // RPC Call +// const ( +// // System +// RPC_SYS_Properties = "system_properties" +// RPC_SYS_SyncState = "system_syncState" +// RPC_SYS_Version = "system_version" +// RPC_SYS_Chain = "system_chain" + +// //Net +// RPC_NET_Listening = "net_listening" +// ) + +// const ( +// Active = iota +// Calculate +// Missing +// Recovery +// ) + +// const ( +// MINER_STATE_POSITIVE = "positive" +// MINER_STATE_FROZEN = "frozen" +// MINER_STATE_EXIT = "exit" +// MINER_STATE_LOCK = "lock" +// MINER_STATE_OFFLINE = "offline" +// ) + +// // 0:Full 1:Verifier 2:Marker +// const ( +// TeeType_Full uint8 = 0 +// TeeType_Verifier uint8 = 1 +// TeeType_Marker uint8 = 2 +// ) + +// const ( +// ERR_Failed = "failed" +// ERR_Timeout = "timeout" +// ERR_Empty = "empty" +// ) + +// const ( +// MinBucketNameLength = 3 +// MaxBucketNameLength = 63 +// MaxDomainNameLength = 50 +// ) + +// // byte size +// const ( +// SIZE_1KiB = 1024 +// SIZE_1MiB = 1024 * SIZE_1KiB +// SIZE_1GiB = 1024 * SIZE_1MiB +// SIZE_1TiB = 1024 * SIZE_1GiB +// ) + +// const ( +// SegmentSize = 32 * SIZE_1MiB +// FragmentSize = 8 * SIZE_1MiB +// DataShards = 4 +// ParShards = 8 +// ) + +// var ( +// ERR_RPC_CONNECTION = errors.New("rpc err: connection failed") +// ERR_RPC_IP_FORMAT = errors.New("unsupported ip format") +// ERR_RPC_TIMEOUT = errors.New("timeout") +// ERR_RPC_EMPTY_VALUE = errors.New("empty") +// ERR_RPC_PRIORITYTOOLOW = "Priority is too low" +// ) + +// const ( +// FileHashLen = 64 +// RandomLen = 20 +// PeerIdPublicKeyLen = 38 +// PoISKeyLen = 256 +// TeeSignatureLen = 256 +// AccumulatorLen = 256 +// SpaceChallengeParamLen = 8 +// BloomFilterLen = 256 +// MaxSegmentNum = 1000 +// WorkerPublicKeyLen = 32 +// MasterPublicKeyLen = 32 +// EcdhPublicKeyLen = 32 +// TeeSigLen = 64 +// ) + +// type FileHash [FileHashLen]types.U8 +// type Random [RandomLen]types.U8 +// type PeerId [PeerIdPublicKeyLen]types.U8 +// type PoISKey_G [PoISKeyLen]types.U8 +// type PoISKey_N [PoISKeyLen]types.U8 +// type TeeSignature [TeeSignatureLen]types.U8 +// type Accumulator [AccumulatorLen]types.U8 +// type SpaceChallengeParam [SpaceChallengeParamLen]types.U64 +// type BloomFilter [BloomFilterLen]types.U64 +// type WorkerPublicKey [WorkerPublicKeyLen]types.U8 +// type MasterPublicKey [MasterPublicKeyLen]types.U8 +// type EcdhPublicKey [EcdhPublicKeyLen]types.U8 +// type TeeSig [TeeSigLen]types.U8 + +// type SysProperties struct { +// Ss58Format types.Bytes +// TokenDecimals types.U8 +// TokenSymbol types.Text +// SS58Prefix types.U32 +// } + +// type SysSyncState struct { +// StartingBlock types.U32 +// CurrentBlock types.U32 +// HighestBlock types.U32 +// } + +// type OssInfo struct { +// Peerid PeerId +// Domain types.Bytes +// } + +// type BucketInfo struct { +// ObjectsList []FileHash +// Authority []types.AccountID +// } + +// type MinerInfo struct { +// BeneficiaryAccount types.AccountID +// StakingAccount types.AccountID +// PeerId PeerId +// Collaterals types.U128 +// Debt types.U128 +// State types.Bytes // positive, exit, frozen, lock +// DeclarationSpace types.U128 +// IdleSpace types.U128 +// ServiceSpace types.U128 +// LockSpace types.U128 +// SpaceProofInfo types.Option[SpaceProofInfo] +// ServiceBloomFilter BloomFilter +// TeeSig TeeSig +// } + +// type SpaceProofInfo struct { +// Miner types.AccountID +// Front types.U64 +// Rear types.U64 +// PoisKey PoISKeyInfo +// Accumulator Accumulator +// } + +// type MinerReward struct { +// TotalReward types.U128 +// RewardIssued types.U128 +// OrderList []RewardOrder +// } + +// type RewardOrder struct { +// ReceiveCount types.U8 +// MaxCount types.U8 +// Atonce types.Bool +// OrderReward types.U128 +// EachAmount types.U128 +// LastReceiveBlock types.U32 +// } + +// type FileMetadata struct { +// SegmentList []SegmentInfo +// Owner []UserBrief +// FileSize types.U128 +// Completion types.U32 +// State types.U8 +// } + +// type SegmentInfo struct { +// Hash FileHash +// FragmentList []FragmentInfo +// } + +// type UserBrief struct { +// User types.AccountID +// FileName types.Bytes +// BucketName types.Bytes +// } + +// type FragmentInfo struct { +// Hash FileHash +// Avail types.Bool +// Tag types.Option[types.U32] +// Miner types.AccountID +// } + +// type StorageOrder struct { +// FileSize types.U128 +// SegmentList []SegmentList +// User UserBrief +// CompleteList []CompleteInfo +// } + +// type SegmentList struct { +// SegmentHash FileHash +// FragmentHash []FileHash +// } + +// type CompleteInfo struct { +// Index types.U8 +// Miner types.AccountID +// } + +// type UserSpaceInfo struct { +// TotalSpace types.U128 +// UsedSpace types.U128 +// LockedSpace types.U128 +// RemainingSpace types.U128 +// Start types.U32 +// Deadline types.U32 +// State types.Bytes +// } + +// type ChallengeSnapShot struct { +// MinerSnapshot MinerSnapShot +// ChallengeElement ChallengeElement +// ProveInfo ProveInfo +// } + +// type ProveInfo struct { +// Assign types.U8 +// IdleProve types.Option[IdleProveInfo] +// ServiceProve types.Option[ServiceProveInfo] +// } + +// type ChallengeElement struct { +// Start types.U32 +// IdleSlip types.U32 +// ServiceSlip types.U32 +// VerifySlip types.U32 +// SpaceParam SpaceChallengeParam +// ServiceParam QElement +// } + +// type QElement struct { +// Index []types.U32 +// Value []Random +// } + +// type MinerSnapShot struct { +// IdleSpace types.U128 +// ServiceSpace types.U128 +// ServiceBloomFilter BloomFilter +// SpaceProofInfo SpaceProofInfo +// TeeSig TeeSig +// } + +// type IdleProveInfo struct { +// TeePubkey WorkerPublicKey +// IdleProve types.Bytes +// VerifyResult types.Option[bool] +// } + +// type ServiceProveInfo struct { +// TeePubkey WorkerPublicKey +// ServiceProve types.Bytes +// VerifyResult types.Option[bool] +// } + +// type TeeWorkerInfo struct { +// Pubkey WorkerPublicKey +// EcdhPubkey EcdhPublicKey +// Version types.U32 +// LastUpdated types.U64 +// StashAccount types.Option[types.AccountID] +// AttestationProvider types.Option[types.U8] +// ConfidenceLevel types.U8 +// Features []types.U32 +// Role types.U8 // 0:Full 1:Verifier 2:Marker +// } + +// type RestoralOrderInfo struct { +// Count types.U32 +// Miner types.AccountID +// OriginMiner types.AccountID +// FragmentHash FileHash +// FileHash FileHash +// GenBlock types.U32 +// Deadline types.U32 +// } + +// type RestoralTargetInfo struct { +// Miner types.AccountID +// ServiceSpace types.U128 +// RestoredSpace types.U128 +// CoolingBlock types.U32 +// } + +// type ExpendersInfo struct { +// K types.U64 +// N types.U64 +// D types.U64 +// } + +// type PoISKeyInfo struct { +// G PoISKey_G +// N PoISKey_N +// } + +// type IdleSignInfo struct { +// Miner types.AccountID +// Rear types.U64 +// Front types.U64 +// Accumulator Accumulator +// LastOperationBlock types.U32 +// PoisKey PoISKeyInfo +// } +// type TagSigInfo struct { +// Miner types.AccountID +// Digest []DigestInfo +// Filehash FileHash +// } + +// type DigestInfo struct { +// Fragment FileHash +// TeePubkey WorkerPublicKey +// } + +// type StakingExposure struct { +// Total types.U128 +// Own types.U128 +// Others []OtherStakingExposure +// } + +// type OtherStakingExposure struct { +// Who types.AccountID +// Value types.U128 +// } + +// type StakingEraRewardPoints struct { +// Total types.U32 +// Individual []Individual +// } + +// type Individual struct { +// Acc types.AccountID +// Reward types.U32 +// } + +// type StakingNominations struct { +// Targets []types.AccountID +// SubmittedIn types.U32 +// Suppressed types.Bool +// } + +// type StakingValidatorPrefs struct { +// Commission types.U32 +// Blocked types.Bool +// } + +// type CompleteSnapShotType struct { +// MinerCount types.U32 +// TotalPower types.U128 +// } + +// type RoundRewardType struct { +// TotalReward types.U128 +// OtherReward types.U128 +// } + +// // --------------------customer----------------- +// type IdleFileMeta struct { +// BlockNum uint32 +// MinerAcc []byte +// Hash string +// } + +// type UserSpaceSt struct { +// TotalSpace string +// UsedSpace string +// LockedSpace string +// RemainingSpace string +// State string +// Start uint32 +// Deadline uint32 +// } + +// type NetSnapshot struct { +// Start uint32 +// Life uint32 +// Total_reward string +// Total_idle_space string +// Total_service_space string +// Random_index_list []uint32 +// Random [][]byte +// } + +// type MinerSnapshot struct { +// Miner string +// Idle_space string +// Service_space string +// } + +// type TeeInfo struct { +// Pubkey string +// EcdhPubkey string +// Version uint32 +// LastUpdated uint64 +// StashAccount string +// AttestationProvider uint8 +// ConfidenceLevel uint8 +// Features []uint32 +// WorkerRole uint8 // 0:Full 1:Verifier 2:Marker +// } + +// type RewardsType struct { +// Total string +// Claimed string +// } + +// type SegmentDataInfo struct { +// SegmentHash string +// FragmentHash []string +// } + +// type UserInfo struct { +// UserAccount string +// FileName string +// BucketName string +// FileSize uint64 +// } + +// type AccessInfo struct { +// r types.H160 +// c []types.H160 +// } From ffea2734c808eb9aefba580e803f640f93e8f917 Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Wed, 8 May 2024 21:05:27 +0800 Subject: [PATCH 06/18] update cfg --- cfg.go | 54 ++ .../eventsName.go => chain/event_name.go | 2 +- core/event/events.go => chain/event_type.go | 47 +- chain/pattern.go | 21 - chain/utils.go | 66 ++ config/config.go | 56 +- core/erasure/rs.go | 14 +- core/pattern/pattern.go | 610 ------------------ core/process/process.go | 25 +- defaults.go | 4 +- example/evm/evm.go | 3 +- example/parseEvent/parseevent.go | 4 +- example/process/process.go | 4 +- options.go | 6 +- sdk.go | 7 - utils/bucket.go | 4 +- utils/typ.go | 68 -- 17 files changed, 192 insertions(+), 803 deletions(-) create mode 100644 cfg.go rename core/event/eventsName.go => chain/event_name.go (99%) mode change 100755 => 100644 rename core/event/events.go => chain/event_type.go (95%) mode change 100755 => 100644 create mode 100644 chain/utils.go delete mode 100755 core/pattern/pattern.go delete mode 100644 utils/typ.go diff --git a/cfg.go b/cfg.go new file mode 100644 index 0000000..56e2b73 --- /dev/null +++ b/cfg.go @@ -0,0 +1,54 @@ +/* + Copyright (C) CESS. All rights reserved. + Copyright (C) Cumulus Encrypted Storage System. All rights reserved. + + SPDX-License-Identifier: Apache-2.0 +*/ + +package sdkgo + +import ( + "context" + "time" + + "github.com/CESSProject/cess-go-sdk/chain" + "github.com/CESSProject/cess-go-sdk/config" +) + +// Config describes a set of settings for a client +type Config struct { + Rpc []string + Mnemonic string + Name string + Timeout time.Duration +} + +// Option is a client config option that can be given to the client constructor +type Option func(cfg *Config) error + +// // Option is a client config option that can be given to the client constructor +// type Option func(cfg *Config) error + +// 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) (*chain.ChainClient, error) { + if cfg.Name == "" { + cfg.Name = config.CharacterName_Default + } + return chain.NewChainClient(ctx, cfg.Name, cfg.Rpc, cfg.Mnemonic, cfg.Timeout) +} + +// Apply applies the given options to the config, returning the first error +// encountered (if any). +func (cfg *Config) Apply(opts ...Option) error { + for _, opt := range opts { + if opt == nil { + continue + } + if err := opt(cfg); err != nil { + return err + } + } + return nil +} diff --git a/core/event/eventsName.go b/chain/event_name.go old mode 100755 new mode 100644 similarity index 99% rename from core/event/eventsName.go rename to chain/event_name.go index dba1358..874d50d --- a/core/event/eventsName.go +++ b/chain/event_name.go @@ -5,7 +5,7 @@ SPDX-License-Identifier: Apache-2.0 */ -package event +package chain const ( // AUDIT diff --git a/core/event/events.go b/chain/event_type.go old mode 100755 new mode 100644 similarity index 95% rename from core/event/events.go rename to chain/event_type.go index 01edcc9..aa921d4 --- a/core/event/events.go +++ b/chain/event_type.go @@ -5,10 +5,9 @@ SPDX-License-Identifier: Apache-2.0 */ -package event +package chain import ( - "github.com/CESSProject/cess-go-sdk/core/pattern" "github.com/centrifuge/go-substrate-rpc-client/v4/types" ) @@ -19,7 +18,7 @@ import ( // ------------------------Audit------------------- type Event_VerifyProof struct { Phase types.Phase - TeeWorker pattern.WorkerPublicKey + TeeWorker WorkerPublicKey Miner types.AccountID Topics []types.Hash } @@ -49,7 +48,7 @@ type Event_SubmitServiceProof struct { type Event_SubmitIdleVerifyResult struct { Phase types.Phase - Tee pattern.WorkerPublicKey + Tee WorkerPublicKey Miner types.AccountID Result types.Bool Topics []types.Hash @@ -57,7 +56,7 @@ type Event_SubmitIdleVerifyResult struct { type Event_SubmitServiceVerifyResult struct { Phase types.Phase - Tee pattern.WorkerPublicKey + Tee WorkerPublicKey Miner types.AccountID Result types.Bool Topics []types.Hash @@ -134,8 +133,8 @@ type Event_UpdateBeneficiary struct { type Event_UpdatePeerId struct { Phase types.Phase Acc types.AccountID - Old pattern.PeerId - New pattern.PeerId + Old PeerId + New PeerId Topics []types.Hash } @@ -157,14 +156,14 @@ type Event_DeleteFile struct { Phase types.Phase Operator types.AccountID Owner types.AccountID - Filehash []pattern.FileHash + Filehash []FileHash Topics []types.Hash } type Event_FillerDelete struct { Phase types.Phase Acc types.AccountID - FillerHash pattern.FileHash + FillerHash FileHash Topics []types.Hash } @@ -179,7 +178,7 @@ type Event_UploadDeclaration struct { Phase types.Phase Operator types.AccountID Owner types.AccountID - Deal_hash pattern.FileHash + Deal_hash FileHash Topics []types.Hash } @@ -202,47 +201,47 @@ type Event_DeleteBucket struct { type Event_TransferReport struct { Phase types.Phase Acc types.AccountID - DealHash pattern.FileHash + DealHash FileHash Topics []types.Hash } type Event_ReplaceFiller struct { Phase types.Phase Acc types.AccountID - Filler_list []pattern.FileHash + Filler_list []FileHash Topics []types.Hash } type Event_CalculateEnd struct { Phase types.Phase - File_hash pattern.FileHash + File_hash FileHash Topics []types.Hash } type Event_GenerateRestoralOrder struct { Phase types.Phase Miner types.AccountID - FragmentHash pattern.FileHash + FragmentHash FileHash Topics []types.Hash } type Event_ClaimRestoralOrder struct { Phase types.Phase Miner types.AccountID - OrderId pattern.FileHash + OrderId FileHash Topics []types.Hash } type Event_RecoveryCompleted struct { Phase types.Phase Miner types.AccountID - OrderId pattern.FileHash + OrderId FileHash Topics []types.Hash } type Event_StorageCompleted struct { Phase types.Phase - FileHash pattern.FileHash + FileHash FileHash Topics []types.Hash } @@ -276,7 +275,7 @@ type Event_ReplaceIdleSpace struct { type Event_CalculateReport struct { Phase types.Phase Miner types.AccountID - FileHash pattern.FileHash + FileHash FileHash Topics []types.Hash } @@ -333,7 +332,7 @@ type Event_MasterKeyLaunched struct { type Event_WorkerAdded struct { Phase types.Phase - Pubkey pattern.WorkerPublicKey + Pubkey WorkerPublicKey AttestationProvider types.Option[types.U8] ConfidenceLevel types.U8 Topics []types.Hash @@ -341,13 +340,13 @@ type Event_WorkerAdded struct { type Event_KeyfairyAdded struct { Phase types.Phase - Pubkey pattern.WorkerPublicKey + Pubkey WorkerPublicKey Topics []types.Hash } type Event_WorkerUpdated struct { Phase types.Phase - Pubkey pattern.WorkerPublicKey + Pubkey WorkerPublicKey AttestationProvider types.Option[types.U8] ConfidenceLevel types.U8 Topics []types.Hash @@ -356,7 +355,7 @@ type Event_WorkerUpdated struct { type Event_MasterKeyRotated struct { Phase types.Phase RotationId types.U64 - MasterPubkey pattern.WorkerPublicKey + MasterPubkey WorkerPublicKey Topics []types.Hash } @@ -379,14 +378,14 @@ type Event_MinimumCesealVersionChangedTo struct { type Event_OssRegister struct { Phase types.Phase Acc types.AccountID - Endpoint pattern.PeerId + Endpoint PeerId Topics []types.Hash } type Event_OssUpdate struct { Phase types.Phase Acc types.AccountID - NewEndpoint pattern.PeerId + NewEndpoint PeerId Topics []types.Hash } diff --git a/chain/pattern.go b/chain/pattern.go index 7efe9d7..4b0638d 100644 --- a/chain/pattern.go +++ b/chain/pattern.go @@ -224,27 +224,6 @@ const ( ERR_Empty = "empty" ) -const ( - MinBucketNameLength = 3 - MaxBucketNameLength = 63 - MaxDomainNameLength = 50 -) - -// byte size -const ( - SIZE_1KiB = 1024 - SIZE_1MiB = 1024 * SIZE_1KiB - SIZE_1GiB = 1024 * SIZE_1MiB - SIZE_1TiB = 1024 * SIZE_1GiB -) - -const ( - SegmentSize = 32 * SIZE_1MiB - FragmentSize = 8 * SIZE_1MiB - DataShards = 4 - ParShards = 8 -) - var ( ERR_RPC_CONNECTION = errors.New("rpc err: connection failed") ERR_RPC_IP_FORMAT = errors.New("unsupported ip format") diff --git a/chain/utils.go b/chain/utils.go new file mode 100644 index 0000000..00f050c --- /dev/null +++ b/chain/utils.go @@ -0,0 +1,66 @@ +/* + Copyright (C) CESS. All rights reserved. + Copyright (C) Cumulus Encrypted Storage System. All rights reserved. + + SPDX-License-Identifier: Apache-2.0 +*/ + +package chain + +import ( + "github.com/centrifuge/go-substrate-rpc-client/v4/types" + "github.com/pkg/errors" +) + +func BytesToFileHash(val []byte) (FileHash, error) { + if len(val) != FileHashLen { + return FileHash{}, errors.New("[BytesToFileHash] invalid length") + } + var filehash FileHash + for k, v := range val { + filehash[k] = types.U8(v) + } + return filehash, nil +} + +func BytesToWorkPublickey(val []byte) (WorkerPublicKey, error) { + if len(val) != WorkerPublicKeyLen { + return WorkerPublicKey{}, errors.New("[BytesToWorkPublickey] invalid length") + } + var pubkey WorkerPublicKey + for k, v := range val { + pubkey[k] = types.U8(v) + } + return pubkey, nil +} + +func BytesToPoISKeyInfo(g, n []byte) (PoISKeyInfo, error) { + if len(g) != PoISKeyLen || len(n) != PoISKeyLen { + return PoISKeyInfo{}, errors.New("[BytesToPoISKeyInfo] invalid length") + } + var poisKey PoISKeyInfo + for i := 0; i < PoISKeyLen; i++ { + poisKey.G[i] = types.U8(g[i]) + poisKey.N[i] = types.U8(n[i]) + } + return poisKey, nil +} +func BytesToBloomFilter(val []byte) (BloomFilter, error) { + if len(val) != BloomFilterLen { + return BloomFilter{}, errors.New("[BytesToBloomFilter] invalid length") + } + var bloomfilter BloomFilter + for i := 0; i < BloomFilterLen; i++ { + bloomfilter[i] = types.U64(val[i]) + } + return bloomfilter, nil +} + +func IsWorkerPublicKeyAllZero(puk WorkerPublicKey) bool { + for i := 0; i < WorkerPublicKeyLen; i++ { + if puk[i] != 0 { + return false + } + } + return true +} diff --git a/config/config.go b/config/config.go index 949db90..15dac5c 100755 --- a/config/config.go +++ b/config/config.go @@ -7,49 +7,25 @@ package config -import ( - "context" - "time" - - "github.com/CESSProject/cess-go-sdk/chain" +const ( + SIZE_1KiB = 1024 + SIZE_1MiB = 1024 * SIZE_1KiB + SIZE_1GiB = 1024 * SIZE_1MiB + SIZE_1TiB = 1024 * SIZE_1GiB + + SegmentSize = 32 * SIZE_1MiB + FragmentSize = 8 * SIZE_1MiB + DataShards = 4 + ParShards = 8 ) -// Config describes a set of settings for a client -type Config struct { - Rpc []string - Mnemonic string - Name string - Timeout time.Duration -} - -// Option is a client config option that can be given to the client constructor -type Option func(cfg *Config) error +const ( + MinBucketNameLength = 3 + MaxBucketNameLength = 63 + MaxDomainNameLength = 50 +) -// default service name +// default name const ( CharacterName_Default = "cess-sdk-go" ) - -// 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) (*chain.ChainClient, error) { - if cfg.Name == "" { - cfg.Name = CharacterName_Default - } - return chain.NewChainClient(ctx, cfg.Name, cfg.Rpc, cfg.Mnemonic, cfg.Timeout) -} - -// Apply applies the given options to the config, returning the first error -// encountered (if any). -func (cfg *Config) Apply(opts ...Option) error { - for _, opt := range opts { - if opt == nil { - continue - } - if err := opt(cfg); err != nil { - return err - } - } - return nil -} diff --git a/core/erasure/rs.go b/core/erasure/rs.go index a2550a5..bd2a17e 100644 --- a/core/erasure/rs.go +++ b/core/erasure/rs.go @@ -13,7 +13,7 @@ import ( "os" "path/filepath" - "github.com/CESSProject/cess-go-sdk/core/pattern" + "github.com/CESSProject/cess-go-sdk/config" "github.com/CESSProject/cess-go-sdk/utils" "github.com/klauspost/reedsolomon" ) @@ -35,13 +35,13 @@ func ReedSolomon(path string) ([]string, error) { if fstat.IsDir() { return nil, errors.New("not a file") } - if fstat.Size() != pattern.SegmentSize { + if fstat.Size() != config.SegmentSize { return nil, errors.New("invalid size") } basedir := filepath.Dir(path) - enc, err := reedsolomon.New(pattern.DataShards, pattern.ParShards) + enc, err := reedsolomon.New(config.DataShards, config.ParShards) if err != nil { return shardspath, err } @@ -94,12 +94,12 @@ func RSRestore(outpath string, shardspath []string) error { return nil } - enc, err := reedsolomon.New(pattern.DataShards, pattern.ParShards) + enc, err := reedsolomon.New(config.DataShards, config.ParShards) if err != nil { return err } - shards := make([][]byte, pattern.DataShards+pattern.ParShards) + shards := make([][]byte, config.DataShards+config.ParShards) for k, v := range shardspath { shards[k], err = os.ReadFile(v) if err != nil { @@ -128,7 +128,7 @@ func RSRestore(outpath string, shardspath []string) error { return err } defer f.Close() - err = enc.Join(f, shards, len(shards[0])*pattern.DataShards) + err = enc.Join(f, shards, len(shards[0])*config.DataShards) return err } @@ -146,7 +146,7 @@ func RSRestoreData(outpath string, sharddata [][]byte) error { return nil } - datashards, parshards := pattern.DataShards, pattern.ParShards + datashards, parshards := config.DataShards, config.ParShards enc, err := reedsolomon.New(datashards, parshards) if err != nil { diff --git a/core/pattern/pattern.go b/core/pattern/pattern.go deleted file mode 100755 index 87bc325..0000000 --- a/core/pattern/pattern.go +++ /dev/null @@ -1,610 +0,0 @@ -// /* -// Copyright (C) CESS. All rights reserved. -// Copyright (C) Cumulus Encrypted Storage System. All rights reserved. - -// SPDX-License-Identifier: Apache-2.0 -// */ - -package pattern - -// import ( -// "time" - -// "github.com/centrifuge/go-substrate-rpc-client/v4/types" -// "github.com/pkg/errors" -// ) - -// // DOT is "." character -// const DOT = "." - -// // Unit precision of CESS token -// const TokenPrecision_CESS = "000000000000000000" - -// const StakingStakePerTiB = 4000 - -// const BlockIntervalSec = 6 - -// // BlockInterval is the time interval for generating blocks, in seconds -// const BlockInterval = time.Second * time.Duration(BlockIntervalSec) - -// const MaxSubmitedIdleFileMeta = 30 - -// const PublicDeoss = "http://deoss-pub-gateway.cess.cloud/" -// const PublicDeossAccount = "cXhwBytXqrZLr1qM5NHJhCzEMckSTzNKw17ci2aHft6ETSQm9" - -// // Pallets -// const ( -// // -// AUDIT = "Audit" -// // OSS is a module about DeOSS -// OSS = "Oss" -// // FILEBANK is a module about data metadata, bucket info, etc. -// FILEBANK = "FileBank" -// // TEEWOEKER is a module about TEE -// TEEWORKER = "TeeWorker" -// // SMINER is a module about storage miners -// SMINER = "Sminer" -// // STAKING is a module about staking -// STAKING = "Staking" -// // SMINER is a module about storage miners -// STORAGEHANDLER = "StorageHandler" -// // BALANCES is a module about the balances -// BALANCES = "Balances" -// // SYSTEM is a module about the system -// SYSTEM = "System" -// // EVM is a module about the evm contract -// EVM = "EVM" -// // -// CessTreasury = "CessTreasury" -// ) - -// // Chain state -// const ( -// //AUDIT -// UNVERIFYPROOF = "UnverifyProof" -// CHALLENGEDURATION = "ChallengeDuration" -// CHALLENGEVERIFYDURATION = "VerifyDuration" -// CHALLENGESNAPSHOT = "ChallengeSnapShot" -// UNVERIFYIDLEPROOF = "UnverifyIdleProof" -// UNVERIFYSERVICEPROOF = "UnverifyServiceProof" - -// // OSS -// // OSS -// AUTHORITYLIST = "AuthorityList" - -// // SMINER -// ALLMINER = "AllMiner" -// MINERITEMS = "MinerItems" -// REWARDMAP = "RewardMap" -// EXPENDERS = "Expenders" -// RESTORALTARGETINFO = "RestoralTarget" -// STAKINGSTARTBLOCK = "StakingStartBlock" -// CompleteSnapShot = "CompleteSnapShot" - -// // TEEWORKER -// TEEWorkers = "Workers" -// TEEMasterPubkey = "MasterPubkey" -// TEEEndpoints = "Endpoints" -// TEEWorkerAddedAt = "WorkerAddedAt" - -// // FILEBANK -// FILE = "File" -// BUCKET = "Bucket" -// BUCKETLIST = "UserBucketList" -// DEALMAP = "DealMap" -// FILLERMAP = "FillerMap" -// PENDINGREPLACE = "PendingReplacements" -// RESTORALORDER = "RestoralOrder" - -// // STAKING -// COUNTERFORVALIDATORS = "CounterForValidators" -// CounterForNominators = "CounterForNominators" -// ErasTotalStake = "ErasTotalStake" -// CurrentEra = "CurrentEra" -// ErasStakers = "ErasStakers" -// ErasRewardPoints = "ErasRewardPoints" -// Nominators = "Nominators" -// Bonded = "Bonded" -// Validators = "Validators" -// ErasValidatorReward = "ErasValidatorReward" -// ValidatorCount = "ValidatorCount" - -// // STORAGE_HANDLER -// USERSPACEINFO = "UserOwnedSpace" -// UNITPRICE = "UnitPrice" -// TOTALIDLESPACE = "TotalIdleSpace" -// TOTALSERVICESPACE = "TotalServiceSpace" -// PURCHASEDSPACE = "PurchasedSpace" - -// // BALANCES -// TOTALISSUANCE = "TotalIssuance" - -// // SYSTEM -// ACCOUNT = "Account" -// EVENTS = "Events" - -// // CessTreasury -// RoundReward = "RoundReward" -// ) - -// // Extrinsics -// const ( -// //AUDIT -// TX_AUDIT_SUBMITPROOF = AUDIT + DOT + "submit_proof" -// TX_AUDIT_SUBMITIDLEPROOF = AUDIT + DOT + "submit_idle_proof" -// TX_AUDIT_SUBMITSERVICEPROOF = AUDIT + DOT + "submit_service_proof" -// TX_AUDIT_SUBMITIDLEPROOFRESULT = AUDIT + DOT + "submit_verify_idle_result" -// TX_AUDIT_SUBMITSERVICEPROOFRESULT = AUDIT + DOT + "submit_verify_service_result" - -// // OSS -// TX_OSS_REGISTER = OSS + DOT + "register" -// TX_OSS_UPDATE = OSS + DOT + "update" -// TX_OSS_DESTROY = OSS + DOT + "destroy" -// TX_OSS_AUTHORIZE = OSS + DOT + "authorize" -// TX_OSS_UNAUTHORIZE = OSS + DOT + "cancel_authorize" - -// // SMINER -// TX_SMINER_REGISTER = SMINER + DOT + "regnstk" -// TX_SMINER_REGISTERASSIGNSTAKING = SMINER + DOT + "regnstk_assign_staking" -// TX_SMINER_INCREASESTAKES = SMINER + DOT + "increase_collateral" -// TX_SMINER_UPDATEPEERID = SMINER + DOT + "update_peer_id" -// TX_SMINER_UPDATEINCOME = SMINER + DOT + "update_beneficiary" -// TX_SMINER_CLAIMREWARD = SMINER + DOT + "receive_reward" -// TX_SMINER_MINEREXITPREP = SMINER + DOT + "miner_exit_prep" -// TX_SMINER_WITHDRAW = SMINER + DOT + "miner_withdraw" -// TX_SMINER_REGISTERPOISKEY = SMINER + DOT + "register_pois_key" -// TX_SMINER_INCREASEDECSPACE = SMINER + DOT + "increase_declaration_space" - -// // FILEBANK -// TX_FILEBANK_PUTBUCKET = FILEBANK + DOT + "create_bucket" -// TX_FILEBANK_DELBUCKET = FILEBANK + DOT + "delete_bucket" -// TX_FILEBANK_DELFILE = FILEBANK + DOT + "delete_file" -// TX_FILEBANK_UPLOADDEC = FILEBANK + DOT + "upload_declaration" -// TX_FILEBANK_FILEREPORT = FILEBANK + DOT + "transfer_report" -// TX_FILEBANK_GENRESTOREORDER = FILEBANK + DOT + "generate_restoral_order" -// 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" -// TX_FILEBANK_CERTIDLESPACE = FILEBANK + DOT + "cert_idle_space" -// TX_FILEBANK_REPLACEIDLESPACE = FILEBANK + DOT + "replace_idle_space" -// TX_FILEBANK_CALCULATEREPORT = FILEBANK + DOT + "calculate_report" - -// // STORAGE_HANDLER -// TX_STORAGE_BUYSPACE = STORAGEHANDLER + DOT + "buy_space" -// TX_STORAGE_EXPANSIONSPACE = STORAGEHANDLER + DOT + "expansion_space" -// TX_STORAGE_RENEWALSPACE = STORAGEHANDLER + DOT + "renewal_space" - -// // BALANCES -// TX_BALANCES_FORCETRANSFER = "Balances" + DOT + "transfer" - -// // EVM -// TX_EVM_CALL = EVM + DOT + "call" -// ) - -// // RPC Call -// const ( -// // System -// RPC_SYS_Properties = "system_properties" -// RPC_SYS_SyncState = "system_syncState" -// RPC_SYS_Version = "system_version" -// RPC_SYS_Chain = "system_chain" - -// //Net -// RPC_NET_Listening = "net_listening" -// ) - -// const ( -// Active = iota -// Calculate -// Missing -// Recovery -// ) - -// const ( -// MINER_STATE_POSITIVE = "positive" -// MINER_STATE_FROZEN = "frozen" -// MINER_STATE_EXIT = "exit" -// MINER_STATE_LOCK = "lock" -// MINER_STATE_OFFLINE = "offline" -// ) - -// // 0:Full 1:Verifier 2:Marker -// const ( -// TeeType_Full uint8 = 0 -// TeeType_Verifier uint8 = 1 -// TeeType_Marker uint8 = 2 -// ) - -// const ( -// ERR_Failed = "failed" -// ERR_Timeout = "timeout" -// ERR_Empty = "empty" -// ) - -// const ( -// MinBucketNameLength = 3 -// MaxBucketNameLength = 63 -// MaxDomainNameLength = 50 -// ) - -// // byte size -// const ( -// SIZE_1KiB = 1024 -// SIZE_1MiB = 1024 * SIZE_1KiB -// SIZE_1GiB = 1024 * SIZE_1MiB -// SIZE_1TiB = 1024 * SIZE_1GiB -// ) - -// const ( -// SegmentSize = 32 * SIZE_1MiB -// FragmentSize = 8 * SIZE_1MiB -// DataShards = 4 -// ParShards = 8 -// ) - -// var ( -// ERR_RPC_CONNECTION = errors.New("rpc err: connection failed") -// ERR_RPC_IP_FORMAT = errors.New("unsupported ip format") -// ERR_RPC_TIMEOUT = errors.New("timeout") -// ERR_RPC_EMPTY_VALUE = errors.New("empty") -// ERR_RPC_PRIORITYTOOLOW = "Priority is too low" -// ) - -// const ( -// FileHashLen = 64 -// RandomLen = 20 -// PeerIdPublicKeyLen = 38 -// PoISKeyLen = 256 -// TeeSignatureLen = 256 -// AccumulatorLen = 256 -// SpaceChallengeParamLen = 8 -// BloomFilterLen = 256 -// MaxSegmentNum = 1000 -// WorkerPublicKeyLen = 32 -// MasterPublicKeyLen = 32 -// EcdhPublicKeyLen = 32 -// TeeSigLen = 64 -// ) - -// type FileHash [FileHashLen]types.U8 -// type Random [RandomLen]types.U8 -// type PeerId [PeerIdPublicKeyLen]types.U8 -// type PoISKey_G [PoISKeyLen]types.U8 -// type PoISKey_N [PoISKeyLen]types.U8 -// type TeeSignature [TeeSignatureLen]types.U8 -// type Accumulator [AccumulatorLen]types.U8 -// type SpaceChallengeParam [SpaceChallengeParamLen]types.U64 -// type BloomFilter [BloomFilterLen]types.U64 -// type WorkerPublicKey [WorkerPublicKeyLen]types.U8 -// type MasterPublicKey [MasterPublicKeyLen]types.U8 -// type EcdhPublicKey [EcdhPublicKeyLen]types.U8 -// type TeeSig [TeeSigLen]types.U8 - -// type SysProperties struct { -// Ss58Format types.Bytes -// TokenDecimals types.U8 -// TokenSymbol types.Text -// SS58Prefix types.U32 -// } - -// type SysSyncState struct { -// StartingBlock types.U32 -// CurrentBlock types.U32 -// HighestBlock types.U32 -// } - -// type OssInfo struct { -// Peerid PeerId -// Domain types.Bytes -// } - -// type BucketInfo struct { -// ObjectsList []FileHash -// Authority []types.AccountID -// } - -// type MinerInfo struct { -// BeneficiaryAccount types.AccountID -// StakingAccount types.AccountID -// PeerId PeerId -// Collaterals types.U128 -// Debt types.U128 -// State types.Bytes // positive, exit, frozen, lock -// DeclarationSpace types.U128 -// IdleSpace types.U128 -// ServiceSpace types.U128 -// LockSpace types.U128 -// SpaceProofInfo types.Option[SpaceProofInfo] -// ServiceBloomFilter BloomFilter -// TeeSig TeeSig -// } - -// type SpaceProofInfo struct { -// Miner types.AccountID -// Front types.U64 -// Rear types.U64 -// PoisKey PoISKeyInfo -// Accumulator Accumulator -// } - -// type MinerReward struct { -// TotalReward types.U128 -// RewardIssued types.U128 -// OrderList []RewardOrder -// } - -// type RewardOrder struct { -// ReceiveCount types.U8 -// MaxCount types.U8 -// Atonce types.Bool -// OrderReward types.U128 -// EachAmount types.U128 -// LastReceiveBlock types.U32 -// } - -// type FileMetadata struct { -// SegmentList []SegmentInfo -// Owner []UserBrief -// FileSize types.U128 -// Completion types.U32 -// State types.U8 -// } - -// type SegmentInfo struct { -// Hash FileHash -// FragmentList []FragmentInfo -// } - -// type UserBrief struct { -// User types.AccountID -// FileName types.Bytes -// BucketName types.Bytes -// } - -// type FragmentInfo struct { -// Hash FileHash -// Avail types.Bool -// Tag types.Option[types.U32] -// Miner types.AccountID -// } - -// type StorageOrder struct { -// FileSize types.U128 -// SegmentList []SegmentList -// User UserBrief -// CompleteList []CompleteInfo -// } - -// type SegmentList struct { -// SegmentHash FileHash -// FragmentHash []FileHash -// } - -// type CompleteInfo struct { -// Index types.U8 -// Miner types.AccountID -// } - -// type UserSpaceInfo struct { -// TotalSpace types.U128 -// UsedSpace types.U128 -// LockedSpace types.U128 -// RemainingSpace types.U128 -// Start types.U32 -// Deadline types.U32 -// State types.Bytes -// } - -// type ChallengeSnapShot struct { -// MinerSnapshot MinerSnapShot -// ChallengeElement ChallengeElement -// ProveInfo ProveInfo -// } - -// type ProveInfo struct { -// Assign types.U8 -// IdleProve types.Option[IdleProveInfo] -// ServiceProve types.Option[ServiceProveInfo] -// } - -// type ChallengeElement struct { -// Start types.U32 -// IdleSlip types.U32 -// ServiceSlip types.U32 -// VerifySlip types.U32 -// SpaceParam SpaceChallengeParam -// ServiceParam QElement -// } - -// type QElement struct { -// Index []types.U32 -// Value []Random -// } - -// type MinerSnapShot struct { -// IdleSpace types.U128 -// ServiceSpace types.U128 -// ServiceBloomFilter BloomFilter -// SpaceProofInfo SpaceProofInfo -// TeeSig TeeSig -// } - -// type IdleProveInfo struct { -// TeePubkey WorkerPublicKey -// IdleProve types.Bytes -// VerifyResult types.Option[bool] -// } - -// type ServiceProveInfo struct { -// TeePubkey WorkerPublicKey -// ServiceProve types.Bytes -// VerifyResult types.Option[bool] -// } - -// type TeeWorkerInfo struct { -// Pubkey WorkerPublicKey -// EcdhPubkey EcdhPublicKey -// Version types.U32 -// LastUpdated types.U64 -// StashAccount types.Option[types.AccountID] -// AttestationProvider types.Option[types.U8] -// ConfidenceLevel types.U8 -// Features []types.U32 -// Role types.U8 // 0:Full 1:Verifier 2:Marker -// } - -// type RestoralOrderInfo struct { -// Count types.U32 -// Miner types.AccountID -// OriginMiner types.AccountID -// FragmentHash FileHash -// FileHash FileHash -// GenBlock types.U32 -// Deadline types.U32 -// } - -// type RestoralTargetInfo struct { -// Miner types.AccountID -// ServiceSpace types.U128 -// RestoredSpace types.U128 -// CoolingBlock types.U32 -// } - -// type ExpendersInfo struct { -// K types.U64 -// N types.U64 -// D types.U64 -// } - -// type PoISKeyInfo struct { -// G PoISKey_G -// N PoISKey_N -// } - -// type IdleSignInfo struct { -// Miner types.AccountID -// Rear types.U64 -// Front types.U64 -// Accumulator Accumulator -// LastOperationBlock types.U32 -// PoisKey PoISKeyInfo -// } -// type TagSigInfo struct { -// Miner types.AccountID -// Digest []DigestInfo -// Filehash FileHash -// } - -// type DigestInfo struct { -// Fragment FileHash -// TeePubkey WorkerPublicKey -// } - -// type StakingExposure struct { -// Total types.U128 -// Own types.U128 -// Others []OtherStakingExposure -// } - -// type OtherStakingExposure struct { -// Who types.AccountID -// Value types.U128 -// } - -// type StakingEraRewardPoints struct { -// Total types.U32 -// Individual []Individual -// } - -// type Individual struct { -// Acc types.AccountID -// Reward types.U32 -// } - -// type StakingNominations struct { -// Targets []types.AccountID -// SubmittedIn types.U32 -// Suppressed types.Bool -// } - -// type StakingValidatorPrefs struct { -// Commission types.U32 -// Blocked types.Bool -// } - -// type CompleteSnapShotType struct { -// MinerCount types.U32 -// TotalPower types.U128 -// } - -// type RoundRewardType struct { -// TotalReward types.U128 -// OtherReward types.U128 -// } - -// // --------------------customer----------------- -// type IdleFileMeta struct { -// BlockNum uint32 -// MinerAcc []byte -// Hash string -// } - -// type UserSpaceSt struct { -// TotalSpace string -// UsedSpace string -// LockedSpace string -// RemainingSpace string -// State string -// Start uint32 -// Deadline uint32 -// } - -// type NetSnapshot struct { -// Start uint32 -// Life uint32 -// Total_reward string -// Total_idle_space string -// Total_service_space string -// Random_index_list []uint32 -// Random [][]byte -// } - -// type MinerSnapshot struct { -// Miner string -// Idle_space string -// Service_space string -// } - -// type TeeInfo struct { -// Pubkey string -// EcdhPubkey string -// Version uint32 -// LastUpdated uint64 -// StashAccount string -// AttestationProvider uint8 -// ConfidenceLevel uint8 -// Features []uint32 -// WorkerRole uint8 // 0:Full 1:Verifier 2:Marker -// } - -// type RewardsType struct { -// Total string -// Claimed string -// } - -// type SegmentDataInfo struct { -// SegmentHash string -// FragmentHash []string -// } - -// type UserInfo struct { -// UserAccount string -// FileName string -// BucketName string -// FileSize uint64 -// } - -// type AccessInfo struct { -// r types.H160 -// c []types.H160 -// } diff --git a/core/process/process.go b/core/process/process.go index bb534c1..488ec7e 100755 --- a/core/process/process.go +++ b/core/process/process.go @@ -12,10 +12,11 @@ import ( "os" "path/filepath" + "github.com/CESSProject/cess-go-sdk/chain" + "github.com/CESSProject/cess-go-sdk/config" "github.com/CESSProject/cess-go-sdk/core/crypte" "github.com/CESSProject/cess-go-sdk/core/erasure" "github.com/CESSProject/cess-go-sdk/core/hashtree" - "github.com/CESSProject/cess-go-sdk/core/pattern" "github.com/CESSProject/cess-go-sdk/utils" "github.com/pkg/errors" ) @@ -29,7 +30,7 @@ import ( // - segmentDataInfo: segment and fragment information of the file. // - string: [fid] unique identifier for the file. // - error: error message. -func ProcessingData(file string) ([]pattern.SegmentDataInfo, string, error) { +func ProcessingData(file string) ([]chain.SegmentDataInfo, string, error) { segmentPath, err := cutfile(file) if err != nil { if segmentPath != nil { @@ -40,7 +41,7 @@ func ProcessingData(file string) ([]pattern.SegmentDataInfo, string, error) { return nil, "", errors.Wrapf(err, "[cutfile]") } - var segmentDataInfo = make([]pattern.SegmentDataInfo, len(segmentPath)) + var segmentDataInfo = make([]chain.SegmentDataInfo, len(segmentPath)) for i := 0; i < len(segmentPath); i++ { segmentDataInfo[i].SegmentHash = segmentPath[i] @@ -78,7 +79,7 @@ func ProcessingData(file string) ([]pattern.SegmentDataInfo, string, error) { // - segmentDataInfo: segment and fragment information of the file. // - string: [fid] unique identifier for the file. // - error: error message. -func ShardedEncryptionProcessing(file string, cipher string) ([]pattern.SegmentDataInfo, string, error) { +func ShardedEncryptionProcessing(file string, cipher string) ([]chain.SegmentDataInfo, string, error) { var err error var segmentPath []string if cipher != "" { @@ -112,7 +113,7 @@ func ShardedEncryptionProcessing(file string, cipher string) ([]pattern.SegmentD } } - var segmentDataInfo = make([]pattern.SegmentDataInfo, len(segmentPath)) + var segmentDataInfo = make([]chain.SegmentDataInfo, len(segmentPath)) for i := 0; i < len(segmentPath); i++ { segmentDataInfo[i].SegmentHash = segmentPath[i] @@ -152,13 +153,13 @@ func cutfile(file string) ([]string, error) { return nil, errors.New("empty file") } baseDir := filepath.Dir(file) - segmentCount := fstat.Size() / pattern.SegmentSize - if fstat.Size()%int64(pattern.SegmentSize) != 0 { + segmentCount := fstat.Size() / config.SegmentSize + if fstat.Size()%int64(config.SegmentSize) != 0 { segmentCount++ } segment := make([]string, segmentCount) - buf := make([]byte, pattern.SegmentSize) + buf := make([]byte, config.SegmentSize) f, err := os.Open(file) if err != nil { return segment, err @@ -167,7 +168,7 @@ func cutfile(file string) ([]string, error) { var num int for i := int64(0); i < segmentCount; i++ { - f.Seek(pattern.SegmentSize*i, 0) + f.Seek(config.SegmentSize*i, 0) num, err = f.Read(buf) if err != nil && err != io.EOF { return segment, err @@ -175,11 +176,11 @@ func cutfile(file string) ([]string, error) { if num == 0 { return segment, errors.New("read file is empty") } - if num < pattern.SegmentSize { + if num < config.SegmentSize { if i+1 != segmentCount { return segment, errors.New("read file err") } - copy(buf[num:], make([]byte, pattern.SegmentSize-num, pattern.SegmentSize-num)) + copy(buf[num:], make([]byte, config.SegmentSize-num, config.SegmentSize-num)) } hash, err := utils.CalcSHA256(buf) @@ -208,7 +209,7 @@ func cutFileWithEncryption(file string) ([]string, error) { return nil, errors.New("empty file") } baseDir := filepath.Dir(file) - segmentSize := pattern.SegmentSize - 16 + segmentSize := config.SegmentSize - 16 segmentCount := fstat.Size() / int64(segmentSize) if fstat.Size()%int64(segmentSize) != 0 { segmentCount++ diff --git a/defaults.go b/defaults.go index fe21937..e19cc48 100755 --- a/defaults.go +++ b/defaults.go @@ -7,7 +7,7 @@ package sdkgo -import "github.com/CESSProject/cess-go-sdk/core/pattern" +import "github.com/CESSProject/cess-go-sdk/chain" // DefaultRpcAddrs configures the default rpc address var DefaultRpcAddrs = func(cfg *Config) error { @@ -21,7 +21,7 @@ var DefaultRpcAddrs = func(cfg *Config) error { // DefaultTimeout configures the default transaction waiting timeout var DefaultTimeout = func(cfg *Config) error { - return cfg.Apply(TransactionTimeout(pattern.BlockInterval)) + return cfg.Apply(TransactionTimeout(chain.BlockInterval)) } // Complete list of default options and when to fallback on them. diff --git a/example/evm/evm.go b/example/evm/evm.go index c299cee..83407f9 100644 --- a/example/evm/evm.go +++ b/example/evm/evm.go @@ -10,7 +10,6 @@ import ( cess "github.com/CESSProject/cess-go-sdk" "github.com/CESSProject/cess-go-sdk/chain" - "github.com/CESSProject/cess-go-sdk/core/pattern" "github.com/centrifuge/go-substrate-rpc-client/v4/types" ) @@ -42,7 +41,7 @@ func main() { value types.U256 gasLimit types.U64 maxFeePerGas types.U256 - accessList []pattern.AccessInfo + accessList []chain.AccessInfo ) s_h160, err := hex.DecodeString("1e3e1c69dfbd27d398e92da4844a9abdc2786ac0") diff --git a/example/parseEvent/parseevent.go b/example/parseEvent/parseevent.go index 8de3144..7e314e6 100755 --- a/example/parseEvent/parseevent.go +++ b/example/parseEvent/parseevent.go @@ -6,7 +6,7 @@ import ( "time" cess "github.com/CESSProject/cess-go-sdk" - "github.com/CESSProject/cess-go-sdk/core/event" + "github.com/CESSProject/cess-go-sdk/chain" "github.com/centrifuge/go-substrate-rpc-client/v4/types" ) @@ -26,7 +26,7 @@ var RPC_ADDRS = []string{ } type MyEvent struct { - Sminer_FaucetTopUpMoney []event.Event_FaucetTopUpMoney + Sminer_FaucetTopUpMoney []chain.Event_FaucetTopUpMoney types.EventRecords } diff --git a/example/process/process.go b/example/process/process.go index 2c9d518..2c741ed 100755 --- a/example/process/process.go +++ b/example/process/process.go @@ -11,13 +11,13 @@ import ( "fmt" "log" - "github.com/CESSProject/cess-go-sdk/core/pattern" + "github.com/CESSProject/cess-go-sdk/chain" "github.com/CESSProject/cess-go-sdk/core/process" ) type result struct { Fid string - SegmentData []pattern.SegmentDataInfo + SegmentData []chain.SegmentDataInfo } func main() { diff --git a/options.go b/options.go index 29090ef..258d122 100755 --- a/options.go +++ b/options.go @@ -10,7 +10,7 @@ package sdkgo import ( "time" - "github.com/CESSProject/cess-go-sdk/core/pattern" + "github.com/CESSProject/cess-go-sdk/chain" ) // ConnectRpcAddrs configuration rpc address @@ -32,8 +32,8 @@ func Mnemonic(mnemonic string) Option { // TransactionTimeout configures the waiting timeout for a transaction func TransactionTimeout(timeout time.Duration) Option { return func(cfg *Config) error { - if timeout < pattern.BlockInterval { - cfg.Timeout = pattern.BlockInterval + if timeout < chain.BlockInterval { + cfg.Timeout = chain.BlockInterval } else { cfg.Timeout = timeout } diff --git a/sdk.go b/sdk.go index b08c21b..e5a32d2 100755 --- a/sdk.go +++ b/sdk.go @@ -11,15 +11,8 @@ import ( "context" "github.com/CESSProject/cess-go-sdk/chain" - "github.com/CESSProject/cess-go-sdk/config" ) -// Config describes a set of settings for the sdk. -type Config = config.Config - -// Option is a client config option that can be given to the client constructor -type Option = config.Option - // New constructs a new sdk client with the given options, falling back on // reasonable defaults. The defaults are: // diff --git a/utils/bucket.go b/utils/bucket.go index 758fb03..49701fd 100755 --- a/utils/bucket.go +++ b/utils/bucket.go @@ -11,13 +11,13 @@ import ( "regexp" "strings" - "github.com/CESSProject/cess-go-sdk/core/pattern" + "github.com/CESSProject/cess-go-sdk/config" ) var re = regexp.MustCompile(`^[a-zA-Z0-9._-]+$`) func CheckBucketName(name string) bool { - if len(name) < pattern.MinBucketNameLength || len(name) > pattern.MaxBucketNameLength { + if len(name) < config.MinBucketNameLength || len(name) > config.MaxBucketNameLength { return false } diff --git a/utils/typ.go b/utils/typ.go deleted file mode 100644 index 248d78d..0000000 --- a/utils/typ.go +++ /dev/null @@ -1,68 +0,0 @@ -/* - Copyright (C) CESS. All rights reserved. - Copyright (C) Cumulus Encrypted Storage System. All rights reserved. - - SPDX-License-Identifier: Apache-2.0 -*/ - -package utils - -import ( - "errors" - - "github.com/CESSProject/cess-go-sdk/core/pattern" - "github.com/centrifuge/go-substrate-rpc-client/v4/types" -) - -func BytesToFileHash(val []byte) (pattern.FileHash, error) { - if len(val) != pattern.FileHashLen { - return pattern.FileHash{}, errors.New("[BytesToFileHash] invalid length") - } - var filehash pattern.FileHash - for k, v := range val { - filehash[k] = types.U8(v) - } - return filehash, nil -} - -func BytesToWorkPublickey(val []byte) (pattern.WorkerPublicKey, error) { - if len(val) != pattern.WorkerPublicKeyLen { - return pattern.WorkerPublicKey{}, errors.New("[BytesToWorkPublickey] invalid length") - } - var pubkey pattern.WorkerPublicKey - for k, v := range val { - pubkey[k] = types.U8(v) - } - return pubkey, nil -} - -func BytesToPoISKeyInfo(g, n []byte) (pattern.PoISKeyInfo, error) { - if len(g) != pattern.PoISKeyLen || len(n) != pattern.PoISKeyLen { - return pattern.PoISKeyInfo{}, errors.New("[BytesToPoISKeyInfo] invalid length") - } - var poisKey pattern.PoISKeyInfo - for i := 0; i < pattern.PoISKeyLen; i++ { - poisKey.G[i] = types.U8(g[i]) - poisKey.N[i] = types.U8(n[i]) - } - return poisKey, nil -} -func BytesToBloomFilter(val []byte) (pattern.BloomFilter, error) { - if len(val) != pattern.BloomFilterLen { - return pattern.BloomFilter{}, errors.New("[BytesToBloomFilter] invalid length") - } - var bloomfilter pattern.BloomFilter - for i := 0; i < pattern.BloomFilterLen; i++ { - bloomfilter[i] = types.U64(val[i]) - } - return bloomfilter, nil -} - -func IsWorkerPublicKeyAllZero(puk pattern.WorkerPublicKey) bool { - for i := 0; i < pattern.WorkerPublicKeyLen; i++ { - if puk[i] != 0 { - return false - } - } - return true -} From c9cc5ce0b58f63b0101e7c6cba7d641ea1e5345c Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Thu, 9 May 2024 12:01:27 +0800 Subject: [PATCH 07/18] update oss --- chain/deoss.go | 584 ++++++++++++++------------ chain/event.go | 423 ++++++++++--------- chain/pattern.go | 14 +- config/config.go | 2 +- core/sdk/sdk.go | 1034 +++++++++++++++++++++++----------------------- 5 files changed, 1059 insertions(+), 998 deletions(-) diff --git a/chain/deoss.go b/chain/deoss.go index b86ff3d..3fe182c 100755 --- a/chain/deoss.go +++ b/chain/deoss.go @@ -13,7 +13,7 @@ import ( "strings" "time" - "github.com/CESSProject/cess-go-sdk/core/pattern" + "github.com/CESSProject/cess-go-sdk/config" "github.com/CESSProject/cess-go-sdk/utils" "github.com/centrifuge/go-substrate-rpc-client/v4/types" "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" @@ -21,67 +21,108 @@ import ( "github.com/pkg/errors" ) -func (c *ChainClient) QueryDeOSSInfo(accountID []byte) (pattern.OssInfo, error) { +// QueryOss query oss info +// - accountID: oss's account +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - OssInfo: oss info +// - error: error message +func (c *ChainClient) QueryOss(accountID []byte, block int32) (OssInfo, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data pattern.OssInfo + var data OssInfo if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + return data, ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.OSS, pattern.OSS, accountID) + key, err := types.CreateStorageKey(c.metadata, Oss, Oss, accountID) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.OSS, pattern.OSS, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Oss, Oss, err) return data, err } - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + 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(), Oss, Oss, err) + c.SetChainState(false) + return data, err + } + if !ok { + return data, ERR_RPC_EMPTY_VALUE + } + return data, nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + return data, err + } + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.OSS, pattern.OSS, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), Oss, Oss, err) c.SetChainState(false) return data, err } if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + return data, ERR_RPC_EMPTY_VALUE } return data, nil } -func (c *ChainClient) QueryAllDeOSSInfo() ([]pattern.OssInfo, error) { +// QueryAllOss query all oss info +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - []OssInfo: all oss info +// - error: error message +func (c *ChainClient) QueryAllOss(block int32) ([]OssInfo, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var result []pattern.OssInfo + var result []OssInfo if !c.GetChainState() { - return nil, pattern.ERR_RPC_CONNECTION + return nil, ERR_RPC_CONNECTION } - key := createPrefixedKey(pattern.OSS, pattern.OSS) + key := createPrefixedKey(Oss, Oss) keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), pattern.OSS, pattern.OSS, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), Oss, Oss, err) c.SetChainState(false) return nil, err } - set, err := c.api.RPC.State.QueryStorageAtLatest(keys) - if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), pattern.OSS, pattern.OSS, err) - c.SetChainState(false) - return nil, err + var set []types.StorageChangeSet + if block < 0 { + set, err = c.api.RPC.State.QueryStorageAtLatest(keys) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Oss, Oss, err) + c.SetChainState(false) + return nil, err + } + } else { + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + return nil, err + } + set, err = c.api.RPC.State.QueryStorageAt(keys, blockhash) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Oss, Oss, err) + c.SetChainState(false) + return nil, err + } } - for _, elem := range set { for _, change := range elem.Changes { - var data pattern.OssInfo + var data OssInfo if err := codec.Decode(change.StorageData, &data); err != nil { continue } @@ -91,7 +132,13 @@ func (c *ChainClient) QueryAllDeOSSInfo() ([]pattern.OssInfo, error) { return result, nil } -func (c *ChainClient) QueryAllDeOSSPeerId() ([]string, error) { +// QueryAllOssPeerId query all oss's peer id +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - []string: all oss's peer id +// - error: error message +func (c *ChainClient) QueryAllOssPeerId(block int32) ([]string, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -100,27 +147,41 @@ func (c *ChainClient) QueryAllDeOSSPeerId() ([]string, error) { var result []string if !c.GetChainState() { - return nil, pattern.ERR_RPC_CONNECTION + return nil, ERR_RPC_CONNECTION } - key := createPrefixedKey(pattern.OSS, pattern.OSS) + key := createPrefixedKey(Oss, Oss) keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), pattern.OSS, pattern.OSS, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), Oss, Oss, err) c.SetChainState(false) return nil, err } - set, err := c.api.RPC.State.QueryStorageAtLatest(keys) - if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), pattern.OSS, pattern.OSS, err) - c.SetChainState(false) - return nil, err + var set []types.StorageChangeSet + if block < 0 { + set, err = c.api.RPC.State.QueryStorageAtLatest(keys) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Oss, Oss, err) + c.SetChainState(false) + return nil, err + } + } else { + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + return nil, err + } + set, err = c.api.RPC.State.QueryStorageAt(keys, blockhash) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Oss, Oss, err) + c.SetChainState(false) + return nil, err + } } for _, elem := range set { for _, change := range elem.Changes { - var data pattern.OssInfo + var data OssInfo if err := codec.Decode(change.StorageData, &data); err != nil { continue } @@ -130,7 +191,14 @@ func (c *ChainClient) QueryAllDeOSSPeerId() ([]string, error) { return result, nil } -func (c *ChainClient) QueryAuthorizedAccountIDs(accountID []byte) ([]types.AccountID, error) { +// QueryAuthorityList query authorised all accounts +// - accountID: account to be queried +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - []types.AccountID: authorised all accounts +// - error: error message +func (c *ChainClient) QueryAuthorityList(accountID []byte, block int32) ([]types.AccountID, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -139,41 +207,54 @@ func (c *ChainClient) QueryAuthorizedAccountIDs(accountID []byte) ([]types.Accou var data []types.AccountID if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + return data, ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.OSS, pattern.AUTHORITYLIST, accountID) + key, err := types.CreateStorageKey(c.metadata, Oss, AuthorityList, accountID) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.OSS, pattern.AUTHORITYLIST, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Oss, AuthorityList, err) return data, err } - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + 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(), Oss, AuthorityList, err) + c.SetChainState(false) + return data, err + } + if !ok { + return data, ERR_RPC_EMPTY_VALUE + } + return data, nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.OSS, pattern.AUTHORITYLIST, err) c.SetChainState(false) return data, err } - if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE - } - return data, nil -} - -func (c *ChainClient) QueryAuthorizedAccounts(accountID []byte) ([]string, error) { - acc, err := c.QueryAuthorizedAccountIDs(accountID) + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - return nil, err + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), Oss, AuthorityList, err) + c.SetChainState(false) + return data, err } - var result = make([]string, len(acc)) - for k, v := range acc { - result[k], _ = utils.EncodePublicKeyAsCessAccount(v[:]) + if !ok { + return data, ERR_RPC_EMPTY_VALUE } - return result, nil + return data, nil } -func (c *ChainClient) RegisterDeOSS(peerId []byte, domain string) (string, error) { +// Authorize to authorise space usage to another account +// - accountID: authorised account +// +// Return: +// - string: block hash +// - error: error message +// +// Node: +// - accountID should be oss account +func (c *ChainClient) Authorize(accountID []byte) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -183,63 +264,41 @@ func (c *ChainClient) RegisterDeOSS(peerId []byte, domain string) (string, error }() var ( - err error - txhash string - call types.Call + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - var peerid pattern.PeerId - if len(peerid) != len(peerId) { - return txhash, errors.New("register deoss: invalid peerid") - } - for i := 0; i < len(peerid); i++ { - peerid[i] = types.U8(peerId[i]) - } - - if len(domain) > pattern.MaxDomainNameLength { - return txhash, fmt.Errorf("register deoss: Domain name length cannot exceed %v characters", pattern.MaxDomainNameLength) - } - - err = utils.CheckDomain(domain) + acc, err := types.NewAccountID(accountID) if err != nil { - return txhash, errors.New("register deoss: invalid domain") + return blockhash, errors.Wrap(err, "[NewAccountID]") } - call, err = types.NewCall(c.metadata, pattern.TX_OSS_REGISTER, peerid, types.NewBytes([]byte(domain))) + call, err := types.NewCall(c.metadata, TX_Oss_Authorize, *acc) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_REGISTER, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Oss_Authorize, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_REGISTER, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Oss_Authorize, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_REGISTER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Oss_Authorize, err) c.SetChainState(false) - return txhash, err + return blockhash, err } - if !ok { - keyStr, _ := utils.NumsToByteStr(key, map[string]bool{}) - return txhash, fmt.Errorf( - "chain rpc.state.GetStorageLatest[%v]: %v", - keyStr, - pattern.ERR_RPC_EMPTY_VALUE, - ) + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -255,30 +314,33 @@ func (c *ChainClient) RegisterDeOSS(peerId []byte, domain string) (string, error // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_REGISTER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Oss_Authorize, err) c.SetChainState(false) - return txhash, err + return blockhash, err } + <-c.txTicker.C + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_REGISTER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_Authorize, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_REGISTER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_Authorize, err) c.SetChainState(false) - return txhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]") + return blockhash, err } } defer sub.Unsubscribe() @@ -290,19 +352,25 @@ func (c *ChainClient) RegisterDeOSS(peerId []byte, domain string) (string, error select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() - _, err = c.RetrieveEvent_Oss_OssRegister(status.AsInBlock) - return txhash, err + blockhash = status.AsInBlock.Hex() + _, err = c.RetrieveEvent_Oss_Authorize(status.AsInBlock) + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) UpdateDeOSS(peerId string, domain string) (string, error) { +// CancelAuthorize cancels authorisation for an account +// - accountID: account with cancelled authorisations +// +// Return: +// - string: block hash +// - error: error message +func (c *ChainClient) CancelAuthorize(accountID []byte) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -312,56 +380,36 @@ func (c *ChainClient) UpdateDeOSS(peerId string, domain string) (string, error) }() var ( - err error - txhash string - call types.Call + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - var peerid pattern.PeerId - if len(peerid) != len(peerId) { - return txhash, errors.New("update deoss: invalid peerid") - } - for i := 0; i < len(peerid); i++ { - peerid[i] = types.U8(peerId[i]) - } - - if len(domain) > pattern.MaxSubmitedIdleFileMeta { - return txhash, fmt.Errorf("register deoss: domain name length cannot exceed %v", pattern.MaxSubmitedIdleFileMeta) - } - - err = utils.CheckDomain(domain) - if err != nil { - return txhash, errors.New("register deoss: invalid domain name") - } - - call, err = types.NewCall(c.metadata, pattern.TX_OSS_UPDATE, peerid, types.NewBytes([]byte(domain))) + call, err := types.NewCall(c.metadata, TX_Oss_CancelAuthorize, accountID) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_UPDATE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Oss_CancelAuthorize, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_UPDATE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Oss_CancelAuthorize, err) + return blockhash, err } + ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_UPDATE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Oss_CancelAuthorize, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -377,9 +425,9 @@ func (c *ChainClient) UpdateDeOSS(peerId string, domain string) (string, error) // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_UPDATE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Oss_CancelAuthorize, err) c.SetChainState(false) - return txhash, err + return blockhash, err } <-c.txTicker.C @@ -387,44 +435,54 @@ func (c *ChainClient) UpdateDeOSS(peerId string, domain string) (string, error) // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_UPDATE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_CancelAuthorize, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_UPDATE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_CancelAuthorize, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() + timeout := time.NewTimer(c.packingTime) defer timeout.Stop() + for { select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() - _, err = c.RetrieveEvent_Oss_OssUpdate(status.AsInBlock) - return txhash, err + blockhash = status.AsInBlock.Hex() + _, err = c.RetrieveEvent_Oss_CancelAuthorize(status.AsInBlock) + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) ExitDeOSS() (string, error) { +// RegisterOss registered as oss role +// - peerId: peer id +// - domain: domain name, can be empty +// +// Return: +// - string: block hash +// - error: error message +func (c *ChainClient) RegisterOss(peerId []byte, domain string) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -434,41 +492,59 @@ func (c *ChainClient) ExitDeOSS() (string, error) { }() var ( - err error - txhash string - call types.Call + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION + } + + var peerid PeerId + if len(peerId) != PeerIdPublicKeyLen { + return blockhash, errors.New("register oss: invalid peerid") + } + for i := 0; i < len(peerid); i++ { + peerid[i] = types.U8(peerId[i]) + } + + if len(domain) > config.MaxDomainNameLength { + return blockhash, fmt.Errorf("register deoss: Domain name length cannot exceed %v characters", config.MaxDomainNameLength) } - call, err = types.NewCall(c.metadata, pattern.TX_OSS_DESTROY) + err := utils.CheckDomain(domain) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_DESTROY, err) - c.SetChainState(false) - return txhash, err + return blockhash, errors.New("register oss: invalid domain") + } + + call, err := types.NewCall(c.metadata, TX_Oss_Register, peerid, types.NewBytes([]byte(domain))) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Oss_Register, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_DESTROY, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Oss_Register, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_DESTROY, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Oss_Register, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + keyStr, _ := utils.NumsToByteStr(key, map[string]bool{}) + return blockhash, fmt.Errorf( + "chain rpc.state.GetStorageLatest[%v]: %v", + keyStr, + ERR_RPC_EMPTY_VALUE, + ) } o := types.SignatureOptions{ @@ -484,32 +560,31 @@ func (c *ChainClient) ExitDeOSS() (string, error) { // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_DESTROY, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Oss_Register, err) c.SetChainState(false) - return txhash, err + return blockhash, err } - <-c.txTicker.C - // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_DESTROY, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_Register, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_DESTROY, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_Register, err) c.SetChainState(false) - return txhash, err + return blockhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]") } } defer sub.Unsubscribe() @@ -521,19 +596,26 @@ func (c *ChainClient) ExitDeOSS() (string, error) { select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() - _, err = c.RetrieveEvent_Oss_OssDestroy(status.AsInBlock) - return txhash, err + blockhash = status.AsInBlock.Hex() + _, err = c.RetrieveEvent_Oss_OssRegister(status.AsInBlock) + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) AuthorizeSpace(ossAccount string) (string, error) { +// UpdateOss update oss's peerId or domain +// - peerId: peer id +// - domain: domain name +// +// Return: +// - string: block hash +// - error: error message +func (c *ChainClient) UpdateOss(peerId string, domain string) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -543,61 +625,52 @@ func (c *ChainClient) AuthorizeSpace(ossAccount string) (string, error) { }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - puk, err := utils.ParsingPublickey(ossAccount) - if err != nil { - return txhash, errors.Wrap(err, "[ParsingPublickey]") + var peerid PeerId + if len(peerid) != len(peerId) { + return blockhash, errors.New("update oss: invalid peerid") + } + for i := 0; i < len(peerid); i++ { + peerid[i] = types.U8(peerId[i]) } - acc, err := types.NewAccountID(puk) - if err != nil { - return txhash, errors.Wrap(err, "[NewAccountID]") + if len(domain) > config.MaxDomainNameLength { + return blockhash, fmt.Errorf("update oss: domain name length cannot exceed %v", config.MaxDomainNameLength) } - list, err := c.QueryAuthorizedAccounts(c.GetSignatureAccPulickey()) + err := utils.CheckDomain(domain) if err != nil { - if err.Error() != pattern.ERR_Empty { - return txhash, errors.Wrap(err, "[QueryAuthorizedAccounts]") - } - } else { - for _, v := range list { - if v == ossAccount { - return "", nil - } - } + return blockhash, errors.New("update oss: invalid domain name") } - call, err := types.NewCall(c.metadata, pattern.TX_OSS_AUTHORIZE, *acc) + call, err := types.NewCall(c.metadata, TX_Oss_Update, peerid, types.NewBytes([]byte(domain))) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_AUTHORIZE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Oss_Update, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_AUTHORIZE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Oss_Update, err) + return blockhash, err } - ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_AUTHORIZE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Oss_Update, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -613,9 +686,9 @@ func (c *ChainClient) AuthorizeSpace(ossAccount string) (string, error) { // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_AUTHORIZE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Oss_Update, err) c.SetChainState(false) - return txhash, err + return blockhash, err } <-c.txTicker.C @@ -623,46 +696,50 @@ func (c *ChainClient) AuthorizeSpace(ossAccount string) (string, error) { // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_AUTHORIZE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_Update, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_AUTHORIZE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_Update, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() - timeout := time.NewTimer(c.packingTime) defer timeout.Stop() - for { select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() - _, err = c.RetrieveEvent_Oss_Authorize(status.AsInBlock) - return txhash, err + blockhash = status.AsInBlock.Hex() + _, err = c.RetrieveEvent_Oss_OssUpdate(status.AsInBlock) + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) UnAuthorizeSpace(oss_acc string) (string, error) { +// DestroyOss destroys the oss role of the current account +// +// Return: +// - string: block hash +// - error: error message +func (c *ChainClient) DestroyOss() (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -672,43 +749,37 @@ func (c *ChainClient) UnAuthorizeSpace(oss_acc string) (string, error) { }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - pubkey, err := utils.ParsingPublickey(oss_acc) + call, err := types.NewCall(c.metadata, TX_Oss_Destroy) if err != nil { - return txhash, errors.Wrap(err, "[ParsingPublickey]") - } - - call, err := types.NewCall(c.metadata, pattern.TX_OSS_UNAUTHORIZE) - if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_AUTHORIZE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Oss_Destroy, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, pubkey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_AUTHORIZE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Oss_Destroy, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_AUTHORIZE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Oss_Destroy, err) c.SetChainState(false) - return txhash, err + return blockhash, err } + if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -724,9 +795,9 @@ func (c *ChainClient) UnAuthorizeSpace(oss_acc string) (string, error) { // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_AUTHORIZE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Oss_Destroy, err) c.SetChainState(false) - return txhash, err + return blockhash, err } <-c.txTicker.C @@ -734,22 +805,23 @@ func (c *ChainClient) UnAuthorizeSpace(oss_acc string) (string, error) { // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_AUTHORIZE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_Destroy, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_OSS_AUTHORIZE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_Destroy, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -761,14 +833,14 @@ func (c *ChainClient) UnAuthorizeSpace(oss_acc string) (string, error) { select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() - _, err = c.RetrieveEvent_Oss_CancelAuthorize(status.AsInBlock) - return txhash, err + blockhash = status.AsInBlock.Hex() + _, err = c.RetrieveEvent_Oss_OssDestroy(status.AsInBlock) + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } diff --git a/chain/event.go b/chain/event.go index 188dd68..2a5619c 100755 --- a/chain/event.go +++ b/chain/event.go @@ -15,7 +15,6 @@ import ( "strconv" "strings" - "github.com/CESSProject/cess-go-sdk/core/event" "github.com/CESSProject/cess-go-sdk/core/pattern" "github.com/CESSProject/cess-go-sdk/utils" "github.com/centrifuge/go-substrate-rpc-client/v4/registry/parser" @@ -57,14 +56,14 @@ func (c *ChainClient) DecodeEventNameFromBlockhash(blockhash types.Hash) ([]stri return result, nil } -func (c *ChainClient) RetrieveEvent_FileBank_ClaimRestoralOrder(blockhash types.Hash) (event.Event_ClaimRestoralOrder, error) { - var result event.Event_ClaimRestoralOrder +func (c *ChainClient) RetrieveEvent_FileBank_ClaimRestoralOrder(blockhash types.Hash) (Event_ClaimRestoralOrder, error) { + var result Event_ClaimRestoralOrder events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.FileBankClaimRestoralOrder { + if e.Name == FileBankClaimRestoralOrder { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -102,17 +101,17 @@ func (c *ChainClient) RetrieveEvent_FileBank_ClaimRestoralOrder(blockhash types. } } } - return result, errors.Errorf("failed: no %s event found", event.FileBankClaimRestoralOrder) + return result, errors.Errorf("failed: no %s event found", FileBankClaimRestoralOrder) } -func (c *ChainClient) RetrieveEvent_Audit_SubmitIdleProof(blockhash types.Hash) (event.Event_SubmitIdleProof, error) { - var result event.Event_SubmitIdleProof +func (c *ChainClient) RetrieveEvent_Audit_SubmitIdleProof(blockhash types.Hash) (Event_SubmitIdleProof, error) { + var result Event_SubmitIdleProof events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.AuditSubmitIdleProof { + if e.Name == AuditSubmitIdleProof { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -150,17 +149,17 @@ func (c *ChainClient) RetrieveEvent_Audit_SubmitIdleProof(blockhash types.Hash) } } } - return result, errors.Errorf("failed: no %s event found", event.AuditSubmitIdleProof) + return result, errors.Errorf("failed: no %s event found", AuditSubmitIdleProof) } -func (c *ChainClient) RetrieveEvent_Audit_SubmitServiceProof(blockhash types.Hash) (event.Event_SubmitServiceProof, error) { - var result event.Event_SubmitServiceProof +func (c *ChainClient) RetrieveEvent_Audit_SubmitServiceProof(blockhash types.Hash) (Event_SubmitServiceProof, error) { + var result Event_SubmitServiceProof events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.AuditSubmitServiceProof { + if e.Name == AuditSubmitServiceProof { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -198,17 +197,17 @@ func (c *ChainClient) RetrieveEvent_Audit_SubmitServiceProof(blockhash types.Has } } } - return result, errors.Errorf("failed: no %s event found", event.AuditSubmitServiceProof) + return result, errors.Errorf("failed: no %s event found", AuditSubmitServiceProof) } -func (c *ChainClient) RetrieveEvent_Audit_SubmitIdleVerifyResult(blockhash types.Hash) (event.Event_SubmitIdleVerifyResult, error) { - var result event.Event_SubmitIdleVerifyResult +func (c *ChainClient) RetrieveEvent_Audit_SubmitIdleVerifyResult(blockhash types.Hash) (Event_SubmitIdleVerifyResult, error) { + var result Event_SubmitIdleVerifyResult events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.AuditSubmitIdleVerifyResult { + if e.Name == AuditSubmitIdleVerifyResult { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -245,17 +244,17 @@ func (c *ChainClient) RetrieveEvent_Audit_SubmitIdleVerifyResult(blockhash types } } } - return result, errors.Errorf("failed: no %s event found", event.AuditSubmitIdleVerifyResult) + return result, errors.Errorf("failed: no %s event found", AuditSubmitIdleVerifyResult) } -func (c *ChainClient) RetrieveEvent_Audit_SubmitServiceVerifyResult(blockhash types.Hash) (event.Event_SubmitServiceVerifyResult, error) { - var result event.Event_SubmitServiceVerifyResult +func (c *ChainClient) RetrieveEvent_Audit_SubmitServiceVerifyResult(blockhash types.Hash) (Event_SubmitServiceVerifyResult, error) { + var result Event_SubmitServiceVerifyResult events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.AuditSubmitServiceVerifyResult { + if e.Name == AuditSubmitServiceVerifyResult { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -292,17 +291,17 @@ func (c *ChainClient) RetrieveEvent_Audit_SubmitServiceVerifyResult(blockhash ty } } } - return result, errors.Errorf("failed: no %s event found", event.AuditSubmitServiceVerifyResult) + return result, errors.Errorf("failed: no %s event found", AuditSubmitServiceVerifyResult) } -func (c *ChainClient) RetrieveEvent_Oss_OssUpdate(blockhash types.Hash) (event.Event_OssUpdate, error) { - var result event.Event_OssUpdate +func (c *ChainClient) RetrieveEvent_Oss_OssUpdate(blockhash types.Hash) (Event_OssUpdate, error) { + var result Event_OssUpdate events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.OssOssUpdate { + if e.Name == OssOssUpdate { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -339,17 +338,17 @@ func (c *ChainClient) RetrieveEvent_Oss_OssUpdate(blockhash types.Hash) (event.E } } } - return result, errors.Errorf("failed: no %s event found", event.OssOssUpdate) + return result, errors.Errorf("failed: no %s event found", OssOssUpdate) } -func (c *ChainClient) RetrieveEvent_Oss_OssRegister(blockhash types.Hash) (event.Event_OssRegister, error) { - var result event.Event_OssRegister +func (c *ChainClient) RetrieveEvent_Oss_OssRegister(blockhash types.Hash) (Event_OssRegister, error) { + var result Event_OssRegister events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.OssOssRegister { + if e.Name == OssOssRegister { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -386,17 +385,17 @@ func (c *ChainClient) RetrieveEvent_Oss_OssRegister(blockhash types.Hash) (event } } } - return result, errors.Errorf("failed: no %s event found", event.OssOssRegister) + return result, errors.Errorf("failed: no %s event found", OssOssRegister) } -func (c *ChainClient) RetrieveEvent_Oss_OssDestroy(blockhash types.Hash) (event.Event_OssDestroy, error) { - var result event.Event_OssDestroy +func (c *ChainClient) RetrieveEvent_Oss_OssDestroy(blockhash types.Hash) (Event_OssDestroy, error) { + var result Event_OssDestroy events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.OssOssDestroy { + if e.Name == OssOssDestroy { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -433,17 +432,17 @@ func (c *ChainClient) RetrieveEvent_Oss_OssDestroy(blockhash types.Hash) (event. } } } - return result, errors.Errorf("failed: no %s event found", event.OssOssDestroy) + return result, errors.Errorf("failed: no %s event found", OssOssDestroy) } -func (c *ChainClient) RetrieveEvent_Oss_Authorize(blockhash types.Hash) (event.Event_Authorize, error) { - var result event.Event_Authorize +func (c *ChainClient) RetrieveEvent_Oss_Authorize(blockhash types.Hash) (Event_Authorize, error) { + var result Event_Authorize events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.OssAuthorize { + if e.Name == OssAuthorize { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -480,17 +479,17 @@ func (c *ChainClient) RetrieveEvent_Oss_Authorize(blockhash types.Hash) (event.E } } } - return result, errors.Errorf("failed: no %s event found", event.OssAuthorize) + return result, errors.Errorf("failed: no %s event found", OssAuthorize) } -func (c *ChainClient) RetrieveEvent_Oss_CancelAuthorize(blockhash types.Hash) (event.Event_CancelAuthorize, error) { - var result event.Event_CancelAuthorize +func (c *ChainClient) RetrieveEvent_Oss_CancelAuthorize(blockhash types.Hash) (Event_CancelAuthorize, error) { + var result Event_CancelAuthorize events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.OssCancelAuthorize { + if e.Name == OssCancelAuthorize { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -527,17 +526,17 @@ func (c *ChainClient) RetrieveEvent_Oss_CancelAuthorize(blockhash types.Hash) (e } } } - return result, errors.Errorf("failed: no %s event found", event.OssCancelAuthorize) + return result, errors.Errorf("failed: no %s event found", OssCancelAuthorize) } -func (c *ChainClient) RetrieveEvent_FileBank_UploadDeclaration(blockhash types.Hash) (event.Event_UploadDeclaration, error) { - var result event.Event_UploadDeclaration +func (c *ChainClient) RetrieveEvent_FileBank_UploadDeclaration(blockhash types.Hash) (Event_UploadDeclaration, error) { + var result Event_UploadDeclaration events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.FileBankUploadDeclaration { + if e.Name == FileBankUploadDeclaration { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -574,17 +573,17 @@ func (c *ChainClient) RetrieveEvent_FileBank_UploadDeclaration(blockhash types.H } } } - return result, errors.Errorf("failed: no %s event found", event.FileBankUploadDeclaration) + return result, errors.Errorf("failed: no %s event found", FileBankUploadDeclaration) } -func (c *ChainClient) RetrieveEvent_FileBank_CreateBucket(blockhash types.Hash) (event.Event_CreateBucket, error) { - var result event.Event_CreateBucket +func (c *ChainClient) RetrieveEvent_FileBank_CreateBucket(blockhash types.Hash) (Event_CreateBucket, error) { + var result Event_CreateBucket events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.FileBankCreateBucket { + if e.Name == FileBankCreateBucket { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -621,17 +620,17 @@ func (c *ChainClient) RetrieveEvent_FileBank_CreateBucket(blockhash types.Hash) } } } - return result, errors.Errorf("failed: no %s event found", event.FileBankCreateBucket) + return result, errors.Errorf("failed: no %s event found", FileBankCreateBucket) } -func (c *ChainClient) RetrieveEvent_FileBank_DeleteBucket(blockhash types.Hash) (event.Event_DeleteBucket, error) { - var result event.Event_DeleteBucket +func (c *ChainClient) RetrieveEvent_FileBank_DeleteBucket(blockhash types.Hash) (Event_DeleteBucket, error) { + var result Event_DeleteBucket events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.FileBankDeleteBucket { + if e.Name == FileBankDeleteBucket { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -668,17 +667,17 @@ func (c *ChainClient) RetrieveEvent_FileBank_DeleteBucket(blockhash types.Hash) } } } - return result, errors.Errorf("failed: no %s event found", event.FileBankDeleteBucket) + return result, errors.Errorf("failed: no %s event found", FileBankDeleteBucket) } -func (c *ChainClient) RetrieveEvent_FileBank_DeleteFile(blockhash types.Hash) (event.Event_DeleteFile, error) { - var result event.Event_DeleteFile +func (c *ChainClient) RetrieveEvent_FileBank_DeleteFile(blockhash types.Hash) (Event_DeleteFile, error) { + var result Event_DeleteFile events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.FileBankDeleteFile { + if e.Name == FileBankDeleteFile { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -715,17 +714,17 @@ func (c *ChainClient) RetrieveEvent_FileBank_DeleteFile(blockhash types.Hash) (e } } } - return result, errors.Errorf("failed: no %s event found", event.FileBankDeleteFile) + return result, errors.Errorf("failed: no %s event found", FileBankDeleteFile) } -func (c *ChainClient) RetrieveEvent_FileBank_TransferReport(blockhash types.Hash) (event.Event_TransferReport, error) { - var result event.Event_TransferReport +func (c *ChainClient) RetrieveEvent_FileBank_TransferReport(blockhash types.Hash) (Event_TransferReport, error) { + var result Event_TransferReport events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.FileBankTransferReport { + if e.Name == FileBankTransferReport { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -762,17 +761,17 @@ func (c *ChainClient) RetrieveEvent_FileBank_TransferReport(blockhash types.Hash } } } - return result, errors.Errorf("failed: no %s event found", event.FileBankTransferReport) + return result, errors.Errorf("failed: no %s event found", FileBankTransferReport) } -func (c *ChainClient) RetrieveEvent_FileBank_RecoveryCompleted(blockhash types.Hash) (event.Event_RecoveryCompleted, error) { - var result event.Event_RecoveryCompleted +func (c *ChainClient) RetrieveEvent_FileBank_RecoveryCompleted(blockhash types.Hash) (Event_RecoveryCompleted, error) { + var result Event_RecoveryCompleted events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.FileBankRecoveryCompleted { + if e.Name == FileBankRecoveryCompleted { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -809,17 +808,17 @@ func (c *ChainClient) RetrieveEvent_FileBank_RecoveryCompleted(blockhash types.H } } } - return result, errors.Errorf("failed: no %s event found", event.FileBankRecoveryCompleted) + return result, errors.Errorf("failed: no %s event found", FileBankRecoveryCompleted) } -func (c *ChainClient) RetrieveEvent_FileBank_IdleSpaceCert(blockhash types.Hash) (event.Event_IdleSpaceCert, error) { - var result event.Event_IdleSpaceCert +func (c *ChainClient) RetrieveEvent_FileBank_IdleSpaceCert(blockhash types.Hash) (Event_IdleSpaceCert, error) { + var result Event_IdleSpaceCert events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.FileBankIdleSpaceCert { + if e.Name == FileBankIdleSpaceCert { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -856,17 +855,17 @@ func (c *ChainClient) RetrieveEvent_FileBank_IdleSpaceCert(blockhash types.Hash) } } } - return result, errors.Errorf("failed: no %s event found", event.FileBankIdleSpaceCert) + return result, errors.Errorf("failed: no %s event found", FileBankIdleSpaceCert) } -func (c *ChainClient) RetrieveEvent_FileBank_ReplaceIdleSpace(blockhash types.Hash) (event.Event_ReplaceIdleSpace, error) { - var result event.Event_ReplaceIdleSpace +func (c *ChainClient) RetrieveEvent_FileBank_ReplaceIdleSpace(blockhash types.Hash) (Event_ReplaceIdleSpace, error) { + var result Event_ReplaceIdleSpace events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.FileBankReplaceIdleSpace { + if e.Name == FileBankReplaceIdleSpace { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -903,17 +902,17 @@ func (c *ChainClient) RetrieveEvent_FileBank_ReplaceIdleSpace(blockhash types.Ha } } } - return result, errors.Errorf("failed: no %s event found", event.FileBankReplaceIdleSpace) + return result, errors.Errorf("failed: no %s event found", FileBankReplaceIdleSpace) } -func (c *ChainClient) RetrieveEvent_FileBank_CalculateReport(blockhash types.Hash) (event.Event_CalculateReport, error) { - var result event.Event_CalculateReport +func (c *ChainClient) RetrieveEvent_FileBank_CalculateReport(blockhash types.Hash) (Event_CalculateReport, error) { + var result Event_CalculateReport events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.FileBankCalculateReport { + if e.Name == FileBankCalculateReport { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -950,17 +949,17 @@ func (c *ChainClient) RetrieveEvent_FileBank_CalculateReport(blockhash types.Has } } } - return result, errors.Errorf("failed: no %s event found", event.FileBankCalculateReport) + return result, errors.Errorf("failed: no %s event found", FileBankCalculateReport) } -func (c *ChainClient) RetrieveEvent_Sminer_UpdataIp(blockhash types.Hash) (event.Event_UpdatePeerId, error) { - var result event.Event_UpdatePeerId +func (c *ChainClient) RetrieveEvent_Sminer_UpdataIp(blockhash types.Hash) (Event_UpdatePeerId, error) { + var result Event_UpdatePeerId events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.SminerUpdatePeerId { + if e.Name == SminerUpdatePeerId { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -997,17 +996,17 @@ func (c *ChainClient) RetrieveEvent_Sminer_UpdataIp(blockhash types.Hash) (event } } } - return result, errors.Errorf("failed: no %s event found", event.SminerUpdatePeerId) + return result, errors.Errorf("failed: no %s event found", SminerUpdatePeerId) } -func (c *ChainClient) RetrieveEvent_Sminer_UpdataBeneficiary(blockhash types.Hash) (event.Event_UpdateBeneficiary, error) { - var result event.Event_UpdateBeneficiary +func (c *ChainClient) RetrieveEvent_Sminer_UpdataBeneficiary(blockhash types.Hash) (Event_UpdateBeneficiary, error) { + var result Event_UpdateBeneficiary events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.SminerUpdateBeneficiary { + if e.Name == SminerUpdateBeneficiary { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -1044,17 +1043,17 @@ func (c *ChainClient) RetrieveEvent_Sminer_UpdataBeneficiary(blockhash types.Has } } } - return result, errors.Errorf("failed: no %s event found", event.SminerUpdateBeneficiary) + return result, errors.Errorf("failed: no %s event found", SminerUpdateBeneficiary) } -func (c *ChainClient) RetrieveEvent_Sminer_Registered(blockhash types.Hash) (event.Event_Registered, error) { - var result event.Event_Registered +func (c *ChainClient) RetrieveEvent_Sminer_Registered(blockhash types.Hash) (Event_Registered, error) { + var result Event_Registered events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.SminerRegistered { + if e.Name == SminerRegistered { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -1091,17 +1090,17 @@ func (c *ChainClient) RetrieveEvent_Sminer_Registered(blockhash types.Hash) (eve } } } - return result, errors.Errorf("failed: no %s event found", event.SminerRegistered) + return result, errors.Errorf("failed: no %s event found", SminerRegistered) } -func (c *ChainClient) RetrieveEvent_Sminer_RegisterPoisKey(blockhash types.Hash) (event.Event_RegisterPoisKey, error) { - var result event.Event_RegisterPoisKey +func (c *ChainClient) RetrieveEvent_Sminer_RegisterPoisKey(blockhash types.Hash) (Event_RegisterPoisKey, error) { + var result Event_RegisterPoisKey events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.SminerRegisterPoisKey { + if e.Name == SminerRegisterPoisKey { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -1138,17 +1137,17 @@ func (c *ChainClient) RetrieveEvent_Sminer_RegisterPoisKey(blockhash types.Hash) } } } - return result, errors.Errorf("failed: no %s event found", event.SminerRegisterPoisKey) + return result, errors.Errorf("failed: no %s event found", SminerRegisterPoisKey) } -func (c *ChainClient) RetrieveEvent_Sminer_MinerExitPrep(blockhash types.Hash) (event.Event_MinerExitPrep, error) { - var result event.Event_MinerExitPrep +func (c *ChainClient) RetrieveEvent_Sminer_MinerExitPrep(blockhash types.Hash) (Event_MinerExitPrep, error) { + var result Event_MinerExitPrep events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.SminerMinerExitPrep { + if e.Name == SminerMinerExitPrep { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -1185,17 +1184,17 @@ func (c *ChainClient) RetrieveEvent_Sminer_MinerExitPrep(blockhash types.Hash) ( } } } - return result, errors.Errorf("failed: no %s event found", event.SminerMinerExitPrep) + return result, errors.Errorf("failed: no %s event found", SminerMinerExitPrep) } -func (c *ChainClient) RetrieveEvent_Sminer_IncreaseCollateral(blockhash types.Hash) (event.Event_IncreaseCollateral, error) { - var result event.Event_IncreaseCollateral +func (c *ChainClient) RetrieveEvent_Sminer_IncreaseCollateral(blockhash types.Hash) (Event_IncreaseCollateral, error) { + var result Event_IncreaseCollateral events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.SminerIncreaseCollateral { + if e.Name == SminerIncreaseCollateral { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -1232,17 +1231,17 @@ func (c *ChainClient) RetrieveEvent_Sminer_IncreaseCollateral(blockhash types.Ha } } } - return result, errors.Errorf("failed: no %s event found", event.SminerIncreaseCollateral) + return result, errors.Errorf("failed: no %s event found", SminerIncreaseCollateral) } -func (c *ChainClient) RetrieveEvent_Sminer_IncreaseDeclarationSpace(blockhash types.Hash) (event.Event_IncreaseDeclarationSpace, error) { - var result event.Event_IncreaseDeclarationSpace +func (c *ChainClient) RetrieveEvent_Sminer_IncreaseDeclarationSpace(blockhash types.Hash) (Event_IncreaseDeclarationSpace, error) { + var result Event_IncreaseDeclarationSpace events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.SminerIncreaseDeclarationSpace { + if e.Name == SminerIncreaseDeclarationSpace { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -1279,17 +1278,17 @@ func (c *ChainClient) RetrieveEvent_Sminer_IncreaseDeclarationSpace(blockhash ty } } } - return result, errors.Errorf("failed: no %s event found", event.SminerIncreaseDeclarationSpace) + return result, errors.Errorf("failed: no %s event found", SminerIncreaseDeclarationSpace) } -func (c *ChainClient) RetrieveEvent_Sminer_Receive(blockhash types.Hash) (event.Event_Receive, error) { - var result event.Event_Receive +func (c *ChainClient) RetrieveEvent_Sminer_Receive(blockhash types.Hash) (Event_Receive, error) { + var result Event_Receive events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.SminerReceive { + if e.Name == SminerReceive { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -1326,17 +1325,17 @@ func (c *ChainClient) RetrieveEvent_Sminer_Receive(blockhash types.Hash) (event. } } } - return result, errors.Errorf("failed: no %s event found", event.SminerReceive) + return result, errors.Errorf("failed: no %s event found", SminerReceive) } -func (c *ChainClient) RetrieveEvent_Sminer_Withdraw(blockhash types.Hash) (event.Event_Withdraw, error) { - var result event.Event_Withdraw +func (c *ChainClient) RetrieveEvent_Sminer_Withdraw(blockhash types.Hash) (Event_Withdraw, error) { + var result Event_Withdraw events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.SminerWithdraw { + if e.Name == SminerWithdraw { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -1373,17 +1372,17 @@ func (c *ChainClient) RetrieveEvent_Sminer_Withdraw(blockhash types.Hash) (event } } } - return result, errors.Errorf("failed: no %s event found", event.SminerWithdraw) + return result, errors.Errorf("failed: no %s event found", SminerWithdraw) } -func (c *ChainClient) RetrieveEvent_StorageHandler_BuySpace(blockhash types.Hash) (event.Event_BuySpace, error) { - var result event.Event_BuySpace +func (c *ChainClient) RetrieveEvent_StorageHandler_BuySpace(blockhash types.Hash) (Event_BuySpace, error) { + var result Event_BuySpace events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.StorageHandlerBuySpace { + if e.Name == StorageHandlerBuySpace { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -1420,17 +1419,17 @@ func (c *ChainClient) RetrieveEvent_StorageHandler_BuySpace(blockhash types.Hash } } } - return result, errors.Errorf("failed: no %s event found", event.StorageHandlerBuySpace) + return result, errors.Errorf("failed: no %s event found", StorageHandlerBuySpace) } -func (c *ChainClient) RetrieveEvent_StorageHandler_ExpansionSpace(blockhash types.Hash) (event.Event_ExpansionSpace, error) { - var result event.Event_ExpansionSpace +func (c *ChainClient) RetrieveEvent_StorageHandler_ExpansionSpace(blockhash types.Hash) (Event_ExpansionSpace, error) { + var result Event_ExpansionSpace events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.StorageHandlerExpansionSpace { + if e.Name == StorageHandlerExpansionSpace { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -1467,17 +1466,17 @@ func (c *ChainClient) RetrieveEvent_StorageHandler_ExpansionSpace(blockhash type } } } - return result, errors.Errorf("failed: no %s event found", event.StorageHandlerExpansionSpace) + return result, errors.Errorf("failed: no %s event found", StorageHandlerExpansionSpace) } -func (c *ChainClient) RetrieveEvent_StorageHandler_RenewalSpace(blockhash types.Hash) (event.Event_RenewalSpace, error) { - var result event.Event_RenewalSpace +func (c *ChainClient) RetrieveEvent_StorageHandler_RenewalSpace(blockhash types.Hash) (Event_RenewalSpace, error) { + var result Event_RenewalSpace events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.StorageHandlerRenewalSpace { + if e.Name == StorageHandlerRenewalSpace { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -1514,7 +1513,7 @@ func (c *ChainClient) RetrieveEvent_StorageHandler_RenewalSpace(blockhash types. } } } - return result, errors.Errorf("failed: no %s event found", event.StorageHandlerRenewalSpace) + return result, errors.Errorf("failed: no %s event found", StorageHandlerRenewalSpace) } func (c *ChainClient) RetrieveEvent_Balances_Transfer(blockhash types.Hash) (types.EventBalancesTransfer, error) { @@ -1524,7 +1523,7 @@ func (c *ChainClient) RetrieveEvent_Balances_Transfer(blockhash types.Hash) (typ return result, err } for _, e := range events { - if e.Name == event.BalanceTransfer { + if e.Name == BalanceTransfer { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -1561,17 +1560,17 @@ func (c *ChainClient) RetrieveEvent_Balances_Transfer(blockhash types.Hash) (typ } } } - return result, errors.Errorf("failed: no %s event found", event.BalanceTransfer) + return result, errors.Errorf("failed: no %s event found", BalanceTransfer) } -func (c *ChainClient) RetrieveEvent_FileBank_GenRestoralOrder(blockhash types.Hash) (event.Event_GenerateRestoralOrder, error) { - var result event.Event_GenerateRestoralOrder +func (c *ChainClient) RetrieveEvent_FileBank_GenRestoralOrder(blockhash types.Hash) (Event_GenerateRestoralOrder, error) { + var result Event_GenerateRestoralOrder events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.FileBankGenerateRestoralOrder { + if e.Name == FileBankGenerateRestoralOrder { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -1608,19 +1607,19 @@ func (c *ChainClient) RetrieveEvent_FileBank_GenRestoralOrder(blockhash types.Ha } } } - return result, errors.Errorf("failed: no %s event found", event.FileBankGenerateRestoralOrder) + return result, errors.Errorf("failed: no %s event found", FileBankGenerateRestoralOrder) } -func (c *ChainClient) RetrieveAllEvent_FileBank_UploadDeclaration(blockhash types.Hash) ([]event.AllUploadDeclarationEvent, error) { - var result = make([]event.AllUploadDeclarationEvent, 0) +func (c *ChainClient) RetrieveAllEvent_FileBank_UploadDeclaration(blockhash types.Hash) ([]AllUploadDeclarationEvent, error) { + var result = make([]AllUploadDeclarationEvent, 0) events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.FileBankUploadDeclaration { - var ele event.AllUploadDeclarationEvent + if e.Name == FileBankUploadDeclaration { + var ele AllUploadDeclarationEvent for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -1694,7 +1693,7 @@ func (c *ChainClient) RetrieveAllEvent_FileBank_StorageCompleted(blockhash types return result, err } for _, e := range events { - if e.Name == event.FileBankStorageCompleted { + if e.Name == FileBankStorageCompleted { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -1726,15 +1725,15 @@ func (c *ChainClient) RetrieveAllEvent_FileBank_StorageCompleted(blockhash types return result, nil } -func (c *ChainClient) RetrieveAllEvent_FileBank_DeleteFile(blockhash types.Hash) ([]event.AllDeleteFileEvent, error) { - var result = make([]event.AllDeleteFileEvent, 0) +func (c *ChainClient) RetrieveAllEvent_FileBank_DeleteFile(blockhash types.Hash) ([]AllDeleteFileEvent, error) { + var result = make([]AllDeleteFileEvent, 0) events, err := c.eventRetriever.GetEvents(blockhash) if err != nil { return result, err } for _, e := range events { - if e.Name == event.FileBankDeleteFile { - var ele event.AllDeleteFileEvent + if e.Name == FileBankDeleteFile { + var ele AllDeleteFileEvent for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -1851,11 +1850,11 @@ func (c *ChainClient) RetrieveAllEventFromBlock(blockhash types.Hash) ([]string, return systemEvents, extrinsicsEvents, nil } -func (c *ChainClient) RetrieveBlock(blocknumber uint64) ([]string, []event.ExtrinsicsInfo, []event.TransferInfo, string, string, string, string, int64, error) { +func (c *ChainClient) RetrieveBlock(blocknumber uint64) ([]string, []ExtrinsicsInfo, []TransferInfo, string, string, string, string, int64, error) { var timeUnixMilli int64 var systemEvents = make([]string, 0) - var extrinsicsInfo = make([]event.ExtrinsicsInfo, 0) - var transferInfo = make([]event.TransferInfo, 0) + var extrinsicsInfo = make([]ExtrinsicsInfo, 0) + var transferInfo = make([]TransferInfo, 0) blockhash, err := c.GetSubstrateAPI().RPC.Chain.GetBlockHash(blocknumber) if err != nil { return systemEvents, extrinsicsInfo, transferInfo, "", "", "", "", 0, err @@ -1887,7 +1886,7 @@ func (c *ChainClient) RetrieveBlock(blocknumber uint64) ([]string, []event.Extri return systemEvents, extrinsicsInfo, transferInfo, "", "", "", "", 0, err } timeUnixMilli = timestamp.Int64() - extrinsicsInfo = append(extrinsicsInfo, event.ExtrinsicsInfo{ + extrinsicsInfo = append(extrinsicsInfo, ExtrinsicsInfo{ Name: name, Events: []string{e.Name}, Result: true, @@ -1895,13 +1894,13 @@ func (c *ChainClient) RetrieveBlock(blocknumber uint64) ([]string, []event.Extri continue } eventsBuf = append(eventsBuf, e.Name) - if e.Name == event.TransactionPaymentTransactionFeePaid || - e.Name == event.EvmAccountMappingTransactionFeePaid { + if e.Name == TransactionPaymentTransactionFeePaid || + e.Name == EvmAccountMappingTransactionFeePaid { signer, fee, _ = parseSignerAndFeePaidFromEvent(e) - } else if e.Name == event.BalancesTransfer { + } else if e.Name == BalancesTransfer { //parsedBalancesTransfer = false from, to, amount, _ := ParseTransferInfoFromEvent(e) - transferInfo = append(transferInfo, event.TransferInfo{ + transferInfo = append(transferInfo, TransferInfo{ From: from, To: to, Amount: amount, @@ -1914,9 +1913,9 @@ func (c *ChainClient) RetrieveBlock(blocknumber uint64) ([]string, []event.Extri // if len(transfers) > 0 { // transferInfo = append(transferInfo, transfers...) // } - } else if e.Name == event.SystemExtrinsicSuccess { + } else if e.Name == SystemExtrinsicSuccess { if len(eventsBuf) > 0 { - extrinsicsInfo = append(extrinsicsInfo, event.ExtrinsicsInfo{ + extrinsicsInfo = append(extrinsicsInfo, ExtrinsicsInfo{ Name: name, Signer: signer, FeePaid: fee, @@ -1925,9 +1924,9 @@ func (c *ChainClient) RetrieveBlock(blocknumber uint64) ([]string, []event.Extri }) eventsBuf = make([]string, 0) } - } else if e.Name == event.SystemExtrinsicFailed { + } else if e.Name == SystemExtrinsicFailed { if len(eventsBuf) > 0 { - extrinsicsInfo = append(extrinsicsInfo, event.ExtrinsicsInfo{ + extrinsicsInfo = append(extrinsicsInfo, ExtrinsicsInfo{ Name: name, Signer: signer, FeePaid: fee, @@ -1945,11 +1944,11 @@ func (c *ChainClient) RetrieveBlock(blocknumber uint64) ([]string, []event.Extri return systemEvents, extrinsicsInfo, transferInfo, blockhash.Hex(), block.Block.Header.ParentHash.Hex(), block.Block.Header.ExtrinsicsRoot.Hex(), block.Block.Header.StateRoot.Hex(), timeUnixMilli, nil } -func (c *ChainClient) RetrieveBlockAndAll(blocknumber uint64) ([]string, []event.ExtrinsicsInfo, []event.TransferInfo, []string, []string, string, string, string, string, string, int64, error) { +func (c *ChainClient) RetrieveBlockAndAll(blocknumber uint64) ([]string, []ExtrinsicsInfo, []TransferInfo, []string, []string, string, string, string, string, string, int64, error) { var timeUnixMilli int64 var systemEvents = make([]string, 0) - var extrinsicsInfo = make([]event.ExtrinsicsInfo, 0) - var transferInfo = make([]event.TransferInfo, 0) + var extrinsicsInfo = make([]ExtrinsicsInfo, 0) + var transferInfo = make([]TransferInfo, 0) var sminerRegInfo = make([]string, 0) var newAccounts = make([]string, 0) var allGasFee = new(big.Int) @@ -1982,19 +1981,19 @@ func (c *ChainClient) RetrieveBlockAndAll(blocknumber uint64) ([]string, []event var fee string var ok bool var name string - var extInfo event.ExtrinsicsInfo + var extInfo ExtrinsicsInfo for _, e := range events { if e.Phase.IsApplyExtrinsic { if name, ok = ExtrinsicsName[block.Block.Extrinsics[e.Phase.AsApplyExtrinsic].Method.CallIndex]; ok { if name == ExtName_Timestamp_set { - extInfo = event.ExtrinsicsInfo{} + extInfo = ExtrinsicsInfo{} timeDecoder := scale.NewDecoder(bytes.NewReader(block.Block.Extrinsics[e.Phase.AsApplyExtrinsic].Method.Args)) timestamp, err := timeDecoder.DecodeUintCompact() if err != nil { return systemEvents, extrinsicsInfo, transferInfo, sminerRegInfo, newAccounts, "", "", "", "", "", 0, err } timeUnixMilli = timestamp.Int64() - extrinsicsInfo = append(extrinsicsInfo, event.ExtrinsicsInfo{ + extrinsicsInfo = append(extrinsicsInfo, ExtrinsicsInfo{ Name: name, Events: []string{e.Name}, Result: true, @@ -2002,16 +2001,16 @@ func (c *ChainClient) RetrieveBlockAndAll(blocknumber uint64) ([]string, []event continue } eventsBuf = append(eventsBuf, e.Name) - if e.Name == event.TransactionPaymentTransactionFeePaid || - e.Name == event.EvmAccountMappingTransactionFeePaid { + if e.Name == TransactionPaymentTransactionFeePaid || + e.Name == EvmAccountMappingTransactionFeePaid { signer, fee, _ = parseSignerAndFeePaidFromEvent(e) tmp, ok := new(big.Int).SetString(fee, 10) if ok { allGasFee = allGasFee.Add(allGasFee, tmp) } - } else if e.Name == event.BalancesTransfer { + } else if e.Name == BalancesTransfer { from, to, amount, _ := ParseTransferInfoFromEvent(e) - transferInfo = append(transferInfo, event.TransferInfo{ + transferInfo = append(transferInfo, TransferInfo{ ExtrinsicName: name, From: from, To: to, @@ -2020,17 +2019,17 @@ func (c *ChainClient) RetrieveBlockAndAll(blocknumber uint64) ([]string, []event }) // extInfo.From = from // extInfo.To = to - } else if e.Name == event.SminerRegistered { + } else if e.Name == SminerRegistered { acc, err := ParseAccountFromEvent(e) if err == nil { sminerRegInfo = append(sminerRegInfo, acc) } - } else if e.Name == event.SystemNewAccount { + } else if e.Name == SystemNewAccount { acc, err := ParseAccountFromEvent(e) if err == nil { newAccounts = append(newAccounts, acc) } - } else if e.Name == event.SystemExtrinsicSuccess { + } else if e.Name == SystemExtrinsicSuccess { if len(eventsBuf) > 0 { extInfo.Name = name extInfo.Signer = signer @@ -2040,8 +2039,8 @@ func (c *ChainClient) RetrieveBlockAndAll(blocknumber uint64) ([]string, []event extrinsicsInfo = append(extrinsicsInfo, extInfo) eventsBuf = make([]string, 0) } - extInfo = event.ExtrinsicsInfo{} - } else if e.Name == event.SystemExtrinsicFailed { + extInfo = ExtrinsicsInfo{} + } else if e.Name == SystemExtrinsicFailed { if len(eventsBuf) > 0 { extInfo.Name = name extInfo.Signer = signer @@ -2051,7 +2050,7 @@ func (c *ChainClient) RetrieveBlockAndAll(blocknumber uint64) ([]string, []event extrinsicsInfo = append(extrinsicsInfo, extInfo) eventsBuf = make([]string, 0) } - extInfo = event.ExtrinsicsInfo{} + extInfo = ExtrinsicsInfo{} } } } else { @@ -2084,15 +2083,15 @@ func (c *ChainClient) RetrieveBlockAndAll(blocknumber uint64) ([]string, []event return systemEvents, extrinsicsInfo, transferInfo, sminerRegInfo, newAccounts, blockhash.Hex(), block.Block.Header.ParentHash.Hex(), block.Block.Header.ExtrinsicsRoot.Hex(), block.Block.Header.StateRoot.Hex(), allGasFee.String(), timeUnixMilli, nil } -func (c *ChainClient) ParseBlockData(blocknumber uint64) (event.BlockData, error) { +func (c *ChainClient) ParseBlockData(blocknumber uint64) (BlockData, error) { var ( ok bool name string err error extBytes []byte extrinsicIndex int - blockdata event.BlockData - extInfo event.ExtrinsicsInfo + blockdata BlockData + extInfo ExtrinsicsInfo allGasFee = new(big.Int) ) @@ -2112,7 +2111,7 @@ func (c *ChainClient) ParseBlockData(blocknumber uint64) (event.BlockData, error blockdata.ExtHash = block.Block.Header.ExtrinsicsRoot.Hex() blockdata.StHash = block.Block.Header.StateRoot.Hex() - blockdata.Extrinsics = make([]event.ExtrinsicsInfo, len(block.Block.Extrinsics)) + blockdata.Extrinsics = make([]ExtrinsicsInfo, len(block.Block.Extrinsics)) for k, v := range block.Block.Extrinsics { extBytes, err = codec.Encode(v) if err != nil { @@ -2142,7 +2141,7 @@ func (c *ChainClient) ParseBlockData(blocknumber uint64) (event.BlockData, error return blockdata, errors.New("The number of extrinsics hashes does not equal the number of extrinsics") } if name == ExtName_Timestamp_set { - extInfo = event.ExtrinsicsInfo{} + extInfo = ExtrinsicsInfo{} timestamp, err := scale.NewDecoder(bytes.NewReader(block.Block.Extrinsics[e.Phase.AsApplyExtrinsic].Method.Args)).DecodeUintCompact() if err != nil { return blockdata, err @@ -2155,8 +2154,8 @@ func (c *ChainClient) ParseBlockData(blocknumber uint64) (event.BlockData, error continue } eventsBuf = append(eventsBuf, e.Name) - if e.Name == event.TransactionPaymentTransactionFeePaid || - e.Name == event.EvmAccountMappingTransactionFeePaid { + if e.Name == TransactionPaymentTransactionFeePaid || + e.Name == EvmAccountMappingTransactionFeePaid { signer, fee, err = parseSignerAndFeePaidFromEvent(e) if err != nil { return blockdata, err @@ -2165,20 +2164,20 @@ func (c *ChainClient) ParseBlockData(blocknumber uint64) (event.BlockData, error if ok { allGasFee = allGasFee.Add(allGasFee, tmp) } - } else if e.Name == event.BalancesTransfer { + } else if e.Name == BalancesTransfer { from, to, amount, err := ParseTransferInfoFromEvent(e) if err != nil { return blockdata, err } if to == c.treasuryAcc { - blockdata.Punishment = append(blockdata.Punishment, event.Punishment{ + blockdata.Punishment = append(blockdata.Punishment, Punishment{ ExtrinsicHash: blockdata.Extrinsics[extrinsicIndex].Hash, From: from, To: to, Amount: amount, }) } - blockdata.TransferInfo = append(blockdata.TransferInfo, event.TransferInfo{ + blockdata.TransferInfo = append(blockdata.TransferInfo, TransferInfo{ ExtrinsicName: name, ExtrinsicHash: blockdata.Extrinsics[extrinsicIndex].Hash, From: from, @@ -2186,22 +2185,22 @@ func (c *ChainClient) ParseBlockData(blocknumber uint64) (event.BlockData, error Amount: amount, Result: true, }) - } else if e.Name == event.SminerRegistered { + } else if e.Name == SminerRegistered { acc, err := ParseAccountFromEvent(e) if err != nil { return blockdata, err } - blockdata.MinerReg = append(blockdata.MinerReg, event.MinerRegInfo{ + blockdata.MinerReg = append(blockdata.MinerReg, MinerRegInfo{ ExtrinsicHash: blockdata.Extrinsics[extrinsicIndex].Hash, Account: acc, }) - } else if e.Name == event.SystemNewAccount { + } else if e.Name == SystemNewAccount { acc, err := ParseAccountFromEvent(e) if err != nil { return blockdata, err } blockdata.NewAccounts = append(blockdata.NewAccounts, acc) - } else if e.Name == event.FileBankUploadDeclaration { + } else if e.Name == FileBankUploadDeclaration { acc, err := ParseAccountFromEvent(e) if err != nil { return blockdata, err @@ -2210,12 +2209,12 @@ func (c *ChainClient) ParseBlockData(blocknumber uint64) (event.BlockData, error if err != nil { return blockdata, err } - blockdata.UploadDecInfo = append(blockdata.UploadDecInfo, event.UploadDecInfo{ + blockdata.UploadDecInfo = append(blockdata.UploadDecInfo, UploadDecInfo{ ExtrinsicHash: blockdata.Extrinsics[extrinsicIndex].Hash, Owner: acc, Fid: fid, }) - } else if e.Name == event.FileBankDeleteFile { + } else if e.Name == FileBankDeleteFile { acc, err := ParseAccountFromEvent(e) if err != nil { return blockdata, err @@ -2224,12 +2223,12 @@ func (c *ChainClient) ParseBlockData(blocknumber uint64) (event.BlockData, error if err != nil { return blockdata, err } - blockdata.DeleteFileInfo = append(blockdata.DeleteFileInfo, event.DeleteFileInfo{ + blockdata.DeleteFileInfo = append(blockdata.DeleteFileInfo, DeleteFileInfo{ ExtrinsicHash: blockdata.Extrinsics[extrinsicIndex].Hash, Owner: acc, Fid: fid, }) - } else if e.Name == event.FileBankCreateBucket { + } else if e.Name == FileBankCreateBucket { acc, err := ParseAccountFromEvent(e) if err != nil { return blockdata, err @@ -2238,12 +2237,12 @@ func (c *ChainClient) ParseBlockData(blocknumber uint64) (event.BlockData, error if err != nil { return blockdata, err } - blockdata.CreateBucketInfo = append(blockdata.CreateBucketInfo, event.CreateBucketInfo{ + blockdata.CreateBucketInfo = append(blockdata.CreateBucketInfo, CreateBucketInfo{ ExtrinsicHash: blockdata.Extrinsics[extrinsicIndex].Hash, Owner: acc, BucketName: bucketname, }) - } else if e.Name == event.FileBankDeleteBucket { + } else if e.Name == FileBankDeleteBucket { acc, err := ParseAccountFromEvent(e) if err != nil { return blockdata, err @@ -2252,50 +2251,50 @@ func (c *ChainClient) ParseBlockData(blocknumber uint64) (event.BlockData, error if err != nil { return blockdata, err } - blockdata.DeleteBucketInfo = append(blockdata.DeleteBucketInfo, event.DeleteBucketInfo{ + blockdata.DeleteBucketInfo = append(blockdata.DeleteBucketInfo, DeleteBucketInfo{ ExtrinsicHash: blockdata.Extrinsics[extrinsicIndex].Hash, Owner: acc, BucketName: bucketname, }) - } else if e.Name == event.AuditSubmitIdleProof { + } else if e.Name == AuditSubmitIdleProof { acc, err := ParseAccountFromEvent(e) if err != nil { return blockdata, err } - blockdata.SubmitIdleProve = append(blockdata.SubmitIdleProve, event.SubmitIdleProve{ + blockdata.SubmitIdleProve = append(blockdata.SubmitIdleProve, SubmitIdleProve{ ExtrinsicHash: blockdata.Extrinsics[extrinsicIndex].Hash, Miner: acc, }) - } else if e.Name == event.AuditSubmitServiceProof { + } else if e.Name == AuditSubmitServiceProof { acc, err := ParseAccountFromEvent(e) if err != nil { return blockdata, err } - blockdata.SubmitServiceProve = append(blockdata.SubmitServiceProve, event.SubmitServiceProve{ + blockdata.SubmitServiceProve = append(blockdata.SubmitServiceProve, SubmitServiceProve{ ExtrinsicHash: blockdata.Extrinsics[extrinsicIndex].Hash, Miner: acc, }) - } else if e.Name == event.AuditSubmitIdleVerifyResult { + } else if e.Name == AuditSubmitIdleVerifyResult { acc, result, err := ParseChallResultFromEvent(e) if err != nil { return blockdata, err } - blockdata.SubmitIdleResult = append(blockdata.SubmitIdleResult, event.SubmitIdleResult{ + blockdata.SubmitIdleResult = append(blockdata.SubmitIdleResult, SubmitIdleResult{ ExtrinsicHash: blockdata.Extrinsics[extrinsicIndex].Hash, Miner: acc, Result: result, }) - } else if e.Name == event.AuditSubmitServiceVerifyResult { + } else if e.Name == AuditSubmitServiceVerifyResult { acc, result, err := ParseChallResultFromEvent(e) if err != nil { return blockdata, err } - blockdata.SubmitServiceResult = append(blockdata.SubmitServiceResult, event.SubmitServiceResult{ + blockdata.SubmitServiceResult = append(blockdata.SubmitServiceResult, SubmitServiceResult{ ExtrinsicHash: blockdata.Extrinsics[extrinsicIndex].Hash, Miner: acc, Result: result, }) - } else if e.Name == event.SystemExtrinsicSuccess { + } else if e.Name == SystemExtrinsicSuccess { extInfo.Events = append(make([]string, 0), eventsBuf...) extInfo.Name = name extInfo.Signer = signer @@ -2304,9 +2303,9 @@ func (c *ChainClient) ParseBlockData(blocknumber uint64) (event.BlockData, error extInfo.Hash = blockdata.Extrinsics[extrinsicIndex].Hash blockdata.Extrinsics[extrinsicIndex] = extInfo eventsBuf = make([]string, 0) - extInfo = event.ExtrinsicsInfo{} + extInfo = ExtrinsicsInfo{} extrinsicIndex++ - } else if e.Name == event.SystemExtrinsicFailed { + } else if e.Name == SystemExtrinsicFailed { for m := 0; m < len(blockdata.UploadDecInfo); m++ { if blockdata.UploadDecInfo[m].ExtrinsicHash == blockdata.Extrinsics[extrinsicIndex].Hash { if len(blockdata.UploadDecInfo) == 1 { @@ -2415,16 +2414,16 @@ func (c *ChainClient) ParseBlockData(blocknumber uint64) (event.BlockData, error extInfo.Hash = blockdata.Extrinsics[extrinsicIndex].Hash blockdata.Extrinsics[extrinsicIndex] = extInfo eventsBuf = make([]string, 0) - extInfo = event.ExtrinsicsInfo{} + extInfo = ExtrinsicsInfo{} extrinsicIndex++ } } } else { blockdata.SysEvents = append(blockdata.SysEvents, e.Name) - if e.Name == event.StakingStakersElected { + if e.Name == StakingStakersElected { blockdata.IsNewEra = true } - if e.Name == event.AuditGenerateChallenge { + if e.Name == AuditGenerateChallenge { acc, err := ParseAccountFromEvent(e) if err != nil { return blockdata, err @@ -2441,9 +2440,9 @@ func parseSignerAndFeePaidFromEvent(e *parser.Event) (string, string, error) { if e == nil { return "", "", errors.New("event is nil") } - if e.Name != event.TransactionPaymentTransactionFeePaid && - e.Name != event.EvmAccountMappingTransactionFeePaid { - return "", "", fmt.Errorf("event is not %s or %s", event.TransactionPaymentTransactionFeePaid, event.EvmAccountMappingTransactionFeePaid) + if e.Name != TransactionPaymentTransactionFeePaid && + e.Name != EvmAccountMappingTransactionFeePaid { + return "", "", fmt.Errorf("event is not %s or %s", TransactionPaymentTransactionFeePaid, EvmAccountMappingTransactionFeePaid) } var signAcc string var fee string @@ -2561,8 +2560,8 @@ func ExplicitBigInt(v reflect.Value, depth int) string { return fee } -func (c *ChainClient) parseTransferInfoFromBlock(blockhash types.Hash) ([]event.TransferInfo, error) { - var transferEvents = make([]event.TransferInfo, 0) +func (c *ChainClient) parseTransferInfoFromBlock(blockhash types.Hash) ([]TransferInfo, error) { + var transferEvents = make([]TransferInfo, 0) var data types.StorageDataRaw ok, err := c.GetSubstrateAPI().RPC.State.GetStorage(c.GetKeyEvents(), &data, blockhash) if err != nil { @@ -2571,7 +2570,7 @@ func (c *ChainClient) parseTransferInfoFromBlock(blockhash types.Hash) ([]event. var from string var to string if ok { - events := event.EventRecords{} + events := EventRecords{} err = types.EventRecordsRaw(data).DecodeEventRecords(c.GetMetadata(), &events) if err != nil { return transferEvents, err @@ -2579,7 +2578,7 @@ func (c *ChainClient) parseTransferInfoFromBlock(blockhash types.Hash) ([]event. for _, e := range events.Balances_Transfer { from, _ = utils.EncodePublicKeyAsCessAccount(e.From[:]) to, _ = utils.EncodePublicKeyAsCessAccount(e.To[:]) - transferEvents = append(transferEvents, event.TransferInfo{ + transferEvents = append(transferEvents, TransferInfo{ From: from, To: to, Amount: e.Value.String(), @@ -2594,8 +2593,8 @@ func ParseTransferInfoFromEvent(e *parser.Event) (string, string, string, error) if e == nil { return "", "", "", errors.New("event is nil") } - if e.Name != event.BalancesTransfer { - return "", "", "", fmt.Errorf("event is not %s", event.BalancesTransfer) + if e.Name != BalancesTransfer { + return "", "", "", fmt.Errorf("event is not %s", BalancesTransfer) } var from string var to string diff --git a/chain/pattern.go b/chain/pattern.go index 4b0638d..d450ea8 100644 --- a/chain/pattern.go +++ b/chain/pattern.go @@ -133,18 +133,18 @@ const ( // Extrinsics const ( - //AUDIT + // Audit TX_Audit_SubmitIdleProof = Audit + DOT + "submit_idle_proof" TX_Audit_SubmitServiceProof = Audit + DOT + "submit_service_proof" TX_Audit_SubmitVerifyIdleResult = Audit + DOT + "submit_verify_idle_result" TX_Audit_SubmitVerifyServiceResult = Audit + DOT + "submit_verify_service_result" - // OSS - TX_OSS_REGISTER = OSS + DOT + "register" - TX_OSS_UPDATE = OSS + DOT + "update" - TX_OSS_DESTROY = OSS + DOT + "destroy" - TX_OSS_AUTHORIZE = OSS + DOT + "authorize" - TX_OSS_UNAUTHORIZE = OSS + DOT + "cancel_authorize" + // Oss + TX_Oss_Register = Oss + DOT + "register" + TX_Oss_Update = Oss + DOT + "update" + TX_Oss_Destroy = Oss + DOT + "destroy" + TX_Oss_Authorize = Oss + DOT + "authorize" + TX_Oss_CancelAuthorize = Oss + DOT + "cancel_authorize" // SMINER TX_SMINER_REGISTER = SMINER + DOT + "regnstk" diff --git a/config/config.go b/config/config.go index 15dac5c..8ea08e5 100755 --- a/config/config.go +++ b/config/config.go @@ -22,7 +22,7 @@ const ( const ( MinBucketNameLength = 3 MaxBucketNameLength = 63 - MaxDomainNameLength = 50 + MaxDomainNameLength = 100 ) // default name diff --git a/core/sdk/sdk.go b/core/sdk/sdk.go index 657f19f..2799373 100755 --- a/core/sdk/sdk.go +++ b/core/sdk/sdk.go @@ -7,528 +7,518 @@ package sdk -import ( - "io" - "math/big" - - "github.com/CESSProject/cess-go-sdk/core/event" - "github.com/CESSProject/cess-go-sdk/core/pattern" - gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4" - "github.com/centrifuge/go-substrate-rpc-client/v4/types" -) - // CESS Go SDK Interface Description type SDK interface { - // Audit-State - - // QueryChallengeVerifyExpiration Query Challenge Expiration Block High - QueryChallengeVerifyExpiration() (uint32, error) - // QueryChallengeInfo queries accountID's challenge information - // - accountID: signature account of the storage node - // - block: block number, less than 0 indicates the latest block - QueryChallengeInfo(accountID []byte, block int32) (bool, pattern.ChallengeInfo, error) - - // Audit-Extrinsics - - // SubmitIdleProof submits proof of idle data to the chain - // Tip: This method can only be used for storage nodes - SubmitIdleProof(idleProve []types.U8) (string, error) - // SubmitServiceProof submits proof of service data to the chain - // Tip: This method can only be used for storage nodes - SubmitServiceProof(serviceProof []types.U8) (string, error) - // SubmitIdleProofResult submits the proof verification results of idle data to the chain - // Tip: This method can only be used for storage nodes - SubmitIdleProofResult(totalProofHash []types.U8, front, rear types.U64, accumulator pattern.Accumulator, result types.Bool, sig types.Bytes, teePuk pattern.WorkerPublicKey) (string, error) - // SubmitServiceProofResult submits the proof verification results of service data to the chain - // Tip: This method can only be used for storage nodes - SubmitServiceProofResult(result types.Bool, sign types.Bytes, bloomFilter pattern.BloomFilter, teePuk pattern.WorkerPublicKey) (string, error) - - // Filebank-State - - // QueryBucketInfo query the bucket information of the accountID - QueryBucketInfo(accountID []byte, bucketName string) (pattern.BucketInfo, error) - // QueryAllBucket query all buckets of the accountID - QueryAllBucket(accountID []byte) ([]types.Bytes, error) - // QueryAllBucketString query all bucket names as string of the accountID - QueryAllBucketString(accountID []byte) ([]string, error) - // QueryStorageOrder query storage order information. - QueryStorageOrder(fid string) (pattern.StorageOrder, error) - // QueryFileMetadata queries the metadata of the roothash file. - QueryFileMetadata(fid string) (pattern.FileMetadata, error) - // QueryFileMetadataByBlock queries the metadata of the roothash file. - QueryFileMetadataByBlock(fid string, block uint64) (pattern.FileMetadata, error) - // QueryRestoralOrder queries a restore order info. - QueryRestoralOrder(roothash string) (pattern.RestoralOrderInfo, error) - QueryRestoralOrderList() ([]pattern.RestoralOrderInfo, error) - - // Filebank-Extrinsics - - // ClaimRestoralNoExistOrder is used to receive recovery orders from exiting miners. - ClaimRestoralNoExistOrder(accountID []byte, fid, restoralFragmentHash string) (string, error) - // ClaimRestoralOrder is used to collect restoration orders. - ClaimRestoralOrder(fragmentHash string) (string, error) - // CreateBucket creates a bucket for accountID - // For details on bucket naming rules, see: - // https://docs.cess.cloud/deoss/get-started/deoss-gateway/step-1-create-a-bucket#naming-conventions-for-a-bucket - CreateBucket(accountID []byte, bucketName string) (string, error) - // DeleteBucket deletes buckets for accountID - // Tip: Only empty buckets can be deleted - DeleteBucket(accountID []byte, bucketName string) (string, error) - // DeleteFile deletes files for accountID - DeleteFile(puk []byte, filehash string) (string, error) - // GenerateRestoralOrder generates data for restoration orders. - GenerateRestoralOrder(fid, fragmentHash string) (string, error) - // RestoralComplete reports order recovery completion. - RestoralComplete(restoralFragmentHash string) (string, error) - // SubmitFileReport submits a stored file report. - SubmitFileReport(index types.U8, roothash pattern.FileHash) (string, error) - ReportFile(index uint8, roothash string) (string, error) - // UploadDeclaration creates a storage order. - UploadDeclaration(filehash string, dealinfo []pattern.SegmentList, user pattern.UserBrief, filesize uint64) (string, error) - // GenerateStorageOrder for generating storage orders - GenerateStorageOrder(roothash string, segment []pattern.SegmentDataInfo, owner []byte, filename, buckname string, filesize uint64) (string, error) - // CertIdleSpace - CertIdleSpace(idleSignInfo pattern.SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk pattern.WorkerPublicKey) (string, error) - // ReplaceIdleSpace - ReplaceIdleSpace(idleSignInfo pattern.SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk pattern.WorkerPublicKey) (string, error) - // ReportTagCalculated - ReportTagCalculated(teeSig types.Bytes, tagSigInfo pattern.TagSigInfo) (string, error) - - // Oss-State - - // QueryAuthorizedAccountIDs queries all DeOSS accountIDs authorized by accountID - QueryAuthorizedAccountIDs(accountID []byte) ([]types.AccountID, error) - // QueryAuthorizedAccounts queries all DeOSS accounts authorized by accountID - QueryAuthorizedAccounts(accountID []byte) ([]string, error) - // QueryDeOSSInfo Query the DeOSS information registered by accountID account - QueryDeOSSInfo(accountID []byte) (pattern.OssInfo, error) - // QueryAllDeOSSInfo queries all deoss information - QueryAllDeOSSInfo() ([]pattern.OssInfo, error) - // QueryAllDeOSSPeerId queries peerID of all DeOSS - QueryAllDeOSSPeerId() ([]string, error) - - // Oss-Extrinsics - - // RegisterDeOSS register as deoss node - RegisterDeOSS(peerId []byte, domain string) (string, error) - // UpdateDeOSS updates the peerID and domain of deoss - // Tip: This method can only be used for DeOSS nodes - UpdateDeOSS(peerId string, domain string) (string, error) - // ExitDeoss exit deoss - // Tip: This method can only be used for DeOSS nodes - ExitDeOSS() (string, error) - // AuthorizeSpace authorizes space to oss account - // Tip: accountID can only be a DeOSS node account - AuthorizeSpace(accountID string) (string, error) - // UnAuthorizeSpace cancels space authorization - // Tip: accountID can only be a DeOSS node account - UnAuthorizeSpace(accountID string) (string, error) - - // Sminer-State - - // QueryExpenders queries expenders information - QueryExpenders() (pattern.ExpendersInfo, error) - // // QueryStorageMinerByAccount queries storage node information by account. - QueryStorageMinerByAccount(account string) (pattern.MinerInfo, error) - // QueryStorageMiner queries storage node information. - QueryStorageMiner(accountID []byte) (pattern.MinerInfo, error) - // QueryStorageMinerByBlock queries storage node information in specified block - // - if block < 0, then query the latest block - QueryStorageMinerByBlock(puk []byte, block int32) (pattern.MinerInfo, error) - // QueryAllSminerAccount queries the accounts of all storage miners. - QueryAllSminerAccount() ([]types.AccountID, error) - // QueryAllSminerAccountByBlock queries accounts of all storage miners in specified block. - // - if block < 0, then query the latest block - QueryAllSminerAccountByBlock(block int32) ([]types.AccountID, error) - // QueryRewardsMap queries rewardsMap for accountID - // Tip: accountID can only be a storage node account - QueryRewardsMap(accountID []byte) (pattern.MinerReward, error) - // QueryRewards queries rewards for accountID - // Tip: accountID can only be a storage node account - QueryRewards(accountID []byte) (pattern.RewardsType, error) - // QueryStorageMinerStakingStartBlock - QueryStorageMinerStakingStartBlock(puk []byte) (types.U32, error) - // QueryRestoralTarget queries the space recovery information of the exited storage node - // Tip: accountID can only be a storage node account - QueryRestoralTarget(accountID []byte) (pattern.RestoralTargetInfo, error) - // QueryRestoralTargetList queries the space recovery information of all exited storage nodes - QueryRestoralTargetList() ([]pattern.RestoralTargetInfo, error) - // QueryPendingReplacements queries the amount of idle data that can be replaced - // Tip: accountID can only be a storage node account - QueryPendingReplacements(accountID []byte) (types.U128, error) - // - QueryCompleteSnapShot(era uint32, block int32) (uint32, uint64, error) - - // Sminer-Extrinsics - - // - IncreaseDeclarationSpace(tibCount uint32) (string, error) - // RegisterSminer register sminer information - RegisterSminer(earnings string, peerId []byte, pledge uint64, tib_count uint32) (string, error) - // RegisterSminerAssignStaking - RegisterSminerAssignStaking(beneficiaryAcc string, peerId []byte, stakingAcc string, tib_count uint32) (string, error) - // RegisterSminerPOISKey register the pois key of sminer - RegisterSminerPOISKey(poisKey pattern.PoISKeyInfo, teeSignWithAcc, teeSign types.Bytes, teePuk pattern.WorkerPublicKey) (string, error) - // ExitSminer exit mining - ExitSminer(miner string) (string, error) - // UpdateEarningsAcc update earnings account. - UpdateEarningsAcc(puk []byte) (string, error) - UpdateEarningsAccount(earnings string) (string, error) - // UpdateSminerPeerId update miner peerid - UpdateSminerPeerId(peerid pattern.PeerId) (string, error) - // IncreaseStakingAmount increase staking amount. - IncreaseStakingAmount(miner string, tokens *big.Int) (string, error) - IncreaseStorageNodeStakingAmount(miner string, token string) (string, error) - // ClaimRewards is used to claim rewards. - ClaimRewards() (string, error) - // Withdraw is used to withdraw staking. - Withdraw() (string, error) - - // Staking-State - - // QueryAllValidatorCount queries all the validator count - QueryAllValidatorCount(block int) (uint32, error) - // - QueryValidatorsCount(block int) (uint32, error) - // - QueryNominatorCount(block int) (uint32, error) - // - QueryErasTotalStake(era uint32, block int) (string, error) - // - QueryCurrentEra(block int) (uint32, error) - // - //QueryeErasStakers(era uint32) ([]pattern.StakingExposure, error) - // - QueryStakingEraRewardPoints(era uint32) (pattern.StakingEraRewardPoints, error) - // - QueryNominatorsLatest() ([]pattern.StakingNominations, error) - // - QueryBondedList(block int32) ([]types.AccountID, error) - // - QueryValidatorCommission(account string, block int) (uint8, error) - // - QueryEraValidatorReward(era uint32) (string, error) - - // StorageHandler-State - - // QueryUserSpaceInfo queries the space information purchased by the user. - QueryUserSpaceInfo(puk []byte) (pattern.UserSpaceInfo, error) - QueryUserSpaceSt(puk []byte) (pattern.UserSpaceSt, error) - // QuerySpacePricePerGib query space price per GiB. - QuerySpacePricePerGib() (string, error) - // QueryTotalIdleSpace query total idle space - QueryTotalIdleSpace() (uint64, error) - // QueryTotalServiceSpace query total service space - QueryTotalServiceSpace() (uint64, error) - // QueryPurchasedSpace query purchased space - QueryPurchasedSpace() (uint64, error) - - // StorageHandler-Extrinsics - - // BuySpace for purchasing space. - BuySpace(count uint32) (string, error) - // ExpansionSpace for expansion space. - ExpansionSpace(count uint32) (string, error) - // RenewalSpace is used to extend the validity of space. - RenewalSpace(days uint32) (string, error) - - // TeeWorker-State - - // QueryTeeWorker queries the information of the Tee worker. - QueryTeeWorker(puk pattern.WorkerPublicKey) (pattern.TeeWorkerInfo, error) - // QueryTeeInfo queries the information of the Tee worker. - QueryTeeInfo(puk pattern.WorkerPublicKey) (pattern.TeeInfo, error) - // QueryMasterPublicKey queries the master publicKey. - QueryMasterPublicKey() ([]byte, error) - // QueryAllTeeWorkerMap queries the information of all tee workers. - QueryAllTeeWorkerMap() ([]pattern.TeeWorkerInfo, error) - // QueryAllTeeInfo queries the information of all tee workers. - QueryAllTeeInfo() ([]pattern.TeeInfo, error) - // QueryTeeWorkEndpoint queries the endpoint of tee workers. - QueryTeeWorkEndpoint(workPuk pattern.WorkerPublicKey) (string, error) - // - QueryWorkerAddedAt(workPuk pattern.WorkerPublicKey) (types.U32, error) - - // System - - // QueryBlockHeight queries the block height corresponding to the block hash, - // If the blockhash is empty, query the latest block height. - QueryBlockHeight(blockhash string) (uint32, error) - // QueryNodeSynchronizationSt returns the synchronization status of the current node. - QueryNodeSynchronizationSt() (bool, error) - // QueryAccountInfoByAccount query account information by account. - QueryAccountInfoByAccount(acc string) (types.AccountInfo, error) - // QueryAccountInfo query account information by public key. - QueryAccountInfo(puk []byte) (types.AccountInfo, error) - // GetTokenSymbol returns the token symbol - GetTokenSymbol() string - // GetNetworkEnv returns the network environment - GetNetworkEnv() string - // SysProperties returns the system properties. - SysProperties() (pattern.SysProperties, error) - // SyncState returns the system sync state. - SyncState() (pattern.SysSyncState, error) - // SysVersion returns the system version. - ChainVersion() (string, error) - // NetListening returns whether the current node is listening. - NetListening() (bool, error) - // - DecodeEventNameFromBlock(block uint64) ([]string, error) - // - DecodeEventNameFromBlockhash(blockhash types.Hash) ([]string, error) - // - QueryAllAccountInfoFromBlock(block int) ([]types.AccountInfo, error) - // - QueryTotalIssuance(block int) (string, error) - - // CessTreasury - QueryRoundReward(era uint32, block int32) (string, error) - - // TransferToken to dest. - // - // Receive parameter: - // - dest: target account. - // - amount: transfer amount. - // Return parameter: - // - string: transaction hash. - // - string: target account. - // - error: error message. - TransferToken(dest string, amount uint64) (string, string, error) - - // Other - - // GetSignatureAcc returns the signature account. - GetSignatureAcc() string - // GetSignatureAccPulickey returns the signature account public key. - GetSignatureAccPulickey() []byte - // GetSubstrateAPI returns Substrate API. - GetSubstrateAPI() *gsrpc.SubstrateAPI - // GetChainState returns chain node state. - GetChainState() bool - // SetChainState sets the state of the chain node. - SetChainState(state bool) - // GetSdkName return sdk name - GetSDKName() string - // GetCurrentRpcAddr return current rpc address - GetCurrentRpcAddr() string - // SetSdkName set sdk name - SetSDKName(name string) - // ReconnectRPC for reconnecting chains. - ReconnectRPC() error - // GetMetadata returns the metadata of the chain. - GetMetadata() *types.Metadata - // GetKeyEvents returns the events storage key. - GetKeyEvents() types.StorageKey - // GetURI returns URI. - GetURI() string - // GetTreasuryAccount returns the treasury account - GetTreasuryAccount() string - // Sign returns the signature of the msg with the private key of the signing account. - Sign(msg []byte) ([]byte, error) - // Verify the signature of the msg with the public key of the signing account. - Verify(msg []byte, sig []byte) (bool, error) - // Verify Polka Signature With JavaScript - VerifyPolkaSignatureWithJS(account, msg, signature string) (bool, error) - // Verify Polka Signature With Base58 - VerifyPolkaSignatureWithBase58(account, msg, signature string) (bool, error) - // - Close() - - // Upload file to CESS gateway. - // - // Receive parameter: - // - url: the address of the gateway. - // - file: uploaded file. - // - bucket: the bucket for storing files, it will be created automatically. - // Return parameter: - // - string: [fid] unique identifier for the file. - // - error: error message. - // Preconditions: - // 1. Account requires purchasing space, refer to [BuySpace] interface. - // 2. Authorize the space usage rights of the account to the gateway account, - // refer to the [AuthorizeSpace] interface. - // 3. Make sure the name of the bucket is legal, use the [CheckBucketName] method to check. - // Explanation: - // - 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] - StoreFile(url, file, bucket string) (string, error) - - // Upload object to CESS gateway. - // - // Receive parameter: - // - url: the address of the gateway. - // - reader: strings, byte data, file streams, network streams, etc. - // - bucket: the bucket for storing object, it will be created automatically. - // Return parameter: - // - string: [fid] unique identifier for the file. - // - error: error message. - // Preconditions: - // 1. Account requires purchasing space, refer to [BuySpace] interface. - // 2. Authorize the space usage rights of the account to the gateway account, - // refer to the [AuthorizeSpace] interface. - // 3. Make sure the name of the bucket is legal, use the [CheckBucketName] method to check. - // Explanation: - // - 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] - StoreObject(url string, reader io.Reader, bucket string) (string, error) - - // Download file from CESS public gateway. - // - // Receive parameter: - // - url: the address of the gateway. - // - fid: unique identifier for the file. - // - savepath: file save location. - // Return parameter: - // - error: error message. - RetrieveFile(url, fid, savepath string) error - - // Download object from CESS gateway. - // - // Receive parameter: - // - url: the address of the gateway. - // - fid: unique identifier for the file. - // Return parameter: - // - Reader: object stream. - // - error: error message. - RetrieveObject(url, fid string) (io.ReadCloser, error) - - // Split File into Chunks. - // - // Receive parameter: - // - fpath: the path of the file to be split. - // - chunksDir: directory path to store file chunks, please do not mix it elsewhere. - // - chunkSize: the size of each chunk, it does not exceed the file size - // Return parameter: - // - int64: chunks total size (byte). - // - int: number of file chunks. - // - error: error message. - SplitFile(fpath, chunksDir string, chunkSize int64, filling bool) (int64, int, error) - - // Split File into Chunks with standard size. - // It split file into chunks of the default size and fills the last chunk that does not meet the size. - // - // Receive parameter: - // - fpath: the path of the file to be split. - // - chunksDir: directory path to store file chunks, please do not mix it elsewhere. - // Return parameter: - // - int64: chunks total size (byte). - // - int: number of file chunks. - // - error: error message. - SplitFileWithstandardSize(fpath, chunksDir string) (int64, int, error) - - // Upload chunk of file to the gateway - // - // Receive parameter: - // - url: the address of the gateway. - // - chunksDir: directory path to store file chunks, please do not mix it elsewhere. - // - bucket: the bucket name to store user data. - // - fname: the name of the file. - // - chunksNum: total number of file chunks. - // - chunksId: index of the current chunk to be uploaded ([0,chunksNum)). - // - totalSize: chunks total size (byte), can be obtained from the first return value of SplitFile - // Return parameter: - // - Reader: number of file chunks. - // - error: error message. - UploadFileChunk(url, chunksDir, bucket, fname string, chunksNum, chunksId int, totalSize int64) (string, error) - - // Upload file chunks in the directory to the gateway as much as possible, chunks will be removed after being uploaded, if the chunks are not transferred successfuly, jus - // - // Receive parameter: - // - url: the address of the gateway. - // - chunksDir: directory path to store file chunks, please do not mix it elsewhere. - // - bucket: the bucket name to store user data. - // - fname: the name of the file. - // - chunksNum: total number of file chunks. - // - totalSize: chunks total size (byte), can be obtained from the first return value of SplitFile - // Return parameter: - // - Reader: number of file chunks. - // - error: error message. - UploadFileChunks(url, chunksDir, bucket, fname string, chunksNum int, totalSize int64) (string, error) - - // retrieve event - - // - RetrieveEvent_Audit_SubmitIdleProof(blockhash types.Hash) (event.Event_SubmitIdleProof, error) - // - RetrieveEvent_Audit_SubmitServiceProof(blockhash types.Hash) (event.Event_SubmitServiceProof, error) - // - RetrieveEvent_Audit_SubmitIdleVerifyResult(blockhash types.Hash) (event.Event_SubmitIdleVerifyResult, error) - // - RetrieveEvent_Audit_SubmitServiceVerifyResult(blockhash types.Hash) (event.Event_SubmitServiceVerifyResult, error) - // - RetrieveEvent_Oss_OssUpdate(blockhash types.Hash) (event.Event_OssUpdate, error) - // - RetrieveEvent_Oss_OssRegister(blockhash types.Hash) (event.Event_OssRegister, error) - // - RetrieveEvent_Oss_OssDestroy(blockhash types.Hash) (event.Event_OssDestroy, error) - // - RetrieveEvent_Oss_Authorize(blockhash types.Hash) (event.Event_Authorize, error) - // - RetrieveEvent_Oss_CancelAuthorize(blockhash types.Hash) (event.Event_CancelAuthorize, error) - // - RetrieveEvent_FileBank_UploadDeclaration(blockhash types.Hash) (event.Event_UploadDeclaration, error) - // - RetrieveEvent_FileBank_CreateBucket(blockhash types.Hash) (event.Event_CreateBucket, error) - // - RetrieveEvent_FileBank_DeleteFile(blockhash types.Hash) (event.Event_DeleteFile, error) - // - RetrieveEvent_FileBank_TransferReport(blockhash types.Hash) (event.Event_TransferReport, error) - // - RetrieveEvent_FileBank_ClaimRestoralOrder(blockhash types.Hash) (event.Event_ClaimRestoralOrder, error) - // - RetrieveEvent_FileBank_RecoveryCompleted(blockhash types.Hash) (event.Event_RecoveryCompleted, error) - // - RetrieveEvent_FileBank_IdleSpaceCert(blockhash types.Hash) (event.Event_IdleSpaceCert, error) - // - RetrieveEvent_FileBank_ReplaceIdleSpace(blockhash types.Hash) (event.Event_ReplaceIdleSpace, error) - // - RetrieveEvent_FileBank_CalculateReport(blockhash types.Hash) (event.Event_CalculateReport, error) - // - RetrieveEvent_Sminer_Registered(blockhash types.Hash) (event.Event_Registered, error) - // - RetrieveEvent_Sminer_RegisterPoisKey(blockhash types.Hash) (event.Event_RegisterPoisKey, error) - // - RetrieveEvent_Sminer_UpdataIp(blockhash types.Hash) (event.Event_UpdatePeerId, error) - // - RetrieveEvent_Sminer_UpdataBeneficiary(blockhash types.Hash) (event.Event_UpdateBeneficiary, error) - // - RetrieveEvent_Sminer_MinerExitPrep(blockhash types.Hash) (event.Event_MinerExitPrep, error) - // - RetrieveEvent_Sminer_IncreaseCollateral(blockhash types.Hash) (event.Event_IncreaseCollateral, error) - // - RetrieveEvent_Sminer_Receive(blockhash types.Hash) (event.Event_Receive, error) - // - RetrieveEvent_Sminer_Withdraw(blockhash types.Hash) (event.Event_Withdraw, error) - // - RetrieveEvent_Sminer_IncreaseDeclarationSpace(blockhash types.Hash) (event.Event_IncreaseDeclarationSpace, error) - // - RetrieveEvent_StorageHandler_BuySpace(blockhash types.Hash) (event.Event_BuySpace, error) - // - RetrieveEvent_StorageHandler_ExpansionSpace(blockhash types.Hash) (event.Event_ExpansionSpace, error) - // - RetrieveEvent_StorageHandler_RenewalSpace(blockhash types.Hash) (event.Event_RenewalSpace, error) - // - RetrieveEvent_Balances_Transfer(blockhash types.Hash) (types.EventBalancesTransfer, error) - // - RetrieveEvent_FileBank_GenRestoralOrder(blockhash types.Hash) (event.Event_GenerateRestoralOrder, error) - // - RetrieveAllEvent_FileBank_UploadDeclaration(blockhash types.Hash) ([]event.AllUploadDeclarationEvent, error) - // - RetrieveAllEvent_FileBank_StorageCompleted(blockhash types.Hash) ([]string, error) - // - RetrieveAllEvent_FileBank_DeleteFile(blockhash types.Hash) ([]event.AllDeleteFileEvent, error) - // - //RetrieveAllEvent(blockhash types.Hash) ([]string, []string, error) - // - RetrieveAllEventFromBlock(blockhash types.Hash) ([]string, map[string][]string, error) - // - RetrieveBlock(blocknumber uint64) ([]string, []event.ExtrinsicsInfo, []event.TransferInfo, string, string, string, string, int64, error) - // - RetrieveBlockAndAll(blocknumber uint64) ([]string, []event.ExtrinsicsInfo, []event.TransferInfo, []string, []string, string, string, string, string, string, int64, error) - // - ParseBlockData(blocknumber uint64) (event.BlockData, error) - // - InitExtrinsicsName() error + // // Audit-State + + // // QueryChallengeVerifyExpiration Query Challenge Expiration Block High + // QueryChallengeVerifyExpiration() (uint32, error) + // // QueryChallengeInfo queries accountID's challenge information + // // - accountID: signature account of the storage node + // // - block: block number, less than 0 indicates the latest block + // QueryChallengeInfo(accountID []byte, block int32) (bool, pattern.ChallengeInfo, error) + + // // Audit-Extrinsics + + // // SubmitIdleProof submits proof of idle data to the chain + // // Tip: This method can only be used for storage nodes + // SubmitIdleProof(idleProve []types.U8) (string, error) + // // SubmitServiceProof submits proof of service data to the chain + // // Tip: This method can only be used for storage nodes + // SubmitServiceProof(serviceProof []types.U8) (string, error) + // // SubmitIdleProofResult submits the proof verification results of idle data to the chain + // // Tip: This method can only be used for storage nodes + // SubmitIdleProofResult(totalProofHash []types.U8, front, rear types.U64, accumulator pattern.Accumulator, result types.Bool, sig types.Bytes, teePuk pattern.WorkerPublicKey) (string, error) + // // SubmitServiceProofResult submits the proof verification results of service data to the chain + // // Tip: This method can only be used for storage nodes + // SubmitServiceProofResult(result types.Bool, sign types.Bytes, bloomFilter pattern.BloomFilter, teePuk pattern.WorkerPublicKey) (string, error) + + // // Filebank-State + + // // QueryBucketInfo query the bucket information of the accountID + // QueryBucketInfo(accountID []byte, bucketName string) (pattern.BucketInfo, error) + // // QueryAllBucket query all buckets of the accountID + // QueryAllBucket(accountID []byte) ([]types.Bytes, error) + // // QueryAllBucketString query all bucket names as string of the accountID + // QueryAllBucketString(accountID []byte) ([]string, error) + // // QueryStorageOrder query storage order information. + // QueryStorageOrder(fid string) (pattern.StorageOrder, error) + // // QueryFileMetadata queries the metadata of the roothash file. + // QueryFileMetadata(fid string) (pattern.FileMetadata, error) + // // QueryFileMetadataByBlock queries the metadata of the roothash file. + // QueryFileMetadataByBlock(fid string, block uint64) (pattern.FileMetadata, error) + // // QueryRestoralOrder queries a restore order info. + // QueryRestoralOrder(roothash string) (pattern.RestoralOrderInfo, error) + // QueryRestoralOrderList() ([]pattern.RestoralOrderInfo, error) + + // // Filebank-Extrinsics + + // // ClaimRestoralNoExistOrder is used to receive recovery orders from exiting miners. + // ClaimRestoralNoExistOrder(accountID []byte, fid, restoralFragmentHash string) (string, error) + // // ClaimRestoralOrder is used to collect restoration orders. + // ClaimRestoralOrder(fragmentHash string) (string, error) + // // CreateBucket creates a bucket for accountID + // // For details on bucket naming rules, see: + // // https://docs.cess.cloud/deoss/get-started/deoss-gateway/step-1-create-a-bucket#naming-conventions-for-a-bucket + // CreateBucket(accountID []byte, bucketName string) (string, error) + // // DeleteBucket deletes buckets for accountID + // // Tip: Only empty buckets can be deleted + // DeleteBucket(accountID []byte, bucketName string) (string, error) + // // DeleteFile deletes files for accountID + // DeleteFile(puk []byte, filehash string) (string, error) + // // GenerateRestoralOrder generates data for restoration orders. + // GenerateRestoralOrder(fid, fragmentHash string) (string, error) + // // RestoralComplete reports order recovery completion. + // RestoralComplete(restoralFragmentHash string) (string, error) + // // SubmitFileReport submits a stored file report. + // SubmitFileReport(index types.U8, roothash pattern.FileHash) (string, error) + // ReportFile(index uint8, roothash string) (string, error) + // // UploadDeclaration creates a storage order. + // UploadDeclaration(filehash string, dealinfo []pattern.SegmentList, user pattern.UserBrief, filesize uint64) (string, error) + // // GenerateStorageOrder for generating storage orders + // GenerateStorageOrder(roothash string, segment []pattern.SegmentDataInfo, owner []byte, filename, buckname string, filesize uint64) (string, error) + // // CertIdleSpace + // CertIdleSpace(idleSignInfo pattern.SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk pattern.WorkerPublicKey) (string, error) + // // ReplaceIdleSpace + // ReplaceIdleSpace(idleSignInfo pattern.SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk pattern.WorkerPublicKey) (string, error) + // // ReportTagCalculated + // ReportTagCalculated(teeSig types.Bytes, tagSigInfo pattern.TagSigInfo) (string, error) + + // // Oss-State + + // // QueryAuthorizedAccountIDs queries all DeOSS accountIDs authorized by accountID + // QueryAuthorizedAccountIDs(accountID []byte) ([]types.AccountID, error) + // // QueryAuthorizedAccounts queries all DeOSS accounts authorized by accountID + // QueryAuthorizedAccounts(accountID []byte) ([]string, error) + // // QueryDeOSSInfo Query the DeOSS information registered by accountID account + // QueryDeOSSInfo(accountID []byte) (pattern.OssInfo, error) + // // QueryAllDeOSSInfo queries all deoss information + // QueryAllDeOSSInfo() ([]pattern.OssInfo, error) + // // QueryAllDeOSSPeerId queries peerID of all DeOSS + // QueryAllDeOSSPeerId() ([]string, error) + + // // Oss-Extrinsics + + // // RegisterDeOSS register as deoss node + // RegisterDeOSS(peerId []byte, domain string) (string, error) + // // UpdateDeOSS updates the peerID and domain of deoss + // // Tip: This method can only be used for DeOSS nodes + // UpdateDeOSS(peerId string, domain string) (string, error) + // // ExitDeoss exit deoss + // // Tip: This method can only be used for DeOSS nodes + // ExitDeOSS() (string, error) + // // AuthorizeSpace authorizes space to oss account + // // Tip: accountID can only be a DeOSS node account + // AuthorizeSpace(accountID string) (string, error) + // // UnAuthorizeSpace cancels space authorization + // // Tip: accountID can only be a DeOSS node account + // UnAuthorizeSpace(accountID string) (string, error) + + // // Sminer-State + + // // QueryExpenders queries expenders information + // QueryExpenders() (pattern.ExpendersInfo, error) + // // // QueryStorageMinerByAccount queries storage node information by account. + // QueryStorageMinerByAccount(account string) (pattern.MinerInfo, error) + // // QueryStorageMiner queries storage node information. + // QueryStorageMiner(accountID []byte) (pattern.MinerInfo, error) + // // QueryStorageMinerByBlock queries storage node information in specified block + // // - if block < 0, then query the latest block + // QueryStorageMinerByBlock(puk []byte, block int32) (pattern.MinerInfo, error) + // // QueryAllSminerAccount queries the accounts of all storage miners. + // QueryAllSminerAccount() ([]types.AccountID, error) + // // QueryAllSminerAccountByBlock queries accounts of all storage miners in specified block. + // // - if block < 0, then query the latest block + // QueryAllSminerAccountByBlock(block int32) ([]types.AccountID, error) + // // QueryRewardsMap queries rewardsMap for accountID + // // Tip: accountID can only be a storage node account + // QueryRewardsMap(accountID []byte) (pattern.MinerReward, error) + // // QueryRewards queries rewards for accountID + // // Tip: accountID can only be a storage node account + // QueryRewards(accountID []byte) (pattern.RewardsType, error) + // // QueryStorageMinerStakingStartBlock + // QueryStorageMinerStakingStartBlock(puk []byte) (types.U32, error) + // // QueryRestoralTarget queries the space recovery information of the exited storage node + // // Tip: accountID can only be a storage node account + // QueryRestoralTarget(accountID []byte) (pattern.RestoralTargetInfo, error) + // // QueryRestoralTargetList queries the space recovery information of all exited storage nodes + // QueryRestoralTargetList() ([]pattern.RestoralTargetInfo, error) + // // QueryPendingReplacements queries the amount of idle data that can be replaced + // // Tip: accountID can only be a storage node account + // QueryPendingReplacements(accountID []byte) (types.U128, error) + // // + // QueryCompleteSnapShot(era uint32, block int32) (uint32, uint64, error) + + // // Sminer-Extrinsics + + // // + // IncreaseDeclarationSpace(tibCount uint32) (string, error) + // // RegisterSminer register sminer information + // RegisterSminer(earnings string, peerId []byte, pledge uint64, tib_count uint32) (string, error) + // // RegisterSminerAssignStaking + // RegisterSminerAssignStaking(beneficiaryAcc string, peerId []byte, stakingAcc string, tib_count uint32) (string, error) + // // RegisterSminerPOISKey register the pois key of sminer + // RegisterSminerPOISKey(poisKey pattern.PoISKeyInfo, teeSignWithAcc, teeSign types.Bytes, teePuk pattern.WorkerPublicKey) (string, error) + // // ExitSminer exit mining + // ExitSminer(miner string) (string, error) + // // UpdateEarningsAcc update earnings account. + // UpdateEarningsAcc(puk []byte) (string, error) + // UpdateEarningsAccount(earnings string) (string, error) + // // UpdateSminerPeerId update miner peerid + // UpdateSminerPeerId(peerid pattern.PeerId) (string, error) + // // IncreaseStakingAmount increase staking amount. + // IncreaseStakingAmount(miner string, tokens *big.Int) (string, error) + // IncreaseStorageNodeStakingAmount(miner string, token string) (string, error) + // // ClaimRewards is used to claim rewards. + // ClaimRewards() (string, error) + // // Withdraw is used to withdraw staking. + // Withdraw() (string, error) + + // // Staking-State + + // // QueryAllValidatorCount queries all the validator count + // QueryAllValidatorCount(block int) (uint32, error) + // // + // QueryValidatorsCount(block int) (uint32, error) + // // + // QueryNominatorCount(block int) (uint32, error) + // // + // QueryErasTotalStake(era uint32, block int) (string, error) + // // + // QueryCurrentEra(block int) (uint32, error) + // // + // //QueryeErasStakers(era uint32) ([]pattern.StakingExposure, error) + // // + // QueryStakingEraRewardPoints(era uint32) (pattern.StakingEraRewardPoints, error) + // // + // QueryNominatorsLatest() ([]pattern.StakingNominations, error) + // // + // QueryBondedList(block int32) ([]types.AccountID, error) + // // + // QueryValidatorCommission(account string, block int) (uint8, error) + // // + // QueryEraValidatorReward(era uint32) (string, error) + + // // StorageHandler-State + + // // QueryUserSpaceInfo queries the space information purchased by the user. + // QueryUserSpaceInfo(puk []byte) (pattern.UserSpaceInfo, error) + // QueryUserSpaceSt(puk []byte) (pattern.UserSpaceSt, error) + // // QuerySpacePricePerGib query space price per GiB. + // QuerySpacePricePerGib() (string, error) + // // QueryTotalIdleSpace query total idle space + // QueryTotalIdleSpace() (uint64, error) + // // QueryTotalServiceSpace query total service space + // QueryTotalServiceSpace() (uint64, error) + // // QueryPurchasedSpace query purchased space + // QueryPurchasedSpace() (uint64, error) + + // // StorageHandler-Extrinsics + + // // BuySpace for purchasing space. + // BuySpace(count uint32) (string, error) + // // ExpansionSpace for expansion space. + // ExpansionSpace(count uint32) (string, error) + // // RenewalSpace is used to extend the validity of space. + // RenewalSpace(days uint32) (string, error) + + // // TeeWorker-State + + // // QueryTeeWorker queries the information of the Tee worker. + // QueryTeeWorker(puk pattern.WorkerPublicKey) (pattern.TeeWorkerInfo, error) + // // QueryTeeInfo queries the information of the Tee worker. + // QueryTeeInfo(puk pattern.WorkerPublicKey) (pattern.TeeInfo, error) + // // QueryMasterPublicKey queries the master publicKey. + // QueryMasterPublicKey() ([]byte, error) + // // QueryAllTeeWorkerMap queries the information of all tee workers. + // QueryAllTeeWorkerMap() ([]pattern.TeeWorkerInfo, error) + // // QueryAllTeeInfo queries the information of all tee workers. + // QueryAllTeeInfo() ([]pattern.TeeInfo, error) + // // QueryTeeWorkEndpoint queries the endpoint of tee workers. + // QueryTeeWorkEndpoint(workPuk pattern.WorkerPublicKey) (string, error) + // // + // QueryWorkerAddedAt(workPuk pattern.WorkerPublicKey) (types.U32, error) + + // // System + + // // QueryBlockHeight queries the block height corresponding to the block hash, + // // If the blockhash is empty, query the latest block height. + // QueryBlockHeight(blockhash string) (uint32, error) + // // QueryNodeSynchronizationSt returns the synchronization status of the current node. + // QueryNodeSynchronizationSt() (bool, error) + // // QueryAccountInfoByAccount query account information by account. + // QueryAccountInfoByAccount(acc string) (types.AccountInfo, error) + // // QueryAccountInfo query account information by public key. + // QueryAccountInfo(puk []byte) (types.AccountInfo, error) + // // GetTokenSymbol returns the token symbol + // GetTokenSymbol() string + // // GetNetworkEnv returns the network environment + // GetNetworkEnv() string + // // SysProperties returns the system properties. + // SysProperties() (pattern.SysProperties, error) + // // SyncState returns the system sync state. + // SyncState() (pattern.SysSyncState, error) + // // SysVersion returns the system version. + // ChainVersion() (string, error) + // // NetListening returns whether the current node is listening. + // NetListening() (bool, error) + // // + // DecodeEventNameFromBlock(block uint64) ([]string, error) + // // + // DecodeEventNameFromBlockhash(blockhash types.Hash) ([]string, error) + // // + // QueryAllAccountInfoFromBlock(block int) ([]types.AccountInfo, error) + // // + // QueryTotalIssuance(block int) (string, error) + + // // CessTreasury + // QueryRoundReward(era uint32, block int32) (string, error) + + // // TransferToken to dest. + // // + // // Receive parameter: + // // - dest: target account. + // // - amount: transfer amount. + // // Return parameter: + // // - string: transaction hash. + // // - string: target account. + // // - error: error message. + // TransferToken(dest string, amount uint64) (string, string, error) + + // // Other + + // // GetSignatureAcc returns the signature account. + // GetSignatureAcc() string + // // GetSignatureAccPulickey returns the signature account public key. + // GetSignatureAccPulickey() []byte + // // GetSubstrateAPI returns Substrate API. + // GetSubstrateAPI() *gsrpc.SubstrateAPI + // // GetChainState returns chain node state. + // GetChainState() bool + // // SetChainState sets the state of the chain node. + // SetChainState(state bool) + // // GetSdkName return sdk name + // GetSDKName() string + // // GetCurrentRpcAddr return current rpc address + // GetCurrentRpcAddr() string + // // SetSdkName set sdk name + // SetSDKName(name string) + // // ReconnectRPC for reconnecting chains. + // ReconnectRPC() error + // // GetMetadata returns the metadata of the chain. + // GetMetadata() *types.Metadata + // // GetKeyEvents returns the events storage key. + // GetKeyEvents() types.StorageKey + // // GetURI returns URI. + // GetURI() string + // // GetTreasuryAccount returns the treasury account + // GetTreasuryAccount() string + // // Sign returns the signature of the msg with the private key of the signing account. + // Sign(msg []byte) ([]byte, error) + // // Verify the signature of the msg with the public key of the signing account. + // Verify(msg []byte, sig []byte) (bool, error) + // // Verify Polka Signature With JavaScript + // VerifyPolkaSignatureWithJS(account, msg, signature string) (bool, error) + // // Verify Polka Signature With Base58 + // VerifyPolkaSignatureWithBase58(account, msg, signature string) (bool, error) + // // + // Close() + + // // Upload file to CESS gateway. + // // + // // Receive parameter: + // // - url: the address of the gateway. + // // - file: uploaded file. + // // - bucket: the bucket for storing files, it will be created automatically. + // // Return parameter: + // // - string: [fid] unique identifier for the file. + // // - error: error message. + // // Preconditions: + // // 1. Account requires purchasing space, refer to [BuySpace] interface. + // // 2. Authorize the space usage rights of the account to the gateway account, + // // refer to the [AuthorizeSpace] interface. + // // 3. Make sure the name of the bucket is legal, use the [CheckBucketName] method to check. + // // Explanation: + // // - 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] + // StoreFile(url, file, bucket string) (string, error) + + // // Upload object to CESS gateway. + // // + // // Receive parameter: + // // - url: the address of the gateway. + // // - reader: strings, byte data, file streams, network streams, etc. + // // - bucket: the bucket for storing object, it will be created automatically. + // // Return parameter: + // // - string: [fid] unique identifier for the file. + // // - error: error message. + // // Preconditions: + // // 1. Account requires purchasing space, refer to [BuySpace] interface. + // // 2. Authorize the space usage rights of the account to the gateway account, + // // refer to the [AuthorizeSpace] interface. + // // 3. Make sure the name of the bucket is legal, use the [CheckBucketName] method to check. + // // Explanation: + // // - 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] + // StoreObject(url string, reader io.Reader, bucket string) (string, error) + + // // Download file from CESS public gateway. + // // + // // Receive parameter: + // // - url: the address of the gateway. + // // - fid: unique identifier for the file. + // // - savepath: file save location. + // // Return parameter: + // // - error: error message. + // RetrieveFile(url, fid, savepath string) error + + // // Download object from CESS gateway. + // // + // // Receive parameter: + // // - url: the address of the gateway. + // // - fid: unique identifier for the file. + // // Return parameter: + // // - Reader: object stream. + // // - error: error message. + // RetrieveObject(url, fid string) (io.ReadCloser, error) + + // // Split File into Chunks. + // // + // // Receive parameter: + // // - fpath: the path of the file to be split. + // // - chunksDir: directory path to store file chunks, please do not mix it elsewhere. + // // - chunkSize: the size of each chunk, it does not exceed the file size + // // Return parameter: + // // - int64: chunks total size (byte). + // // - int: number of file chunks. + // // - error: error message. + // SplitFile(fpath, chunksDir string, chunkSize int64, filling bool) (int64, int, error) + + // // Split File into Chunks with standard size. + // // It split file into chunks of the default size and fills the last chunk that does not meet the size. + // // + // // Receive parameter: + // // - fpath: the path of the file to be split. + // // - chunksDir: directory path to store file chunks, please do not mix it elsewhere. + // // Return parameter: + // // - int64: chunks total size (byte). + // // - int: number of file chunks. + // // - error: error message. + // SplitFileWithstandardSize(fpath, chunksDir string) (int64, int, error) + + // // Upload chunk of file to the gateway + // // + // // Receive parameter: + // // - url: the address of the gateway. + // // - chunksDir: directory path to store file chunks, please do not mix it elsewhere. + // // - bucket: the bucket name to store user data. + // // - fname: the name of the file. + // // - chunksNum: total number of file chunks. + // // - chunksId: index of the current chunk to be uploaded ([0,chunksNum)). + // // - totalSize: chunks total size (byte), can be obtained from the first return value of SplitFile + // // Return parameter: + // // - Reader: number of file chunks. + // // - error: error message. + // UploadFileChunk(url, chunksDir, bucket, fname string, chunksNum, chunksId int, totalSize int64) (string, error) + + // // Upload file chunks in the directory to the gateway as much as possible, chunks will be removed after being uploaded, if the chunks are not transferred successfuly, jus + // // + // // Receive parameter: + // // - url: the address of the gateway. + // // - chunksDir: directory path to store file chunks, please do not mix it elsewhere. + // // - bucket: the bucket name to store user data. + // // - fname: the name of the file. + // // - chunksNum: total number of file chunks. + // // - totalSize: chunks total size (byte), can be obtained from the first return value of SplitFile + // // Return parameter: + // // - Reader: number of file chunks. + // // - error: error message. + // UploadFileChunks(url, chunksDir, bucket, fname string, chunksNum int, totalSize int64) (string, error) + + // // retrieve event + + // // + // RetrieveEvent_Audit_SubmitIdleProof(blockhash types.Hash) (event.Event_SubmitIdleProof, error) + // // + // RetrieveEvent_Audit_SubmitServiceProof(blockhash types.Hash) (event.Event_SubmitServiceProof, error) + // // + // RetrieveEvent_Audit_SubmitIdleVerifyResult(blockhash types.Hash) (event.Event_SubmitIdleVerifyResult, error) + // // + // RetrieveEvent_Audit_SubmitServiceVerifyResult(blockhash types.Hash) (event.Event_SubmitServiceVerifyResult, error) + // // + // RetrieveEvent_Oss_OssUpdate(blockhash types.Hash) (event.Event_OssUpdate, error) + // // + // RetrieveEvent_Oss_OssRegister(blockhash types.Hash) (event.Event_OssRegister, error) + // // + // RetrieveEvent_Oss_OssDestroy(blockhash types.Hash) (event.Event_OssDestroy, error) + // // + // RetrieveEvent_Oss_Authorize(blockhash types.Hash) (event.Event_Authorize, error) + // // + // RetrieveEvent_Oss_CancelAuthorize(blockhash types.Hash) (event.Event_CancelAuthorize, error) + // // + // RetrieveEvent_FileBank_UploadDeclaration(blockhash types.Hash) (event.Event_UploadDeclaration, error) + // // + // RetrieveEvent_FileBank_CreateBucket(blockhash types.Hash) (event.Event_CreateBucket, error) + // // + // RetrieveEvent_FileBank_DeleteFile(blockhash types.Hash) (event.Event_DeleteFile, error) + // // + // RetrieveEvent_FileBank_TransferReport(blockhash types.Hash) (event.Event_TransferReport, error) + // // + // RetrieveEvent_FileBank_ClaimRestoralOrder(blockhash types.Hash) (event.Event_ClaimRestoralOrder, error) + // // + // RetrieveEvent_FileBank_RecoveryCompleted(blockhash types.Hash) (event.Event_RecoveryCompleted, error) + // // + // RetrieveEvent_FileBank_IdleSpaceCert(blockhash types.Hash) (event.Event_IdleSpaceCert, error) + // // + // RetrieveEvent_FileBank_ReplaceIdleSpace(blockhash types.Hash) (event.Event_ReplaceIdleSpace, error) + // // + // RetrieveEvent_FileBank_CalculateReport(blockhash types.Hash) (event.Event_CalculateReport, error) + // // + // RetrieveEvent_Sminer_Registered(blockhash types.Hash) (event.Event_Registered, error) + // // + // RetrieveEvent_Sminer_RegisterPoisKey(blockhash types.Hash) (event.Event_RegisterPoisKey, error) + // // + // RetrieveEvent_Sminer_UpdataIp(blockhash types.Hash) (event.Event_UpdatePeerId, error) + // // + // RetrieveEvent_Sminer_UpdataBeneficiary(blockhash types.Hash) (event.Event_UpdateBeneficiary, error) + // // + // RetrieveEvent_Sminer_MinerExitPrep(blockhash types.Hash) (event.Event_MinerExitPrep, error) + // // + // RetrieveEvent_Sminer_IncreaseCollateral(blockhash types.Hash) (event.Event_IncreaseCollateral, error) + // // + // RetrieveEvent_Sminer_Receive(blockhash types.Hash) (event.Event_Receive, error) + // // + // RetrieveEvent_Sminer_Withdraw(blockhash types.Hash) (event.Event_Withdraw, error) + // // + // RetrieveEvent_Sminer_IncreaseDeclarationSpace(blockhash types.Hash) (event.Event_IncreaseDeclarationSpace, error) + // // + // RetrieveEvent_StorageHandler_BuySpace(blockhash types.Hash) (event.Event_BuySpace, error) + // // + // RetrieveEvent_StorageHandler_ExpansionSpace(blockhash types.Hash) (event.Event_ExpansionSpace, error) + // // + // RetrieveEvent_StorageHandler_RenewalSpace(blockhash types.Hash) (event.Event_RenewalSpace, error) + // // + // RetrieveEvent_Balances_Transfer(blockhash types.Hash) (types.EventBalancesTransfer, error) + // // + // RetrieveEvent_FileBank_GenRestoralOrder(blockhash types.Hash) (event.Event_GenerateRestoralOrder, error) + // // + // RetrieveAllEvent_FileBank_UploadDeclaration(blockhash types.Hash) ([]event.AllUploadDeclarationEvent, error) + // // + // RetrieveAllEvent_FileBank_StorageCompleted(blockhash types.Hash) ([]string, error) + // // + // RetrieveAllEvent_FileBank_DeleteFile(blockhash types.Hash) ([]event.AllDeleteFileEvent, error) + // // + // //RetrieveAllEvent(blockhash types.Hash) ([]string, []string, error) + // // + // RetrieveAllEventFromBlock(blockhash types.Hash) ([]string, map[string][]string, error) + // // + // RetrieveBlock(blocknumber uint64) ([]string, []event.ExtrinsicsInfo, []event.TransferInfo, string, string, string, string, int64, error) + // // + // RetrieveBlockAndAll(blocknumber uint64) ([]string, []event.ExtrinsicsInfo, []event.TransferInfo, []string, []string, string, string, string, string, string, int64, error) + // // + // ParseBlockData(blocknumber uint64) (event.BlockData, error) + // // + // InitExtrinsicsName() error } From e4da0d7a134299c90135bbec1af567ae5c3d6e46 Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Thu, 9 May 2024 14:41:25 +0800 Subject: [PATCH 08/18] update storage handler --- chain/event.go | 25 ++- chain/file.go | 4 +- chain/fileBank.go | 3 +- chain/pattern.go | 43 ++-- chain/storage.go | 386 +++++++++++++++++++++++------------- chain/{utils.go => util.go} | 0 example/file/file.go | 7 +- 7 files changed, 294 insertions(+), 174 deletions(-) rename chain/{utils.go => util.go} (100%) diff --git a/chain/event.go b/chain/event.go index 2a5619c..6cdfc13 100755 --- a/chain/event.go +++ b/chain/event.go @@ -15,7 +15,6 @@ import ( "strconv" "strings" - "github.com/CESSProject/cess-go-sdk/core/pattern" "github.com/CESSProject/cess-go-sdk/utils" "github.com/centrifuge/go-substrate-rpc-client/v4/registry/parser" "github.com/centrifuge/go-substrate-rpc-client/v4/types" @@ -1662,13 +1661,13 @@ func (c *ChainClient) RetrieveAllEvent_FileBank_UploadDeclaration(blockhash type } else if strings.Contains(v.Name, "deal_hash") { temp := strings.Split(allValue, "] ") for _, v := range temp { - if strings.Count(v, " ") == (pattern.FileHashLen - 1) { + if strings.Count(v, " ") == (FileHashLen - 1) { subValue := strings.TrimPrefix(v, "[") ids := strings.Split(subValue, " ") - if len(ids) != pattern.FileHashLen { + if len(ids) != FileHashLen { continue } - var fhash pattern.FileHash + var fhash FileHash for kk, vv := range ids { intv, _ := strconv.Atoi(vv) fhash[kk] = types.U8(intv) @@ -1702,13 +1701,13 @@ func (c *ChainClient) RetrieveAllEvent_FileBank_StorageCompleted(blockhash types if strings.Contains(v.Name, "file_hash") { temp := strings.Split(allValue, "] ") for _, v := range temp { - if strings.Count(v, " ") == (pattern.FileHashLen - 1) { + if strings.Count(v, " ") == (FileHashLen - 1) { subValue := strings.TrimPrefix(v, "[") ids := strings.Split(subValue, " ") - if len(ids) != pattern.FileHashLen { + if len(ids) != FileHashLen { continue } - var fhash pattern.FileHash + var fhash FileHash for kk, vv := range ids { intv, _ := strconv.Atoi(vv) fhash[kk] = types.U8(intv) @@ -1778,13 +1777,13 @@ func (c *ChainClient) RetrieveAllEvent_FileBank_DeleteFile(blockhash types.Hash) allValue := fmt.Sprintf("%v", vf.Index(0)) temp := strings.Split(allValue, "] ") for _, v := range temp { - if strings.Count(v, " ") == (pattern.FileHashLen - 1) { + if strings.Count(v, " ") == (FileHashLen - 1) { subValue := strings.TrimPrefix(v, "[") ids := strings.Split(subValue, " ") - if len(ids) != pattern.FileHashLen { + if len(ids) != FileHashLen { continue } - var fhash pattern.FileHash + var fhash FileHash for kk, vv := range ids { intv, _ := strconv.Atoi(vv) fhash[kk] = types.U8(intv) @@ -2491,12 +2490,12 @@ func parseFidString(v reflect.Value) string { if v.Len() > 0 { allValue := fmt.Sprintf("%v", v.Index(0)) temp := strings.Split(allValue, "] ") - puk := make([]byte, pattern.FileHashLen) + puk := make([]byte, FileHashLen) for _, v := range temp { - if strings.Count(v, " ") == (pattern.FileHashLen - 1) { + if strings.Count(v, " ") == (FileHashLen - 1) { subValue := strings.TrimPrefix(v, "[") ids := strings.Split(subValue, " ") - if len(ids) != pattern.FileHashLen { + if len(ids) != FileHashLen { continue } for kk, vv := range ids { diff --git a/chain/file.go b/chain/file.go index 6f5f26d..3112475 100755 --- a/chain/file.go +++ b/chain/file.go @@ -18,7 +18,7 @@ import ( "path/filepath" "strings" - "github.com/CESSProject/cess-go-sdk/core/pattern" + "github.com/CESSProject/cess-go-sdk/config" "github.com/CESSProject/cess-go-sdk/utils" keyring "github.com/CESSProject/go-keyring" "github.com/btcsuite/btcutil/base58" @@ -302,7 +302,7 @@ func (c *ChainClient) SplitFile(fpath, chunksDir string, chunkSize int64, fillin } func (c *ChainClient) SplitFileWithstandardSize(fpath, chunksDir string) (int64, int, error) { - return c.SplitFile(fpath, chunksDir, pattern.SegmentSize, true) + return c.SplitFile(fpath, chunksDir, config.SegmentSize, true) } func (c *ChainClient) UploadFileChunks(url, chunksDir, bucket, fname string, chunksNum int, totalSize int64) (string, error) { diff --git a/chain/fileBank.go b/chain/fileBank.go index 8cbd415..5be7a14 100755 --- a/chain/fileBank.go +++ b/chain/fileBank.go @@ -15,6 +15,7 @@ import ( "strings" "time" + "github.com/CESSProject/cess-go-sdk/config" "github.com/CESSProject/cess-go-sdk/utils" "github.com/centrifuge/go-substrate-rpc-client/v4/types" "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" @@ -1044,7 +1045,7 @@ func (c *ChainClient) TransferReport(index uint8, fid string) (string, error) { return blockhash, ERR_RPC_CONNECTION } - if index <= 0 || int(index) > (DataShards+ParShards) { + if index <= 0 || int(index) > (config.DataShards+config.ParShards) { return "", errors.New("invalid index") } diff --git a/chain/pattern.go b/chain/pattern.go index d450ea8..d26a985 100644 --- a/chain/pattern.go +++ b/chain/pattern.go @@ -67,11 +67,11 @@ const ( CountedServiceFailed = "CountedServiceFailed" VerifySlip = "VerifySlip" - // OSS - // OSS + // Oss + // Oss AuthorityList = "AuthorityList" - // SMINER + // Sminer AllMiner = "AllMiner" MinerItems = "MinerItems" RewardMap = "RewardMap" @@ -86,7 +86,7 @@ const ( TEEEndpoints = "Endpoints" TEEWorkerAddedAt = "WorkerAddedAt" - // FILEBANK + // FileBank File = "File" Bucket = "Bucket" DealMap = "DealMap" @@ -96,7 +96,7 @@ const ( UserBucketList = "UserBucketList" UserHoldFileList = "UserHoldFileList" - // STAKING + // Staking COUNTERFORVALIDATORS = "CounterForValidators" CounterForNominators = "CounterForNominators" ErasTotalStake = "ErasTotalStake" @@ -109,7 +109,7 @@ const ( ErasValidatorReward = "ErasValidatorReward" ValidatorCount = "ValidatorCount" - // STORAGE_HANDLER + // StorageHandler UserOwnedSpace = "UserOwnedSpace" UnitPrice = "UnitPrice" TotalIdleSpace = "TotalIdleSpace" @@ -120,7 +120,7 @@ const ( TotalIssuance = "TotalIssuance" InactiveIssuance = "InactiveIssuance" - // SYSTEM + // System Account = "Account" Events = "Events" @@ -172,10 +172,10 @@ const ( TX_FileBank_ReplaceIdleSpace = FileBank + DOT + "replace_idle_space" TX_FileBank_CalculateReport = FileBank + DOT + "calculate_report" - // STORAGE_HANDLER - TX_STORAGE_BUYSPACE = STORAGEHANDLER + DOT + "buy_space" - TX_STORAGE_EXPANSIONSPACE = STORAGEHANDLER + DOT + "expansion_space" - TX_STORAGE_RENEWALSPACE = STORAGEHANDLER + DOT + "renewal_space" + // StorageHandler + TX_StorageHandler_BuySpace = StorageHandler + DOT + "buy_space" + TX_StorageHandler_ExpansionSpace = StorageHandler + DOT + "expansion_space" + TX_StorageHandler_RenewalSpace = StorageHandler + DOT + "renewal_space" // BALANCES TX_Balances_Transfer = "Balances" + DOT + "transfer" @@ -341,6 +341,17 @@ type OssInfo struct { Domain types.Bytes } +// StorageHandler +type UserSpaceInfo struct { + TotalSpace types.U128 + UsedSpace types.U128 + LockedSpace types.U128 + RemainingSpace types.U128 + Start types.U32 + Deadline types.U32 + State types.Bytes +} + type MinerInfo struct { BeneficiaryAccount types.AccountID StakingAccount types.AccountID @@ -400,16 +411,6 @@ type CompleteInfo struct { Miner types.AccountID } -type UserSpaceInfo struct { - TotalSpace types.U128 - UsedSpace types.U128 - LockedSpace types.U128 - RemainingSpace types.U128 - Start types.U32 - Deadline types.U32 - State types.Bytes -} - type ProveInfo struct { Assign types.U8 IdleProve types.Option[IdleProveInfo] diff --git a/chain/storage.go b/chain/storage.go index ab157e6..8fef1ac 100755 --- a/chain/storage.go +++ b/chain/storage.go @@ -13,14 +13,19 @@ import ( "strings" "time" - "github.com/CESSProject/cess-go-sdk/core/pattern" "github.com/CESSProject/cess-go-sdk/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) QuerySpacePricePerGib() (string, error) { +// QueryUnitPrice query price per GiB space +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - string: price per GiB space +// - error: error message +func (c *ChainClient) QueryUnitPrice(block int32) (string, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -30,88 +35,119 @@ func (c *ChainClient) QuerySpacePricePerGib() (string, error) { var data types.U128 if !c.GetChainState() { - return "", pattern.ERR_RPC_CONNECTION + return "", ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.STORAGEHANDLER, pattern.UNITPRICE) + key, err := types.CreateStorageKey(c.metadata, StorageHandler, UnitPrice) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.STORAGEHANDLER, pattern.UNITPRICE, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), StorageHandler, UnitPrice, err) c.SetChainState(false) return "", err } - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + 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, UnitPrice, err) + c.SetChainState(false) + return "", err + } + if !ok { + return "", ERR_RPC_EMPTY_VALUE + } + + return fmt.Sprintf("%v", data), nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + c.SetChainState(false) + return "", err + } + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.STORAGEHANDLER, pattern.UNITPRICE, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), StorageHandler, UnitPrice, err) c.SetChainState(false) return "", err } if !ok { - return "", pattern.ERR_RPC_EMPTY_VALUE + return "", ERR_RPC_EMPTY_VALUE } return fmt.Sprintf("%v", data), nil } -func (c *ChainClient) QueryUserSpaceInfo(puk []byte) (pattern.UserSpaceInfo, error) { +// QueryUserOwnedSpace to query user purchased space information +// - accountID: user account +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - UserSpaceInfo: space information +// - error: error message +func (c *ChainClient) QueryUserOwnedSpace(accountID []byte, block int32) (UserSpaceInfo, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data pattern.UserSpaceInfo + var data UserSpaceInfo if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + return data, ERR_RPC_CONNECTION } - acc, err := types.NewAccountID(puk) + acc, err := types.NewAccountID(accountID) if err != nil { return data, errors.Wrap(err, "[NewAccountID]") } - owner, err := codec.Encode(*acc) + user, err := codec.Encode(*acc) if err != nil { return data, errors.Wrap(err, "[EncodeToBytes]") } - key, err := types.CreateStorageKey(c.metadata, pattern.STORAGEHANDLER, pattern.USERSPACEINFO, owner) + key, err := types.CreateStorageKey(c.metadata, StorageHandler, UserOwnedSpace, user) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.STORAGEHANDLER, pattern.USERSPACEINFO, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), StorageHandler, UserOwnedSpace, err) return data, err } - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + 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, UserOwnedSpace, err) + c.SetChainState(false) + return data, err + } + if !ok { + return data, ERR_RPC_EMPTY_VALUE + } + return data, nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.STORAGEHANDLER, pattern.USERSPACEINFO, err) + c.SetChainState(false) + return data, err + } + 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, UserOwnedSpace, err) c.SetChainState(false) return data, err } if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + return data, ERR_RPC_EMPTY_VALUE } return data, nil } -func (c *ChainClient) QueryUserSpaceSt(puk []byte) (pattern.UserSpaceSt, error) { - var userSpaceSt pattern.UserSpaceSt - spaceinfo, err := c.QueryUserSpaceInfo(puk) - if err != nil { - return userSpaceSt, err - } - userSpaceSt.Start = uint32(spaceinfo.Start) - userSpaceSt.Deadline = uint32(spaceinfo.Deadline) - userSpaceSt.TotalSpace = spaceinfo.TotalSpace.String() - userSpaceSt.UsedSpace = spaceinfo.UsedSpace.String() - userSpaceSt.RemainingSpace = spaceinfo.RemainingSpace.String() - userSpaceSt.LockedSpace = spaceinfo.LockedSpace.String() - userSpaceSt.State = string(spaceinfo.State) - return userSpaceSt, nil -} - -func (c *ChainClient) QueryTotalIdleSpace() (uint64, error) { +// QueryTotalIdleSpace query the size of all idle space +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - uint64: the size of all idle space +// - error: error message +func (c *ChainClient) QueryTotalIdleSpace(block int32) (uint64, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -121,29 +157,52 @@ func (c *ChainClient) QueryTotalIdleSpace() (uint64, error) { var data types.U128 if !c.GetChainState() { - return 0, pattern.ERR_RPC_CONNECTION + return 0, ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.STORAGEHANDLER, pattern.TOTALIDLESPACE) + key, err := types.CreateStorageKey(c.metadata, StorageHandler, TotalIdleSpace) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.STORAGEHANDLER, pattern.TOTALIDLESPACE, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), StorageHandler, TotalIdleSpace, err) c.SetChainState(false) return 0, err } - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + 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, TotalIdleSpace, err) + c.SetChainState(false) + return 0, err + } + if !ok { + return 0, ERR_RPC_EMPTY_VALUE + } + return data.Uint64(), nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + c.SetChainState(false) + return 0, err + } + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.STORAGEHANDLER, pattern.TOTALIDLESPACE, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), StorageHandler, TotalIdleSpace, err) c.SetChainState(false) return 0, err } if !ok { - return 0, pattern.ERR_RPC_EMPTY_VALUE + return 0, ERR_RPC_EMPTY_VALUE } return data.Uint64(), nil } -func (c *ChainClient) QueryTotalServiceSpace() (uint64, error) { +// QueryTotalServiceSpace query the size of all service space +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - uint64: the size of all service space +// - error: error message +func (c *ChainClient) QueryTotalServiceSpace(block int32) (uint64, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -153,29 +212,52 @@ func (c *ChainClient) QueryTotalServiceSpace() (uint64, error) { var data types.U128 if !c.GetChainState() { - return 0, pattern.ERR_RPC_CONNECTION + return 0, ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.STORAGEHANDLER, pattern.TOTALSERVICESPACE) + key, err := types.CreateStorageKey(c.metadata, StorageHandler, TotalServiceSpace) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.STORAGEHANDLER, pattern.TOTALSERVICESPACE, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), StorageHandler, TotalServiceSpace, err) c.SetChainState(false) return 0, err } - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + 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, TotalServiceSpace, err) + c.SetChainState(false) + return 0, err + } + if !ok { + return 0, ERR_RPC_EMPTY_VALUE + } + return data.Uint64(), nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + c.SetChainState(false) + return 0, err + } + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.STORAGEHANDLER, pattern.TOTALSERVICESPACE, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), StorageHandler, TotalServiceSpace, err) c.SetChainState(false) return 0, err } if !ok { - return 0, pattern.ERR_RPC_EMPTY_VALUE + return 0, ERR_RPC_EMPTY_VALUE } return data.Uint64(), nil } -func (c *ChainClient) QueryPurchasedSpace() (uint64, error) { +// QueryPurchasedSpace query all purchased space size +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - uint64: all purchased space size +// - error: error message +func (c *ChainClient) QueryPurchasedSpace(block int32) (uint64, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -185,28 +267,54 @@ func (c *ChainClient) QueryPurchasedSpace() (uint64, error) { var data types.U128 if !c.GetChainState() { - return 0, pattern.ERR_RPC_CONNECTION + return 0, ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.STORAGEHANDLER, pattern.PURCHASEDSPACE) + key, err := types.CreateStorageKey(c.metadata, StorageHandler, PurchasedSpace) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.STORAGEHANDLER, pattern.PURCHASEDSPACE, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), StorageHandler, PurchasedSpace, err) return 0, err } - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + 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, PurchasedSpace, err) + c.SetChainState(false) + return 0, err + } + if !ok { + return 0, ERR_RPC_EMPTY_VALUE + } + return data.Uint64(), nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.STORAGEHANDLER, pattern.PURCHASEDSPACE, err) + c.SetChainState(false) + return 0, err + } + 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, PurchasedSpace, err) c.SetChainState(false) return 0, err } if !ok { - return 0, pattern.ERR_RPC_EMPTY_VALUE + return 0, ERR_RPC_EMPTY_VALUE } return data.Uint64(), nil } +// BuySpace purchase space for current account +// - count: size of space purchased in gib +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - if you have already purchased space and you are unable to purchase it again, +// you have the option to expand your space. func (c *ChainClient) BuySpace(count uint32) (string, error) { c.lock.Lock() defer func() { @@ -217,7 +325,7 @@ func (c *ChainClient) BuySpace(count uint32) (string, error) { }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) @@ -226,33 +334,31 @@ func (c *ChainClient) BuySpace(count uint32) (string, error) { } if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - call, err := types.NewCall(c.metadata, pattern.TX_STORAGE_BUYSPACE, types.NewU32(count)) + call, err := types.NewCall(c.metadata, TX_StorageHandler_BuySpace, types.NewU32(count)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_BUYSPACE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_BuySpace, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_BUYSPACE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_BuySpace, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_BUYSPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_BuySpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -268,9 +374,8 @@ func (c *ChainClient) BuySpace(count uint32) (string, error) { // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_BUYSPACE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_BuySpace, err) + return blockhash, err } <-c.txTicker.C @@ -278,22 +383,23 @@ func (c *ChainClient) BuySpace(count uint32) (string, error) { // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_BUYSPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_BuySpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_BUYSPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_BuySpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -305,18 +411,24 @@ func (c *ChainClient) BuySpace(count uint32) (string, error) { select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_StorageHandler_BuySpace(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } +// ExpansionSpace expands the size of your space +// - count: size of space expands in gib +// +// Return: +// - string: block hash +// - error: error message func (c *ChainClient) ExpansionSpace(count uint32) (string, error) { c.lock.Lock() defer func() { @@ -327,42 +439,40 @@ func (c *ChainClient) ExpansionSpace(count uint32) (string, error) { }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if count == 0 { - return "", errors.New("[ExpansionSpace] invalid count") + return "", errors.New("[ExpansionSpace] space is 0") } if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - call, err := types.NewCall(c.metadata, pattern.TX_STORAGE_EXPANSIONSPACE, types.NewU32(count)) + call, err := types.NewCall(c.metadata, TX_StorageHandler_ExpansionSpace, types.NewU32(count)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_EXPANSIONSPACE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_ExpansionSpace, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_EXPANSIONSPACE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_ExpansionSpace, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_EXPANSIONSPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_ExpansionSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -378,9 +488,8 @@ func (c *ChainClient) ExpansionSpace(count uint32) (string, error) { // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_EXPANSIONSPACE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_ExpansionSpace, err) + return blockhash, err } <-c.txTicker.C @@ -388,22 +497,23 @@ func (c *ChainClient) ExpansionSpace(count uint32) (string, error) { // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_EXPANSIONSPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_ExpansionSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_EXPANSIONSPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_ExpansionSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -415,18 +525,24 @@ func (c *ChainClient) ExpansionSpace(count uint32) (string, error) { select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_StorageHandler_ExpansionSpace(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } +// RenewalSpace renew your space +// - days: renewal time, in days +// +// Return: +// - string: block hash +// - error: error message func (c *ChainClient) RenewalSpace(days uint32) (string, error) { c.lock.Lock() defer func() { @@ -437,7 +553,7 @@ func (c *ChainClient) RenewalSpace(days uint32) (string, error) { }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) @@ -446,33 +562,31 @@ func (c *ChainClient) RenewalSpace(days uint32) (string, error) { } if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - call, err := types.NewCall(c.metadata, pattern.TX_STORAGE_RENEWALSPACE, types.NewU32(days)) + call, err := types.NewCall(c.metadata, TX_StorageHandler_RenewalSpace, types.NewU32(days)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_RENEWALSPACE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_RenewalSpace, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_RENEWALSPACE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_RenewalSpace, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_RENEWALSPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_RenewalSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -488,9 +602,8 @@ func (c *ChainClient) RenewalSpace(days uint32) (string, error) { // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_RENEWALSPACE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_RenewalSpace, err) + return blockhash, err } <-c.txTicker.C @@ -498,22 +611,23 @@ func (c *ChainClient) RenewalSpace(days uint32) (string, error) { // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_RENEWALSPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_RenewalSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_STORAGE_RENEWALSPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_RenewalSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -525,14 +639,14 @@ func (c *ChainClient) RenewalSpace(days uint32) (string, error) { select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_StorageHandler_RenewalSpace(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } diff --git a/chain/utils.go b/chain/util.go similarity index 100% rename from chain/utils.go rename to chain/util.go diff --git a/example/file/file.go b/example/file/file.go index 310d764..b14235d 100755 --- a/example/file/file.go +++ b/example/file/file.go @@ -17,6 +17,7 @@ import ( cess "github.com/CESSProject/cess-go-sdk" "github.com/CESSProject/cess-go-sdk/chain" + "github.com/CESSProject/cess-go-sdk/utils" ) // Substrate well-known mnemonic: @@ -52,7 +53,11 @@ func main() { } // 3. authorize space to deoss - _, err = sdk.AuthorizeSpace(PublicGatewayAccount) + pub, err := utils.ParsingPublickey(PublicGatewayAccount) + if err != nil { + panic(err) + } + _, err = sdk.Authorize(pub) if err != nil { panic(err) } From a2a28a823a1ec3cc8ac4096e5d297031be06d580 Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Thu, 9 May 2024 17:29:17 +0800 Subject: [PATCH 09/18] update sminer --- chain/pattern.go | 60 +-- chain/sminer.go | 1182 ++++++++++++++++++++++++++-------------------- 2 files changed, 707 insertions(+), 535 deletions(-) diff --git a/chain/pattern.go b/chain/pattern.go index d26a985..159931e 100644 --- a/chain/pattern.go +++ b/chain/pattern.go @@ -72,13 +72,14 @@ const ( AuthorityList = "AuthorityList" // Sminer - AllMiner = "AllMiner" - MinerItems = "MinerItems" - RewardMap = "RewardMap" - Expenders = "Expenders" - RestoralTarget = "RestoralTarget" - StakingStartBlock = "StakingStartBlock" - CompleteSnapShot = "CompleteSnapShot" + AllMiner = "AllMiner" + CounterForMinerItems = "CounterForMinerItems" + MinerItems = "MinerItems" + RewardMap = "RewardMap" + Expenders = "Expenders" + RestoralTarget = "RestoralTarget" + StakingStartBlock = "StakingStartBlock" + CompleteSnapShot = "CompleteSnapShot" // TEEWORKER TEEWorkers = "Workers" @@ -146,17 +147,17 @@ const ( TX_Oss_Authorize = Oss + DOT + "authorize" TX_Oss_CancelAuthorize = Oss + DOT + "cancel_authorize" - // SMINER - TX_SMINER_REGISTER = SMINER + DOT + "regnstk" - TX_SMINER_REGISTERASSIGNSTAKING = SMINER + DOT + "regnstk_assign_staking" - TX_SMINER_INCREASESTAKES = SMINER + DOT + "increase_collateral" - TX_SMINER_UPDATEPEERID = SMINER + DOT + "update_peer_id" - TX_SMINER_UPDATEINCOME = SMINER + DOT + "update_beneficiary" - TX_SMINER_CLAIMREWARD = SMINER + DOT + "receive_reward" - TX_SMINER_MINEREXITPREP = SMINER + DOT + "miner_exit_prep" - TX_SMINER_WITHDRAW = SMINER + DOT + "miner_withdraw" - TX_SMINER_REGISTERPOISKEY = SMINER + DOT + "register_pois_key" - TX_SMINER_INCREASEDECSPACE = SMINER + DOT + "increase_declaration_space" + // Sminer + TX_Sminer_Regnstk = Sminer + DOT + "regnstk" + TX_Sminer_RegnstkAssignStaking = Sminer + DOT + "regnstk_assign_staking" + TX_Sminer_IncreaseCollateral = Sminer + DOT + "increase_collateral" + TX_Sminer_UpdatePeerId = Sminer + DOT + "update_peer_id" + TX_Sminer_UpdateBeneficiary = Sminer + DOT + "update_beneficiary" + TX_Sminer_ReceiveReward = Sminer + DOT + "receive_reward" + TX_Sminer_MinerExitPrep = Sminer + DOT + "miner_exit_prep" + TX_Sminer_MinerWithdraw = Sminer + DOT + "miner_withdraw" + TX_Sminer_RegisterPoisKey = Sminer + DOT + "register_pois_key" + TX_Sminer_IncreaseDeclarationSpace = Sminer + DOT + "increase_declaration_space" // FileBank TX_FileBank_CreateBucket = FileBank + DOT + "create_bucket" @@ -341,17 +342,7 @@ type OssInfo struct { Domain types.Bytes } -// StorageHandler -type UserSpaceInfo struct { - TotalSpace types.U128 - UsedSpace types.U128 - LockedSpace types.U128 - RemainingSpace types.U128 - Start types.U32 - Deadline types.U32 - State types.Bytes -} - +// Sminer type MinerInfo struct { BeneficiaryAccount types.AccountID StakingAccount types.AccountID @@ -383,6 +374,17 @@ type RewardOrder struct { LastReceiveBlock types.U32 } +// StorageHandler +type UserSpaceInfo struct { + TotalSpace types.U128 + UsedSpace types.U128 + LockedSpace types.U128 + RemainingSpace types.U128 + Start types.U32 + Deadline types.U32 + State types.Bytes +} + type SegmentInfo struct { Hash FileHash FragmentList []FragmentInfo diff --git a/chain/sminer.go b/chain/sminer.go index e2dd214..198edb3 100755 --- a/chain/sminer.go +++ b/chain/sminer.go @@ -15,328 +15,453 @@ import ( "strings" "time" - "github.com/CESSProject/cess-go-sdk/core/pattern" "github.com/CESSProject/cess-go-sdk/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) QueryExpenders() (pattern.ExpendersInfo, error) { +// QueryExpenders query expenders (idle data specification) +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - ExpendersInfo: idle data specification +// - error: error message +func (c *ChainClient) QueryExpenders(block int32) (ExpendersInfo, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data pattern.ExpendersInfo + var data ExpendersInfo if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + return data, ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.SMINER, pattern.EXPENDERS) + key, err := types.CreateStorageKey(c.metadata, Sminer, Expenders) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.EXPENDERS, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Sminer, Expenders, err) return data, err } - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + 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(), Sminer, Expenders, err) + c.SetChainState(false) + return data, err + } + if !ok { + return data, ERR_RPC_EMPTY_VALUE + } + return data, nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.EXPENDERS, err) + c.SetChainState(false) + return data, err + } + 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(), Sminer, Expenders, err) c.SetChainState(false) return data, err } if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + return data, ERR_RPC_EMPTY_VALUE } return data, nil } -func (c *ChainClient) QueryStorageMinerByAccount(account string) (pattern.MinerInfo, error) { - publickey, err := utils.ParsingPublickey(account) - if err != nil { - return pattern.MinerInfo{}, err - } - return c.QueryStorageMiner(publickey) -} -func (c *ChainClient) QueryStorageMiner(puk []byte) (pattern.MinerInfo, error) { +// QueryMinerItems query storage miner info +// - accountID: storage miner account +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - MinerInfo: storage miner info +// - error: error message +func (c *ChainClient) QueryMinerItems(accountID []byte, block int32) (MinerInfo, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data pattern.MinerInfo + var data MinerInfo if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + return data, ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.SMINER, pattern.MINERITEMS, puk) + key, err := types.CreateStorageKey(c.metadata, Sminer, MinerItems, accountID) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.MINERITEMS, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Sminer, MinerItems, err) return data, err } - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + 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(), Sminer, MinerItems, err) + c.SetChainState(false) + return data, err + } + if !ok { + return data, ERR_RPC_EMPTY_VALUE + } + return data, nil + } + + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + c.SetChainState(false) + return data, err + } + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.MINERITEMS, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), Sminer, MinerItems, err) c.SetChainState(false) return data, err } if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + return data, ERR_RPC_EMPTY_VALUE } return data, nil } -func (c *ChainClient) QueryStorageMinerByBlock(puk []byte, block int32) (pattern.MinerInfo, error) { +// QueryStakingStartBlock query storage miner's starting staking block +// - accountID: storage miner account +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - uint32: starting staking block +// - error: error message +func (c *ChainClient) QueryStakingStartBlock(accountID []byte, block int32) (uint32, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data pattern.MinerInfo + var data types.U32 if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + return 0, ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.SMINER, pattern.MINERITEMS, puk) + key, err := types.CreateStorageKey(c.metadata, Sminer, StakingStartBlock, accountID) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.MINERITEMS, err) - c.SetChainState(false) - return data, err + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Sminer, StakingStartBlock, err) + return 0, 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(), pattern.SMINER, pattern.MINERITEMS, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Sminer, StakingStartBlock, err) c.SetChainState(false) - return data, err + return 0, err } if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + return 0, ERR_RPC_EMPTY_VALUE } - return data, nil + return uint32(data), nil } - blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.MINERITEMS, err) c.SetChainState(false) - return data, err + return 0, err } - 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(), pattern.SMINER, pattern.MINERITEMS, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Sminer, StakingStartBlock, err) c.SetChainState(false) - return data, err + return 0, err } if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + return 0, ERR_RPC_EMPTY_VALUE } - - return data, nil + return uint32(data), nil } -func (c *ChainClient) QueryStorageMinerStakingStartBlock(puk []byte) (types.U32, error) { +// QueryAllMiner query all storage miner accounts +// - accountID: storage miner account +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - []types.AccountID: all storage miner accounts +// - error: error message +func (c *ChainClient) QueryAllMiner(block int32) ([]types.AccountID, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data types.U32 + var data []types.AccountID if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + return data, ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.SMINER, pattern.STAKINGSTARTBLOCK, puk) + key, err := types.CreateStorageKey(c.metadata, Sminer, AllMiner) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.STAKINGSTARTBLOCK, err) - c.SetChainState(false) - return data, err + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Sminer, AllMiner, err) + return nil, err } - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + 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(), Sminer, AllMiner, err) + c.SetChainState(false) + return nil, err + } + if !ok { + return nil, ERR_RPC_EMPTY_VALUE + } + return data, nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.STAKINGSTARTBLOCK, err) c.SetChainState(false) return data, err } + 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(), Sminer, AllMiner, err) + c.SetChainState(false) + return nil, err + } if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + return nil, ERR_RPC_EMPTY_VALUE } return data, nil } -func (c *ChainClient) QueryAllSminerAccount() ([]types.AccountID, error) { +// QueryCounterForMinerItems query all storage miner count +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - uint32: all storage miner count +// - error: error message +func (c *ChainClient) QueryCounterForMinerItems(block int32) (uint32, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data []types.AccountID + var data types.U32 if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + return 0, ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.SMINER, pattern.ALLMINER) + key, err := types.CreateStorageKey(c.metadata, Sminer, CounterForMinerItems) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.ALLMINER, err) - c.SetChainState(false) - return nil, err + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Sminer, CounterForMinerItems, err) + return 0, err } - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + 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(), Sminer, CounterForMinerItems, err) + c.SetChainState(false) + return 0, err + } + if !ok { + return 0, ERR_RPC_EMPTY_VALUE + } + return uint32(data), nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.ALLMINER, err) c.SetChainState(false) - return nil, err + return 0, err + } + 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(), Sminer, CounterForMinerItems, err) + c.SetChainState(false) + return 0, err } if !ok { - return nil, pattern.ERR_RPC_EMPTY_VALUE + return 0, ERR_RPC_EMPTY_VALUE } - return data, nil + return uint32(data), nil } -func (c *ChainClient) QueryAllSminerAccountByBlock(block int32) ([]types.AccountID, error) { +// QueryRewardMap query all reward information for storage miner +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - MinerReward: all reward information +// - error: error message +func (c *ChainClient) QueryRewardMap(accountID []byte, block int32) (MinerReward, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data []types.AccountID + var data MinerReward if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + return data, ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.SMINER, pattern.ALLMINER) + key, err := types.CreateStorageKey(c.metadata, Sminer, RewardMap, accountID) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.ALLMINER, err) - c.SetChainState(false) - return nil, err + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Sminer, RewardMap, 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(), pattern.SMINER, pattern.ALLMINER, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Sminer, RewardMap, err) c.SetChainState(false) - return nil, err + return data, err } if !ok { - return nil, pattern.ERR_RPC_EMPTY_VALUE + return data, ERR_RPC_EMPTY_VALUE } + return data, nil } - blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.ALLMINER, err) c.SetChainState(false) return data, err } - 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(), pattern.SMINER, pattern.ALLMINER, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), Sminer, RewardMap, err) c.SetChainState(false) return data, err } if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + return data, ERR_RPC_EMPTY_VALUE } - return data, nil } -func (c *ChainClient) QueryRewardsMap(puk []byte) (pattern.MinerReward, error) { +// QueryRestoralTarget query the data recovery information of exited storage miner +// - accountID: storage miner account +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - RestoralTargetInfo: the data recovery information +// - error: error message +func (c *ChainClient) QueryRestoralTarget(accountID []byte, block int32) (RestoralTargetInfo, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data pattern.MinerReward + var data RestoralTargetInfo if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + return data, ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.SMINER, pattern.REWARDMAP, puk) + acc, err := types.NewAccountID(accountID) + if err != nil { + return data, errors.Wrap(err, "[NewAccountID]") + } + + account, err := codec.Encode(*acc) + if err != nil { + return data, errors.Wrap(err, "[EncodeToBytes]") + } + + key, err := types.CreateStorageKey(c.metadata, Sminer, RestoralTarget, account) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Sminer, RestoralTarget, 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(), Sminer, RestoralTarget, err) + c.SetChainState(false) + return data, err + } + if !ok { + return data, ERR_RPC_EMPTY_VALUE + } + return data, nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.REWARDMAP, err) c.SetChainState(false) return data, err } - - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.REWARDMAP, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), Sminer, RestoralTarget, err) c.SetChainState(false) return data, err } if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + return data, ERR_RPC_EMPTY_VALUE } return data, nil } -func (c *ChainClient) QueryRewards(puk []byte) (pattern.RewardsType, error) { - var reward pattern.RewardsType - rewards, err := c.QueryRewardsMap(puk) - if err != nil { - return reward, err - } - - reward.Total = rewards.TotalReward.String() - reward.Claimed = rewards.RewardIssued.String() - - return reward, nil -} - -func (c *ChainClient) QueryRestoralTargetList() ([]pattern.RestoralTargetInfo, error) { +// QueryAllRestoralTarget query the data recovery information of all exited storage miner +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - []RestoralTargetInfo: all the data recovery information +// - error: error message +func (c *ChainClient) QueryAllRestoralTarget(block int32) ([]RestoralTargetInfo, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var result []pattern.RestoralTargetInfo + var result []RestoralTargetInfo if !c.GetChainState() { - return nil, pattern.ERR_RPC_CONNECTION + return nil, ERR_RPC_CONNECTION } - key := createPrefixedKey(pattern.SMINER, pattern.RESTORALTARGETINFO) + key := createPrefixedKey(Sminer, RestoralTarget) keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.RESTORALTARGETINFO, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), Sminer, RestoralTarget, err) c.SetChainState(false) return nil, err } - - set, err := c.api.RPC.State.QueryStorageAtLatest(keys) - if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.RESTORALTARGETINFO, err) - c.SetChainState(false) - return nil, err + var set []types.StorageChangeSet + if block < 0 { + set, err = c.api.RPC.State.QueryStorageAtLatest(keys) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Sminer, RestoralTarget, err) + c.SetChainState(false) + return nil, err + } + } else { + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + c.SetChainState(false) + return nil, err + } + set, err = c.api.RPC.State.QueryStorageAt(keys, blockhash) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAt: %v", c.GetCurrentRpcAddr(), Sminer, RestoralTarget, err) + c.SetChainState(false) + return nil, err + } } - for _, elem := range set { for _, change := range elem.Changes { - var data pattern.RestoralTargetInfo + var data RestoralTargetInfo if err := codec.Decode(change.StorageData, &data); err != nil { continue } @@ -346,90 +471,79 @@ func (c *ChainClient) QueryRestoralTargetList() ([]pattern.RestoralTargetInfo, e return result, nil } -func (c *ChainClient) QueryRestoralTarget(puk []byte) (pattern.RestoralTargetInfo, error) { +// QueryPendingReplacements query the size of the storage miner's replaceable idle data +// - accountID: storage miner account +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - types.U128: the size of replaceable idle data +// - error: error message +func (c *ChainClient) QueryPendingReplacements(accountID []byte, block int32) (types.U128, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data pattern.RestoralTargetInfo - - if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION - } + var data types.U128 - acc, err := types.NewAccountID(puk) + acc, err := types.NewAccountID(accountID) if err != nil { return data, errors.Wrap(err, "[NewAccountID]") } - owner, err := codec.Encode(*acc) + account, err := codec.Encode(*acc) if err != nil { return data, errors.Wrap(err, "[EncodeToBytes]") } - key, err := types.CreateStorageKey(c.metadata, pattern.SMINER, pattern.RESTORALTARGETINFO, owner) - if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.RESTORALTARGETINFO, err) - c.SetChainState(false) - return data, err + if !c.GetChainState() { + return data, ERR_RPC_CONNECTION } - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + key, err := types.CreateStorageKey(c.metadata, Sminer, PendingReplacements, account) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.RESTORALTARGETINFO, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Sminer, PendingReplacements, err) return data, err } - if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE - } - return data, nil -} -func (c *ChainClient) QueryPendingReplacements(puk []byte) (types.U128, error) { - defer func() { - if err := recover(); err != nil { - log.Println(utils.RecoverError(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(), Sminer, PendingReplacements, err) + c.SetChainState(false) + return data, err } - }() - - var data types.U128 - - acc, err := types.NewAccountID(puk) - if err != nil { - return data, errors.Wrap(err, "[NewAccountID]") - } - - owner, err := codec.Encode(*acc) - if err != nil { - return data, errors.Wrap(err, "[EncodeToBytes]") - } - - if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + if !ok { + return data, ERR_RPC_EMPTY_VALUE + } + return data, nil } - - key, err := types.CreateStorageKey(c.metadata, pattern.SMINER, pattern.PENDINGREPLACE, owner) + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.PENDINGREPLACE, err) c.SetChainState(false) return data, err } - - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.PENDINGREPLACE, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), Sminer, PendingReplacements, err) c.SetChainState(false) return data, err } if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + return data, ERR_RPC_EMPTY_VALUE } return data, nil } +// QueryCompleteSnapShot query the number of storage miners and storage miner power in each era +// - era: era id +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - uint32: the number of storage miners in current era +// - uint64: all storage miners power in current era +// - error: error message func (c *ChainClient) QueryCompleteSnapShot(era uint32, block int32) (uint32, uint64, error) { defer func() { if err := recover(); err != nil { @@ -437,10 +551,10 @@ func (c *ChainClient) QueryCompleteSnapShot(era uint32, block int32) (uint32, ui } }() - var data pattern.CompleteSnapShotType + var data CompleteSnapShotType if !c.GetChainState() { - return 0, 0, pattern.ERR_RPC_CONNECTION + return 0, 0, ERR_RPC_CONNECTION } param, err := codec.Encode(era) @@ -448,47 +562,57 @@ func (c *ChainClient) QueryCompleteSnapShot(era uint32, block int32) (uint32, ui return 0, 0, err } - key, err := types.CreateStorageKey(c.metadata, pattern.SMINER, pattern.CompleteSnapShot, param) + key, err := types.CreateStorageKey(c.metadata, Sminer, CompleteSnapShot, param) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.CompleteSnapShot, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Sminer, CompleteSnapShot, err) return 0, 0, 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(), pattern.SMINER, pattern.CompleteSnapShot, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Sminer, CompleteSnapShot, err) c.SetChainState(false) return 0, 0, err } if !ok { - return 0, 0, pattern.ERR_RPC_EMPTY_VALUE + return 0, 0, ERR_RPC_EMPTY_VALUE } return uint32(data.MinerCount), data.TotalPower.Uint64(), nil } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), pattern.SMINER, pattern.CompleteSnapShot, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Sminer, CompleteSnapShot, err) c.SetChainState(false) return 0, 0, err } 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(), pattern.SMINER, pattern.CompleteSnapShot, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), Sminer, CompleteSnapShot, err) c.SetChainState(false) return 0, 0, err } if !ok { - return 0, 0, pattern.ERR_RPC_EMPTY_VALUE + return 0, 0, ERR_RPC_EMPTY_VALUE } return uint32(data.MinerCount), data.TotalPower.Uint64(), nil } -func (c *ChainClient) UpdateSminerPeerId(peerid pattern.PeerId) (string, error) { +// IncreaseCollateral increases the number of staking for storage miner +// - accountID: storage miner account +// - token: number of staking +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - The number of staking to be added is calculated in the smallest unit, +// if you want to add 1CESS staking, you need to fill in "1000000000000000000" +func (c *ChainClient) IncreaseCollateral(accountID []byte, token string) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -498,38 +622,46 @@ func (c *ChainClient) UpdateSminerPeerId(peerid pattern.PeerId) (string, error) }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - call, err := types.NewCall(c.metadata, pattern.TX_SMINER_UPDATEPEERID, peerid) + tokens, ok := new(big.Int).SetString(token, 10) + if !ok { + return "", fmt.Errorf("[IncreaseCollateral] invalid token: %s", token) + } + + acc, err := types.NewAccountID(accountID) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_UPDATEPEERID, err) - c.SetChainState(false) - return txhash, err + return blockhash, errors.Wrap(err, "[NewAccountID]") + } + + call, err := types.NewCall(c.metadata, TX_Sminer_IncreaseCollateral, *acc, types.NewUCompact(tokens)) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseCollateral, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_UPDATEPEERID, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseCollateral, err) + return blockhash, err } - ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) + ok, err = c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_UPDATEPEERID, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseCollateral, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -545,9 +677,8 @@ func (c *ChainClient) UpdateSminerPeerId(peerid pattern.PeerId) (string, error) // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_UPDATEPEERID, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseCollateral, err) + return blockhash, err } <-c.txTicker.C @@ -555,44 +686,57 @@ func (c *ChainClient) UpdateSminerPeerId(peerid pattern.PeerId) (string, error) // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_UPDATEPEERID, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseCollateral, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_UPDATEPEERID, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseCollateral, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() + timeout := time.NewTimer(c.packingTime) defer timeout.Stop() + for { select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() - _, err = c.RetrieveEvent_Sminer_UpdataIp(status.AsInBlock) - return txhash, err + blockhash = status.AsInBlock.Hex() + _, err = c.RetrieveEvent_Sminer_IncreaseCollateral(status.AsInBlock) + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) ExitSminer(miner string) (string, error) { +// IncreaseDeclarationSpace increases the size of space declared on the chain +// - tibCount: the size of the declaration space increased, in TiB +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - the size of the declared space cannot be reduced +// - when the staking does not meet the declared space size, you will be frozen +func (c *ChainClient) IncreaseDeclarationSpace(tibCount uint32) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -602,50 +746,36 @@ func (c *ChainClient) ExitSminer(miner string) (string, error) { }() var ( - err error - txhash string - call types.Call + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION - } - - pubkey, err := utils.ParsingPublickey(miner) - if err != nil { - return txhash, errors.Wrap(err, "[ParsingPublickey]") - } - acc, err := types.NewAccountID(pubkey) - if err != nil { - return txhash, errors.Wrap(err, "[NewAccountID]") + return blockhash, ERR_RPC_CONNECTION } - call, err = types.NewCall(c.metadata, pattern.TX_SMINER_MINEREXITPREP, *acc) + call, err := types.NewCall(c.metadata, TX_Sminer_IncreaseDeclarationSpace, types.NewU32(tibCount)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_MINEREXITPREP, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseDeclarationSpace, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_MINEREXITPREP, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseDeclarationSpace, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_MINEREXITPREP, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseDeclarationSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } - if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -661,9 +791,9 @@ func (c *ChainClient) ExitSminer(miner string) (string, error) { // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_MINEREXITPREP, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseDeclarationSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } <-c.txTicker.C @@ -671,22 +801,22 @@ func (c *ChainClient) ExitSminer(miner string) (string, error) { // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_MINEREXITPREP, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseDeclarationSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_MINEREXITPREP, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseDeclarationSpace, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -698,27 +828,28 @@ func (c *ChainClient) ExitSminer(miner string) (string, error) { select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() - _, err = c.RetrieveEvent_Sminer_MinerExitPrep(status.AsInBlock) - return txhash, err + blockhash = status.AsInBlock.Hex() + _, err = c.RetrieveEvent_Sminer_IncreaseDeclarationSpace(status.AsInBlock) + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) UpdateEarningsAccount(earnings string) (string, error) { - puk, err := utils.ParsingPublickey(earnings) - if err != nil { - return "", err - } - return c.UpdateEarningsAcc(puk) -} - -func (c *ChainClient) UpdateEarningsAcc(puk []byte) (string, error) { +// MinerExitPrep pre-exit storage miner +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - after pre-exit, you need to wait for one day before it will automatically exit +// - cannot register as a storage miner again after pre-exit +func (c *ChainClient) MinerExitPrep() (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -728,43 +859,44 @@ func (c *ChainClient) UpdateEarningsAcc(puk []byte) (string, error) { }() var ( + err error txhash string + call types.Call accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return txhash, ERR_RPC_CONNECTION } - acc, err := types.NewAccountID(puk) + acc, err := types.NewAccountID(c.GetSignatureAccPulickey()) if err != nil { return txhash, errors.Wrap(err, "[NewAccountID]") } - call, err := types.NewCall(c.metadata, pattern.TX_SMINER_UPDATEINCOME, *acc) + call, err = types.NewCall(c.metadata, TX_Sminer_MinerExitPrep, *acc) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_UPDATEINCOME, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerExitPrep, err) return txhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_UPDATEINCOME, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerExitPrep, err) return txhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_UPDATEINCOME, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerExitPrep, err) c.SetChainState(false) return txhash, err } + if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return txhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -780,7 +912,7 @@ func (c *ChainClient) UpdateEarningsAcc(puk []byte) (string, error) { // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_UPDATEINCOME, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerExitPrep, err) c.SetChainState(false) return txhash, err } @@ -790,20 +922,21 @@ func (c *ChainClient) UpdateEarningsAcc(puk []byte) (string, error) { // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { return txhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_UPDATEINCOME, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerExitPrep, err) c.SetChainState(false) return txhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_UPDATEINCOME, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerExitPrep, err) c.SetChainState(false) return txhash, err } @@ -818,26 +951,27 @@ func (c *ChainClient) UpdateEarningsAcc(puk []byte) (string, error) { case status := <-sub.Chan(): if status.IsInBlock { txhash = status.AsInBlock.Hex() - _, err = c.RetrieveEvent_Sminer_UpdataBeneficiary(status.AsInBlock) + _, err = c.RetrieveEvent_Sminer_MinerExitPrep(status.AsInBlock) return txhash, err } case err = <-sub.Err(): return txhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return txhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) IncreaseStorageNodeStakingAmount(miner string, token string) (string, error) { - tokens, ok := new(big.Int).SetString(token+pattern.TokenPrecision_CESS, 10) - if !ok { - return "", fmt.Errorf("invalid tokens: %s", token) - } - return c.IncreaseStakingAmount(miner, tokens) -} - -func (c *ChainClient) IncreaseStakingAmount(miner string, tokens *big.Int) (string, error) { +// MinerWithdraw withdraws all staking +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - must be an exited miner to withdraw +// - wait a day to withdraw after pre-exit +func (c *ChainClient) MinerWithdraw() (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -852,42 +986,31 @@ func (c *ChainClient) IncreaseStakingAmount(miner string, tokens *big.Int) (stri ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return txhash, ERR_RPC_CONNECTION } - pubkey, err := utils.ParsingPublickey(miner) + call, err := types.NewCall(c.metadata, TX_Sminer_MinerWithdraw) if err != nil { - return txhash, errors.Wrap(err, "[ParsingPublickey]") - } - acc, err := types.NewAccountID(pubkey) - if err != nil { - return txhash, errors.Wrap(err, "[NewAccountID]") - } - - call, err := types.NewCall(c.metadata, pattern.TX_SMINER_INCREASESTAKES, *acc, types.NewUCompact(tokens)) - if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_INCREASESTAKES, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerWithdraw, err) return txhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_INCREASESTAKES, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerWithdraw, err) return txhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_INCREASESTAKES, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerWithdraw, err) c.SetChainState(false) return txhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return txhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -903,8 +1026,7 @@ func (c *ChainClient) IncreaseStakingAmount(miner string, tokens *big.Int) (stri // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_INCREASESTAKES, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerWithdraw, err) return txhash, err } @@ -913,22 +1035,23 @@ func (c *ChainClient) IncreaseStakingAmount(miner string, tokens *big.Int) (stri // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { return txhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_INCREASESTAKES, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerWithdraw, err) c.SetChainState(false) return txhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_INCREASESTAKES, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerWithdraw, err) c.SetChainState(false) - return txhash, err + return txhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]") } } defer sub.Unsubscribe() @@ -941,18 +1064,27 @@ func (c *ChainClient) IncreaseStakingAmount(miner string, tokens *big.Int) (stri case status := <-sub.Chan(): if status.IsInBlock { txhash = status.AsInBlock.Hex() - _, err = c.RetrieveEvent_Sminer_IncreaseCollateral(status.AsInBlock) + _, err = c.RetrieveEvent_Sminer_Withdraw(status.AsInBlock) return txhash, err } case err = <-sub.Err(): return txhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return txhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) ClaimRewards() (string, error) { +// ReceiveReward to receive rewards +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - for storage miner only +// - pass at least one idle and service challenge at the same time to get the reward +func (c *ChainClient) ReceiveReward() (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -962,38 +1094,36 @@ func (c *ChainClient) ClaimRewards() (string, error) { }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - call, err := types.NewCall(c.metadata, pattern.TX_SMINER_CLAIMREWARD) + call, err := types.NewCall(c.metadata, TX_Sminer_ReceiveReward) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_CLAIMREWARD, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Sminer_ReceiveReward, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_CLAIMREWARD, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Sminer_ReceiveReward, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_CLAIMREWARD, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_ReceiveReward, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -1009,9 +1139,8 @@ func (c *ChainClient) ClaimRewards() (string, error) { // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_CLAIMREWARD, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Sminer_ReceiveReward, err) + return blockhash, err } <-c.txTicker.C @@ -1019,22 +1148,22 @@ func (c *ChainClient) ClaimRewards() (string, error) { // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_CLAIMREWARD, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_ReceiveReward, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_CLAIMREWARD, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_ReceiveReward, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -1046,19 +1175,33 @@ func (c *ChainClient) ClaimRewards() (string, error) { select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_Sminer_Receive(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) Withdraw() (string, error) { +// RegisterPoisKey register pois key, storage miner registration +// requires two stages, this is the second one. +// +// - poisKey: pois key +// - teeSignWithAcc: tee's sign with account +// - teeSign: tee's sign +// - teePuk: tee's work public key +// +// Return: +// - string: block hash +// - error: error message +// +// Note: +// - storage miners must complete the first stage to register for the second stage +func (c *ChainClient) RegisterPoisKey(poisKey PoISKeyInfo, teeSignWithAcc, teeSign types.Bytes, teePuk WorkerPublicKey) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -1068,38 +1211,42 @@ func (c *ChainClient) Withdraw() (string, error) { }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - call, err := types.NewCall(c.metadata, pattern.TX_SMINER_WITHDRAW) + call, err := types.NewCall(c.metadata, TX_Sminer_RegisterPoisKey, poisKey, teeSignWithAcc, teeSign, teePuk) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_WITHDRAW, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegisterPoisKey, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_WITHDRAW, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegisterPoisKey, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_WITHDRAW, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegisterPoisKey, err) c.SetChainState(false) - return txhash, err + return blockhash, err } + if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + keyStr, _ := utils.NumsToByteStr(key, map[string]bool{}) + return blockhash, fmt.Errorf( + "chain rpc.state.GetStorageLatest[%v]: %v", + keyStr, + ERR_RPC_EMPTY_VALUE, + ) } o := types.SignatureOptions{ @@ -1115,9 +1262,8 @@ func (c *ChainClient) Withdraw() (string, error) { // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_WITHDRAW, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegisterPoisKey, err) + return blockhash, err } <-c.txTicker.C @@ -1125,22 +1271,23 @@ func (c *ChainClient) Withdraw() (string, error) { // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_WITHDRAW, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegisterPoisKey, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_WITHDRAW, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegisterPoisKey, err) c.SetChainState(false) - return txhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]") + return blockhash, err } } defer sub.Unsubscribe() @@ -1152,19 +1299,30 @@ func (c *ChainClient) Withdraw() (string, error) { select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() - _, err = c.RetrieveEvent_Sminer_Withdraw(status.AsInBlock) - return txhash, err + blockhash = status.AsInBlock.Hex() + _, err = c.RetrieveEvent_Sminer_RegisterPoisKey(status.AsInBlock) + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) RegisterSminer(earnings string, peerId []byte, pledge uint64, tib_count uint32) (string, error) { +// RegnstkSminer registers as a storage miner, +// which is the first stage of storage miner registration. +// +// - earnings: earnings account +// - peerId: peer id +// - staking: number of staking, the unit is CESS +// - tibCount: the size of declaration space, in TiB +// +// Return: +// - string: block hash +// - error: error message +func (c *ChainClient) RegnstkSminer(earnings string, peerId []byte, staking uint64, tibCount uint32) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -1174,68 +1332,62 @@ func (c *ChainClient) RegisterSminer(earnings string, peerId []byte, pledge uint }() var ( - err error - txhash string - pubkey []byte - acc *types.AccountID - call types.Call + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - var peerid pattern.PeerId - if len(peerid) != len(peerId) { - return txhash, fmt.Errorf("invalid peerid: %v", peerId) + var peerid PeerId + if len(peerId) != PeerIdPublicKeyLen { + return blockhash, fmt.Errorf("[RegnstkSminer] invalid peerid: %v", peerId) } for i := 0; i < len(peerid); i++ { peerid[i] = types.U8(peerId[i]) } - pubkey, err = utils.ParsingPublickey(earnings) + pubkey, err := utils.ParsingPublickey(earnings) if err != nil { - return txhash, errors.Wrap(err, "[DecodeToPub]") + return blockhash, errors.Wrap(err, "[DecodeToPub]") } - acc, err = types.NewAccountID(pubkey) + acc, err := types.NewAccountID(pubkey) if err != nil { - return txhash, errors.Wrap(err, "[NewAccountID]") + return blockhash, errors.Wrap(err, "[NewAccountID]") } - realTokens, ok := new(big.Int).SetString(strconv.FormatUint(pledge, 10)+pattern.TokenPrecision_CESS, 10) + realTokens, ok := new(big.Int).SetString(strconv.FormatUint(staking, 10)+TokenPrecision_CESS, 10) if !ok { - return txhash, errors.New("[big.Int.SetString]") + return blockhash, errors.New("[big.Int.SetString]") } - call, err = types.NewCall(c.metadata, pattern.TX_SMINER_REGISTER, *acc, peerid, types.NewU128(*realTokens), types.U32(tib_count)) + call, err := types.NewCall(c.metadata, TX_Sminer_Regnstk, *acc, peerid, types.NewU128(*realTokens), types.U32(tibCount)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTER, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Sminer_Regnstk, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTER, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Sminer_Regnstk, err) + return blockhash, err } ok, err = c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_Regnstk, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { keyStr, _ := utils.NumsToByteStr(key, map[string]bool{}) - return txhash, fmt.Errorf( + return blockhash, fmt.Errorf( "chain rpc.state.GetStorageLatest[%v]: %v", keyStr, - pattern.ERR_RPC_EMPTY_VALUE, + ERR_RPC_EMPTY_VALUE, ) } @@ -1252,9 +1404,9 @@ func (c *ChainClient) RegisterSminer(earnings string, peerId []byte, pledge uint // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Sminer_Regnstk, err) c.SetChainState(false) - return txhash, err + return blockhash, err } <-c.txTicker.C @@ -1262,22 +1414,22 @@ func (c *ChainClient) RegisterSminer(earnings string, peerId []byte, pledge uint // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_Regnstk, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTER, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_Regnstk, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -1289,19 +1441,31 @@ func (c *ChainClient) RegisterSminer(earnings string, peerId []byte, pledge uint select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_Sminer_Registered(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) RegisterSminerAssignStaking(beneficiaryAcc string, peerId []byte, stakingAcc string, tib_count uint32) (string, error) { +// RegnstkAssignStaking is registered as a storage miner, unlike RegnstkSminer, +// needs to be actively staking by the staking account, which is the first stage +// of storage miner registration. +// +// - earnings: earnings account +// - peerId: peer id +// - stakingAcc: staking account +// - tibCount: the size of declaration space, in TiB +// +// Return: +// - string: block hash +// - error: error message +func (c *ChainClient) RegnstkAssignStaking(earnings string, peerId []byte, stakingAcc string, tibCount uint32) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -1311,78 +1475,73 @@ func (c *ChainClient) RegisterSminerAssignStaking(beneficiaryAcc string, peerId }() var ( - err error - txhash string - pubkey []byte - call types.Call + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - var peerid pattern.PeerId - if len(peerid) != len(peerId) { - return txhash, fmt.Errorf("invalid peerid: %v", peerId) + var peerid PeerId + if len(peerId) != PeerIdPublicKeyLen { + return blockhash, fmt.Errorf("invalid peerid: %v", peerId) } for i := 0; i < len(peerid); i++ { peerid[i] = types.U8(peerId[i]) } - pubkey, err = utils.ParsingPublickey(beneficiaryAcc) + pubkey, err := utils.ParsingPublickey(earnings) if err != nil { - return txhash, errors.Wrap(err, "[DecodeToPub]") + return blockhash, errors.Wrap(err, "[DecodeToPub]") } beneficiaryacc, err := types.NewAccountID(pubkey) if err != nil { - return txhash, errors.Wrap(err, "[NewAccountID]") + return blockhash, errors.Wrap(err, "[NewAccountID]") } pubkey, err = utils.ParsingPublickey(stakingAcc) if err != nil { - return txhash, errors.Wrap(err, "[DecodeToPub]") + return blockhash, errors.Wrap(err, "[DecodeToPub]") } stakingacc, err := types.NewAccountID(pubkey) if err != nil { - return txhash, errors.Wrap(err, "[NewAccountID]") + return blockhash, errors.Wrap(err, "[NewAccountID]") } - call, err = types.NewCall( + call, err := types.NewCall( c.metadata, - pattern.TX_SMINER_REGISTERASSIGNSTAKING, + TX_Sminer_RegnstkAssignStaking, *beneficiaryacc, peerid, *stakingacc, - types.U32(tib_count), + types.U32(tibCount), ) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTERASSIGNSTAKING, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegnstkAssignStaking, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTERASSIGNSTAKING, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegnstkAssignStaking, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTERASSIGNSTAKING, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegnstkAssignStaking, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { keyStr, _ := utils.NumsToByteStr(key, map[string]bool{}) - return txhash, fmt.Errorf( + return blockhash, fmt.Errorf( "chain rpc.state.GetStorageLatest[%v]: %v", keyStr, - pattern.ERR_RPC_EMPTY_VALUE, + ERR_RPC_EMPTY_VALUE, ) } @@ -1399,9 +1558,9 @@ func (c *ChainClient) RegisterSminerAssignStaking(beneficiaryAcc string, peerId // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTERASSIGNSTAKING, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegnstkAssignStaking, err) c.SetChainState(false) - return txhash, err + return blockhash, err } <-c.txTicker.C @@ -1409,22 +1568,23 @@ func (c *ChainClient) RegisterSminerAssignStaking(beneficiaryAcc string, peerId // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTERASSIGNSTAKING, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegnstkAssignStaking, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTERASSIGNSTAKING, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegnstkAssignStaking, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -1436,19 +1596,26 @@ func (c *ChainClient) RegisterSminerAssignStaking(beneficiaryAcc string, peerId select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() + blockhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_Sminer_Registered(status.AsInBlock) - return txhash, err + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) RegisterSminerPOISKey(poisKey pattern.PoISKeyInfo, teeSignWithAcc, teeSign types.Bytes, teePuk pattern.WorkerPublicKey) (string, error) { +// UpdateBeneficiary updates earnings account for storage miner +// +// - earnings: earnings account +// +// Return: +// - string: block hash +// - error: error message +func (c *ChainClient) UpdateBeneficiary(earnings string) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -1458,46 +1625,46 @@ func (c *ChainClient) RegisterSminerPOISKey(poisKey pattern.PoISKeyInfo, teeSign }() var ( - err error - txhash string - call types.Call + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - call, err = types.NewCall(c.metadata, pattern.TX_SMINER_REGISTERPOISKEY, poisKey, teeSignWithAcc, teeSign, teePuk) + puk, err := utils.ParsingPublickey(earnings) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTERPOISKEY, err) - c.SetChainState(false) - return txhash, err + return "", err + } + + acc, err := types.NewAccountID(puk) + if err != nil { + return blockhash, errors.Wrap(err, "[NewAccountID]") + } + + call, err := types.NewCall(c.metadata, TX_Sminer_UpdateBeneficiary, *acc) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdateBeneficiary, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTERPOISKEY, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdateBeneficiary, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTERPOISKEY, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdateBeneficiary, err) c.SetChainState(false) - return txhash, err + return blockhash, err } - if !ok { - keyStr, _ := utils.NumsToByteStr(key, map[string]bool{}) - return txhash, fmt.Errorf( - "chain rpc.state.GetStorageLatest[%v]: %v", - keyStr, - pattern.ERR_RPC_EMPTY_VALUE, - ) + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -1513,9 +1680,8 @@ func (c *ChainClient) RegisterSminerPOISKey(poisKey pattern.PoISKeyInfo, teeSign // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTERPOISKEY, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdateBeneficiary, err) + return blockhash, err } <-c.txTicker.C @@ -1523,22 +1689,23 @@ func (c *ChainClient) RegisterSminerPOISKey(poisKey pattern.PoISKeyInfo, teeSign // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTERPOISKEY, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdateBeneficiary, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_REGISTERPOISKEY, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdateBeneficiary, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() @@ -1550,19 +1717,26 @@ func (c *ChainClient) RegisterSminerPOISKey(poisKey pattern.PoISKeyInfo, teeSign select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() - _, err = c.RetrieveEvent_Sminer_RegisterPoisKey(status.AsInBlock) - return txhash, err + blockhash = status.AsInBlock.Hex() + _, err = c.RetrieveEvent_Sminer_UpdataBeneficiary(status.AsInBlock) + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } -func (c *ChainClient) IncreaseDeclarationSpace(tibCount uint32) (string, error) { +// UpdateSminerPeerId update peer id for storage miner +// +// - peerid: peer id +// +// Return: +// - string: block hash +// - error: error message +func (c *ChainClient) UpdateSminerPeerId(peerid PeerId) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -1572,38 +1746,36 @@ func (c *ChainClient) IncreaseDeclarationSpace(tibCount uint32) (string, error) }() var ( - txhash string + blockhash string accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, pattern.ERR_RPC_CONNECTION + return blockhash, ERR_RPC_CONNECTION } - call, err := types.NewCall(c.metadata, pattern.TX_SMINER_INCREASEDECSPACE, types.NewU32(tibCount)) + call, err := types.NewCall(c.metadata, TX_Sminer_UpdatePeerId, peerid) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_INCREASEDECSPACE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] NewCall: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdatePeerId, err) + return blockhash, err } ext := types.NewExtrinsic(call) - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + key, err := types.CreateStorageKey(c.metadata, System, Account, c.keyring.PublicKey) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_INCREASEDECSPACE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdatePeerId, err) + return blockhash, err } ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_INCREASEDECSPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdatePeerId, err) c.SetChainState(false) - return txhash, err + return blockhash, err } if !ok { - return txhash, pattern.ERR_RPC_EMPTY_VALUE + return blockhash, ERR_RPC_EMPTY_VALUE } o := types.SignatureOptions{ @@ -1619,9 +1791,8 @@ func (c *ChainClient) IncreaseDeclarationSpace(tibCount uint32) (string, error) // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_INCREASEDECSPACE, err) - c.SetChainState(false) - return txhash, err + err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdatePeerId, err) + return blockhash, err } <-c.txTicker.C @@ -1629,41 +1800,40 @@ func (c *ChainClient) IncreaseDeclarationSpace(tibCount uint32) (string, error) // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - if strings.Contains(err.Error(), pattern.ERR_RPC_PRIORITYTOOLOW) { + if strings.Contains(err.Error(), ERR_RPC_PRIORITYTOOLOW) { o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, errors.Wrap(err, "[Sign]") + return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_INCREASEDECSPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdatePeerId, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } else { - err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), pattern.TX_SMINER_INCREASEDECSPACE, err) + err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdatePeerId, err) c.SetChainState(false) - return txhash, err + return blockhash, err } } defer sub.Unsubscribe() - timeout := time.NewTimer(c.packingTime) defer timeout.Stop() - for { select { case status := <-sub.Chan(): if status.IsInBlock { - txhash = status.AsInBlock.Hex() - _, err = c.RetrieveEvent_Sminer_IncreaseDeclarationSpace(status.AsInBlock) - return txhash, err + blockhash = status.AsInBlock.Hex() + _, err = c.RetrieveEvent_Sminer_UpdataIp(status.AsInBlock) + return blockhash, err } case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") + return blockhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT + return blockhash, ERR_RPC_TIMEOUT } } } From 08d1294e93629bc249e3f50969f6f6bc6d00c867 Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Fri, 10 May 2024 10:52:07 +0800 Subject: [PATCH 10/18] update staking --- chain/pattern.go | 23 +- chain/staking.go | 479 ++++++++++++++--------- chain/{storage.go => storage_handler.go} | 0 3 files changed, 297 insertions(+), 205 deletions(-) rename chain/{storage.go => storage_handler.go} (100%) diff --git a/chain/pattern.go b/chain/pattern.go index 159931e..826f69b 100644 --- a/chain/pattern.go +++ b/chain/pattern.go @@ -98,7 +98,7 @@ const ( UserHoldFileList = "UserHoldFileList" // Staking - COUNTERFORVALIDATORS = "CounterForValidators" + CounterForValidators = "CounterForValidators" CounterForNominators = "CounterForNominators" ErasTotalStake = "ErasTotalStake" CurrentEra = "CurrentEra" @@ -385,6 +385,17 @@ type UserSpaceInfo struct { State types.Bytes } +// Staking +type StakingEraRewardPoints struct { + Total types.U32 + Individual []Individual +} + +type Individual struct { + Acc types.AccountID + Reward types.U32 +} + type SegmentInfo struct { Hash FileHash FragmentList []FragmentInfo @@ -506,16 +517,6 @@ type OtherStakingExposure struct { Value types.U128 } -type StakingEraRewardPoints struct { - Total types.U32 - Individual []Individual -} - -type Individual struct { - Acc types.AccountID - Reward types.U32 -} - type StakingNominations struct { Targets []types.AccountID SubmittedIn types.U32 diff --git a/chain/staking.go b/chain/staking.go index 03906c1..24cb9ee 100755 --- a/chain/staking.go +++ b/chain/staking.go @@ -11,14 +11,19 @@ import ( "fmt" "log" - "github.com/CESSProject/cess-go-sdk/core/pattern" "github.com/CESSProject/cess-go-sdk/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) QueryAllValidatorCount(block int) (uint32, error) { +// QueryCounterForValidators query validator number (waiting nodes included) +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - uint32: validator number +// - error: error message +func (c *ChainClient) QueryCounterForValidators(block int) (uint32, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -28,10 +33,10 @@ func (c *ChainClient) QueryAllValidatorCount(block int) (uint32, error) { var data types.U32 if !c.GetChainState() { - return uint32(data), pattern.ERR_RPC_CONNECTION + return uint32(data), ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.STAKING, pattern.COUNTERFORVALIDATORS) + key, err := types.CreateStorageKey(c.metadata, Staking, CounterForValidators) if err != nil { return uint32(data), errors.Wrap(err, "[CreateStorageKey]") } @@ -39,31 +44,39 @@ func (c *ChainClient) QueryAllValidatorCount(block int) (uint32, error) { if block < 0 { ok, err := c.api.RPC.State.GetStorageLatest(key, &data) if err != nil { + c.SetChainState(false) return uint32(data), errors.Wrap(err, "[GetStorageLatest]") } if !ok { - return uint32(data), pattern.ERR_RPC_EMPTY_VALUE + return uint32(data), ERR_RPC_EMPTY_VALUE } return uint32(data), nil } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), pattern.STAKING, pattern.COUNTERFORVALIDATORS, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Staking, CounterForValidators, err) c.SetChainState(false) return uint32(data), err } ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { + c.SetChainState(false) return uint32(data), errors.Wrap(err, "[GetStorage]") } if !ok { - return uint32(data), pattern.ERR_RPC_EMPTY_VALUE + return uint32(data), ERR_RPC_EMPTY_VALUE } return uint32(data), nil } +// QueryValidatorsCount query validator number (waiting nodes not included) +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - uint32: validator number +// - error: error message func (c *ChainClient) QueryValidatorsCount(block int) (uint32, error) { defer func() { if err := recover(); err != nil { @@ -74,10 +87,10 @@ func (c *ChainClient) QueryValidatorsCount(block int) (uint32, error) { var data types.U32 if !c.GetChainState() { - return uint32(data), pattern.ERR_RPC_CONNECTION + return uint32(data), ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.STAKING, pattern.ValidatorCount) + key, err := types.CreateStorageKey(c.metadata, Staking, ValidatorCount) if err != nil { return uint32(data), errors.Wrap(err, "[CreateStorageKey]") } @@ -85,31 +98,39 @@ func (c *ChainClient) QueryValidatorsCount(block int) (uint32, error) { if block < 0 { ok, err := c.api.RPC.State.GetStorageLatest(key, &data) if err != nil { + c.SetChainState(false) return uint32(data), errors.Wrap(err, "[GetStorageLatest]") } if !ok { - return uint32(data), pattern.ERR_RPC_EMPTY_VALUE + return uint32(data), ERR_RPC_EMPTY_VALUE } return uint32(data), nil } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), pattern.STAKING, pattern.ValidatorCount, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Staking, ValidatorCount, err) c.SetChainState(false) return uint32(data), err } ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { + c.SetChainState(false) return uint32(data), errors.Wrap(err, "[GetStorage]") } if !ok { - return uint32(data), pattern.ERR_RPC_EMPTY_VALUE + return uint32(data), ERR_RPC_EMPTY_VALUE } return uint32(data), nil } +// QueryNominatorCount query nominator number +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - uint32: nominator number +// - error: error message func (c *ChainClient) QueryNominatorCount(block int) (uint32, error) { defer func() { if err := recover(); err != nil { @@ -120,10 +141,10 @@ func (c *ChainClient) QueryNominatorCount(block int) (uint32, error) { var data types.U32 if !c.GetChainState() { - return uint32(data), pattern.ERR_RPC_CONNECTION + return uint32(data), ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.STAKING, pattern.CounterForNominators) + key, err := types.CreateStorageKey(c.metadata, Staking, CounterForNominators) if err != nil { return uint32(data), errors.Wrap(err, "[CreateStorageKey]") } @@ -131,31 +152,40 @@ func (c *ChainClient) QueryNominatorCount(block int) (uint32, error) { if block < 0 { ok, err := c.api.RPC.State.GetStorageLatest(key, &data) if err != nil { + c.SetChainState(false) return uint32(data), errors.Wrap(err, "[GetStorageLatest]") } if !ok { - return uint32(data), pattern.ERR_RPC_EMPTY_VALUE + return uint32(data), ERR_RPC_EMPTY_VALUE } return uint32(data), nil } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), pattern.STAKING, pattern.CounterForNominators, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Staking, CounterForNominators, err) c.SetChainState(false) return uint32(data), err } ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { + c.SetChainState(false) return uint32(data), errors.Wrap(err, "[GetStorage]") } if !ok { - return uint32(data), pattern.ERR_RPC_EMPTY_VALUE + return uint32(data), ERR_RPC_EMPTY_VALUE } return uint32(data), nil } +// QueryErasTotalStake query the total number of staking for each era +// - era: era id +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - string: the total number of staking +// - error: error message func (c *ChainClient) QueryErasTotalStake(era uint32, block int) (string, error) { defer func() { if err := recover(); err != nil { @@ -166,7 +196,7 @@ func (c *ChainClient) QueryErasTotalStake(era uint32, block int) (string, error) var data types.U128 if !c.GetChainState() { - return "", pattern.ERR_RPC_CONNECTION + return "", ERR_RPC_CONNECTION } param, err := codec.Encode(era) @@ -174,43 +204,48 @@ func (c *ChainClient) QueryErasTotalStake(era uint32, block int) (string, error) return "", err } - key, err := types.CreateStorageKey(c.metadata, pattern.STAKING, pattern.ErasTotalStake, param) + key, err := types.CreateStorageKey(c.metadata, Staking, ErasTotalStake, param) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.STAKING, pattern.ErasTotalStake, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Staking, ErasTotalStake, err) return "", 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(), pattern.STAKING, pattern.ErasTotalStake, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Staking, ErasTotalStake, err) c.SetChainState(false) return "", err } if !ok { - return "", pattern.ERR_RPC_EMPTY_VALUE + return "", ERR_RPC_EMPTY_VALUE } return data.String(), nil } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), pattern.STAKING, pattern.ErasTotalStake, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Staking, ErasTotalStake, err) c.SetChainState(false) return data.String(), err } 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(), pattern.STAKING, pattern.ErasTotalStake, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), Staking, ErasTotalStake, err) c.SetChainState(false) return "", err } if !ok { - return "", pattern.ERR_RPC_EMPTY_VALUE + return "", ERR_RPC_EMPTY_VALUE } return data.String(), nil } +// QueryCurrentEra query the current era id +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - uint32: era id +// - error: error message func (c *ChainClient) QueryCurrentEra(block int) (uint32, error) { defer func() { if err := recover(); err != nil { @@ -221,139 +256,62 @@ func (c *ChainClient) QueryCurrentEra(block int) (uint32, error) { var data types.U32 if !c.GetChainState() { - return 0, pattern.ERR_RPC_CONNECTION + return 0, ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.STAKING, pattern.CurrentEra) + key, err := types.CreateStorageKey(c.metadata, Staking, CurrentEra) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.STAKING, pattern.CurrentEra, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Staking, CurrentEra, err) return 0, 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(), pattern.STAKING, pattern.CurrentEra, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Staking, CurrentEra, err) c.SetChainState(false) return 0, err } if !ok { - return 0, pattern.ERR_RPC_EMPTY_VALUE + return 0, ERR_RPC_EMPTY_VALUE } return uint32(data), nil } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), pattern.STAKING, pattern.CurrentEra, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Staking, CurrentEra, err) c.SetChainState(false) return uint32(data), err } ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.STAKING, pattern.CurrentEra, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Staking, CurrentEra, err) c.SetChainState(false) return 0, err } if !ok { - return 0, pattern.ERR_RPC_EMPTY_VALUE + return 0, ERR_RPC_EMPTY_VALUE } return uint32(data), nil } -// func (c *ChainClient) QueryeErasStakers(era uint32) ([]pattern.StakingExposure, error) { -// defer func() { -// if err := recover(); err != nil { -// log.Println(utils.RecoverError(err)) -// } -// }() -// var result []pattern.StakingExposure - -// if !c.GetChainState() { -// return result, pattern.ERR_RPC_CONNECTION -// } - -// // key := createPrefixedKey(pattern.STAKING, pattern.ErasStakers) -// param1, err := codec.Encode(types.NewU32(era)) -// if err != nil { -// return result, err -// } - -// var p2 types.OptionAccountID - -// pk, err := utils.ParsingPublickey("cXjGzUnWJcNXBzKBEJKvs3ZBJ5f1aEEca38abpuNxvwGNZ5Gy") -// if err != nil { -// return result, err -// } - -// accid, err := types.NewAccountID(pk) -// if err != nil { -// return result, err -// } -// p2.SetSome(*accid) -// b := bytes.NewBuffer(make([]byte, 0)) - -// err = p2.Encode(*scale.NewEncoder(b)) -// if err != nil { -// return result, err -// } -// param2, err := codec.Encode(p2) -// if err != nil { -// return result, err -// } -// _ = param2 -// //fmt.Println(p2.HasValue()) -// key, err := types.CreateStorageKey(c.metadata, pattern.STAKING, "ErasStakersClipped", param1, param2) -// if err != nil { -// return result, err -// } -// _ = key -// kkey := createPrefixedKey(pattern.STAKING, pattern.ErasStakers) -// //kkey = append(kkey, []byte(" ")...) -// //kkey = append(kkey, param1...) //xxhash.New128(param1).Sum(nil)...) - -// entryMeta, err := c.GetMetadata().FindStorageEntryMetadata(pattern.STAKING, pattern.ErasStakers) -// if err != nil { -// return nil, err -// } -// hashers, err := entryMeta.Hashers() -// if err != nil { -// return nil, err -// } -// _, err = hashers[0].Write(param1) -// if err != nil { -// return nil, err -// } -// kkey = append(kkey, hashers[0].Sum(nil)...) -// _, err = hashers[1].Write(param2) -// if err != nil { -// return nil, err -// } -// kkey = append(kkey, hashers[1].Sum(nil)...) -// var result1 pattern.StakingExposure -// ok, err := c.api.RPC.State.GetStorageLatest(kkey, &result1) -// if err != nil { -// err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.STAKING, pattern.ErasStakers, err) -// c.SetChainState(false) -// return result, err -// } -// fmt.Println(result1) -// if !ok { -// return result, pattern.ERR_RPC_EMPTY_VALUE -// } -// return result, nil -// } - -func (c *ChainClient) QueryStakingEraRewardPoints(era uint32) (pattern.StakingEraRewardPoints, error) { +// QueryErasRewardPoints query the rewards of consensus nodes in each era +// - era: era id +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - StakingEraRewardPoints: the rewards of consensus nodes +// - error: error message +func (c *ChainClient) QueryErasRewardPoints(era uint32, block int32) (StakingEraRewardPoints, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var result pattern.StakingEraRewardPoints + var result StakingEraRewardPoints if !c.GetChainState() { - return result, pattern.ERR_RPC_CONNECTION + return result, ERR_RPC_CONNECTION } param1, err := codec.Encode(types.NewU32(era)) @@ -361,55 +319,91 @@ func (c *ChainClient) QueryStakingEraRewardPoints(era uint32) (pattern.StakingEr return result, err } - key, err := types.CreateStorageKey(c.metadata, pattern.STAKING, pattern.ErasRewardPoints, param1) + key, err := types.CreateStorageKey(c.metadata, Staking, ErasRewardPoints, param1) if err != nil { return result, err } - - ok, err := c.api.RPC.State.GetStorageLatest(key, &result) + if block < 0 { + ok, err := c.api.RPC.State.GetStorageLatest(key, &result) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Staking, ErasRewardPoints, err) + c.SetChainState(false) + return result, err + } + if !ok { + return result, ERR_RPC_EMPTY_VALUE + } + return result, nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + c.SetChainState(false) + return result, err + } + ok, err := c.api.RPC.State.GetStorage(key, &result, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.STAKING, pattern.ErasRewardPoints, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), Staking, ErasRewardPoints, err) c.SetChainState(false) return result, err } if !ok { - return result, pattern.ERR_RPC_EMPTY_VALUE + return result, ERR_RPC_EMPTY_VALUE } return result, nil } -func (c *ChainClient) QueryNominatorsLatest() ([]pattern.StakingNominations, error) { +// QueryAllNominators query all nominators info +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - []StakingNominations: all nominators info +// - error: error message +func (c *ChainClient) QueryAllNominators(block int32) ([]StakingNominations, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - //var keyList []string - var result []pattern.StakingNominations + + var result []StakingNominations if !c.GetChainState() { - return nil, pattern.ERR_RPC_CONNECTION + return nil, ERR_RPC_CONNECTION } - key := createPrefixedKey(pattern.STAKING, pattern.Nominators) + key := createPrefixedKey(Staking, Nominators) keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), pattern.STAKING, pattern.Nominators, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), Staking, Nominators, err) c.SetChainState(false) return nil, err } - - set, err := c.api.RPC.State.QueryStorageAtLatest(keys) - if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), pattern.STAKING, pattern.Nominators, err) - c.SetChainState(false) - return nil, err + var set []types.StorageChangeSet + if block < 0 { + set, err = c.api.RPC.State.QueryStorageAtLatest(keys) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Staking, Nominators, err) + c.SetChainState(false) + return nil, err + } + } else { + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + c.SetChainState(false) + return nil, err + } + set, err = c.api.RPC.State.QueryStorageAt(keys, blockhash) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Staking, Nominators, err) + c.SetChainState(false) + return nil, err + } } for _, elem := range set { for _, change := range elem.Changes { if change.HasStorageData { - var data pattern.StakingNominations + var data StakingNominations if err := codec.Decode(change.StorageData, &data); err != nil { continue } @@ -421,62 +415,55 @@ func (c *ChainClient) QueryNominatorsLatest() ([]pattern.StakingNominations, err return result, nil } -func (c *ChainClient) QueryBondedList(block int32) ([]types.AccountID, error) { +// QueryAllBonded query all consensus and nominators accounts +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - []types.AccountID: all consensus and nominators accounts +// - error: error message +func (c *ChainClient) QueryAllBonded(block int32) ([]types.AccountID, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - //var keyList []string + var result []types.AccountID if !c.GetChainState() { - return nil, pattern.ERR_RPC_CONNECTION + return nil, ERR_RPC_CONNECTION } - key := createPrefixedKey(pattern.STAKING, pattern.Bonded) + key := createPrefixedKey(Staking, Bonded) keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), pattern.STAKING, pattern.Bonded, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), Staking, Bonded, err) c.SetChainState(false) return nil, err } + var set []types.StorageChangeSet if block < 0 { - set, err := c.api.RPC.State.QueryStorageAtLatest(keys) + set, err = c.api.RPC.State.QueryStorageAtLatest(keys) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), pattern.STAKING, pattern.Bonded, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Staking, Bonded, err) c.SetChainState(false) return nil, err } - - for _, elem := range set { - for _, change := range elem.Changes { - if change.HasStorageData { - var data types.AccountID - if err := codec.Decode(change.StorageData, &data); err != nil { - continue - } - result = append(result, data) - } - - } + } else { + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Staking, Bonded, err) + c.SetChainState(false) + return result, err } - return result, nil - } - - blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) - if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), pattern.STAKING, pattern.Bonded, err) - c.SetChainState(false) - return result, err - } - set, err := c.api.RPC.State.QueryStorageAt(keys, blockhash) - if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), pattern.STAKING, pattern.Bonded, err) - c.SetChainState(false) - return nil, err + set, err = c.api.RPC.State.QueryStorageAt(keys, blockhash) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Staking, Bonded, err) + c.SetChainState(false) + return nil, err + } } for _, elem := range set { @@ -494,28 +481,27 @@ func (c *ChainClient) QueryBondedList(block int32) ([]types.AccountID, error) { return result, nil } -func (c *ChainClient) QueryValidatorCommission(account string, block int) (uint8, error) { +// QueryValidatorCommission query validator commission +// - accountID: validator account +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - uint8: validator commission +// - error: error message +func (c *ChainClient) QueryValidatorCommission(accountID []byte, block int) (uint8, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - //var keyList []string - var result pattern.StakingValidatorPrefs - if !c.GetChainState() { - return 0, pattern.ERR_RPC_CONNECTION - } + var result StakingValidatorPrefs - pk, _ := utils.ParsingPublickey(account) - acc, _ := types.NewAccountID(pk) - // key := createPrefixedKey(pattern.STAKING, pattern.ErasStakers) - param1, err := codec.Encode(*acc) - if err != nil { - return 0, err + if !c.GetChainState() { + return 0, ERR_RPC_CONNECTION } - key, err := types.CreateStorageKey(c.metadata, pattern.STAKING, pattern.Validators, param1) + key, err := types.CreateStorageKey(c.metadata, Staking, Validators, accountID) if err != nil { return 0, err } @@ -526,13 +512,13 @@ func (c *ChainClient) QueryValidatorCommission(account string, block int) (uint8 return 0, nil } if !ok { - return 0, pattern.ERR_RPC_EMPTY_VALUE + return 0, ERR_RPC_EMPTY_VALUE } return uint8(uint32(result.Commission-2) / uint32(40000000)), nil } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), pattern.STAKING, pattern.Validators, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Staking, Validators, err) c.SetChainState(false) return 0, err } @@ -541,22 +527,29 @@ func (c *ChainClient) QueryValidatorCommission(account string, block int) (uint8 return 0, nil } if !ok { - return 0, pattern.ERR_RPC_EMPTY_VALUE + return 0, ERR_RPC_EMPTY_VALUE } return uint8(uint32(result.Commission-2) / uint32(40000000)), nil } -func (c *ChainClient) QueryEraValidatorReward(era uint32) (string, error) { +// QueryEraValidatorReward query the total rewards for each era +// - era: era id +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - string: total rewards +// - error: error message +func (c *ChainClient) QueryEraValidatorReward(era uint32, block int) (string, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - //var keyList []string + var result types.U128 if !c.GetChainState() { - return "", pattern.ERR_RPC_CONNECTION + return "", ERR_RPC_CONNECTION } param, err := codec.Encode(types.NewU32(era)) @@ -564,17 +557,115 @@ func (c *ChainClient) QueryEraValidatorReward(era uint32) (string, error) { return "", err } - key, err := types.CreateStorageKey(c.metadata, pattern.STAKING, pattern.ErasValidatorReward, param) + key, err := types.CreateStorageKey(c.metadata, Staking, ErasValidatorReward, param) if err != nil { return "", err } - ok, err := c.api.RPC.State.GetStorageLatest(key, &result) + if block < 0 { + ok, err := c.api.RPC.State.GetStorageLatest(key, &result) + if err != nil { + return "", nil + } + if !ok { + return "", ERR_RPC_EMPTY_VALUE + } + return result.String(), nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + c.SetChainState(false) + return "", err + } + ok, err := c.api.RPC.State.GetStorage(key, &result, blockhash) if err != nil { return "", nil } if !ok { - return "", pattern.ERR_RPC_EMPTY_VALUE + return "", ERR_RPC_EMPTY_VALUE } return result.String(), nil } + +// func (c *ChainClient) QueryeErasStakers(era uint32) ([]StakingExposure, error) { +// defer func() { +// if err := recover(); err != nil { +// log.Println(utils.RecoverError(err)) +// } +// }() +// var result []StakingExposure + +// if !c.GetChainState() { +// return result, ERR_RPC_CONNECTION +// } + +// // key := createPrefixedKey(Staking, ErasStakers) +// param1, err := codec.Encode(types.NewU32(era)) +// if err != nil { +// return result, err +// } + +// var p2 types.OptionAccountID + +// pk, err := utils.ParsingPublickey("cXjGzUnWJcNXBzKBEJKvs3ZBJ5f1aEEca38abpuNxvwGNZ5Gy") +// if err != nil { +// return result, err +// } + +// accid, err := types.NewAccountID(pk) +// if err != nil { +// return result, err +// } +// p2.SetSome(*accid) +// b := bytes.NewBuffer(make([]byte, 0)) + +// err = p2.Encode(*scale.NewEncoder(b)) +// if err != nil { +// return result, err +// } +// param2, err := codec.Encode(p2) +// if err != nil { +// return result, err +// } +// _ = param2 +// //fmt.Println(p2.HasValue()) +// key, err := types.CreateStorageKey(c.metadata, Staking, "ErasStakersClipped", param1, param2) +// if err != nil { +// return result, err +// } +// _ = key +// kkey := createPrefixedKey(Staking, ErasStakers) +// //kkey = append(kkey, []byte(" ")...) +// //kkey = append(kkey, param1...) //xxhash.New128(param1).Sum(nil)...) + +// entryMeta, err := c.GetMetadata().FindStorageEntryMetadata(Staking, ErasStakers) +// if err != nil { +// return nil, err +// } +// hashers, err := entryMeta.Hashers() +// if err != nil { +// return nil, err +// } +// _, err = hashers[0].Write(param1) +// if err != nil { +// return nil, err +// } +// kkey = append(kkey, hashers[0].Sum(nil)...) +// _, err = hashers[1].Write(param2) +// if err != nil { +// return nil, err +// } +// kkey = append(kkey, hashers[1].Sum(nil)...) +// var result1 StakingExposure +// ok, err := c.api.RPC.State.GetStorageLatest(kkey, &result1) +// if err != nil { +// err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Staking, ErasStakers, err) +// c.SetChainState(false) +// return result, err +// } +// fmt.Println(result1) +// if !ok { +// return result, ERR_RPC_EMPTY_VALUE +// } +// return result, nil +// } diff --git a/chain/storage.go b/chain/storage_handler.go similarity index 100% rename from chain/storage.go rename to chain/storage_handler.go From 01b23e9a32da64dab6a439b563baae172eeece93 Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Fri, 10 May 2024 11:15:11 +0800 Subject: [PATCH 11/18] update TeeWorker --- chain/pattern.go | 35 +++--- chain/tee.go | 308 +++++++++++++++++++++++++++-------------------- 2 files changed, 194 insertions(+), 149 deletions(-) diff --git a/chain/pattern.go b/chain/pattern.go index 826f69b..7e85e12 100644 --- a/chain/pattern.go +++ b/chain/pattern.go @@ -81,11 +81,11 @@ const ( StakingStartBlock = "StakingStartBlock" CompleteSnapShot = "CompleteSnapShot" - // TEEWORKER - TEEWorkers = "Workers" - TEEMasterPubkey = "MasterPubkey" - TEEEndpoints = "Endpoints" - TEEWorkerAddedAt = "WorkerAddedAt" + // TeeWorker + Workers = "Workers" + MasterPubkey = "MasterPubkey" + Endpoints = "Endpoints" + WorkerAddedAt = "WorkerAddedAt" // FileBank File = "File" @@ -396,6 +396,19 @@ type Individual struct { Reward types.U32 } +// TeeWorker +type WorkerInfo struct { + Pubkey WorkerPublicKey + EcdhPubkey EcdhPublicKey + Version types.U32 + LastUpdated types.U64 + StashAccount types.Option[types.AccountID] + AttestationProvider types.Option[types.U8] + ConfidenceLevel types.U8 + Features []types.U32 + Role types.U8 // 0:Full 1:Verifier 2:Marker +} + type SegmentInfo struct { Hash FileHash FragmentList []FragmentInfo @@ -464,18 +477,6 @@ type ServiceProveInfo struct { VerifyResult types.Option[bool] } -type TeeWorkerInfo struct { - Pubkey WorkerPublicKey - EcdhPubkey EcdhPublicKey - Version types.U32 - LastUpdated types.U64 - StashAccount types.Option[types.AccountID] - AttestationProvider types.Option[types.U8] - ConfidenceLevel types.U8 - Features []types.U32 - Role types.U8 // 0:Full 1:Verifier 2:Marker -} - type ExpendersInfo struct { K types.U64 N types.U64 diff --git a/chain/tee.go b/chain/tee.go index 831cc28..9a1fcec 100755 --- a/chain/tee.go +++ b/chain/tee.go @@ -11,147 +11,176 @@ import ( "fmt" "log" - "github.com/CESSProject/cess-go-sdk/core/pattern" "github.com/CESSProject/cess-go-sdk/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) QueryTeeWorker(puk pattern.WorkerPublicKey) (pattern.TeeWorkerInfo, error) { +// QueryMasterPubKey query master public key +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - []byte: master public key +// - error: error message +func (c *ChainClient) QueryMasterPubKey(block int32) ([]byte, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data pattern.TeeWorkerInfo + var data MasterPublicKey if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + return nil, ERR_RPC_CONNECTION } - publickey, err := codec.Encode(puk) + key, err := types.CreateStorageKey(c.metadata, TeeWorker, MasterPubkey) if err != nil { - return data, errors.Wrap(err, "[EncodeToBytes]") + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TeeWorker, MasterPubkey, err) + return nil, err } - key, err := types.CreateStorageKey(c.metadata, pattern.TEEWORKER, pattern.TEEWorkers, publickey) + if block < 0 { + ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + if err != nil { + c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TeeWorker, MasterPubkey, err) + return nil, err + } + if !ok { + return nil, ERR_RPC_EMPTY_VALUE + } + return []byte(string(data[:])), nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEWorkers, err) - return data, errors.Wrap(err, "[CreateStorageKey]") + c.SetChainState(false) + return nil, err } - - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEWorkers, err) - return data, errors.Wrap(err, "[GetStorageLatest]") + c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), TeeWorker, MasterPubkey, err) + return nil, err } if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + return nil, ERR_RPC_EMPTY_VALUE } - - return data, nil + return []byte(string(data[:])), nil } -func (c *ChainClient) QueryTeeInfo(puk pattern.WorkerPublicKey) (pattern.TeeInfo, error) { +// QueryWorkers query tee work info +// - puk: tee's work public key +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - WorkerInfo: tee worker info +// - error: error message +func (c *ChainClient) QueryWorkers(puk WorkerPublicKey, block int32) (WorkerInfo, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var data pattern.TeeInfo + var data WorkerInfo - teeWorkerInfo, err := c.QueryTeeWorker(puk) - if err != nil { - return data, err - } - data.Pubkey = string(teeWorkerInfo.Pubkey[:]) - data.EcdhPubkey = string(teeWorkerInfo.EcdhPubkey[:]) - data.Version = uint32(teeWorkerInfo.Version) - data.LastUpdated = uint64(teeWorkerInfo.LastUpdated) - if teeWorkerInfo.StashAccount.HasValue() { - if ok, puk := teeWorkerInfo.StashAccount.Unwrap(); ok { - data.StashAccount, _ = utils.EncodePublicKeyAsCessAccount(puk[:]) - } + if !c.GetChainState() { + return data, ERR_RPC_CONNECTION } - if teeWorkerInfo.AttestationProvider.HasValue() { - if ok, val := teeWorkerInfo.AttestationProvider.Unwrap(); ok { - data.AttestationProvider = uint8(val) - } + + publickey, err := codec.Encode(puk) + if err != nil { + return data, errors.Wrap(err, "[EncodeToBytes]") } - data.ConfidenceLevel = uint8(teeWorkerInfo.ConfidenceLevel) - data.Features = make([]uint32, len(teeWorkerInfo.Features)) - for i := 0; i < len(teeWorkerInfo.Features); i++ { - data.Features[i] = uint32(teeWorkerInfo.Features[i]) + + key, err := types.CreateStorageKey(c.metadata, TeeWorker, Workers, publickey) + if err != nil { + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TeeWorker, Workers, err) + return data, errors.Wrap(err, "[CreateStorageKey]") } - data.WorkerRole = uint8(teeWorkerInfo.Role) - return data, nil -} -func (c *ChainClient) QueryMasterPublicKey() ([]byte, error) { - defer func() { - if err := recover(); err != nil { - log.Println(utils.RecoverError(err)) + if block < 0 { + ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + if err != nil { + c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TeeWorker, Workers, err) + return data, errors.Wrap(err, "[GetStorageLatest]") } - }() - - var data pattern.MasterPublicKey - - if !c.GetChainState() { - return nil, pattern.ERR_RPC_CONNECTION + if !ok { + return data, ERR_RPC_EMPTY_VALUE + } + return data, nil } - - key, err := types.CreateStorageKey(c.metadata, pattern.TEEWORKER, pattern.TEEMasterPubkey) + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEMasterPubkey, err) - return nil, errors.Wrap(err, "[CreateStorageKey]") + c.SetChainState(false) + return data, err } - - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEMasterPubkey, err) - return nil, errors.Wrap(err, "[GetStorageLatest]") + c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TeeWorker, Workers, err) + return data, errors.Wrap(err, "[GetStorageLatest]") } if !ok { - return nil, pattern.ERR_RPC_EMPTY_VALUE + return data, ERR_RPC_EMPTY_VALUE } - - return []byte(string(data[:])), nil + return data, nil } -func (c *ChainClient) QueryAllTeeWorkerMap() ([]pattern.TeeWorkerInfo, error) { +// QueryAllWorkers query all tee work info +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - []WorkerInfo: all tee worker info +// - error: error message +func (c *ChainClient) QueryAllWorkers(block int32) ([]WorkerInfo, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - var list []pattern.TeeWorkerInfo + var list []WorkerInfo if !c.GetChainState() { - return list, pattern.ERR_RPC_CONNECTION + return list, ERR_RPC_CONNECTION } - key := createPrefixedKey(pattern.TEEWORKER, pattern.TEEWorkers) + key := createPrefixedKey(TeeWorker, Workers) keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEWorkers, err) - return list, errors.Wrap(err, "[GetKeysLatest]") + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), TeeWorker, Workers, err) + return list, err } - - set, err := c.api.RPC.State.QueryStorageAtLatest(keys) - if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEWorkers, err) - return list, errors.Wrap(err, "[QueryStorageAtLatest]") + var set []types.StorageChangeSet + if block < 0 { + set, err = c.api.RPC.State.QueryStorageAtLatest(keys) + if err != nil { + c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), TeeWorker, Workers, err) + return list, err + } + } else { + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + c.SetChainState(false) + return list, err + } + set, err = c.api.RPC.State.QueryStorageAt(keys, blockhash) + if err != nil { + c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAt: %v", c.GetCurrentRpcAddr(), TeeWorker, Workers, err) + return list, err + } } - for _, elem := range set { for _, change := range elem.Changes { - var teeWorker pattern.TeeWorkerInfo + var teeWorker WorkerInfo if err := codec.Decode(change.StorageData, &teeWorker); err != nil { - log.Println(err) continue } list = append(list, teeWorker) @@ -160,38 +189,14 @@ func (c *ChainClient) QueryAllTeeWorkerMap() ([]pattern.TeeWorkerInfo, error) { return list, nil } -func (c *ChainClient) QueryAllTeeInfo() ([]pattern.TeeInfo, error) { - teelist, err := c.QueryAllTeeWorkerMap() - if err != nil { - return nil, err - } - var results = make([]pattern.TeeInfo, len(teelist)) - for k, v := range teelist { - results[k].Pubkey = string(v.Pubkey[:]) - results[k].EcdhPubkey = string(v.EcdhPubkey[:]) - results[k].Version = uint32(v.Version) - results[k].LastUpdated = uint64(v.LastUpdated) - if v.StashAccount.HasValue() { - if ok, puk := v.StashAccount.Unwrap(); ok { - results[k].StashAccount, _ = utils.EncodePublicKeyAsCessAccount(puk[:]) - } - } - if v.AttestationProvider.HasValue() { - if ok, val := v.AttestationProvider.Unwrap(); ok { - results[k].AttestationProvider = uint8(val) - } - } - results[k].ConfidenceLevel = uint8(v.ConfidenceLevel) - results[k].Features = make([]uint32, len(v.Features)) - for i := 0; i < len(v.Features); i++ { - results[k].Features[i] = uint32(v.Features[i]) - } - results[k].WorkerRole = uint8(v.Role) - } - return results, nil -} - -func (c *ChainClient) QueryTeeWorkEndpoint(workPuk pattern.WorkerPublicKey) (string, error) { +// QueryEndpoints query tee's endpoint +// - puk: tee's work public key +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - string: tee's endpoint +// - error: error message +func (c *ChainClient) QueryEndpoints(puk WorkerPublicKey, block int32) (string, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -201,32 +206,55 @@ func (c *ChainClient) QueryTeeWorkEndpoint(workPuk pattern.WorkerPublicKey) (str var data types.Text if !c.GetChainState() { - return "", pattern.ERR_RPC_CONNECTION + return "", ERR_RPC_CONNECTION } - val, err := codec.Encode(workPuk) + val, err := codec.Encode(puk) if err != nil { return "", errors.Wrap(err, "[Encode]") } - key, err := types.CreateStorageKey(c.metadata, pattern.TEEWORKER, pattern.TEEEndpoints, val) + key, err := types.CreateStorageKey(c.metadata, TeeWorker, Endpoints, val) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEEndpoints, err) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TeeWorker, Endpoints, err) return "", errors.Wrap(err, "[CreateStorageKey]") } - - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + if block < 0 { + ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + if err != nil { + c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TeeWorker, Endpoints, err) + return "", errors.Wrap(err, "[GetStorageLatest]") + } + if !ok { + return "", ERR_RPC_EMPTY_VALUE + } + return string(data), nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) + if err != nil { + c.SetChainState(false) + return string(data), err + } + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEEndpoints, err) - return "", errors.Wrap(err, "[GetStorageLatest]") + c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), TeeWorker, Endpoints, err) + return "", errors.Wrap(err, "[GetStorage]") } if !ok { - return "", pattern.ERR_RPC_EMPTY_VALUE + return "", ERR_RPC_EMPTY_VALUE } - return string(data), nil } -func (c *ChainClient) QueryWorkerAddedAt(workPuk pattern.WorkerPublicKey) (types.U32, error) { +// QueryWorkerAddedAt query tee work registered block +// - puk: tee's work public key +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - uint32: tee work registered block +// - error: error message +func (c *ChainClient) QueryWorkerAddedAt(puk WorkerPublicKey, block int32) (uint32, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -236,27 +264,43 @@ func (c *ChainClient) QueryWorkerAddedAt(workPuk pattern.WorkerPublicKey) (types var data types.U32 if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + return uint32(data), ERR_RPC_CONNECTION } - val, err := codec.Encode(workPuk) + val, err := codec.Encode(puk) if err != nil { - return data, errors.Wrap(err, "[Encode]") + return uint32(data), errors.Wrap(err, "[Encode]") } - key, err := types.CreateStorageKey(c.metadata, pattern.TEEWORKER, pattern.TEEWorkerAddedAt, val) + key, err := types.CreateStorageKey(c.metadata, TeeWorker, WorkerAddedAt, val) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEWorkerAddedAt, err) - return data, errors.Wrap(err, "[CreateStorageKey]") + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TeeWorker, WorkerAddedAt, err) + return uint32(data), err } - - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + if block < 0 { + ok, err := c.api.RPC.State.GetStorageLatest(key, &data) + if err != nil { + c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TeeWorker, WorkerAddedAt, err) + return uint32(data), err + } + if !ok { + return uint32(data), ERR_RPC_EMPTY_VALUE + } + return uint32(data), nil + } + blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), pattern.TEEWORKER, pattern.TEEWorkerAddedAt, err) - return data, errors.Wrap(err, "[GetStorageLatest]") + c.SetChainState(false) + return uint32(data), err + } + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) + if err != nil { + c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), TeeWorker, WorkerAddedAt, err) + return uint32(data), err } if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE + return uint32(data), ERR_RPC_EMPTY_VALUE } - - return data, nil + return uint32(data), nil } From 453317f469f0529446a3d383e486173b5407e024 Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Fri, 10 May 2024 16:26:02 +0800 Subject: [PATCH 12/18] update chain, rpc_call --- chain/audit.go | 52 +++++---- chain/balances.go | 20 ++-- chain/chain.go | 178 +++++++++++++++--------------- chain/deoss.go | 77 +++++++------ chain/event.go | 38 +------ chain/evm.go | 10 +- chain/file.go | 4 + chain/fileBank.go | 156 +++++++++++++-------------- chain/pattern.go | 2 + chain/rpc_call.go | 110 +++++++++++++++++++ chain/sminer.go | 158 +++++++++++++-------------- chain/staking.go | 74 ++++++------- chain/storage_handler.go | 64 +++++------ chain/system.go | 227 +++++++-------------------------------- core/sdk/sdk.go | 2 - defaults.go | 6 +- 16 files changed, 527 insertions(+), 651 deletions(-) create mode 100644 chain/rpc_call.go diff --git a/chain/audit.go b/chain/audit.go index b2a42a0..9b31718 100755 --- a/chain/audit.go +++ b/chain/audit.go @@ -35,7 +35,7 @@ func (c *ChainClient) QueryChallengeSnapShot(accountID []byte, block int32) (boo var data ChallengeInfo - if !c.GetChainState() { + if !c.GetRpcState() { return false, data, ERR_RPC_CONNECTION } @@ -49,7 +49,7 @@ func (c *ChainClient) QueryChallengeSnapShot(accountID []byte, block int32) (boo 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(), Audit, ChallengeSnapShot, err) - c.SetChainState(false) + c.SetRpcState(false) return false, data, err } return ok, data, nil @@ -61,7 +61,7 @@ func (c *ChainClient) QueryChallengeSnapShot(accountID []byte, block int32) (boo ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Audit, ChallengeSnapShot, err) - c.SetChainState(false) + c.SetRpcState(false) return false, data, err } return ok, data, nil @@ -83,7 +83,7 @@ func (c *ChainClient) QueryCountedClear(accountID []byte, block int32) (uint8, e var data types.U8 - if !c.GetChainState() { + if !c.GetRpcState() { return uint8(data), ERR_RPC_CONNECTION } @@ -97,7 +97,7 @@ func (c *ChainClient) QueryCountedClear(accountID []byte, block int32) (uint8, e 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(), Audit, CountedClear, err) - c.SetChainState(false) + c.SetRpcState(false) return uint8(data), err } if !ok { @@ -112,7 +112,7 @@ func (c *ChainClient) QueryCountedClear(accountID []byte, block int32) (uint8, e ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Audit, CountedClear, err) - c.SetChainState(false) + c.SetRpcState(false) return uint8(data), err } if !ok { @@ -136,14 +136,13 @@ func (c *ChainClient) QueryCountedServiceFailed(accountID []byte, block int32) ( }() var data types.U32 - if !c.GetChainState() { + if !c.GetRpcState() { return uint32(data), ERR_RPC_CONNECTION } key, err := types.CreateStorageKey(c.metadata, Audit, CountedServiceFailed, accountID) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), Audit, CountedServiceFailed, err) - c.SetChainState(false) return uint32(data), err } @@ -151,7 +150,7 @@ func (c *ChainClient) QueryCountedServiceFailed(accountID []byte, block int32) ( 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(), Audit, CountedServiceFailed, err) - c.SetChainState(false) + c.SetRpcState(false) return 0, err } if !ok { @@ -166,7 +165,7 @@ func (c *ChainClient) QueryCountedServiceFailed(accountID []byte, 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(), Audit, CountedServiceFailed, err) - c.SetChainState(false) + c.SetRpcState(false) return uint32(data), err } if !ok { @@ -199,7 +198,7 @@ func (c *ChainClient) SubmitIdleProof(idleProof []types.U8) (string, error) { return blockhash, ERR_IdleProofIsEmpty } - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -220,7 +219,7 @@ func (c *ChainClient) SubmitIdleProof(idleProof []types.U8) (string, error) { ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitIdleProof, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -241,7 +240,6 @@ func (c *ChainClient) SubmitIdleProof(idleProof []types.U8) (string, error) { err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitIdleProof, err) - c.SetChainState(false) return blockhash, err } @@ -260,12 +258,12 @@ func (c *ChainClient) SubmitIdleProof(idleProof []types.U8) (string, error) { sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitIdleProof, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitIdleProof, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -310,7 +308,7 @@ func (c *ChainClient) SubmitServiceProof(serviceProof []types.U8) (string, error accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -331,7 +329,7 @@ func (c *ChainClient) SubmitServiceProof(serviceProof []types.U8) (string, error ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitServiceProof, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -370,12 +368,12 @@ func (c *ChainClient) SubmitServiceProof(serviceProof []types.U8) (string, error sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitServiceProof, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitServiceProof, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -426,7 +424,7 @@ func (c *ChainClient) SubmitVerifyIdleResult(totalProofHash []types.U8, front, r accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -447,7 +445,7 @@ func (c *ChainClient) SubmitVerifyIdleResult(totalProofHash []types.U8, front, r ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyIdleResult, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -486,12 +484,12 @@ func (c *ChainClient) SubmitVerifyIdleResult(totalProofHash []types.U8, front, r sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyIdleResult, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyIdleResult, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -539,7 +537,7 @@ func (c *ChainClient) SubmitVerifyServiceResult(result types.Bool, sign types.By accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -560,7 +558,7 @@ func (c *ChainClient) SubmitVerifyServiceResult(result types.Bool, sign types.By ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyServiceResult, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -599,12 +597,12 @@ func (c *ChainClient) SubmitVerifyServiceResult(result types.Bool, sign types.By sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyServiceResult, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Audit_SubmitVerifyServiceResult, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } diff --git a/chain/balances.go b/chain/balances.go index 49e7f0b..6ce74d2 100644 --- a/chain/balances.go +++ b/chain/balances.go @@ -31,7 +31,7 @@ func (c *ChainClient) QueryTotalIssuance(block int) (string, error) { }() var data types.U128 - if !c.GetChainState() { + if !c.GetRpcState() { return "", ERR_RPC_CONNECTION } @@ -45,7 +45,7 @@ func (c *ChainClient) QueryTotalIssuance(block int) (string, error) { 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(), Balances, TotalIssuance, err) - c.SetChainState(false) + c.SetRpcState(false) return "", err } if !ok { @@ -57,14 +57,13 @@ func (c *ChainClient) QueryTotalIssuance(block int) (string, error) { blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Balances, TotalIssuance, err) - c.SetChainState(false) return "", err } 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(), Balances, TotalIssuance, err) - c.SetChainState(false) + c.SetRpcState(false) return "", err } if !ok { @@ -90,7 +89,7 @@ func (c *ChainClient) QueryInactiveIssuance(block int) (string, error) { }() var data types.U128 - if !c.GetChainState() { + if !c.GetRpcState() { return "", ERR_RPC_CONNECTION } @@ -104,7 +103,7 @@ func (c *ChainClient) QueryInactiveIssuance(block int) (string, error) { 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(), Balances, InactiveIssuance, err) - c.SetChainState(false) + c.SetRpcState(false) return "", err } if !ok { @@ -116,14 +115,13 @@ func (c *ChainClient) QueryInactiveIssuance(block int) (string, error) { blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Balances, InactiveIssuance, err) - c.SetChainState(false) return "", err } 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(), Balances, InactiveIssuance, err) - c.SetChainState(false) + c.SetRpcState(false) return "", err } if !ok { @@ -157,7 +155,7 @@ func (c *ChainClient) TransferToken(dest string, amount uint64) (string, string, accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, "", ERR_RPC_CONNECTION } @@ -188,7 +186,7 @@ func (c *ChainClient) TransferToken(dest string, amount uint64) (string, string, ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Balances_Transfer, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, "", err } if !ok { @@ -218,7 +216,7 @@ func (c *ChainClient) TransferToken(dest string, amount uint64) (string, string, sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Balances_Transfer, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, "", err } defer sub.Unsubscribe() diff --git a/chain/chain.go b/chain/chain.go index be8aa3d..a42dbaa 100755 --- a/chain/chain.go +++ b/chain/chain.go @@ -13,7 +13,6 @@ import ( "fmt" "io" "log" - "net/http" "os" "strings" "sync" @@ -39,7 +38,6 @@ type ChainClient struct { metadata *types.Metadata runtimeVersion *types.RuntimeVersion eventRetriever retriever.EventRetriever - keyEvents types.StorageKey genesisHash types.Hash keyring signature.KeyringPair rpcAddr []string @@ -48,28 +46,23 @@ type ChainClient struct { tokenSymbol string networkEnv string signatureAcc string - treasuryAcc string name string - chainState bool + rpcState bool } // var _ sdk.SDK = (*ChainClient)(nil) -var globalTransport = &http.Transport{ - DisableKeepAlives: true, -} - -func NewEmptyChainClient() *ChainClient { - return &ChainClient{} -} - -func NewChainClient( - ctx context.Context, - serviceName string, - rpcs []string, - mnemonic string, - t time.Duration, -) (*ChainClient, error) { +// NewChainClient creates a chainclient +// - ctx: context +// - name: customised name, can be empty +// - rpcs: rpc addresses +// - mnemonic: account mnemonic, can be empty +// - t: waiting time for transaction packing, default is 30 seconds +// +// Return: +// - *ChainClient: chain client +// - error: error message +func NewChainClient(ctx context.Context, name string, rpcs []string, mnemonic string, t time.Duration) (*ChainClient, error) { var ( err error chainClient = &ChainClient{ @@ -78,7 +71,7 @@ func NewChainClient( txTicker: time.NewTicker(BlockInterval), rpcAddr: rpcs, packingTime: t, - name: serviceName, + name: name, } ) @@ -99,7 +92,7 @@ func NewChainClient( return nil, ERR_RPC_CONNECTION } - chainClient.SetChainState(true) + chainClient.SetRpcState(true) chainClient.metadata, err = chainClient.api.RPC.State.GetMetadataLatest() if err != nil { @@ -113,10 +106,6 @@ func NewChainClient( if err != nil { return nil, err } - chainClient.keyEvents, err = types.CreateStorageKey(chainClient.metadata, System, Events, nil) - if err != nil { - return nil, err - } chainClient.eventRetriever, err = retriever.NewDefaultEventRetriever(state.NewEventProvider(chainClient.api.RPC.State), chainClient.api.RPC.State) if err != nil { return nil, err @@ -131,130 +120,132 @@ func NewChainClient( return nil, err } } - properties, err := chainClient.SysProperties() + properties, err := chainClient.SystemProperties() if err != nil { return nil, err } chainClient.tokenSymbol = string(properties.TokenSymbol) - chainClient.networkEnv, err = chainClient.SysChain() + chainClient.networkEnv, err = chainClient.SystemChain() if err != nil { return nil, err } - if strings.Contains(chainClient.networkEnv, "test") { - chainClient.treasuryAcc = "cXhT9Xh3DhrBMDmXcGeMPDmTzDm1J8vDxBtKvogV33pPshnWS" - } else if strings.Contains(chainClient.networkEnv, "main") { - chainClient.treasuryAcc = "cXhT9Xh3DhrBMDmXcGeMPDmTzDm1J8vDxBtKvogV33pPshnWS" - } else { - chainClient.treasuryAcc = "cXhT9Xh3DhrBMDmXcGeMPDmTzDm1J8vDxBtKvogV33pPshnWS" - } - return chainClient, nil } -func (c *ChainClient) ReconnectRPC() error { - var err error - c.lock.Lock() - defer c.lock.Unlock() - if c.GetChainState() { - return nil - } - if c.api != nil { - if c.api.Client != nil { - c.api.Client.Close() - c.api.Client = nil - } - c.api = nil - } - c.api, - c.metadata, - c.runtimeVersion, - c.keyEvents, - c.eventRetriever, - c.genesisHash, - c.currentRpcAddr, err = reconnectChainSDK(c.currentRpcAddr, c.rpcAddr) - if err != nil { - return err - } - c.SetChainState(true) - return nil -} - +// GetSDKName get sdk name func (c *ChainClient) GetSDKName() string { return c.name } +// GetCurrentRpcAddr get the current rpc address being used func (c *ChainClient) GetCurrentRpcAddr() string { return c.currentRpcAddr } -func (c *ChainClient) SetSDKName(name string) { - c.name = name -} - -func (c *ChainClient) SetChainState(state bool) { +// SetChainState set the rpc connection status flag, +// when the rpc connection is normal, set it to true, +// otherwise set it to false. +func (c *ChainClient) SetRpcState(state bool) { c.chainStLock.Lock() - c.chainState = state + c.rpcState = state c.chainStLock.Unlock() } -func (c *ChainClient) GetChainState() bool { +// GetRpcState get the rpc connection status flag +// - true: connection is normal +// - false: connection failed +func (c *ChainClient) GetRpcState() bool { c.chainStLock.Lock() - st := c.chainState + st := c.rpcState c.chainStLock.Unlock() return st } +// GetSignatureAcc get your current account address +// +// Note: +// - make sure you fill in mnemonic when you create the chain client func (c *ChainClient) GetSignatureAcc() string { return c.signatureAcc } -func (c *ChainClient) GetKeyEvents() types.StorageKey { - return c.keyEvents -} - +// GetSignatureAccPulickey get your current account public key +// +// Note: +// - make sure you fill in mnemonic when you create the chain client func (c *ChainClient) GetSignatureAccPulickey() []byte { return c.keyring.PublicKey } +// GetSubstrateAPI get substrate api func (c *ChainClient) GetSubstrateAPI() *gsrpc.SubstrateAPI { return c.api } +// GetMetadata get chain metadata func (c *ChainClient) GetMetadata() *types.Metadata { return c.metadata } +// GetTokenSymbol get token symbol func (c *ChainClient) GetTokenSymbol() string { return c.tokenSymbol } +// GetNetworkEnv get network env func (c *ChainClient) GetNetworkEnv() string { return c.networkEnv } +// GetURI get the mnemonic for your current account func (c *ChainClient) GetURI() string { return c.keyring.URI } -func (c *ChainClient) GetTreasuryAccount() string { - return c.treasuryAcc -} - +// Sign with the mnemonic of your current account func (c *ChainClient) Sign(msg []byte) ([]byte, error) { return signature.Sign(msg, c.keyring.URI) } +// Verify the signature with your current account's mnemonic func (c *ChainClient) Verify(msg []byte, sig []byte) (bool, error) { return signature.Verify(msg, sig, c.keyring.URI) } -func reconnectChainSDK(oldRpc string, rpcs []string) ( +// ReconnectRpc reconnect rpc +func (c *ChainClient) ReconnectRpc() error { + var err error + c.lock.Lock() + defer c.lock.Unlock() + if c.GetRpcState() { + return nil + } + if c.api != nil { + if c.api.Client != nil { + c.api.Client.Close() + c.api.Client = nil + } + c.api = nil + } + c.api, + c.metadata, + c.runtimeVersion, + c.eventRetriever, + c.genesisHash, + c.currentRpcAddr, err = reconnectRpc(c.currentRpcAddr, c.rpcAddr) + if err != nil { + return err + } + c.SetRpcState(true) + return nil +} + +func reconnectRpc(oldRpc string, rpcs []string) ( *gsrpc.SubstrateAPI, *types.Metadata, *types.RuntimeVersion, - types.StorageKey, retriever.EventRetriever, types.Hash, string, @@ -284,35 +275,30 @@ func reconnectChainSDK(oldRpc string, rpcs []string) ( rpcAddr = rpcaddrs[i] } if api == nil { - return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, ERR_RPC_CONNECTION + return nil, nil, nil, nil, types.Hash{}, rpcAddr, ERR_RPC_CONNECTION } var metadata *types.Metadata var runtimeVer *types.RuntimeVersion - var keyEvents types.StorageKey var genesisHash types.Hash var eventRetriever retriever.EventRetriever metadata, err = api.RPC.State.GetMetadataLatest() if err != nil { - return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, ERR_RPC_CONNECTION + return nil, nil, nil, nil, types.Hash{}, rpcAddr, ERR_RPC_CONNECTION } genesisHash, err = api.RPC.Chain.GetBlockHash(0) if err != nil { - return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, ERR_RPC_CONNECTION + return nil, nil, nil, nil, types.Hash{}, rpcAddr, ERR_RPC_CONNECTION } runtimeVer, err = api.RPC.State.GetRuntimeVersionLatest() if err != nil { - return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, ERR_RPC_CONNECTION - } - keyEvents, err = types.CreateStorageKey(metadata, System, Events, nil) - if err != nil { - return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, ERR_RPC_CONNECTION + return nil, nil, nil, nil, types.Hash{}, rpcAddr, ERR_RPC_CONNECTION } eventRetriever, err = retriever.NewDefaultEventRetriever(state.NewEventProvider(api.RPC.State), api.RPC.State) if err != nil { - return nil, nil, nil, nil, nil, types.Hash{}, rpcAddr, ERR_RPC_CONNECTION + return nil, nil, nil, nil, types.Hash{}, rpcAddr, ERR_RPC_CONNECTION } - return api, metadata, runtimeVer, keyEvents, eventRetriever, genesisHash, rpcAddr, err + return api, metadata, runtimeVer, eventRetriever, genesisHash, rpcAddr, err } func createPrefixedKey(pallet, method string) []byte { @@ -367,8 +353,12 @@ func (c *ChainClient) VerifyPolkaSignatureWithBase58(account, msg, signature str return ok, nil } +// close chain client func (c *ChainClient) Close() { - if c.api.Client != nil { - c.api.Client.Close() + if c.api != nil { + if c.api.Client != nil { + c.api.Client.Close() + } + c.api = nil } } diff --git a/chain/deoss.go b/chain/deoss.go index 3fe182c..887b6ee 100755 --- a/chain/deoss.go +++ b/chain/deoss.go @@ -36,7 +36,7 @@ func (c *ChainClient) QueryOss(accountID []byte, block int32) (OssInfo, error) { }() var data OssInfo - if !c.GetChainState() { + if !c.GetRpcState() { return data, ERR_RPC_CONNECTION } @@ -50,7 +50,7 @@ func (c *ChainClient) QueryOss(accountID []byte, block int32) (OssInfo, error) { 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(), Oss, Oss, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -65,7 +65,7 @@ func (c *ChainClient) QueryOss(accountID []byte, block int32) (OssInfo, error) { 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(), Oss, Oss, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -88,7 +88,7 @@ func (c *ChainClient) QueryAllOss(block int32) ([]OssInfo, error) { }() var result []OssInfo - if !c.GetChainState() { + if !c.GetRpcState() { return nil, ERR_RPC_CONNECTION } @@ -96,7 +96,7 @@ func (c *ChainClient) QueryAllOss(block int32) ([]OssInfo, error) { keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), Oss, Oss, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } @@ -105,7 +105,7 @@ func (c *ChainClient) QueryAllOss(block int32) ([]OssInfo, error) { set, err = c.api.RPC.State.QueryStorageAtLatest(keys) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Oss, Oss, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } } else { @@ -116,7 +116,7 @@ func (c *ChainClient) QueryAllOss(block int32) ([]OssInfo, error) { set, err = c.api.RPC.State.QueryStorageAt(keys, blockhash) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Oss, Oss, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } } @@ -146,7 +146,7 @@ func (c *ChainClient) QueryAllOssPeerId(block int32) ([]string, error) { }() var result []string - if !c.GetChainState() { + if !c.GetRpcState() { return nil, ERR_RPC_CONNECTION } @@ -154,7 +154,7 @@ func (c *ChainClient) QueryAllOssPeerId(block int32) ([]string, error) { keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), Oss, Oss, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } @@ -163,7 +163,7 @@ func (c *ChainClient) QueryAllOssPeerId(block int32) ([]string, error) { set, err = c.api.RPC.State.QueryStorageAtLatest(keys) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Oss, Oss, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } } else { @@ -174,7 +174,7 @@ func (c *ChainClient) QueryAllOssPeerId(block int32) ([]string, error) { set, err = c.api.RPC.State.QueryStorageAt(keys, blockhash) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Oss, Oss, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } } @@ -206,7 +206,7 @@ func (c *ChainClient) QueryAuthorityList(accountID []byte, block int32) ([]types }() var data []types.AccountID - if !c.GetChainState() { + if !c.GetRpcState() { return data, ERR_RPC_CONNECTION } @@ -220,7 +220,7 @@ func (c *ChainClient) QueryAuthorityList(accountID []byte, block int32) ([]types 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(), Oss, AuthorityList, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -230,13 +230,12 @@ func (c *ChainClient) QueryAuthorityList(accountID []byte, block int32) ([]types } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return data, err } 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(), Oss, AuthorityList, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -268,7 +267,7 @@ func (c *ChainClient) Authorize(accountID []byte) (string, error) { accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -294,7 +293,7 @@ func (c *ChainClient) Authorize(accountID []byte) (string, error) { ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Oss_Authorize, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -315,7 +314,6 @@ func (c *ChainClient) Authorize(accountID []byte) (string, error) { err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Oss_Authorize, err) - c.SetChainState(false) return blockhash, err } @@ -334,12 +332,12 @@ func (c *ChainClient) Authorize(accountID []byte) (string, error) { sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_Authorize, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_Authorize, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -384,7 +382,7 @@ func (c *ChainClient) CancelAuthorize(accountID []byte) (string, error) { accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -405,7 +403,7 @@ func (c *ChainClient) CancelAuthorize(accountID []byte) (string, error) { ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Oss_CancelAuthorize, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -426,7 +424,6 @@ func (c *ChainClient) CancelAuthorize(accountID []byte) (string, error) { err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Oss_CancelAuthorize, err) - c.SetChainState(false) return blockhash, err } @@ -445,12 +442,12 @@ func (c *ChainClient) CancelAuthorize(accountID []byte) (string, error) { sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_CancelAuthorize, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_CancelAuthorize, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -496,7 +493,7 @@ func (c *ChainClient) RegisterOss(peerId []byte, domain string) (string, error) accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -534,7 +531,7 @@ func (c *ChainClient) RegisterOss(peerId []byte, domain string) (string, error) ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Oss_Register, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } @@ -561,10 +558,11 @@ func (c *ChainClient) RegisterOss(peerId []byte, domain string) (string, error) err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Oss_Register, err) - c.SetChainState(false) return blockhash, err } + <-c.txTicker.C + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -578,12 +576,12 @@ func (c *ChainClient) RegisterOss(peerId []byte, domain string) (string, error) sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_Register, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_Register, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]") } } @@ -629,7 +627,7 @@ func (c *ChainClient) UpdateOss(peerId string, domain string) (string, error) { accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -663,10 +661,11 @@ func (c *ChainClient) UpdateOss(peerId string, domain string) (string, error) { err = fmt.Errorf("rpc err: [%s] [tx] [%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), TX_Oss_Update, err) return blockhash, err } + ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Oss_Update, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -687,7 +686,6 @@ func (c *ChainClient) UpdateOss(peerId string, domain string) (string, error) { err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Oss_Update, err) - c.SetChainState(false) return blockhash, err } @@ -706,12 +704,12 @@ func (c *ChainClient) UpdateOss(peerId string, domain string) (string, error) { sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_Update, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_Update, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -753,7 +751,7 @@ func (c *ChainClient) DestroyOss() (string, error) { accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -774,7 +772,7 @@ func (c *ChainClient) DestroyOss() (string, error) { ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Oss_Destroy, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } @@ -796,7 +794,6 @@ func (c *ChainClient) DestroyOss() (string, error) { err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Oss_Destroy, err) - c.SetChainState(false) return blockhash, err } @@ -815,12 +812,12 @@ func (c *ChainClient) DestroyOss() (string, error) { sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_Destroy, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Oss_Destroy, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } diff --git a/chain/event.go b/chain/event.go index 6cdfc13..9680b4b 100755 --- a/chain/event.go +++ b/chain/event.go @@ -1905,13 +1905,6 @@ func (c *ChainClient) RetrieveBlock(blocknumber uint64) ([]string, []ExtrinsicsI Amount: amount, Result: true, }) - // transfers, err := c.parseTransferInfoFromBlock(blockhash) - // if err != nil { - // return systemEvents, extrinsicsInfo, transferInfo, "", "", "", "", 0, err - // } - // if len(transfers) > 0 { - // transferInfo = append(transferInfo, transfers...) - // } } else if e.Name == SystemExtrinsicSuccess { if len(eventsBuf) > 0 { extrinsicsInfo = append(extrinsicsInfo, ExtrinsicsInfo{ @@ -2168,7 +2161,7 @@ func (c *ChainClient) ParseBlockData(blocknumber uint64) (BlockData, error) { if err != nil { return blockdata, err } - if to == c.treasuryAcc { + if to == TreasuryAccount { blockdata.Punishment = append(blockdata.Punishment, Punishment{ ExtrinsicHash: blockdata.Extrinsics[extrinsicIndex].Hash, From: from, @@ -2559,35 +2552,6 @@ func ExplicitBigInt(v reflect.Value, depth int) string { return fee } -func (c *ChainClient) parseTransferInfoFromBlock(blockhash types.Hash) ([]TransferInfo, error) { - var transferEvents = make([]TransferInfo, 0) - var data types.StorageDataRaw - ok, err := c.GetSubstrateAPI().RPC.State.GetStorage(c.GetKeyEvents(), &data, blockhash) - if err != nil { - return transferEvents, err - } - var from string - var to string - if ok { - events := EventRecords{} - err = types.EventRecordsRaw(data).DecodeEventRecords(c.GetMetadata(), &events) - if err != nil { - return transferEvents, err - } - for _, e := range events.Balances_Transfer { - from, _ = utils.EncodePublicKeyAsCessAccount(e.From[:]) - to, _ = utils.EncodePublicKeyAsCessAccount(e.To[:]) - transferEvents = append(transferEvents, TransferInfo{ - From: from, - To: to, - Amount: e.Value.String(), - Result: true, - }) - } - } - return transferEvents, nil -} - func ParseTransferInfoFromEvent(e *parser.Event) (string, string, string, error) { if e == nil { return "", "", "", errors.New("event is nil") diff --git a/chain/evm.go b/chain/evm.go index c38515c..bb63211 100644 --- a/chain/evm.go +++ b/chain/evm.go @@ -32,8 +32,8 @@ func (c *ChainClient) SendEvmCall(source types.H160, target types.H160, input ty accountInfo types.AccountInfo ) - if !c.GetChainState() { - return blockhash, fmt.Errorf("chainSDK.UploadDeclaration(): GetChainState(): %v", ERR_RPC_CONNECTION) + if !c.GetRpcState() { + return blockhash, fmt.Errorf("chainSDK.UploadDeclaration(): GetRpcState(): %v", ERR_RPC_CONNECTION) } var nonce types.Option[types.U256] @@ -59,7 +59,7 @@ func (c *ChainClient) SendEvmCall(source types.H160, target types.H160, input ty ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_EVM_Call, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } @@ -99,12 +99,12 @@ func (c *ChainClient) SendEvmCall(source types.H160, target types.H160, input ty sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_EVM_Call, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_EVM_Call, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } diff --git a/chain/file.go b/chain/file.go index 3112475..a934d9a 100755 --- a/chain/file.go +++ b/chain/file.go @@ -25,6 +25,10 @@ import ( "github.com/pkg/errors" ) +var globalTransport = &http.Transport{ + DisableKeepAlives: true, +} + func (c *ChainClient) StoreFile(url, file, bucket string) (string, error) { fstat, err := os.Stat(file) if err != nil { diff --git a/chain/fileBank.go b/chain/fileBank.go index 5be7a14..3a3a6fc 100755 --- a/chain/fileBank.go +++ b/chain/fileBank.go @@ -39,7 +39,7 @@ func (c *ChainClient) QueryBucket(accountID []byte, bucketName string, block int var data BucketInfo - if !c.GetChainState() { + if !c.GetRpcState() { return data, ERR_RPC_CONNECTION } @@ -67,7 +67,7 @@ func (c *ChainClient) QueryBucket(accountID []byte, bucketName string, block int 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(), FileBank, Bucket, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -82,7 +82,7 @@ func (c *ChainClient) QueryBucket(accountID []byte, bucketName string, block int 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(), FileBank, Bucket, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -123,7 +123,7 @@ func (c *ChainClient) QueryDealMap(fid string, block int32) (StorageOrder, error return data, errors.Wrap(err, "[Encode]") } - if !c.GetChainState() { + if !c.GetRpcState() { return data, ERR_RPC_CONNECTION } @@ -136,7 +136,7 @@ func (c *ChainClient) QueryDealMap(fid string, block int32) (StorageOrder, error 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(), FileBank, DealMap, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -147,13 +147,12 @@ func (c *ChainClient) QueryDealMap(fid string, block int32) (StorageOrder, error blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), FileBank, DealMap, err) - c.SetChainState(false) return data, err } 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(), FileBank, DealMap, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -181,7 +180,7 @@ func (c *ChainClient) QueryFile(fid string, block int32) (FileMetadata, error) { hash FileHash ) - if !c.GetChainState() { + if !c.GetRpcState() { return data, ERR_RPC_CONNECTION } @@ -208,7 +207,7 @@ func (c *ChainClient) QueryFile(fid string, block int32) (FileMetadata, error) { 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(), FileBank, File, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -220,13 +219,12 @@ func (c *ChainClient) QueryFile(fid string, block int32) (FileMetadata, error) { blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), FileBank, File, err) - c.SetChainState(false) return data, err } 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(), FileBank, File, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -254,7 +252,7 @@ func (c *ChainClient) QueryRestoralOrder(fragmentHash string, block int32) (Rest hash FileHash ) - if !c.GetChainState() { + if !c.GetRpcState() { return data, ERR_RPC_CONNECTION } @@ -281,7 +279,7 @@ func (c *ChainClient) QueryRestoralOrder(fragmentHash string, block int32) (Rest 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(), FileBank, RestoralOrder, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -291,13 +289,12 @@ func (c *ChainClient) QueryRestoralOrder(fragmentHash string, block int32) (Rest } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return data, err } 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(), FileBank, RestoralOrder, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -320,7 +317,7 @@ func (c *ChainClient) QueryAllRestoralOrder(block int32) ([]RestoralOrderInfo, e }() var result []RestoralOrderInfo - if !c.GetChainState() { + if !c.GetRpcState() { return nil, ERR_RPC_CONNECTION } @@ -328,7 +325,7 @@ func (c *ChainClient) QueryAllRestoralOrder(block int32) ([]RestoralOrderInfo, e keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), FileBank, RestoralOrder, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } var set []types.StorageChangeSet @@ -336,7 +333,7 @@ func (c *ChainClient) QueryAllRestoralOrder(block int32) ([]RestoralOrderInfo, e set, err = c.api.RPC.State.QueryStorageAtLatest(keys) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), FileBank, RestoralOrder, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } } else { @@ -347,7 +344,7 @@ func (c *ChainClient) QueryAllRestoralOrder(block int32) ([]RestoralOrderInfo, e set, err = c.api.RPC.State.QueryStorageAt(keys, blockhash) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAt: %v", c.GetCurrentRpcAddr(), FileBank, RestoralOrder, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } } @@ -381,7 +378,7 @@ func (c *ChainClient) QueryAllBucketName(accountID []byte, block int32) ([]strin var data []types.Bytes var value []string - if !c.GetChainState() { + if !c.GetRpcState() { return nil, ERR_RPC_CONNECTION } @@ -404,7 +401,7 @@ func (c *ChainClient) QueryAllBucketName(accountID []byte, block int32) ([]strin 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(), FileBank, UserBucketList, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } if !ok { @@ -417,13 +414,12 @@ func (c *ChainClient) QueryAllBucketName(accountID []byte, block int32) ([]strin } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return nil, err } 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(), FileBank, UserBucketList, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } if !ok { @@ -452,7 +448,7 @@ func (c *ChainClient) QueryAllUserFiles(accountID []byte, block int32) ([]string var data []UserFileSliceInfo var value []string - if !c.GetChainState() { + if !c.GetRpcState() { return nil, ERR_RPC_CONNECTION } @@ -475,7 +471,7 @@ func (c *ChainClient) QueryAllUserFiles(accountID []byte, block int32) ([]string 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(), FileBank, UserHoldFileList, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } if !ok { @@ -488,13 +484,12 @@ func (c *ChainClient) QueryAllUserFiles(accountID []byte, block int32) ([]string } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return nil, err } 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(), FileBank, UserHoldFileList, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } if !ok { @@ -580,7 +575,7 @@ func (c *ChainClient) UploadDeclaration(fid string, segment []SegmentList, user hash[i] = types.U8(fid[i]) } - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -601,7 +596,7 @@ func (c *ChainClient) UploadDeclaration(fid string, segment []SegmentList, user ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_UploadDeclaration, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -627,7 +622,6 @@ func (c *ChainClient) UploadDeclaration(fid string, segment []SegmentList, user err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_UploadDeclaration, err) - c.SetChainState(false) return blockhash, err } @@ -646,12 +640,12 @@ func (c *ChainClient) UploadDeclaration(fid string, segment []SegmentList, user sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_UploadDeclaration, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_UploadDeclaration, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -701,7 +695,7 @@ func (c *ChainClient) CreateBucket(owner []byte, bucketName string) (string, err accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -727,7 +721,7 @@ func (c *ChainClient) CreateBucket(owner []byte, bucketName string) (string, err ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_CreateBucket, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -748,7 +742,6 @@ func (c *ChainClient) CreateBucket(owner []byte, bucketName string) (string, err err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_CreateBucket, err) - c.SetChainState(false) return blockhash, err } @@ -758,7 +751,7 @@ func (c *ChainClient) CreateBucket(owner []byte, bucketName string) (string, err sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_CreateBucket, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } defer sub.Unsubscribe() @@ -806,7 +799,7 @@ func (c *ChainClient) DeleteBucket(owner []byte, bucketName string) (string, err accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -832,7 +825,7 @@ func (c *ChainClient) DeleteBucket(owner []byte, bucketName string) (string, err ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteBucket, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -853,7 +846,6 @@ func (c *ChainClient) DeleteBucket(owner []byte, bucketName string) (string, err err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteBucket, err) - c.SetChainState(false) return blockhash, err } @@ -863,7 +855,7 @@ func (c *ChainClient) DeleteBucket(owner []byte, bucketName string) (string, err sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteBucket, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } defer sub.Unsubscribe() @@ -911,7 +903,7 @@ func (c *ChainClient) DeleteFile(owner []byte, fid string) (string, error) { accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -946,7 +938,7 @@ func (c *ChainClient) DeleteFile(owner []byte, fid string) (string, error) { ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteFile, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -967,7 +959,6 @@ func (c *ChainClient) DeleteFile(owner []byte, fid string) (string, error) { err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteFile, err) - c.SetChainState(false) return blockhash, err } @@ -986,12 +977,12 @@ func (c *ChainClient) DeleteFile(owner []byte, fid string) (string, error) { sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteFile, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_DeleteFile, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -1041,7 +1032,7 @@ func (c *ChainClient) TransferReport(index uint8, fid string) (string, error) { accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -1072,7 +1063,7 @@ func (c *ChainClient) TransferReport(index uint8, fid string) (string, error) { ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_TransferReport, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -1093,7 +1084,6 @@ func (c *ChainClient) TransferReport(index uint8, fid string) (string, error) { err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_TransferReport, err) - c.SetChainState(false) return blockhash, err } @@ -1112,12 +1102,12 @@ func (c *ChainClient) TransferReport(index uint8, fid string) (string, error) { sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_TransferReport, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_TransferReport, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -1166,7 +1156,7 @@ func (c *ChainClient) GenerateRestoralOrder(fid, fragmentHash string) (string, e accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -1206,7 +1196,7 @@ func (c *ChainClient) GenerateRestoralOrder(fid, fragmentHash string) (string, e ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_GenerateRestoralOrder, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -1227,7 +1217,6 @@ func (c *ChainClient) GenerateRestoralOrder(fid, fragmentHash string) (string, e err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_GenerateRestoralOrder, err) - c.SetChainState(false) return blockhash, err } @@ -1237,7 +1226,7 @@ func (c *ChainClient) GenerateRestoralOrder(fid, fragmentHash string) (string, e sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_GenerateRestoralOrder, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } defer sub.Unsubscribe() @@ -1284,7 +1273,7 @@ func (c *ChainClient) ClaimRestoralOrder(fragmentHash string) (string, error) { accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -1314,7 +1303,7 @@ func (c *ChainClient) ClaimRestoralOrder(fragmentHash string) (string, error) { ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralOrder, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -1335,7 +1324,6 @@ func (c *ChainClient) ClaimRestoralOrder(fragmentHash string) (string, error) { err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralOrder, err) - c.SetChainState(false) return blockhash, err } @@ -1354,12 +1342,12 @@ func (c *ChainClient) ClaimRestoralOrder(fragmentHash string) (string, error) { sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralOrder, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralOrder, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -1409,7 +1397,7 @@ func (c *ChainClient) ClaimRestoralNoExistOrder(puk []byte, fid, fragmentHash st accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -1454,7 +1442,7 @@ func (c *ChainClient) ClaimRestoralNoExistOrder(puk []byte, fid, fragmentHash st ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralNoexistOrder, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -1492,12 +1480,12 @@ func (c *ChainClient) ClaimRestoralNoExistOrder(puk []byte, fid, fragmentHash st sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralNoexistOrder, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_ClaimRestoralNoexistOrder, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -1545,7 +1533,7 @@ func (c *ChainClient) RestoralOrderComplete(fragmentHash string) (string, error) accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -1574,7 +1562,7 @@ func (c *ChainClient) RestoralOrderComplete(fragmentHash string) (string, error) ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_RestoralOrderComplete, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -1597,7 +1585,6 @@ func (c *ChainClient) RestoralOrderComplete(fragmentHash string) (string, error) err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_RestoralOrderComplete, err) - c.SetChainState(false) return blockhash, err } @@ -1613,12 +1600,12 @@ func (c *ChainClient) RestoralOrderComplete(fragmentHash string) (string, error) sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_RestoralOrderComplete, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_RestoralOrderComplete, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -1669,7 +1656,7 @@ func (c *ChainClient) CertIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWithAc accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -1688,7 +1675,7 @@ func (c *ChainClient) CertIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWithAc ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_CertIdleSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -1711,10 +1698,11 @@ func (c *ChainClient) CertIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWithAc err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_CertIdleSpace, err) - c.SetChainState(false) return blockhash, err } + <-c.txTicker.C + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1724,15 +1712,16 @@ func (c *ChainClient) CertIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWithAc if err != nil { return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_CertIdleSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_CertIdleSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -1783,7 +1772,7 @@ func (c *ChainClient) ReplaceIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWit accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -1802,7 +1791,7 @@ func (c *ChainClient) ReplaceIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWit ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_ReplaceIdleSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -1825,10 +1814,11 @@ func (c *ChainClient) ReplaceIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWit err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_ReplaceIdleSpace, err) - c.SetChainState(false) return blockhash, err } + <-c.txTicker.C + // Do the transfer and track the actual status sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { @@ -1838,15 +1828,16 @@ func (c *ChainClient) ReplaceIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWit if err != nil { return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_ReplaceIdleSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_ReplaceIdleSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -1895,7 +1886,7 @@ func (c *ChainClient) CalculateReport(teeSig types.Bytes, tagSigInfo TagSigInfo) accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -1916,7 +1907,7 @@ func (c *ChainClient) CalculateReport(teeSig types.Bytes, tagSigInfo TagSigInfo) ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_FileBank_CalculateReport, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -1937,7 +1928,6 @@ func (c *ChainClient) CalculateReport(teeSig types.Bytes, tagSigInfo TagSigInfo) err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_FileBank_CalculateReport, err) - c.SetChainState(false) return blockhash, err } @@ -1956,12 +1946,12 @@ func (c *ChainClient) CalculateReport(teeSig types.Bytes, tagSigInfo TagSigInfo) sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_CalculateReport, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_FileBank_CalculateReport, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } diff --git a/chain/pattern.go b/chain/pattern.go index 7e85e12..0bddb9d 100644 --- a/chain/pattern.go +++ b/chain/pattern.go @@ -32,6 +32,8 @@ const MaxSubmitedIdleFileMeta = 30 const PublicDeoss = "http://deoss-pub-gateway.cess.cloud/" const PublicDeossAccount = "cXhwBytXqrZLr1qM5NHJhCzEMckSTzNKw17ci2aHft6ETSQm9" +const TreasuryAccount = "cXhT9Xh3DhrBMDmXcGeMPDmTzDm1J8vDxBtKvogV33pPshnWS" + // pallet names const ( // Audit diff --git a/chain/rpc_call.go b/chain/rpc_call.go new file mode 100644 index 0000000..2ece41f --- /dev/null +++ b/chain/rpc_call.go @@ -0,0 +1,110 @@ +/* + Copyright (C) CESS. All rights reserved. + Copyright (C) Cumulus Encrypted Storage System. All rights reserved. + + SPDX-License-Identifier: Apache-2.0 +*/ + +package chain + +import ( + "log" + + "github.com/CESSProject/cess-go-sdk/utils" + "github.com/centrifuge/go-substrate-rpc-client/v4/types" +) + +// SystemProperties query system properties +// +// Return: +// - SysProperties: system properties +// - error: error message +func (c *ChainClient) SystemProperties() (SysProperties, error) { + defer func() { + if err := recover(); err != nil { + log.Println(utils.RecoverError(err)) + } + }() + var data SysProperties + if !c.GetRpcState() { + return data, ERR_RPC_CONNECTION + } + err := c.api.Client.Call(&data, RPC_SYS_Properties) + return data, err +} + +// SystemProperties query system properties +// +// Return: +// - string: system chain +// - error: error message +func (c *ChainClient) SystemChain() (string, error) { + defer func() { + if err := recover(); err != nil { + log.Println(utils.RecoverError(err)) + } + }() + var data types.Text + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + err := c.api.Client.Call(&data, RPC_SYS_Chain) + return string(data), err +} + +// SystemSyncState query system sync state +// +// Return: +// - SysSyncState: system sync state +// - error: error message +func (c *ChainClient) SystemSyncState() (SysSyncState, error) { + defer func() { + if err := recover(); err != nil { + log.Println(utils.RecoverError(err)) + } + }() + var data SysSyncState + if !c.GetRpcState() { + return data, ERR_RPC_CONNECTION + } + err := c.api.Client.Call(&data, RPC_SYS_SyncState) + return data, err +} + +// SystemVersion query system version +// +// Return: +// - string: system version +// - error: error message +func (c *ChainClient) SystemVersion() (string, error) { + defer func() { + if err := recover(); err != nil { + log.Println(utils.RecoverError(err)) + } + }() + var data types.Text + if !c.GetRpcState() { + return "", ERR_RPC_CONNECTION + } + err := c.api.Client.Call(&data, RPC_SYS_Version) + return string(data), err +} + +// NetListening query net listenning +// +// Return: +// - bool: net listenning +// - error: error message +func (c *ChainClient) NetListening() (bool, error) { + defer func() { + if err := recover(); err != nil { + log.Println(utils.RecoverError(err)) + } + }() + var data types.Bool + if !c.GetRpcState() { + return false, ERR_RPC_CONNECTION + } + err := c.api.Client.Call(&data, RPC_NET_Listening) + return bool(data), err +} diff --git a/chain/sminer.go b/chain/sminer.go index 198edb3..d980ff7 100755 --- a/chain/sminer.go +++ b/chain/sminer.go @@ -36,7 +36,7 @@ func (c *ChainClient) QueryExpenders(block int32) (ExpendersInfo, error) { var data ExpendersInfo - if !c.GetChainState() { + if !c.GetRpcState() { return data, ERR_RPC_CONNECTION } @@ -50,7 +50,7 @@ func (c *ChainClient) QueryExpenders(block int32) (ExpendersInfo, error) { 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(), Sminer, Expenders, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -60,13 +60,12 @@ func (c *ChainClient) QueryExpenders(block int32) (ExpendersInfo, error) { } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return data, err } 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(), Sminer, Expenders, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -91,7 +90,7 @@ func (c *ChainClient) QueryMinerItems(accountID []byte, block int32) (MinerInfo, var data MinerInfo - if !c.GetChainState() { + if !c.GetRpcState() { return data, ERR_RPC_CONNECTION } @@ -105,7 +104,7 @@ func (c *ChainClient) QueryMinerItems(accountID []byte, block int32) (MinerInfo, 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(), Sminer, MinerItems, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -116,13 +115,12 @@ func (c *ChainClient) QueryMinerItems(accountID []byte, block int32) (MinerInfo, blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return data, err } 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(), Sminer, MinerItems, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -147,7 +145,7 @@ func (c *ChainClient) QueryStakingStartBlock(accountID []byte, block int32) (uin var data types.U32 - if !c.GetChainState() { + if !c.GetRpcState() { return 0, ERR_RPC_CONNECTION } @@ -161,7 +159,7 @@ func (c *ChainClient) QueryStakingStartBlock(accountID []byte, block int32) (uin 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(), Sminer, StakingStartBlock, err) - c.SetChainState(false) + c.SetRpcState(false) return 0, err } if !ok { @@ -171,13 +169,12 @@ func (c *ChainClient) QueryStakingStartBlock(accountID []byte, block int32) (uin } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return 0, err } ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Sminer, StakingStartBlock, err) - c.SetChainState(false) + c.SetRpcState(false) return 0, err } if !ok { @@ -202,7 +199,7 @@ func (c *ChainClient) QueryAllMiner(block int32) ([]types.AccountID, error) { var data []types.AccountID - if !c.GetChainState() { + if !c.GetRpcState() { return data, ERR_RPC_CONNECTION } @@ -216,7 +213,7 @@ func (c *ChainClient) QueryAllMiner(block int32) ([]types.AccountID, error) { 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(), Sminer, AllMiner, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } if !ok { @@ -226,13 +223,12 @@ func (c *ChainClient) QueryAllMiner(block int32) ([]types.AccountID, error) { } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return data, err } 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(), Sminer, AllMiner, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } if !ok { @@ -256,7 +252,7 @@ func (c *ChainClient) QueryCounterForMinerItems(block int32) (uint32, error) { var data types.U32 - if !c.GetChainState() { + if !c.GetRpcState() { return 0, ERR_RPC_CONNECTION } @@ -270,7 +266,7 @@ func (c *ChainClient) QueryCounterForMinerItems(block int32) (uint32, error) { 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(), Sminer, CounterForMinerItems, err) - c.SetChainState(false) + c.SetRpcState(false) return 0, err } if !ok { @@ -280,13 +276,12 @@ func (c *ChainClient) QueryCounterForMinerItems(block int32) (uint32, error) { } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return 0, err } 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(), Sminer, CounterForMinerItems, err) - c.SetChainState(false) + c.SetRpcState(false) return 0, err } if !ok { @@ -310,7 +305,7 @@ func (c *ChainClient) QueryRewardMap(accountID []byte, block int32) (MinerReward var data MinerReward - if !c.GetChainState() { + if !c.GetRpcState() { return data, ERR_RPC_CONNECTION } @@ -324,7 +319,7 @@ func (c *ChainClient) QueryRewardMap(accountID []byte, block int32) (MinerReward 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(), Sminer, RewardMap, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -334,13 +329,12 @@ func (c *ChainClient) QueryRewardMap(accountID []byte, block int32) (MinerReward } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return data, err } 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(), Sminer, RewardMap, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -365,7 +359,7 @@ func (c *ChainClient) QueryRestoralTarget(accountID []byte, block int32) (Restor var data RestoralTargetInfo - if !c.GetChainState() { + if !c.GetRpcState() { return data, ERR_RPC_CONNECTION } @@ -388,7 +382,7 @@ func (c *ChainClient) QueryRestoralTarget(accountID []byte, block int32) (Restor 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(), Sminer, RestoralTarget, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -398,13 +392,12 @@ func (c *ChainClient) QueryRestoralTarget(accountID []byte, block int32) (Restor } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return data, err } 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(), Sminer, RestoralTarget, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -427,7 +420,7 @@ func (c *ChainClient) QueryAllRestoralTarget(block int32) ([]RestoralTargetInfo, }() var result []RestoralTargetInfo - if !c.GetChainState() { + if !c.GetRpcState() { return nil, ERR_RPC_CONNECTION } @@ -435,7 +428,7 @@ func (c *ChainClient) QueryAllRestoralTarget(block int32) ([]RestoralTargetInfo, keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), Sminer, RestoralTarget, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } var set []types.StorageChangeSet @@ -443,19 +436,18 @@ func (c *ChainClient) QueryAllRestoralTarget(block int32) ([]RestoralTargetInfo, set, err = c.api.RPC.State.QueryStorageAtLatest(keys) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Sminer, RestoralTarget, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } } else { blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return nil, err } set, err = c.api.RPC.State.QueryStorageAt(keys, blockhash) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAt: %v", c.GetCurrentRpcAddr(), Sminer, RestoralTarget, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } } @@ -497,7 +489,7 @@ func (c *ChainClient) QueryPendingReplacements(accountID []byte, block int32) (t return data, errors.Wrap(err, "[EncodeToBytes]") } - if !c.GetChainState() { + if !c.GetRpcState() { return data, ERR_RPC_CONNECTION } @@ -511,7 +503,7 @@ func (c *ChainClient) QueryPendingReplacements(accountID []byte, block int32) (t 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(), Sminer, PendingReplacements, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -521,13 +513,12 @@ func (c *ChainClient) QueryPendingReplacements(accountID []byte, block int32) (t } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return data, err } 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(), Sminer, PendingReplacements, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -553,7 +544,7 @@ func (c *ChainClient) QueryCompleteSnapShot(era uint32, block int32) (uint32, ui var data CompleteSnapShotType - if !c.GetChainState() { + if !c.GetRpcState() { return 0, 0, ERR_RPC_CONNECTION } @@ -572,7 +563,7 @@ func (c *ChainClient) QueryCompleteSnapShot(era uint32, block int32) (uint32, ui 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(), Sminer, CompleteSnapShot, err) - c.SetChainState(false) + c.SetRpcState(false) return 0, 0, err } if !ok { @@ -584,14 +575,13 @@ func (c *ChainClient) QueryCompleteSnapShot(era uint32, block int32) (uint32, ui blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Sminer, CompleteSnapShot, err) - c.SetChainState(false) return 0, 0, err } 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(), Sminer, CompleteSnapShot, err) - c.SetChainState(false) + c.SetRpcState(false) return 0, 0, err } if !ok { @@ -626,7 +616,7 @@ func (c *ChainClient) IncreaseCollateral(accountID []byte, token string) (string accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -657,7 +647,7 @@ func (c *ChainClient) IncreaseCollateral(accountID []byte, token string) (string ok, err = c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseCollateral, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -696,12 +686,12 @@ func (c *ChainClient) IncreaseCollateral(accountID []byte, token string) (string sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseCollateral, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseCollateral, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -750,7 +740,7 @@ func (c *ChainClient) IncreaseDeclarationSpace(tibCount uint32) (string, error) accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -771,7 +761,7 @@ func (c *ChainClient) IncreaseDeclarationSpace(tibCount uint32) (string, error) ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseDeclarationSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -792,7 +782,6 @@ func (c *ChainClient) IncreaseDeclarationSpace(tibCount uint32) (string, error) err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseDeclarationSpace, err) - c.SetChainState(false) return blockhash, err } @@ -807,15 +796,16 @@ func (c *ChainClient) IncreaseDeclarationSpace(tibCount uint32) (string, error) if err != nil { return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseDeclarationSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_IncreaseDeclarationSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -865,7 +855,7 @@ func (c *ChainClient) MinerExitPrep() (string, error) { accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return txhash, ERR_RPC_CONNECTION } @@ -891,7 +881,7 @@ func (c *ChainClient) MinerExitPrep() (string, error) { ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerExitPrep, err) - c.SetChainState(false) + c.SetRpcState(false) return txhash, err } @@ -913,7 +903,6 @@ func (c *ChainClient) MinerExitPrep() (string, error) { err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerExitPrep, err) - c.SetChainState(false) return txhash, err } @@ -932,12 +921,12 @@ func (c *ChainClient) MinerExitPrep() (string, error) { sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerExitPrep, err) - c.SetChainState(false) + c.SetRpcState(false) return txhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerExitPrep, err) - c.SetChainState(false) + c.SetRpcState(false) return txhash, err } } @@ -985,7 +974,7 @@ func (c *ChainClient) MinerWithdraw() (string, error) { accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return txhash, ERR_RPC_CONNECTION } @@ -1006,7 +995,7 @@ func (c *ChainClient) MinerWithdraw() (string, error) { ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerWithdraw, err) - c.SetChainState(false) + c.SetRpcState(false) return txhash, err } if !ok { @@ -1045,12 +1034,12 @@ func (c *ChainClient) MinerWithdraw() (string, error) { sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerWithdraw, err) - c.SetChainState(false) + c.SetRpcState(false) return txhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_MinerWithdraw, err) - c.SetChainState(false) + c.SetRpcState(false) return txhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]") } } @@ -1098,7 +1087,7 @@ func (c *ChainClient) ReceiveReward() (string, error) { accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -1119,7 +1108,7 @@ func (c *ChainClient) ReceiveReward() (string, error) { ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_ReceiveReward, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -1157,12 +1146,12 @@ func (c *ChainClient) ReceiveReward() (string, error) { sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_ReceiveReward, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_ReceiveReward, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -1215,7 +1204,7 @@ func (c *ChainClient) RegisterPoisKey(poisKey PoISKeyInfo, teeSignWithAcc, teeSi accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -1236,7 +1225,7 @@ func (c *ChainClient) RegisterPoisKey(poisKey PoISKeyInfo, teeSignWithAcc, teeSi ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegisterPoisKey, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } @@ -1281,12 +1270,12 @@ func (c *ChainClient) RegisterPoisKey(poisKey PoISKeyInfo, teeSignWithAcc, teeSi sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegisterPoisKey, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegisterPoisKey, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -1336,7 +1325,7 @@ func (c *ChainClient) RegnstkSminer(earnings string, peerId []byte, staking uint accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -1378,7 +1367,7 @@ func (c *ChainClient) RegnstkSminer(earnings string, peerId []byte, staking uint ok, err = c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_Regnstk, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } @@ -1405,7 +1394,6 @@ func (c *ChainClient) RegnstkSminer(earnings string, peerId []byte, staking uint err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Sminer_Regnstk, err) - c.SetChainState(false) return blockhash, err } @@ -1420,15 +1408,16 @@ func (c *ChainClient) RegnstkSminer(earnings string, peerId []byte, staking uint if err != nil { return blockhash, errors.Wrap(err, "[Sign]") } + <-c.txTicker.C sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_Regnstk, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_Regnstk, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -1479,7 +1468,7 @@ func (c *ChainClient) RegnstkAssignStaking(earnings string, peerId []byte, staki accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -1532,7 +1521,7 @@ func (c *ChainClient) RegnstkAssignStaking(earnings string, peerId []byte, staki ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegnstkAssignStaking, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } @@ -1559,7 +1548,6 @@ func (c *ChainClient) RegnstkAssignStaking(earnings string, peerId []byte, staki err = ext.Sign(c.keyring, o) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] Sign: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegnstkAssignStaking, err) - c.SetChainState(false) return blockhash, err } @@ -1578,12 +1566,12 @@ func (c *ChainClient) RegnstkAssignStaking(earnings string, peerId []byte, staki sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegnstkAssignStaking, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_RegnstkAssignStaking, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -1629,7 +1617,7 @@ func (c *ChainClient) UpdateBeneficiary(earnings string) (string, error) { accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -1660,7 +1648,7 @@ func (c *ChainClient) UpdateBeneficiary(earnings string) (string, error) { ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdateBeneficiary, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -1699,12 +1687,12 @@ func (c *ChainClient) UpdateBeneficiary(earnings string) (string, error) { sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdateBeneficiary, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdateBeneficiary, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -1750,7 +1738,7 @@ func (c *ChainClient) UpdateSminerPeerId(peerid PeerId) (string, error) { accountInfo types.AccountInfo ) - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -1771,7 +1759,7 @@ func (c *ChainClient) UpdateSminerPeerId(peerid PeerId) (string, error) { ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdatePeerId, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -1810,12 +1798,12 @@ func (c *ChainClient) UpdateSminerPeerId(peerid PeerId) (string, error) { sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdatePeerId, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_Sminer_UpdatePeerId, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } diff --git a/chain/staking.go b/chain/staking.go index 24cb9ee..ca0639b 100755 --- a/chain/staking.go +++ b/chain/staking.go @@ -32,7 +32,7 @@ func (c *ChainClient) QueryCounterForValidators(block int) (uint32, error) { var data types.U32 - if !c.GetChainState() { + if !c.GetRpcState() { return uint32(data), ERR_RPC_CONNECTION } @@ -44,7 +44,7 @@ func (c *ChainClient) QueryCounterForValidators(block int) (uint32, error) { if block < 0 { ok, err := c.api.RPC.State.GetStorageLatest(key, &data) if err != nil { - c.SetChainState(false) + c.SetRpcState(false) return uint32(data), errors.Wrap(err, "[GetStorageLatest]") } if !ok { @@ -56,13 +56,12 @@ func (c *ChainClient) QueryCounterForValidators(block int) (uint32, error) { blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Staking, CounterForValidators, err) - c.SetChainState(false) return uint32(data), err } ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - c.SetChainState(false) + c.SetRpcState(false) return uint32(data), errors.Wrap(err, "[GetStorage]") } if !ok { @@ -86,7 +85,7 @@ func (c *ChainClient) QueryValidatorsCount(block int) (uint32, error) { var data types.U32 - if !c.GetChainState() { + if !c.GetRpcState() { return uint32(data), ERR_RPC_CONNECTION } @@ -98,7 +97,7 @@ func (c *ChainClient) QueryValidatorsCount(block int) (uint32, error) { if block < 0 { ok, err := c.api.RPC.State.GetStorageLatest(key, &data) if err != nil { - c.SetChainState(false) + c.SetRpcState(false) return uint32(data), errors.Wrap(err, "[GetStorageLatest]") } if !ok { @@ -110,13 +109,12 @@ func (c *ChainClient) QueryValidatorsCount(block int) (uint32, error) { blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Staking, ValidatorCount, err) - c.SetChainState(false) return uint32(data), err } ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - c.SetChainState(false) + c.SetRpcState(false) return uint32(data), errors.Wrap(err, "[GetStorage]") } if !ok { @@ -140,7 +138,7 @@ func (c *ChainClient) QueryNominatorCount(block int) (uint32, error) { var data types.U32 - if !c.GetChainState() { + if !c.GetRpcState() { return uint32(data), ERR_RPC_CONNECTION } @@ -152,7 +150,7 @@ func (c *ChainClient) QueryNominatorCount(block int) (uint32, error) { if block < 0 { ok, err := c.api.RPC.State.GetStorageLatest(key, &data) if err != nil { - c.SetChainState(false) + c.SetRpcState(false) return uint32(data), errors.Wrap(err, "[GetStorageLatest]") } if !ok { @@ -164,13 +162,12 @@ func (c *ChainClient) QueryNominatorCount(block int) (uint32, error) { blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Staking, CounterForNominators, err) - c.SetChainState(false) return uint32(data), err } ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - c.SetChainState(false) + c.SetRpcState(false) return uint32(data), errors.Wrap(err, "[GetStorage]") } if !ok { @@ -195,7 +192,7 @@ func (c *ChainClient) QueryErasTotalStake(era uint32, block int) (string, error) var data types.U128 - if !c.GetChainState() { + if !c.GetRpcState() { return "", ERR_RPC_CONNECTION } @@ -214,7 +211,7 @@ func (c *ChainClient) QueryErasTotalStake(era uint32, block int) (string, error) 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(), Staking, ErasTotalStake, err) - c.SetChainState(false) + c.SetRpcState(false) return "", err } if !ok { @@ -225,13 +222,12 @@ func (c *ChainClient) QueryErasTotalStake(era uint32, block int) (string, error) blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Staking, ErasTotalStake, err) - c.SetChainState(false) return data.String(), err } 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(), Staking, ErasTotalStake, err) - c.SetChainState(false) + c.SetRpcState(false) return "", err } if !ok { @@ -255,7 +251,7 @@ func (c *ChainClient) QueryCurrentEra(block int) (uint32, error) { var data types.U32 - if !c.GetChainState() { + if !c.GetRpcState() { return 0, ERR_RPC_CONNECTION } @@ -269,7 +265,7 @@ func (c *ChainClient) QueryCurrentEra(block int) (uint32, error) { 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(), Staking, CurrentEra, err) - c.SetChainState(false) + c.SetRpcState(false) return 0, err } if !ok { @@ -280,13 +276,12 @@ func (c *ChainClient) QueryCurrentEra(block int) (uint32, error) { blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Staking, CurrentEra, err) - c.SetChainState(false) return uint32(data), err } ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Staking, CurrentEra, err) - c.SetChainState(false) + c.SetRpcState(false) return 0, err } if !ok { @@ -310,7 +305,7 @@ func (c *ChainClient) QueryErasRewardPoints(era uint32, block int32) (StakingEra }() var result StakingEraRewardPoints - if !c.GetChainState() { + if !c.GetRpcState() { return result, ERR_RPC_CONNECTION } @@ -327,7 +322,7 @@ func (c *ChainClient) QueryErasRewardPoints(era uint32, block int32) (StakingEra ok, err := c.api.RPC.State.GetStorageLatest(key, &result) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Staking, ErasRewardPoints, err) - c.SetChainState(false) + c.SetRpcState(false) return result, err } if !ok { @@ -337,13 +332,12 @@ func (c *ChainClient) QueryErasRewardPoints(era uint32, block int32) (StakingEra } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return result, err } ok, err := c.api.RPC.State.GetStorage(key, &result, blockhash) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), Staking, ErasRewardPoints, err) - c.SetChainState(false) + c.SetRpcState(false) return result, err } if !ok { @@ -367,7 +361,7 @@ func (c *ChainClient) QueryAllNominators(block int32) ([]StakingNominations, err var result []StakingNominations - if !c.GetChainState() { + if !c.GetRpcState() { return nil, ERR_RPC_CONNECTION } @@ -375,7 +369,7 @@ func (c *ChainClient) QueryAllNominators(block int32) ([]StakingNominations, err keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), Staking, Nominators, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } var set []types.StorageChangeSet @@ -383,19 +377,18 @@ func (c *ChainClient) QueryAllNominators(block int32) ([]StakingNominations, err set, err = c.api.RPC.State.QueryStorageAtLatest(keys) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Staking, Nominators, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } } else { blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return nil, err } set, err = c.api.RPC.State.QueryStorageAt(keys, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Staking, Nominators, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAt: %v", c.GetCurrentRpcAddr(), Staking, Nominators, err) + c.SetRpcState(false) return nil, err } } @@ -430,7 +423,7 @@ func (c *ChainClient) QueryAllBonded(block int32) ([]types.AccountID, error) { var result []types.AccountID - if !c.GetChainState() { + if !c.GetRpcState() { return nil, ERR_RPC_CONNECTION } @@ -438,7 +431,7 @@ func (c *ChainClient) QueryAllBonded(block int32) ([]types.AccountID, error) { keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), Staking, Bonded, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } @@ -447,21 +440,20 @@ func (c *ChainClient) QueryAllBonded(block int32) ([]types.AccountID, error) { set, err = c.api.RPC.State.QueryStorageAtLatest(keys) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Staking, Bonded, err) - c.SetChainState(false) + c.SetRpcState(false) return nil, err } } else { blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Staking, Bonded, err) - c.SetChainState(false) return result, err } set, err = c.api.RPC.State.QueryStorageAt(keys, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), Staking, Bonded, err) - c.SetChainState(false) + err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAt: %v", c.GetCurrentRpcAddr(), Staking, Bonded, err) + c.SetRpcState(false) return nil, err } } @@ -497,7 +489,7 @@ func (c *ChainClient) QueryValidatorCommission(accountID []byte, block int) (uin var result StakingValidatorPrefs - if !c.GetChainState() { + if !c.GetRpcState() { return 0, ERR_RPC_CONNECTION } @@ -519,7 +511,6 @@ func (c *ChainClient) QueryValidatorCommission(accountID []byte, block int) (uin blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), Staking, Validators, err) - c.SetChainState(false) return 0, err } ok, err := c.api.RPC.State.GetStorage(key, &result, blockhash) @@ -548,7 +539,7 @@ func (c *ChainClient) QueryEraValidatorReward(era uint32, block int) (string, er var result types.U128 - if !c.GetChainState() { + if !c.GetRpcState() { return "", ERR_RPC_CONNECTION } @@ -574,7 +565,6 @@ func (c *ChainClient) QueryEraValidatorReward(era uint32, block int) (string, er } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return "", err } ok, err := c.api.RPC.State.GetStorage(key, &result, blockhash) @@ -595,7 +585,7 @@ func (c *ChainClient) QueryEraValidatorReward(era uint32, block int) (string, er // }() // var result []StakingExposure -// if !c.GetChainState() { +// if !c.GetRpcState() { // return result, ERR_RPC_CONNECTION // } @@ -660,7 +650,7 @@ func (c *ChainClient) QueryEraValidatorReward(era uint32, block int) (string, er // ok, err := c.api.RPC.State.GetStorageLatest(kkey, &result1) // if err != nil { // err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), Staking, ErasStakers, err) -// c.SetChainState(false) +// c.SetRpcState(false) // return result, err // } // fmt.Println(result1) diff --git a/chain/storage_handler.go b/chain/storage_handler.go index 8fef1ac..17044b6 100755 --- a/chain/storage_handler.go +++ b/chain/storage_handler.go @@ -34,14 +34,14 @@ func (c *ChainClient) QueryUnitPrice(block int32) (string, error) { var data types.U128 - if !c.GetChainState() { + if !c.GetRpcState() { return "", ERR_RPC_CONNECTION } key, err := types.CreateStorageKey(c.metadata, StorageHandler, UnitPrice) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), StorageHandler, UnitPrice, err) - c.SetChainState(false) + c.SetRpcState(false) return "", err } @@ -49,7 +49,7 @@ func (c *ChainClient) QueryUnitPrice(block int32) (string, error) { 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, UnitPrice, err) - c.SetChainState(false) + c.SetRpcState(false) return "", err } if !ok { @@ -60,13 +60,12 @@ func (c *ChainClient) QueryUnitPrice(block int32) (string, error) { } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return "", err } 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, UnitPrice, err) - c.SetChainState(false) + c.SetRpcState(false) return "", err } if !ok { @@ -92,7 +91,7 @@ func (c *ChainClient) QueryUserOwnedSpace(accountID []byte, block int32) (UserSp var data UserSpaceInfo - if !c.GetChainState() { + if !c.GetRpcState() { return data, ERR_RPC_CONNECTION } @@ -116,7 +115,7 @@ func (c *ChainClient) QueryUserOwnedSpace(accountID []byte, block int32) (UserSp 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, UserOwnedSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -126,13 +125,12 @@ func (c *ChainClient) QueryUserOwnedSpace(accountID []byte, block int32) (UserSp } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return data, err } 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, UserOwnedSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return data, err } if !ok { @@ -156,14 +154,14 @@ func (c *ChainClient) QueryTotalIdleSpace(block int32) (uint64, error) { var data types.U128 - if !c.GetChainState() { + if !c.GetRpcState() { return 0, ERR_RPC_CONNECTION } key, err := types.CreateStorageKey(c.metadata, StorageHandler, TotalIdleSpace) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), StorageHandler, TotalIdleSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return 0, err } @@ -171,7 +169,7 @@ func (c *ChainClient) QueryTotalIdleSpace(block int32) (uint64, error) { 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, TotalIdleSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return 0, err } if !ok { @@ -181,13 +179,12 @@ func (c *ChainClient) QueryTotalIdleSpace(block int32) (uint64, error) { } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return 0, err } 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, TotalIdleSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return 0, err } if !ok { @@ -211,14 +208,13 @@ func (c *ChainClient) QueryTotalServiceSpace(block int32) (uint64, error) { var data types.U128 - if !c.GetChainState() { + if !c.GetRpcState() { return 0, ERR_RPC_CONNECTION } key, err := types.CreateStorageKey(c.metadata, StorageHandler, TotalServiceSpace) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] CreateStorageKey: %v", c.GetCurrentRpcAddr(), StorageHandler, TotalServiceSpace, err) - c.SetChainState(false) return 0, err } @@ -226,7 +222,7 @@ func (c *ChainClient) QueryTotalServiceSpace(block int32) (uint64, error) { 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, TotalServiceSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return 0, err } if !ok { @@ -236,13 +232,12 @@ func (c *ChainClient) QueryTotalServiceSpace(block int32) (uint64, error) { } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return 0, err } 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, TotalServiceSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return 0, err } if !ok { @@ -266,7 +261,7 @@ func (c *ChainClient) QueryPurchasedSpace(block int32) (uint64, error) { var data types.U128 - if !c.GetChainState() { + if !c.GetRpcState() { return 0, ERR_RPC_CONNECTION } @@ -280,7 +275,7 @@ func (c *ChainClient) QueryPurchasedSpace(block int32) (uint64, error) { 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, PurchasedSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return 0, err } if !ok { @@ -290,13 +285,12 @@ func (c *ChainClient) QueryPurchasedSpace(block int32) (uint64, error) { } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return 0, err } 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, PurchasedSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return 0, err } if !ok { @@ -333,7 +327,7 @@ func (c *ChainClient) BuySpace(count uint32) (string, error) { return "", errors.New("[BuySpace] invalid count") } - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -354,7 +348,7 @@ func (c *ChainClient) BuySpace(count uint32) (string, error) { ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_BuySpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -393,12 +387,12 @@ func (c *ChainClient) BuySpace(count uint32) (string, error) { sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_BuySpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_BuySpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -447,7 +441,7 @@ func (c *ChainClient) ExpansionSpace(count uint32) (string, error) { return "", errors.New("[ExpansionSpace] space is 0") } - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -468,7 +462,7 @@ func (c *ChainClient) ExpansionSpace(count uint32) (string, error) { ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_ExpansionSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -507,12 +501,12 @@ func (c *ChainClient) ExpansionSpace(count uint32) (string, error) { sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_ExpansionSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_ExpansionSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } @@ -561,7 +555,7 @@ func (c *ChainClient) RenewalSpace(days uint32) (string, error) { return "", errors.New("[RenewalSpace] invalid days") } - if !c.GetChainState() { + if !c.GetRpcState() { return blockhash, ERR_RPC_CONNECTION } @@ -582,7 +576,7 @@ func (c *ChainClient) RenewalSpace(days uint32) (string, error) { ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_RenewalSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } if !ok { @@ -621,12 +615,12 @@ func (c *ChainClient) RenewalSpace(days uint32) (string, error) { sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_RenewalSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } else { err = fmt.Errorf("rpc err: [%s] [tx] [%s] SubmitAndWatchExtrinsic: %v", c.GetCurrentRpcAddr(), TX_StorageHandler_RenewalSpace, err) - c.SetChainState(false) + c.SetRpcState(false) return blockhash, err } } diff --git a/chain/system.go b/chain/system.go index 396ff4d..f3a830c 100755 --- a/chain/system.go +++ b/chain/system.go @@ -8,45 +8,30 @@ package chain import ( - "fmt" "log" - "github.com/CESSProject/cess-go-sdk/core/pattern" "github.com/CESSProject/cess-go-sdk/utils" "github.com/centrifuge/go-substrate-rpc-client/v4/types" "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" "github.com/pkg/errors" ) -// QueryNodeSynchronizationSt -func (c *ChainClient) QueryNodeSynchronizationSt() (bool, error) { +// QueryBlockNumber query the block number based on the block hash +// - blockhash: hex-encoded block hash, if empty query the latest block number +// +// Return: +// - uint32: block number +// - error: error message +func (c *ChainClient) QueryBlockNumber(blockhash string) (uint32, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) } }() - if !c.GetChainState() { - return false, pattern.ERR_RPC_CONNECTION - } - h, err := c.api.RPC.System.Health() - if err != nil { - return false, err - } - return h.IsSyncing, nil -} - -// QueryBlockHeight -func (c *ChainClient) QueryBlockHeight(hash string) (uint32, error) { - defer func() { - if err := recover(); err != nil { - log.Println(utils.RecoverError(err)) - } - }() - - if hash != "" { + if blockhash != "" { var h types.Hash - err := codec.DecodeFromHex(hash, &h) + err := codec.DecodeFromHex(blockhash, &h) if err != nil { return 0, err } @@ -64,17 +49,29 @@ func (c *ChainClient) QueryBlockHeight(hash string) (uint32, error) { return uint32(block.Block.Header.Number), nil } -// QueryAccountInfo -func (c *ChainClient) QueryAccountInfoByAccount(acc string) (types.AccountInfo, error) { - puk, err := utils.ParsingPublickey(acc) +// QueryAccountInfo query account info +// - account: account +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - types.AccountInfo: account info +// - error: error message +func (c *ChainClient) QueryAccountInfo(account string, block int32) (types.AccountInfo, error) { + puk, err := utils.ParsingPublickey(account) if err != nil { return types.AccountInfo{}, err } - return c.QueryAccountInfo(puk) + return c.QueryAccountInfoByAccountID(puk, block) } -// QueryAccountInfo -func (c *ChainClient) QueryAccountInfo(puk []byte) (types.AccountInfo, error) { +// QueryAccountInfoByAccountID query account info +// - accountID: account id +// - block: block number, less than 0 indicates the latest block +// +// Return: +// - types.AccountInfo: account info +// - error: error message +func (c *ChainClient) QueryAccountInfoByAccountID(accountID []byte, block int32) (types.AccountInfo, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -82,11 +79,11 @@ func (c *ChainClient) QueryAccountInfo(puk []byte) (types.AccountInfo, error) { }() var data types.AccountInfo - if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION + if !c.GetRpcState() { + return data, ERR_RPC_CONNECTION } - acc, err := types.NewAccountID(puk) + acc, err := types.NewAccountID(accountID) if err != nil { return data, errors.Wrap(err, "[NewAccountID]") } @@ -96,177 +93,31 @@ func (c *ChainClient) QueryAccountInfo(puk []byte) (types.AccountInfo, error) { return data, errors.Wrap(err, "[EncodeToBytes]") } - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, b) + key, err := types.CreateStorageKey(c.metadata, System, Account, b) if err != nil { return data, errors.Wrap(err, "[CreateStorageKey]") } - ok, err := c.api.RPC.State.GetStorageLatest(key, &data) - if err != nil { - return data, errors.Wrap(err, "[GetStorageLatest]") - } - if !ok { - return data, pattern.ERR_RPC_EMPTY_VALUE - } - return data, nil -} - -// QueryAllAccountInfoFromBlock -func (c *ChainClient) QueryAllAccountInfoFromBlock(block int) ([]types.AccountInfo, error) { - defer func() { - if err := recover(); err != nil { - log.Println(utils.RecoverError(err)) - } - }() - var data []types.AccountInfo - - if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION - } - - key := createPrefixedKey(pattern.SYSTEM, pattern.ACCOUNT) - if block < 0 { - keys, err := c.api.RPC.State.GetKeysLatest(key) + ok, err := c.api.RPC.State.GetStorageLatest(key, &data) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), pattern.SYSTEM, pattern.ACCOUNT, err) - c.SetChainState(false) - return nil, err + return data, errors.Wrap(err, "[GetStorageLatest]") } - set, err := c.api.RPC.State.QueryStorageAtLatest(keys) - if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), pattern.SYSTEM, pattern.ACCOUNT, err) - c.SetChainState(false) - return nil, err - } - for _, elem := range set { - for _, change := range elem.Changes { - var val types.AccountInfo - if err := codec.Decode(change.StorageData, &val); err != nil { - fmt.Println("Decode StorageData:", err) - continue - } - var kkey types.AccountID - if err := codec.Decode(change.StorageKey, &kkey); err != nil { - fmt.Println("Decode StorageKey:", err) - continue - } - fmt.Println(utils.EncodePublicKeyAsCessAccount(kkey[:])) - data = append(data, val) - } + if !ok { + return data, ERR_RPC_EMPTY_VALUE } return data, nil } - blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), pattern.SYSTEM, pattern.ACCOUNT, err) - c.SetChainState(false) return data, err } - - fmt.Println(">>>>>") - keys, err := c.api.RPC.State.GetKeys(key, blockhash) - if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeys: %v", c.GetCurrentRpcAddr(), pattern.SYSTEM, pattern.ACCOUNT, err) - c.SetChainState(false) - return nil, err - } - set, err := c.api.RPC.State.QueryStorageAt(keys, blockhash) + ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAt: %v", c.GetCurrentRpcAddr(), pattern.SYSTEM, pattern.ACCOUNT, err) - c.SetChainState(false) - return nil, err + return data, errors.Wrap(err, "[GetStorage]") } - for _, elem := range set { - for _, change := range elem.Changes { - if change.HasStorageData { - var val types.AccountInfo - - var kkey types.AccountID - if err := codec.Decode(change.StorageKey, &kkey); err != nil { - fmt.Println("Decode StorageKey:", err) - continue - } - if err := codec.Decode(change.StorageData, &val); err != nil { - fmt.Println("Decode StorageData:", err) - continue - } - fmt.Println(utils.EncodePublicKeyAsCessAccount(kkey[:])) - data = append(data, val) - } - } + if !ok { + return data, ERR_RPC_EMPTY_VALUE } - return data, nil } - -func (c *ChainClient) SysProperties() (pattern.SysProperties, error) { - defer func() { - if err := recover(); err != nil { - log.Println(utils.RecoverError(err)) - } - }() - var data pattern.SysProperties - if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION - } - err := c.api.Client.Call(&data, pattern.RPC_SYS_Properties) - return data, err -} - -func (c *ChainClient) SysChain() (string, error) { - defer func() { - if err := recover(); err != nil { - log.Println(utils.RecoverError(err)) - } - }() - var data types.Text - if !c.GetChainState() { - return "", pattern.ERR_RPC_CONNECTION - } - err := c.api.Client.Call(&data, pattern.RPC_SYS_Chain) - return string(data), err -} - -func (c *ChainClient) SyncState() (pattern.SysSyncState, error) { - defer func() { - if err := recover(); err != nil { - log.Println(utils.RecoverError(err)) - } - }() - var data pattern.SysSyncState - if !c.GetChainState() { - return data, pattern.ERR_RPC_CONNECTION - } - err := c.api.Client.Call(&data, pattern.RPC_SYS_SyncState) - return data, err -} - -func (c *ChainClient) ChainVersion() (string, error) { - defer func() { - if err := recover(); err != nil { - log.Println(utils.RecoverError(err)) - } - }() - var data types.Text - if !c.GetChainState() { - return "", pattern.ERR_RPC_CONNECTION - } - err := c.api.Client.Call(&data, pattern.RPC_SYS_Version) - return string(data), err -} - -func (c *ChainClient) NetListening() (bool, error) { - defer func() { - if err := recover(); err != nil { - log.Println(utils.RecoverError(err)) - } - }() - var data types.Bool - if !c.GetChainState() { - return false, pattern.ERR_RPC_CONNECTION - } - err := c.api.Client.Call(&data, pattern.RPC_NET_Listening) - return bool(data), err -} diff --git a/core/sdk/sdk.go b/core/sdk/sdk.go index 2799373..ad6a676 100755 --- a/core/sdk/sdk.go +++ b/core/sdk/sdk.go @@ -307,8 +307,6 @@ type SDK interface { // ReconnectRPC() error // // GetMetadata returns the metadata of the chain. // GetMetadata() *types.Metadata - // // GetKeyEvents returns the events storage key. - // GetKeyEvents() types.StorageKey // // GetURI returns URI. // GetURI() string // // GetTreasuryAccount returns the treasury account diff --git a/defaults.go b/defaults.go index e19cc48..2f474d9 100755 --- a/defaults.go +++ b/defaults.go @@ -7,7 +7,9 @@ package sdkgo -import "github.com/CESSProject/cess-go-sdk/chain" +import ( + "time" +) // DefaultRpcAddrs configures the default rpc address var DefaultRpcAddrs = func(cfg *Config) error { @@ -21,7 +23,7 @@ var DefaultRpcAddrs = func(cfg *Config) error { // DefaultTimeout configures the default transaction waiting timeout var DefaultTimeout = func(cfg *Config) error { - return cfg.Apply(TransactionTimeout(chain.BlockInterval)) + return cfg.Apply(TransactionTimeout(time.Second * 30)) } // Complete list of default options and when to fallback on them. From 30e5deef51aea2efa0349d73c4d712dc62442635 Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Fri, 10 May 2024 16:29:59 +0800 Subject: [PATCH 13/18] update tee,treasury --- chain/tee.go | 35 +++++++++++++++-------------------- chain/treasury.go | 28 ++++++++++++---------------- example/new/sdk.go | 2 +- 3 files changed, 28 insertions(+), 37 deletions(-) diff --git a/chain/tee.go b/chain/tee.go index 9a1fcec..e5b7ac5 100755 --- a/chain/tee.go +++ b/chain/tee.go @@ -32,7 +32,7 @@ func (c *ChainClient) QueryMasterPubKey(block int32) ([]byte, error) { var data MasterPublicKey - if !c.GetChainState() { + if !c.GetRpcState() { return nil, ERR_RPC_CONNECTION } @@ -45,7 +45,7 @@ func (c *ChainClient) QueryMasterPubKey(block int32) ([]byte, error) { if block < 0 { ok, err := c.api.RPC.State.GetStorageLatest(key, &data) if err != nil { - c.SetChainState(false) + c.SetRpcState(false) err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TeeWorker, MasterPubkey, err) return nil, err } @@ -56,12 +56,11 @@ func (c *ChainClient) QueryMasterPubKey(block int32) ([]byte, error) { } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return nil, err } ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - c.SetChainState(false) + c.SetRpcState(false) err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), TeeWorker, MasterPubkey, err) return nil, err } @@ -87,7 +86,7 @@ func (c *ChainClient) QueryWorkers(puk WorkerPublicKey, block int32) (WorkerInfo var data WorkerInfo - if !c.GetChainState() { + if !c.GetRpcState() { return data, ERR_RPC_CONNECTION } @@ -105,7 +104,7 @@ func (c *ChainClient) QueryWorkers(puk WorkerPublicKey, block int32) (WorkerInfo if block < 0 { ok, err := c.api.RPC.State.GetStorageLatest(key, &data) if err != nil { - c.SetChainState(false) + c.SetRpcState(false) err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TeeWorker, Workers, err) return data, errors.Wrap(err, "[GetStorageLatest]") } @@ -116,12 +115,11 @@ func (c *ChainClient) QueryWorkers(puk WorkerPublicKey, block int32) (WorkerInfo } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return data, err } ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - c.SetChainState(false) + c.SetRpcState(false) err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TeeWorker, Workers, err) return data, errors.Wrap(err, "[GetStorageLatest]") } @@ -146,7 +144,7 @@ func (c *ChainClient) QueryAllWorkers(block int32) ([]WorkerInfo, error) { var list []WorkerInfo - if !c.GetChainState() { + if !c.GetRpcState() { return list, ERR_RPC_CONNECTION } @@ -160,19 +158,18 @@ func (c *ChainClient) QueryAllWorkers(block int32) ([]WorkerInfo, error) { if block < 0 { set, err = c.api.RPC.State.QueryStorageAtLatest(keys) if err != nil { - c.SetChainState(false) + c.SetRpcState(false) err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAtLatest: %v", c.GetCurrentRpcAddr(), TeeWorker, Workers, err) return list, err } } else { blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return list, err } set, err = c.api.RPC.State.QueryStorageAt(keys, blockhash) if err != nil { - c.SetChainState(false) + c.SetRpcState(false) err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] QueryStorageAt: %v", c.GetCurrentRpcAddr(), TeeWorker, Workers, err) return list, err } @@ -205,7 +202,7 @@ func (c *ChainClient) QueryEndpoints(puk WorkerPublicKey, block int32) (string, var data types.Text - if !c.GetChainState() { + if !c.GetRpcState() { return "", ERR_RPC_CONNECTION } @@ -221,7 +218,7 @@ func (c *ChainClient) QueryEndpoints(puk WorkerPublicKey, block int32) (string, if block < 0 { ok, err := c.api.RPC.State.GetStorageLatest(key, &data) if err != nil { - c.SetChainState(false) + c.SetRpcState(false) err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TeeWorker, Endpoints, err) return "", errors.Wrap(err, "[GetStorageLatest]") } @@ -232,12 +229,11 @@ func (c *ChainClient) QueryEndpoints(puk WorkerPublicKey, block int32) (string, } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return string(data), err } ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - c.SetChainState(false) + c.SetRpcState(false) err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), TeeWorker, Endpoints, err) return "", errors.Wrap(err, "[GetStorage]") } @@ -263,7 +259,7 @@ func (c *ChainClient) QueryWorkerAddedAt(puk WorkerPublicKey, block int32) (uint var data types.U32 - if !c.GetChainState() { + if !c.GetRpcState() { return uint32(data), ERR_RPC_CONNECTION } @@ -279,7 +275,7 @@ func (c *ChainClient) QueryWorkerAddedAt(puk WorkerPublicKey, block int32) (uint if block < 0 { ok, err := c.api.RPC.State.GetStorageLatest(key, &data) if err != nil { - c.SetChainState(false) + c.SetRpcState(false) err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorageLatest: %v", c.GetCurrentRpcAddr(), TeeWorker, WorkerAddedAt, err) return uint32(data), err } @@ -290,12 +286,11 @@ func (c *ChainClient) QueryWorkerAddedAt(puk WorkerPublicKey, block int32) (uint } blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { - c.SetChainState(false) return uint32(data), err } ok, err := c.api.RPC.State.GetStorage(key, &data, blockhash) if err != nil { - c.SetChainState(false) + c.SetRpcState(false) err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetStorage: %v", c.GetCurrentRpcAddr(), TeeWorker, WorkerAddedAt, err) return uint32(data), err } diff --git a/chain/treasury.go b/chain/treasury.go index 39887f5..eb98766 100644 --- a/chain/treasury.go +++ b/chain/treasury.go @@ -31,7 +31,7 @@ func (c *ChainClient) QueryCurrencyReward(block int32) (string, error) { var data types.U128 - if !c.GetChainState() { + if !c.GetRpcState() { return "", ERR_RPC_CONNECTION } @@ -45,7 +45,7 @@ func (c *ChainClient) QueryCurrencyReward(block int32) (string, error) { 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(), CessTreasury, CurrencyReward, err) - c.SetChainState(false) + c.SetRpcState(false) return "", err } if !ok { @@ -60,14 +60,13 @@ func (c *ChainClient) QueryCurrencyReward(block int32) (string, error) { blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), CessTreasury, CurrencyReward, err) - c.SetChainState(false) return "", err } 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(), CessTreasury, CurrencyReward, err) - c.SetChainState(false) + c.SetRpcState(false) return "", err } if !ok { @@ -95,7 +94,7 @@ func (c *ChainClient) QueryEraReward(block int32) (string, error) { var data types.U128 - if !c.GetChainState() { + if !c.GetRpcState() { return "", ERR_RPC_CONNECTION } @@ -109,7 +108,7 @@ func (c *ChainClient) QueryEraReward(block int32) (string, error) { 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(), CessTreasury, EraReward, err) - c.SetChainState(false) + c.SetRpcState(false) return "", err } if !ok { @@ -124,14 +123,13 @@ func (c *ChainClient) QueryEraReward(block int32) (string, error) { blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), CessTreasury, EraReward, err) - c.SetChainState(false) return "", err } 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(), CessTreasury, EraReward, err) - c.SetChainState(false) + c.SetRpcState(false) return "", err } if !ok { @@ -159,7 +157,7 @@ func (c *ChainClient) QueryReserveReward(block int32) (string, error) { var data types.U128 - if !c.GetChainState() { + if !c.GetRpcState() { return "", ERR_RPC_CONNECTION } @@ -173,7 +171,7 @@ func (c *ChainClient) QueryReserveReward(block int32) (string, error) { 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(), CessTreasury, ReserveReward, err) - c.SetChainState(false) + c.SetRpcState(false) return "", err } if !ok { @@ -188,14 +186,13 @@ func (c *ChainClient) QueryReserveReward(block int32) (string, error) { blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), CessTreasury, ReserveReward, err) - c.SetChainState(false) return "", err } 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(), CessTreasury, ReserveReward, err) - c.SetChainState(false) + c.SetRpcState(false) return "", err } if !ok { @@ -224,7 +221,7 @@ func (c *ChainClient) QueryRoundReward(era uint32, block int32) (string, error) var data RoundRewardType - if !c.GetChainState() { + if !c.GetRpcState() { return "", ERR_RPC_CONNECTION } @@ -243,7 +240,7 @@ func (c *ChainClient) QueryRoundReward(era uint32, block int32) (string, error) 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(), CessTreasury, RoundReward, err) - c.SetChainState(false) + c.SetRpcState(false) return "", err } if !ok { @@ -258,14 +255,13 @@ func (c *ChainClient) QueryRoundReward(era uint32, block int32) (string, error) blockhash, err := c.api.RPC.Chain.GetBlockHash(uint64(block)) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetBlockHash: %v", c.GetCurrentRpcAddr(), CessTreasury, RoundReward, err) - c.SetChainState(false) return "", err } 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(), CessTreasury, RoundReward, err) - c.SetChainState(false) + c.SetRpcState(false) return "", err } if !ok { diff --git a/example/new/sdk.go b/example/new/sdk.go index 1e97fec..c973a54 100755 --- a/example/new/sdk.go +++ b/example/new/sdk.go @@ -46,7 +46,7 @@ func main() { } defer sdk.GetSubstrateAPI().Client.Close() - fmt.Println(sdk.ChainVersion()) + fmt.Println(sdk.SystemVersion()) fmt.Println(sdk.InitExtrinsicsName()) fmt.Println(sdk.GetCurrentRpcAddr()) // From f1354bbfd3827a7b140edc60b8c738e78d4d822b Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Sat, 11 May 2024 10:45:05 +0800 Subject: [PATCH 14/18] update sign --- example/new/sdk.go | 9 ++-- go.mod | 2 + go.sum | 9 ++++ utils/sign.go | 132 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 utils/sign.go diff --git a/example/new/sdk.go b/example/new/sdk.go index c973a54..3c8224e 100755 --- a/example/new/sdk.go +++ b/example/new/sdk.go @@ -22,7 +22,8 @@ import ( // Substrate well-known mnemonic: // -// https://github.com/substrate-developer-hub/substrate-developer-hub.github.io/issues/613 +// - cXgaee2N8E77JJv9gdsGAckv1Qsf3hqWYf7NL4q6ZuQzuAUtB +// - 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{ @@ -38,18 +39,18 @@ func main() { sdk, err := cess.New( context.Background(), cess.ConnectRpcAddrs(RPC_ADDRS), - //cess.Mnemonic(MY_MNEMONIC), + cess.Mnemonic(MY_MNEMONIC), cess.TransactionTimeout(time.Second*10), ) if err != nil { panic(err) } - defer sdk.GetSubstrateAPI().Client.Close() + defer sdk.Close() fmt.Println(sdk.SystemVersion()) fmt.Println(sdk.InitExtrinsicsName()) fmt.Println(sdk.GetCurrentRpcAddr()) - // + //fmt.Println(sdk.QueryRoundReward(0, -1)) return diff --git a/go.mod b/go.mod index 7b854c8..88d22b9 100644 --- a/go.mod +++ b/go.mod @@ -26,6 +26,8 @@ require ( require ( github.com/ChainSafe/go-schnorrkel v1.0.0 // indirect + github.com/btcsuite/btcd v0.22.0-beta // indirect + github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect github.com/cosmos/go-bip39 v1.0.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/deckarep/golang-set v1.8.0 // indirect diff --git a/go.sum b/go.sum index 1ad9f28..aa69c1c 100644 --- a/go.sum +++ b/go.sum @@ -5,14 +5,18 @@ github.com/ChainSafe/go-schnorrkel v1.0.0/go.mod h1:dpzHYVxLZcp8pjlV+O+UR8K0Hp/z github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ= github.com/btcsuite/btcd v0.22.0-beta h1:LTDpDKUM5EeOFBPM8IXpinEcmZ6FWfNZbE3lfrfdnWo= +github.com/btcsuite/btcd v0.22.0-beta/go.mod h1:9n5ntfhhHQBIhUvlhDvD3Qg6fRUj4jkN0VB8L8svzOA= github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k= +github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU= github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA= github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce h1:YtWJF7RHm2pYCvA5t0RPmAaLUhREsKuKd+SLhxFbFeQ= github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce/go.mod h1:0DVlHczLPewLcPGEIeUEzfOJhqGPQ0mJJRDBtD307+o= github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg= github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY= +github.com/btcsuite/goleveldb v1.0.0/go.mod h1:QiK9vBlgftBg6rWQIj6wFzbPfRjiykIEhBH4obrXJ/I= github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc= +github.com/btcsuite/snappy-go v1.0.0/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc= github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY= github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= github.com/cbergoon/merkletree v0.2.0 h1:Bttqr3OuoiZEo4ed1L7fTasHka9II+BF9fhBfbNEEoQ= @@ -36,6 +40,7 @@ github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5il github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= +github.com/decred/dcrd/lru v1.0.0/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218= github.com/ethereum/go-ethereum v1.10.20 h1:75IW830ClSS40yrQC1ZCMZCt5I+zU16oqId2SiQwdQ4= github.com/ethereum/go-ethereum v1.10.20/go.mod h1:LWUN82TCHGpxB3En5HVmLLzPD7YSrEUFmFfN1nKkVN0= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= @@ -59,6 +64,7 @@ github.com/gtank/ristretto255 v0.1.2 h1:JEqUCPA1NvLq5DwYtuzigd7ss8fwbYay9fi4/5uM github.com/gtank/ristretto255 v0.1.2/go.mod h1:Ph5OpO6c7xKUGROZfWVLiJf9icMDwUeIvY4OmlYW69o= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= @@ -78,6 +84,7 @@ github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o= github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/pierrec/xxHash v0.1.5 h1:n/jBpwTHiER4xYvK3/CdPVnLDPchj8eTJFFLUb4QHBo= github.com/pierrec/xxHash v0.1.5/go.mod h1:w2waW5Zoa/Wc4Yqe0wgrIYAGKqRMf7czn2HNKXmuL+I= @@ -119,11 +126,13 @@ golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnf golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= golang.org/x/exp v0.0.0-20240213143201-ec583247a57a h1:HinSgX1tJRX3KsL//Gxynpw5CTOAIPhgL4W8PNiIpVE= golang.org/x/exp v0.0.0-20240213143201-ec583247a57a/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc= +golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= diff --git a/utils/sign.go b/utils/sign.go new file mode 100644 index 0000000..746a76d --- /dev/null +++ b/utils/sign.go @@ -0,0 +1,132 @@ +/* + Copyright (C) CESS. All rights reserved. + Copyright (C) Cumulus Encrypted Storage System. All rights reserved. + + SPDX-License-Identifier: Apache-2.0 +*/ + +package utils + +import ( + "encoding/hex" + "errors" + "fmt" + "strconv" + "strings" + + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/crypto" + "github.com/vedhavyas/go-subkey/sr25519" +) + +// SignedSR25519WithMnemonic sign sr25519 with mnemonic +// - mnemonic: polkadot account mnemonic +// - msg: message +// +// Return: +// - []byte: sr25519 signature +// - error: error message +func SignedSR25519WithMnemonic(mnemonic string, msg string) ([]byte, error) { + if len(msg) <= 0 { + return nil, errors.New("SignedSR25519WithMnemonic: empty msg") + } + pri, err := sr25519.Scheme{}.FromPhrase(mnemonic, "") + if err != nil { + return nil, errors.New("SignedSR25519WithMnemonic: invalid mnemonic") + } + return pri.Sign([]byte(msg)) +} + +// VerifySR25519WithPublickey verify sr25519 signature with account public key +// - msg: message +// - sign: sr25519 signature +// - account: polkadot account +// +// Return: +// - bool: verification result +// - error: error message +func VerifySR25519WithPublickey(msg string, sign []byte, account string) (bool, error) { + if len(sign) <= 0 { + return false, errors.New("VerifySR25519WithPublickey: empty sign") + } + pk, err := ParsingPublickey(account) + if err != nil { + return false, errors.New("VerifySR25519WithPublickey: invalid account") + } + public, err := sr25519.Scheme{}.FromPublicKey(pk) + if err != nil { + return false, err + } + ok := public.Verify([]byte(msg), sign) + return ok, err +} + +// VerifyPolkadotJsHexSign verify signature signed with polkadot.js +// - account: polkadot account +// - msg: message +// - sign: signature +// +// Return: +// - bool: verification result +// - error: error message +// +// Tip: +// - https://polkadot.js.org/apps/#/signing +func VerifyPolkadotJsHexSign(account, msg, signature string) (bool, error) { + if len(msg) == 0 { + return false, errors.New("msg is empty") + } + + pkey, err := ParsingPublickey(account) + if err != nil { + return false, err + } + + pub, err := sr25519.Scheme{}.FromPublicKey(pkey) + if err != nil { + return false, err + } + + sign_bytes, err := hex.DecodeString(strings.TrimPrefix(signature, "0x")) + if err != nil { + return false, err + } + message := fmt.Sprintf("%s", msg) + ok := pub.Verify([]byte(message), sign_bytes) + return ok, nil +} + +// ParseEthAccFromEthSign parsing eth account public key from eth account signature +// - message: message +// - sign: eth signature +// +// Return: +// - string: eth account +// - error: error message +func ParseEthAccFromEthSign(message string, sign string) (string, error) { + // Hash the unsigned message using EIP-191 + hashedMessage := []byte("\x19Ethereum Signed Message:\n" + strconv.Itoa(len(message)) + message) + hash := crypto.Keccak256Hash(hashedMessage) + + // Get the bytes of the signed message + decodedMessage, err := hexutil.Decode(sign) + if err != nil { + return "", err + } + + // Handles cases where EIP-115 is not implemented (most wallets don't implement it) + if decodedMessage[64] == 27 || decodedMessage[64] == 28 { + decodedMessage[64] -= 27 + } + + // Recover a public key from the signed message + sigPublicKeyECDSA, err := crypto.SigToPub(hash.Bytes(), decodedMessage) + if sigPublicKeyECDSA == nil { + err = errors.New("could not get a public get from the message signature") + } + if err != nil { + return "", err + } + + return crypto.PubkeyToAddress(*sigPublicKeyECDSA).String(), nil +} From dc383d318a6416ba725fe6501e91823759641682 Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Sat, 11 May 2024 11:11:42 +0800 Subject: [PATCH 15/18] update file --- chain/file.go | 418 ------------------------------------- core/process/file.go | 336 +++++++++++++++++++++++++++++ core/process/file_chunk.go | 224 ++++++++++++++++++++ 3 files changed, 560 insertions(+), 418 deletions(-) delete mode 100755 chain/file.go create mode 100644 core/process/file.go create mode 100644 core/process/file_chunk.go diff --git a/chain/file.go b/chain/file.go deleted file mode 100755 index a934d9a..0000000 --- a/chain/file.go +++ /dev/null @@ -1,418 +0,0 @@ -/* - Copyright (C) CESS. All rights reserved. - Copyright (C) Cumulus Encrypted Storage System. All rights reserved. - - SPDX-License-Identifier: Apache-2.0 -*/ - -package chain - -import ( - "bufio" - "bytes" - "fmt" - "io" - "mime/multipart" - "net/http" - "os" - "path/filepath" - "strings" - - "github.com/CESSProject/cess-go-sdk/config" - "github.com/CESSProject/cess-go-sdk/utils" - keyring "github.com/CESSProject/go-keyring" - "github.com/btcsuite/btcutil/base58" - "github.com/pkg/errors" -) - -var globalTransport = &http.Transport{ - DisableKeepAlives: true, -} - -func (c *ChainClient) StoreFile(url, file, bucket string) (string, error) { - fstat, err := os.Stat(file) - if err != nil { - - return "", err - } - - if fstat.IsDir() { - return "", errors.New("not a file") - } - - if fstat.Size() == 0 { - return "", errors.New("empty file") - } - - if !utils.CheckBucketName(bucket) { - return "", errors.New("invalid bucket name") - } - - kr, _ := keyring.FromURI(c.GetURI(), keyring.NetSubstrate{}) - - // sign message - message := utils.GetRandomcode(16) - sig, _ := kr.Sign(kr.SigningContext([]byte(message))) - - body := new(bytes.Buffer) - writer := multipart.NewWriter(body) - // - formFile, err := writer.CreateFormFile("file", fstat.Name()) - if err != nil { - return "", err - } - - f, err := os.Open(file) - if err != nil { - return "", err - } - defer f.Close() - - _, err = io.Copy(formFile, f) - if err != nil { - return "", err - } - err = writer.Close() - if err != nil { - return "", err - } - - req, err := http.NewRequest(http.MethodPut, url, body) - if err != nil { - return "", err - } - - req.Header.Set("BucketName", bucket) - req.Header.Set("Account", c.GetSignatureAcc()) - req.Header.Set("Message", message) - req.Header.Set("Signature", base58.Encode(sig[:])) - req.Header.Set("Content-Type", writer.FormDataContentType()) - - client := &http.Client{} - client.Transport = globalTransport - resp, err := client.Do(req) - if err != nil { - return "", err - } - defer resp.Body.Close() - - respbody, err := io.ReadAll(resp.Body) - if err != nil { - return "", err - } - - if resp.StatusCode != http.StatusOK { - if len(respbody) > 0 { - return "", errors.New(string(respbody)) - } - return "", errors.New(fmt.Sprintf("upload failed, code: %d", resp.StatusCode)) - } - - return strings.TrimPrefix(strings.TrimSuffix(string(respbody), "\""), "\""), nil -} - -func (c *ChainClient) StoreObject(url string, reader io.Reader, bucket string) (string, error) { - if !utils.CheckBucketName(bucket) { - return "", errors.New("invalid bucket name") - } - - kr, _ := keyring.FromURI(c.GetURI(), keyring.NetSubstrate{}) - - // sign message - message := utils.GetRandomcode(16) - sig, _ := kr.Sign(kr.SigningContext([]byte(message))) - - req, err := http.NewRequest(http.MethodPut, url, reader) - if err != nil { - return "", err - } - - req.Header.Set("BucketName", bucket) - req.Header.Set("Account", c.GetSignatureAcc()) - req.Header.Set("Message", message) - req.Header.Set("Signature", base58.Encode(sig[:])) - req.Header.Set("Content-Type", "application/json") - - client := &http.Client{} - client.Transport = globalTransport - resp, err := client.Do(req) - if err != nil { - return "", err - } - defer resp.Body.Close() - - respbody, err := io.ReadAll(resp.Body) - if err != nil { - return "", err - } - - if resp.StatusCode != http.StatusOK { - if len(respbody) > 0 { - return "", errors.New(string(respbody)) - } - return "", errors.New(fmt.Sprintf("upload failed, code: %d", resp.StatusCode)) - } - - return strings.TrimPrefix(strings.TrimSuffix(string(respbody), "\""), "\""), nil -} - -func (c *ChainClient) RetrieveFile(url, fid, savepath string) error { - fstat, err := os.Stat(savepath) - if err == nil { - if fstat.IsDir() { - savepath = filepath.Join(savepath, fid) - } - if fstat.Size() > 0 { - return nil - } - } - - if url == "" { - return errors.New("empty url") - } - - if url[len(url)-1] != byte(47) { - url += "/" - } - - f, err := os.Create(savepath) - if err != nil { - return err - } - defer f.Close() - - req, err := http.NewRequest(http.MethodGet, url+fid, nil) - if err != nil { - return err - } - - kr, _ := keyring.FromURI(c.GetURI(), keyring.NetSubstrate{}) - - // sign message - message := utils.GetRandomcode(16) - sig, _ := kr.Sign(kr.SigningContext([]byte(message))) - req.Header.Set("Message", message) - req.Header.Set("Signature", base58.Encode(sig[:])) - req.Header.Set("Content-Type", "application/json") - req.Header.Set("Operation", "download") - req.Header.Set("Account", c.GetSignatureAcc()) - - client := &http.Client{} - client.Transport = globalTransport - resp, err := client.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - return errors.New("failed") - } - - _, err = io.Copy(f, resp.Body) - if err != nil { - return err - } - - return nil -} - -func (c *ChainClient) RetrieveObject(url, fid string) (io.ReadCloser, error) { - if url == "" { - return nil, errors.New("empty url") - } - - if url[len(url)-1] != byte(47) { - url += "/" - } - - req, err := http.NewRequest(http.MethodGet, url+fid, nil) - if err != nil { - return nil, err - } - kr, _ := keyring.FromURI(c.GetURI(), keyring.NetSubstrate{}) - - // sign message - message := utils.GetRandomcode(16) - sig, _ := kr.Sign(kr.SigningContext([]byte(message))) - req.Header.Set("Message", message) - req.Header.Set("Signature", base58.Encode(sig[:])) - req.Header.Set("Content-Type", "application/json") - req.Header.Set("Account", c.GetSignatureAcc()) - req.Header.Set("Operation", "download") - - client := &http.Client{} - client.Transport = globalTransport - resp, err := client.Do(req) - if err != nil { - return nil, err - } - - if resp.StatusCode != http.StatusOK { - return nil, errors.New(fmt.Sprintf("Retrieve failed, code: %d", resp.StatusCode)) - } - - return resp.Body, nil -} - -func (c *ChainClient) SplitFile(fpath, chunksDir string, chunkSize int64, filling bool) (int64, int, error) { - fstat, err := os.Stat(fpath) - if err != nil { - return 0, 0, err - } - if fstat.IsDir() { - return 0, 0, errors.New("not a file") - } - if fstat.Size() == 0 { - return 0, 0, errors.New("empty file") - } - if fstat.Size() < chunkSize { - chunkSize = fstat.Size() - } - count := fstat.Size() / chunkSize - if fstat.Size()%chunkSize != 0 { - count++ - } - buf := make([]byte, chunkSize) - f, err := os.Open(fpath) - if err != nil { - return 0, 0, err - } - defer f.Close() - reader := bufio.NewReader(f) - size := fstat.Size() - for i := int64(0); i < count; i++ { - n, err := reader.Read(buf) - if err != nil { - return 0, 0, err - } - if n <= 0 { - return 0, 0, errors.New("read a empty block") - } - if n < int(chunkSize) && filling { - if i+1 != count { - return 0, 0, errors.New("read file err") - } - copy(buf[n:], make([]byte, chunkSize-int64(n))) - size += chunkSize - int64(n) - n = int(chunkSize) - } - err = utils.WriteBufToFile(buf[:n], filepath.Join(chunksDir, fmt.Sprintf("chunk-%d", i))) - if err != nil { - return 0, 0, err - } - } - return size, int(count), nil -} - -func (c *ChainClient) SplitFileWithstandardSize(fpath, chunksDir string) (int64, int, error) { - return c.SplitFile(fpath, chunksDir, config.SegmentSize, true) -} - -func (c *ChainClient) UploadFileChunks(url, chunksDir, bucket, fname string, chunksNum int, totalSize int64) (string, error) { - entries, err := os.ReadDir(chunksDir) - if err != nil { - return "", errors.Wrap(err, "upload file chunk error") - } - if len(entries) == 0 { - return "", errors.Wrap(errors.New("empty dir"), "upload file chunk error") - } - if len(entries) > chunksNum { - return "", errors.Wrap(errors.New("bad chunks number"), "upload file chunk error") - } - var res string - for i := chunksNum - len(entries); i < chunksNum; i++ { - res, err = c.UploadFileChunk(url, chunksDir, bucket, fname, chunksNum, i, totalSize) - if err != nil { - return res, errors.Wrap(err, "upload file chunks error") - } - os.Remove(filepath.Join(chunksDir, fmt.Sprintf("chunk-%d", i))) - } - return res, nil -} - -func (c *ChainClient) UploadFileChunk(url, chunksDir, bucket, fname string, chunksNum, chunksId int, totalSize int64) (string, error) { - - file := filepath.Join(chunksDir, fmt.Sprintf("chunk-%d", chunksId)) - fstat, err := os.Stat(file) - if err != nil { - return "", errors.Wrap(err, "upload file chunk error") - } - - if fstat.IsDir() { - return "", errors.Wrap(errors.New("not a file"), "upload file chunk error") - } - - if fstat.Size() == 0 { - return "", errors.New("empty file") - } - - if !utils.CheckBucketName(bucket) { - return "", errors.New("invalid bucket name") - } - - kr, _ := keyring.FromURI(c.GetURI(), keyring.NetSubstrate{}) - - // sign message - message := utils.GetRandomcode(16) - sig, _ := kr.Sign(kr.SigningContext([]byte(message))) - - body := new(bytes.Buffer) - writer := multipart.NewWriter(body) - // - formFile, err := writer.CreateFormFile("file", fstat.Name()) - if err != nil { - return "", err - } - - f, err := os.Open(file) - if err != nil { - return "", err - } - defer f.Close() - - _, err = io.Copy(formFile, f) - if err != nil { - return "", err - } - err = writer.Close() - if err != nil { - return "", err - } - - req, err := http.NewRequest(http.MethodPut, url, body) - if err != nil { - return "", err - } - - req.Header.Set("BucketName", bucket) - req.Header.Set("Account", c.GetSignatureAcc()) - req.Header.Set("Message", message) - req.Header.Set("Signature", base58.Encode(sig[:])) - req.Header.Set("Content-Type", writer.FormDataContentType()) - req.Header.Set("FileName", fname) - req.Header.Set("BlockNumber", fmt.Sprint(chunksNum)) - req.Header.Set("BlockIndex", fmt.Sprint(chunksId)) - req.Header.Set("TotalSize", fmt.Sprint(totalSize)) - - client := &http.Client{} - client.Transport = globalTransport - resp, err := client.Do(req) - if err != nil { - return "", err - } - defer resp.Body.Close() - - respbody, err := io.ReadAll(resp.Body) - if err != nil { - return "", err - } - - if resp.StatusCode != http.StatusOK { - if len(respbody) > 0 { - return "", errors.New(string(respbody)) - } - return "", errors.New(fmt.Sprintf("upload failed, code: %d", resp.StatusCode)) - } - return strings.TrimPrefix(strings.TrimSuffix(string(respbody), "\""), "\""), nil -} diff --git a/core/process/file.go b/core/process/file.go new file mode 100644 index 0000000..6b4254e --- /dev/null +++ b/core/process/file.go @@ -0,0 +1,336 @@ +/* + Copyright (C) CESS. All rights reserved. + Copyright (C) Cumulus Encrypted Storage System. All rights reserved. + + SPDX-License-Identifier: Apache-2.0 +*/ + +package process + +import ( + "bytes" + "fmt" + "io" + "mime/multipart" + "net/http" + "os" + "path/filepath" + "strings" + + "github.com/CESSProject/cess-go-sdk/utils" + "github.com/btcsuite/btcutil/base58" + "github.com/centrifuge/go-substrate-rpc-client/v4/signature" + "github.com/pkg/errors" +) + +var globalTransport = &http.Transport{ + DisableKeepAlives: true, +} + +// StoreFile stores files to the gateway +// - url: gateway url +// - file: stored file +// - bucket: bucket for storing file +// - mnemonic: polkadot account mnemonic +// +// Return: +// - string: fid +// - error: error message +func StoreFile(url, file, bucket, mnemonic string) (string, error) { + fstat, err := os.Stat(file) + if err != nil { + + return "", err + } + + if fstat.IsDir() { + return "", errors.New("not a file") + } + + if fstat.Size() == 0 { + return "", errors.New("empty file") + } + + if !utils.CheckBucketName(bucket) { + return "", errors.New("invalid bucket name") + } + + keyringPair, err := signature.KeyringPairFromSecret(mnemonic, 0) + if err != nil { + return "", fmt.Errorf("[KeyringPairFromSecret] %v", err) + } + + acc, err := utils.EncodePublicKeyAsCessAccount(keyringPair.PublicKey) + if err != nil { + return "", fmt.Errorf("[EncodePublicKeyAsCessAccount] %v", err) + } + + // sign message + message := utils.GetRandomcode(16) + sig, err := utils.SignedSR25519WithMnemonic(keyringPair.URI, message) + if err != nil { + return "", fmt.Errorf("[SignedSR25519WithMnemonic] %v", err) + } + + body := new(bytes.Buffer) + writer := multipart.NewWriter(body) + // + formFile, err := writer.CreateFormFile("file", fstat.Name()) + if err != nil { + return "", err + } + + f, err := os.Open(file) + if err != nil { + return "", err + } + defer f.Close() + + _, err = io.Copy(formFile, f) + if err != nil { + return "", err + } + err = writer.Close() + if err != nil { + return "", err + } + + req, err := http.NewRequest(http.MethodPut, url, body) + if err != nil { + return "", err + } + + req.Header.Set("BucketName", bucket) + req.Header.Set("Account", acc) + req.Header.Set("Message", message) + req.Header.Set("Signature", base58.Encode(sig[:])) + req.Header.Set("Content-Type", writer.FormDataContentType()) + + client := &http.Client{} + client.Transport = globalTransport + resp, err := client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + + respbody, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + + if resp.StatusCode != http.StatusOK { + if len(respbody) > 0 { + return "", errors.New(string(respbody)) + } + return "", errors.New(fmt.Sprintf("upload failed, code: %d", resp.StatusCode)) + } + + return strings.TrimPrefix(strings.TrimSuffix(string(respbody), "\""), "\""), nil +} + +// StoreObject stores object to the gateway +// - url: gateway url +// - bucket: bucket for storing file +// - mnemonic: polkadot account mnemonic +// - reader: reader +// +// Return: +// - string: fid +// - error: error message +func StoreObject(url string, bucket, mnemonic string, reader io.Reader) (string, error) { + if !utils.CheckBucketName(bucket) { + return "", errors.New("invalid bucket name") + } + + keyringPair, err := signature.KeyringPairFromSecret(mnemonic, 0) + if err != nil { + return "", fmt.Errorf("[KeyringPairFromSecret] %v", err) + } + + acc, err := utils.EncodePublicKeyAsCessAccount(keyringPair.PublicKey) + if err != nil { + return "", fmt.Errorf("[EncodePublicKeyAsCessAccount] %v", err) + } + + // sign message + message := utils.GetRandomcode(16) + sig, err := utils.SignedSR25519WithMnemonic(keyringPair.URI, message) + if err != nil { + return "", fmt.Errorf("[SignedSR25519WithMnemonic] %v", err) + } + + req, err := http.NewRequest(http.MethodPut, url, reader) + if err != nil { + return "", err + } + + req.Header.Set("BucketName", bucket) + req.Header.Set("Account", acc) + req.Header.Set("Message", message) + req.Header.Set("Signature", base58.Encode(sig[:])) + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + client.Transport = globalTransport + resp, err := client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + + respbody, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + + if resp.StatusCode != http.StatusOK { + if len(respbody) > 0 { + return "", errors.New(string(respbody)) + } + return "", errors.New(fmt.Sprintf("upload failed, code: %d", resp.StatusCode)) + } + + return strings.TrimPrefix(strings.TrimSuffix(string(respbody), "\""), "\""), nil +} + +// RetrieveFile downloads files from the gateway +// - url: gateway url +// - fid: fid +// - mnemonic: polkadot account mnemonic +// - savepath: file save path +// +// Return: +// - string: fid +// - error: error message +func RetrieveFile(url, fid, mnemonic, savepath string) error { + fstat, err := os.Stat(savepath) + if err == nil { + if fstat.IsDir() { + savepath = filepath.Join(savepath, fid) + } + if fstat.Size() > 0 { + return nil + } + } + + if url == "" { + return errors.New("empty url") + } + + if url[len(url)-1] != byte(47) { + url += "/" + } + + f, err := os.Create(savepath) + if err != nil { + return err + } + defer f.Close() + + req, err := http.NewRequest(http.MethodGet, url+fid, nil) + if err != nil { + return err + } + + keyringPair, err := signature.KeyringPairFromSecret(mnemonic, 0) + if err != nil { + return fmt.Errorf("[KeyringPairFromSecret] %v", err) + } + + acc, err := utils.EncodePublicKeyAsCessAccount(keyringPair.PublicKey) + if err != nil { + return fmt.Errorf("[EncodePublicKeyAsCessAccount] %v", err) + } + + // sign message + message := utils.GetRandomcode(16) + sig, err := utils.SignedSR25519WithMnemonic(keyringPair.URI, message) + if err != nil { + return fmt.Errorf("[SignedSR25519WithMnemonic] %v", err) + } + + req.Header.Set("Message", message) + req.Header.Set("Signature", base58.Encode(sig[:])) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Operation", "download") + req.Header.Set("Account", acc) + + client := &http.Client{} + client.Transport = globalTransport + resp, err := client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return errors.New("failed") + } + + _, err = io.Copy(f, resp.Body) + if err != nil { + return err + } + + return nil +} + +// RetrieveObject gets the object from the gateway +// - url: gateway url +// - fid: fid +// - mnemonic: polkadot account mnemonic +// +// Return: +// - io.ReadCloser: object +// - error: error message +func RetrieveObject(url, fid, mnemonic string) (io.ReadCloser, error) { + if url == "" { + return nil, errors.New("empty url") + } + + if url[len(url)-1] != byte(47) { + url += "/" + } + + req, err := http.NewRequest(http.MethodGet, url+fid, nil) + if err != nil { + return nil, err + } + + keyringPair, err := signature.KeyringPairFromSecret(mnemonic, 0) + if err != nil { + return nil, fmt.Errorf("[KeyringPairFromSecret] %v", err) + } + + acc, err := utils.EncodePublicKeyAsCessAccount(keyringPair.PublicKey) + if err != nil { + return nil, fmt.Errorf("[EncodePublicKeyAsCessAccount] %v", err) + } + + // sign message + message := utils.GetRandomcode(16) + sig, err := utils.SignedSR25519WithMnemonic(keyringPair.URI, message) + if err != nil { + return nil, fmt.Errorf("[SignedSR25519WithMnemonic] %v", err) + } + + req.Header.Set("Message", message) + req.Header.Set("Signature", base58.Encode(sig[:])) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Account", acc) + req.Header.Set("Operation", "download") + + client := &http.Client{} + client.Transport = globalTransport + resp, err := client.Do(req) + if err != nil { + return nil, err + } + + if resp.StatusCode != http.StatusOK { + return nil, errors.New(fmt.Sprintf("Retrieve failed, code: %d", resp.StatusCode)) + } + + return resp.Body, nil +} diff --git a/core/process/file_chunk.go b/core/process/file_chunk.go new file mode 100644 index 0000000..44758e9 --- /dev/null +++ b/core/process/file_chunk.go @@ -0,0 +1,224 @@ +/* + Copyright (C) CESS. All rights reserved. + Copyright (C) Cumulus Encrypted Storage System. All rights reserved. + + SPDX-License-Identifier: Apache-2.0 +*/ + +package process + +import ( + "bufio" + "bytes" + "fmt" + "io" + "mime/multipart" + "net/http" + "os" + "path/filepath" + "strings" + + "github.com/CESSProject/cess-go-sdk/config" + "github.com/CESSProject/cess-go-sdk/utils" + "github.com/btcsuite/btcutil/base58" + "github.com/centrifuge/go-substrate-rpc-client/v4/signature" + "github.com/pkg/errors" +) + +// UploadFileChunks uploading file chunks +// - url: gateway url +// - mnemonic: polkadot account mnemonic +// - chunksDir: file chunks dir +// - bucket: bucket for storing file +// - fname: file name +// - chunksNum: chunk number +// - totalSize: total size +// +// Return: +// - string: fid +// - error: error message +func UploadFileChunks(url, mnemonic, chunksDir, bucket, fname string, chunksNum int, totalSize int64) (string, error) { + entries, err := os.ReadDir(chunksDir) + if err != nil { + return "", errors.Wrap(err, "upload file chunk error") + } + if len(entries) == 0 { + return "", errors.Wrap(errors.New("empty dir"), "upload file chunk error") + } + if len(entries) > chunksNum { + return "", errors.Wrap(errors.New("bad chunks number"), "upload file chunk error") + } + var res string + for i := chunksNum - len(entries); i < chunksNum; i++ { + res, err = UploadFileChunk(url, mnemonic, chunksDir, bucket, fname, chunksNum, i, totalSize) + if err != nil { + return res, errors.Wrap(err, "upload file chunks error") + } + os.Remove(filepath.Join(chunksDir, fmt.Sprintf("chunk-%d", i))) + } + return res, nil +} + +// UploadFileChunk uploading a file chunk +// - url: gateway url +// - mnemonic: polkadot account mnemonic +// - chunksDir: file chunks dir +// - bucket: bucket for storing file +// - fname: file name +// - chunksNum: chunk number +// - chunksId: chunk index +// - totalSize: total size +// +// Return: +// - string: fid +// - error: error message +func UploadFileChunk(url, mnemonic, chunksDir, bucket, fname string, chunksNum, chunksId int, totalSize int64) (string, error) { + + file := filepath.Join(chunksDir, fmt.Sprintf("chunk-%d", chunksId)) + fstat, err := os.Stat(file) + if err != nil { + return "", errors.Wrap(err, "upload file chunk error") + } + + if fstat.IsDir() { + return "", errors.Wrap(errors.New("not a file"), "upload file chunk error") + } + + if fstat.Size() == 0 { + return "", errors.New("empty file") + } + + if !utils.CheckBucketName(bucket) { + return "", errors.New("invalid bucket name") + } + + keyringPair, err := signature.KeyringPairFromSecret(mnemonic, 0) + if err != nil { + return "", fmt.Errorf("[KeyringPairFromSecret] %v", err) + } + + acc, err := utils.EncodePublicKeyAsCessAccount(keyringPair.PublicKey) + if err != nil { + return "", fmt.Errorf("[EncodePublicKeyAsCessAccount] %v", err) + } + + // sign message + message := utils.GetRandomcode(16) + sig, err := utils.SignedSR25519WithMnemonic(keyringPair.URI, message) + if err != nil { + return "", fmt.Errorf("[SignedSR25519WithMnemonic] %v", err) + } + + body := new(bytes.Buffer) + writer := multipart.NewWriter(body) + // + formFile, err := writer.CreateFormFile("file", fstat.Name()) + if err != nil { + return "", err + } + + f, err := os.Open(file) + if err != nil { + return "", err + } + defer f.Close() + + _, err = io.Copy(formFile, f) + if err != nil { + return "", err + } + err = writer.Close() + if err != nil { + return "", err + } + + req, err := http.NewRequest(http.MethodPut, url, body) + if err != nil { + return "", err + } + + req.Header.Set("BucketName", bucket) + req.Header.Set("Account", acc) + req.Header.Set("Message", message) + req.Header.Set("Signature", base58.Encode(sig[:])) + req.Header.Set("Content-Type", writer.FormDataContentType()) + req.Header.Set("FileName", fname) + req.Header.Set("BlockNumber", fmt.Sprint(chunksNum)) + req.Header.Set("BlockIndex", fmt.Sprint(chunksId)) + req.Header.Set("TotalSize", fmt.Sprint(totalSize)) + + client := &http.Client{} + client.Transport = globalTransport + resp, err := client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + + respbody, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + + if resp.StatusCode != http.StatusOK { + if len(respbody) > 0 { + return "", errors.New(string(respbody)) + } + return "", errors.New(fmt.Sprintf("upload failed, code: %d", resp.StatusCode)) + } + return strings.TrimPrefix(strings.TrimSuffix(string(respbody), "\""), "\""), nil +} + +func SplitFileWithstandardSize(fpath, chunksDir string) (int64, int, error) { + return SplitFile(fpath, chunksDir, config.SegmentSize, true) +} + +func SplitFile(fpath, chunksDir string, chunkSize int64, filling bool) (int64, int, error) { + fstat, err := os.Stat(fpath) + if err != nil { + return 0, 0, err + } + if fstat.IsDir() { + return 0, 0, errors.New("not a file") + } + if fstat.Size() == 0 { + return 0, 0, errors.New("empty file") + } + if fstat.Size() < chunkSize { + chunkSize = fstat.Size() + } + count := fstat.Size() / chunkSize + if fstat.Size()%chunkSize != 0 { + count++ + } + buf := make([]byte, chunkSize) + f, err := os.Open(fpath) + if err != nil { + return 0, 0, err + } + defer f.Close() + reader := bufio.NewReader(f) + size := fstat.Size() + for i := int64(0); i < count; i++ { + n, err := reader.Read(buf) + if err != nil { + return 0, 0, err + } + if n <= 0 { + return 0, 0, errors.New("read a empty block") + } + if n < int(chunkSize) && filling { + if i+1 != count { + return 0, 0, errors.New("read file err") + } + copy(buf[n:], make([]byte, chunkSize-int64(n))) + size += chunkSize - int64(n) + n = int(chunkSize) + } + err = utils.WriteBufToFile(buf[:n], filepath.Join(chunksDir, fmt.Sprintf("chunk-%d", i))) + if err != nil { + return 0, 0, err + } + } + return size, int(count), nil +} From 1841031b19527bd391f6ae60b5c9e421d376588a Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Sat, 11 May 2024 11:58:59 +0800 Subject: [PATCH 16/18] update sdk --- chain/chain.go | 56 +-- chain/deoss.go | 4 +- chain/extrinsic_name.go | 9 +- chain/{fileBank.go => file_bank.go} | 5 +- chain/sminer.go | 2 +- chain/staking.go | 8 +- chain/tee.go | 2 +- core/process/file.go | 42 +- core/process/file_chunk.go | 71 ++- core/sdk/sdk.go | 697 ++++++++-------------------- example/file/file.go | 52 +-- example/other/main.go | 148 ------ go.mod | 3 +- go.sum | 2 - 14 files changed, 299 insertions(+), 802 deletions(-) rename chain/{fileBank.go => file_bank.go} (99%) delete mode 100644 example/other/main.go diff --git a/chain/chain.go b/chain/chain.go index a42dbaa..6b78416 100755 --- a/chain/chain.go +++ b/chain/chain.go @@ -9,12 +9,9 @@ package chain import ( "context" - "encoding/hex" - "fmt" "io" "log" "os" - "strings" "sync" "time" @@ -25,9 +22,6 @@ import ( "github.com/centrifuge/go-substrate-rpc-client/v4/signature" "github.com/centrifuge/go-substrate-rpc-client/v4/types" "github.com/centrifuge/go-substrate-rpc-client/v4/xxhash" - "github.com/mr-tron/base58" - "github.com/pkg/errors" - "github.com/vedhavyas/go-subkey/sr25519" ) type ChainClient struct { @@ -301,58 +295,10 @@ func reconnectRpc(oldRpc string, rpcs []string) ( return api, metadata, runtimeVer, eventRetriever, genesisHash, rpcAddr, err } -func createPrefixedKey(pallet, method string) []byte { +func CreatePrefixedKey(pallet, method string) []byte { return append(xxhash.New128([]byte(pallet)).Sum(nil), xxhash.New128([]byte(method)).Sum(nil)...) } -func (c *ChainClient) VerifyPolkaSignatureWithJS(account, msg, signature string) (bool, error) { - if len(msg) == 0 { - return false, errors.New("msg is empty") - } - - pkey, err := utils.ParsingPublickey(account) - if err != nil { - return false, err - } - - pub, err := sr25519.Scheme{}.FromPublicKey(pkey) - if err != nil { - return false, err - } - - sign_bytes, err := hex.DecodeString(strings.TrimPrefix(signature, "0x")) - if err != nil { - return false, err - } - message := fmt.Sprintf("%s", msg) - ok := pub.Verify([]byte(message), sign_bytes) - return ok, nil -} - -func (c *ChainClient) VerifyPolkaSignatureWithBase58(account, msg, signature string) (bool, error) { - if len(msg) == 0 { - return false, errors.New("msg is empty") - } - - pkey, err := utils.ParsingPublickey(account) - if err != nil { - return false, err - } - - pub, err := sr25519.Scheme{}.FromPublicKey(pkey) - if err != nil { - return false, err - } - - sign_bytes, err := base58.Decode(signature) - if err != nil { - return false, err - } - message := fmt.Sprintf("%s", msg) - ok := pub.Verify([]byte(message), sign_bytes) - return ok, nil -} - // close chain client func (c *ChainClient) Close() { if c.api != nil { diff --git a/chain/deoss.go b/chain/deoss.go index 887b6ee..b07ca34 100755 --- a/chain/deoss.go +++ b/chain/deoss.go @@ -92,7 +92,7 @@ func (c *ChainClient) QueryAllOss(block int32) ([]OssInfo, error) { return nil, ERR_RPC_CONNECTION } - key := createPrefixedKey(Oss, Oss) + key := CreatePrefixedKey(Oss, Oss) keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), Oss, Oss, err) @@ -150,7 +150,7 @@ func (c *ChainClient) QueryAllOssPeerId(block int32) ([]string, error) { return nil, ERR_RPC_CONNECTION } - key := createPrefixedKey(Oss, Oss) + key := CreatePrefixedKey(Oss, Oss) keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), Oss, Oss, err) diff --git a/chain/extrinsic_name.go b/chain/extrinsic_name.go index 3cf03db..bfd2503 100644 --- a/chain/extrinsic_name.go +++ b/chain/extrinsic_name.go @@ -353,12 +353,17 @@ const ( ExtName_VoterList_rebag = "VoterList.rebag" ) +// InitExtrinsicsName initialises all transaction names +// +// Return: +// - error: error message +// +// Note: +// - if you need to make a transaction on the chain, you must call this method func (c *ChainClient) InitExtrinsicsName() error { ExtrinsicsName = make(map[types.CallIndex]string, 0) // Assets if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_Assets_approve_transfer); err == nil { - //fmt.Println(ExtName_Timestamp_set, ".callIndex.MethodIndex:", callIndex.MethodIndex) - //fmt.Println(ExtName_Timestamp_set, ".callIndex.SectionIndex:", callIndex.SectionIndex) ExtrinsicsName[callIndex] = ExtName_Assets_approve_transfer } else { return err diff --git a/chain/fileBank.go b/chain/file_bank.go similarity index 99% rename from chain/fileBank.go rename to chain/file_bank.go index 3a3a6fc..b707fca 100755 --- a/chain/fileBank.go +++ b/chain/file_bank.go @@ -321,7 +321,7 @@ func (c *ChainClient) QueryAllRestoralOrder(block int32) ([]RestoralOrderInfo, e return nil, ERR_RPC_CONNECTION } - key := createPrefixedKey(FileBank, RestoralOrder) + key := CreatePrefixedKey(FileBank, RestoralOrder) keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), FileBank, RestoralOrder, err) @@ -681,6 +681,9 @@ func (c *ChainClient) UploadDeclaration(fid string, segment []SegmentList, user // Note: // - cannot create a bucket that already exists // - if you are not the owner, the owner account must be authorised to you +// +// For details on bucket naming rules, see: +// - https://docs.cess.cloud/deoss/get-started/deoss-gateway/step-1-create-a-bucket#naming-conventions-for-a-bucket func (c *ChainClient) CreateBucket(owner []byte, bucketName string) (string, error) { c.lock.Lock() defer func() { diff --git a/chain/sminer.go b/chain/sminer.go index d980ff7..b1e8dd8 100755 --- a/chain/sminer.go +++ b/chain/sminer.go @@ -424,7 +424,7 @@ func (c *ChainClient) QueryAllRestoralTarget(block int32) ([]RestoralTargetInfo, return nil, ERR_RPC_CONNECTION } - key := createPrefixedKey(Sminer, RestoralTarget) + key := CreatePrefixedKey(Sminer, RestoralTarget) keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), Sminer, RestoralTarget, err) diff --git a/chain/staking.go b/chain/staking.go index ca0639b..53abc44 100755 --- a/chain/staking.go +++ b/chain/staking.go @@ -365,7 +365,7 @@ func (c *ChainClient) QueryAllNominators(block int32) ([]StakingNominations, err return nil, ERR_RPC_CONNECTION } - key := createPrefixedKey(Staking, Nominators) + key := CreatePrefixedKey(Staking, Nominators) keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), Staking, Nominators, err) @@ -427,7 +427,7 @@ func (c *ChainClient) QueryAllBonded(block int32) ([]types.AccountID, error) { return nil, ERR_RPC_CONNECTION } - key := createPrefixedKey(Staking, Bonded) + key := CreatePrefixedKey(Staking, Bonded) keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), Staking, Bonded, err) @@ -589,7 +589,7 @@ func (c *ChainClient) QueryEraValidatorReward(era uint32, block int) (string, er // return result, ERR_RPC_CONNECTION // } -// // key := createPrefixedKey(Staking, ErasStakers) +// // key := CreatePrefixedKey(Staking, ErasStakers) // param1, err := codec.Encode(types.NewU32(era)) // if err != nil { // return result, err @@ -624,7 +624,7 @@ func (c *ChainClient) QueryEraValidatorReward(era uint32, block int) (string, er // return result, err // } // _ = key -// kkey := createPrefixedKey(Staking, ErasStakers) +// kkey := CreatePrefixedKey(Staking, ErasStakers) // //kkey = append(kkey, []byte(" ")...) // //kkey = append(kkey, param1...) //xxhash.New128(param1).Sum(nil)...) diff --git a/chain/tee.go b/chain/tee.go index e5b7ac5..7104b6f 100755 --- a/chain/tee.go +++ b/chain/tee.go @@ -148,7 +148,7 @@ func (c *ChainClient) QueryAllWorkers(block int32) ([]WorkerInfo, error) { return list, ERR_RPC_CONNECTION } - key := createPrefixedKey(TeeWorker, Workers) + key := CreatePrefixedKey(TeeWorker, Workers) keys, err := c.api.RPC.State.GetKeysLatest(key) if err != nil { err = fmt.Errorf("rpc err: [%s] [st] [%s.%s] GetKeysLatest: %v", c.GetCurrentRpcAddr(), TeeWorker, Workers, err) diff --git a/core/process/file.go b/core/process/file.go index 6b4254e..e96189b 100644 --- a/core/process/file.go +++ b/core/process/file.go @@ -28,14 +28,27 @@ var globalTransport = &http.Transport{ } // StoreFile stores files to the gateway +// +// Receive parameter: // - url: gateway url // - file: stored file // - bucket: bucket for storing file // - mnemonic: polkadot account mnemonic // -// Return: -// - string: fid -// - error: error message +// Return parameter: +// - string: [fid] unique identifier for the file. +// - error: error message. +// +// Preconditions: +// 1. Account requires purchasing space, refer to [BuySpace] interface. +// 2. Authorize the space usage rights of the account to the gateway account, +// refer to the [AuthorizeSpace] interface. +// 3. Make sure the name of the bucket is legal, use the [CheckBucketName] method to check. +// +// Explanation: +// - 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) { fstat, err := os.Stat(file) if err != nil { @@ -130,14 +143,27 @@ func StoreFile(url, file, bucket, mnemonic string) (string, error) { } // StoreObject stores object to the gateway +// +// Receive parameter: // - url: gateway url -// - bucket: bucket for storing file +// - bucket: the bucket for storing object, it will be created automatically. // - mnemonic: polkadot account mnemonic -// - reader: reader +// - reader: strings, byte data, file streams, network streams, etc. // -// Return: -// - string: fid -// - error: error message +// Return parameter: +// - string: [fid] unique identifier for the file. +// - error: error message. +// +// Preconditions: +// 1. Account requires purchasing space, refer to [BuySpace] interface. +// 2. Authorize the space usage rights of the account to the gateway account, +// refer to the [AuthorizeSpace] interface. +// 3. Make sure the name of the bucket is legal, use the [CheckBucketName] method to check. +// +// Explanation: +// - 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) { if !utils.CheckBucketName(bucket) { return "", errors.New("invalid bucket name") diff --git a/core/process/file_chunk.go b/core/process/file_chunk.go index 44758e9..6bc0f1b 100644 --- a/core/process/file_chunk.go +++ b/core/process/file_chunk.go @@ -25,18 +25,20 @@ import ( "github.com/pkg/errors" ) -// UploadFileChunks uploading file chunks -// - url: gateway url -// - mnemonic: polkadot account mnemonic -// - chunksDir: file chunks dir -// - bucket: bucket for storing file -// - fname: file name -// - chunksNum: chunk number -// - totalSize: total size +// UploadFileChunks upload file chunks in the directory to the gateway as much as possible, +// chunks will be removed after being uploaded, if the chunks are not transferred successfuly, jus // -// Return: -// - string: fid -// - error: error message +// Receive parameter: +// - url: the address of the gateway. +// - chunksDir: directory path to store file chunks, please do not mix it elsewhere. +// - bucket: the bucket name to store user data. +// - fname: the name of the file. +// - chunksNum: total number of file chunks. +// - totalSize: chunks total size (byte), can be obtained from the first return value of SplitFile +// +// Return parameter: +// - Reader: number of file chunks. +// - error: error message. func UploadFileChunks(url, mnemonic, chunksDir, bucket, fname string, chunksNum int, totalSize int64) (string, error) { entries, err := os.ReadDir(chunksDir) if err != nil { @@ -59,19 +61,20 @@ func UploadFileChunks(url, mnemonic, chunksDir, bucket, fname string, chunksNum return res, nil } -// UploadFileChunk uploading a file chunk -// - url: gateway url -// - mnemonic: polkadot account mnemonic -// - chunksDir: file chunks dir -// - bucket: bucket for storing file -// - fname: file name -// - chunksNum: chunk number -// - chunksId: chunk index -// - totalSize: total size +// UploadFileChunk upload chunk of file to the gateway +// +// Receive parameter: +// - url: the address of the gateway. +// - chunksDir: directory path to store file chunks, please do not mix it elsewhere. +// - bucket: the bucket name to store user data. +// - fname: the name of the file. +// - chunksNum: total number of file chunks. +// - chunksId: index of the current chunk to be uploaded ([0,chunksNum)). +// - totalSize: chunks total size (byte), can be obtained from the first return value of SplitFile // -// Return: -// - string: fid -// - error: error message +// Return parameter: +// - Reader: number of file chunks. +// - error: error message. func UploadFileChunk(url, mnemonic, chunksDir, bucket, fname string, chunksNum, chunksId int, totalSize int64) (string, error) { file := filepath.Join(chunksDir, fmt.Sprintf("chunk-%d", chunksId)) @@ -169,10 +172,32 @@ func UploadFileChunk(url, mnemonic, chunksDir, bucket, fname string, chunksNum, return strings.TrimPrefix(strings.TrimSuffix(string(respbody), "\""), "\""), nil } +// Split File into Chunks with standard size. +// It split file into chunks of the default size and fills the last chunk that does not meet the size. +// +// Receive parameter: +// - fpath: the path of the file to be split. +// - chunksDir: directory path to store file chunks, please do not mix it elsewhere. +// +// Return parameter: +// - int64: chunks total size (byte). +// - int: number of file chunks. +// - error: error message. func SplitFileWithstandardSize(fpath, chunksDir string) (int64, int, error) { return SplitFile(fpath, chunksDir, config.SegmentSize, true) } +// Split File into Chunks. +// +// Receive parameter: +// - fpath: the path of the file to be split. +// - chunksDir: directory path to store file chunks, please do not mix it elsewhere. +// - chunkSize: the size of each chunk, it does not exceed the file size +// +// Return parameter: +// - int64: chunks total size (byte). +// - int: number of file chunks. +// - error: error message. func SplitFile(fpath, chunksDir string, chunkSize int64, filling bool) (int64, int, error) { fstat, err := os.Stat(fpath) if err != nil { diff --git a/core/sdk/sdk.go b/core/sdk/sdk.go index ad6a676..5d7314e 100755 --- a/core/sdk/sdk.go +++ b/core/sdk/sdk.go @@ -7,516 +7,193 @@ package sdk +import ( + "github.com/CESSProject/cess-go-sdk/chain" + gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4" + "github.com/centrifuge/go-substrate-rpc-client/v4/types" +) + // CESS Go SDK Interface Description type SDK interface { - // // Audit-State - - // // QueryChallengeVerifyExpiration Query Challenge Expiration Block High - // QueryChallengeVerifyExpiration() (uint32, error) - // // QueryChallengeInfo queries accountID's challenge information - // // - accountID: signature account of the storage node - // // - block: block number, less than 0 indicates the latest block - // QueryChallengeInfo(accountID []byte, block int32) (bool, pattern.ChallengeInfo, error) - - // // Audit-Extrinsics - - // // SubmitIdleProof submits proof of idle data to the chain - // // Tip: This method can only be used for storage nodes - // SubmitIdleProof(idleProve []types.U8) (string, error) - // // SubmitServiceProof submits proof of service data to the chain - // // Tip: This method can only be used for storage nodes - // SubmitServiceProof(serviceProof []types.U8) (string, error) - // // SubmitIdleProofResult submits the proof verification results of idle data to the chain - // // Tip: This method can only be used for storage nodes - // SubmitIdleProofResult(totalProofHash []types.U8, front, rear types.U64, accumulator pattern.Accumulator, result types.Bool, sig types.Bytes, teePuk pattern.WorkerPublicKey) (string, error) - // // SubmitServiceProofResult submits the proof verification results of service data to the chain - // // Tip: This method can only be used for storage nodes - // SubmitServiceProofResult(result types.Bool, sign types.Bytes, bloomFilter pattern.BloomFilter, teePuk pattern.WorkerPublicKey) (string, error) - - // // Filebank-State - - // // QueryBucketInfo query the bucket information of the accountID - // QueryBucketInfo(accountID []byte, bucketName string) (pattern.BucketInfo, error) - // // QueryAllBucket query all buckets of the accountID - // QueryAllBucket(accountID []byte) ([]types.Bytes, error) - // // QueryAllBucketString query all bucket names as string of the accountID - // QueryAllBucketString(accountID []byte) ([]string, error) - // // QueryStorageOrder query storage order information. - // QueryStorageOrder(fid string) (pattern.StorageOrder, error) - // // QueryFileMetadata queries the metadata of the roothash file. - // QueryFileMetadata(fid string) (pattern.FileMetadata, error) - // // QueryFileMetadataByBlock queries the metadata of the roothash file. - // QueryFileMetadataByBlock(fid string, block uint64) (pattern.FileMetadata, error) - // // QueryRestoralOrder queries a restore order info. - // QueryRestoralOrder(roothash string) (pattern.RestoralOrderInfo, error) - // QueryRestoralOrderList() ([]pattern.RestoralOrderInfo, error) - - // // Filebank-Extrinsics - - // // ClaimRestoralNoExistOrder is used to receive recovery orders from exiting miners. - // ClaimRestoralNoExistOrder(accountID []byte, fid, restoralFragmentHash string) (string, error) - // // ClaimRestoralOrder is used to collect restoration orders. - // ClaimRestoralOrder(fragmentHash string) (string, error) - // // CreateBucket creates a bucket for accountID - // // For details on bucket naming rules, see: - // // https://docs.cess.cloud/deoss/get-started/deoss-gateway/step-1-create-a-bucket#naming-conventions-for-a-bucket - // CreateBucket(accountID []byte, bucketName string) (string, error) - // // DeleteBucket deletes buckets for accountID - // // Tip: Only empty buckets can be deleted - // DeleteBucket(accountID []byte, bucketName string) (string, error) - // // DeleteFile deletes files for accountID - // DeleteFile(puk []byte, filehash string) (string, error) - // // GenerateRestoralOrder generates data for restoration orders. - // GenerateRestoralOrder(fid, fragmentHash string) (string, error) - // // RestoralComplete reports order recovery completion. - // RestoralComplete(restoralFragmentHash string) (string, error) - // // SubmitFileReport submits a stored file report. - // SubmitFileReport(index types.U8, roothash pattern.FileHash) (string, error) - // ReportFile(index uint8, roothash string) (string, error) - // // UploadDeclaration creates a storage order. - // UploadDeclaration(filehash string, dealinfo []pattern.SegmentList, user pattern.UserBrief, filesize uint64) (string, error) - // // GenerateStorageOrder for generating storage orders - // GenerateStorageOrder(roothash string, segment []pattern.SegmentDataInfo, owner []byte, filename, buckname string, filesize uint64) (string, error) - // // CertIdleSpace - // CertIdleSpace(idleSignInfo pattern.SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk pattern.WorkerPublicKey) (string, error) - // // ReplaceIdleSpace - // ReplaceIdleSpace(idleSignInfo pattern.SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk pattern.WorkerPublicKey) (string, error) - // // ReportTagCalculated - // ReportTagCalculated(teeSig types.Bytes, tagSigInfo pattern.TagSigInfo) (string, error) - - // // Oss-State - - // // QueryAuthorizedAccountIDs queries all DeOSS accountIDs authorized by accountID - // QueryAuthorizedAccountIDs(accountID []byte) ([]types.AccountID, error) - // // QueryAuthorizedAccounts queries all DeOSS accounts authorized by accountID - // QueryAuthorizedAccounts(accountID []byte) ([]string, error) - // // QueryDeOSSInfo Query the DeOSS information registered by accountID account - // QueryDeOSSInfo(accountID []byte) (pattern.OssInfo, error) - // // QueryAllDeOSSInfo queries all deoss information - // QueryAllDeOSSInfo() ([]pattern.OssInfo, error) - // // QueryAllDeOSSPeerId queries peerID of all DeOSS - // QueryAllDeOSSPeerId() ([]string, error) - - // // Oss-Extrinsics - - // // RegisterDeOSS register as deoss node - // RegisterDeOSS(peerId []byte, domain string) (string, error) - // // UpdateDeOSS updates the peerID and domain of deoss - // // Tip: This method can only be used for DeOSS nodes - // UpdateDeOSS(peerId string, domain string) (string, error) - // // ExitDeoss exit deoss - // // Tip: This method can only be used for DeOSS nodes - // ExitDeOSS() (string, error) - // // AuthorizeSpace authorizes space to oss account - // // Tip: accountID can only be a DeOSS node account - // AuthorizeSpace(accountID string) (string, error) - // // UnAuthorizeSpace cancels space authorization - // // Tip: accountID can only be a DeOSS node account - // UnAuthorizeSpace(accountID string) (string, error) - - // // Sminer-State - - // // QueryExpenders queries expenders information - // QueryExpenders() (pattern.ExpendersInfo, error) - // // // QueryStorageMinerByAccount queries storage node information by account. - // QueryStorageMinerByAccount(account string) (pattern.MinerInfo, error) - // // QueryStorageMiner queries storage node information. - // QueryStorageMiner(accountID []byte) (pattern.MinerInfo, error) - // // QueryStorageMinerByBlock queries storage node information in specified block - // // - if block < 0, then query the latest block - // QueryStorageMinerByBlock(puk []byte, block int32) (pattern.MinerInfo, error) - // // QueryAllSminerAccount queries the accounts of all storage miners. - // QueryAllSminerAccount() ([]types.AccountID, error) - // // QueryAllSminerAccountByBlock queries accounts of all storage miners in specified block. - // // - if block < 0, then query the latest block - // QueryAllSminerAccountByBlock(block int32) ([]types.AccountID, error) - // // QueryRewardsMap queries rewardsMap for accountID - // // Tip: accountID can only be a storage node account - // QueryRewardsMap(accountID []byte) (pattern.MinerReward, error) - // // QueryRewards queries rewards for accountID - // // Tip: accountID can only be a storage node account - // QueryRewards(accountID []byte) (pattern.RewardsType, error) - // // QueryStorageMinerStakingStartBlock - // QueryStorageMinerStakingStartBlock(puk []byte) (types.U32, error) - // // QueryRestoralTarget queries the space recovery information of the exited storage node - // // Tip: accountID can only be a storage node account - // QueryRestoralTarget(accountID []byte) (pattern.RestoralTargetInfo, error) - // // QueryRestoralTargetList queries the space recovery information of all exited storage nodes - // QueryRestoralTargetList() ([]pattern.RestoralTargetInfo, error) - // // QueryPendingReplacements queries the amount of idle data that can be replaced - // // Tip: accountID can only be a storage node account - // QueryPendingReplacements(accountID []byte) (types.U128, error) - // // - // QueryCompleteSnapShot(era uint32, block int32) (uint32, uint64, error) - - // // Sminer-Extrinsics - - // // - // IncreaseDeclarationSpace(tibCount uint32) (string, error) - // // RegisterSminer register sminer information - // RegisterSminer(earnings string, peerId []byte, pledge uint64, tib_count uint32) (string, error) - // // RegisterSminerAssignStaking - // RegisterSminerAssignStaking(beneficiaryAcc string, peerId []byte, stakingAcc string, tib_count uint32) (string, error) - // // RegisterSminerPOISKey register the pois key of sminer - // RegisterSminerPOISKey(poisKey pattern.PoISKeyInfo, teeSignWithAcc, teeSign types.Bytes, teePuk pattern.WorkerPublicKey) (string, error) - // // ExitSminer exit mining - // ExitSminer(miner string) (string, error) - // // UpdateEarningsAcc update earnings account. - // UpdateEarningsAcc(puk []byte) (string, error) - // UpdateEarningsAccount(earnings string) (string, error) - // // UpdateSminerPeerId update miner peerid - // UpdateSminerPeerId(peerid pattern.PeerId) (string, error) - // // IncreaseStakingAmount increase staking amount. - // IncreaseStakingAmount(miner string, tokens *big.Int) (string, error) - // IncreaseStorageNodeStakingAmount(miner string, token string) (string, error) - // // ClaimRewards is used to claim rewards. - // ClaimRewards() (string, error) - // // Withdraw is used to withdraw staking. - // Withdraw() (string, error) - - // // Staking-State - - // // QueryAllValidatorCount queries all the validator count - // QueryAllValidatorCount(block int) (uint32, error) - // // - // QueryValidatorsCount(block int) (uint32, error) - // // - // QueryNominatorCount(block int) (uint32, error) - // // - // QueryErasTotalStake(era uint32, block int) (string, error) - // // - // QueryCurrentEra(block int) (uint32, error) - // // - // //QueryeErasStakers(era uint32) ([]pattern.StakingExposure, error) - // // - // QueryStakingEraRewardPoints(era uint32) (pattern.StakingEraRewardPoints, error) - // // - // QueryNominatorsLatest() ([]pattern.StakingNominations, error) - // // - // QueryBondedList(block int32) ([]types.AccountID, error) - // // - // QueryValidatorCommission(account string, block int) (uint8, error) - // // - // QueryEraValidatorReward(era uint32) (string, error) - - // // StorageHandler-State - - // // QueryUserSpaceInfo queries the space information purchased by the user. - // QueryUserSpaceInfo(puk []byte) (pattern.UserSpaceInfo, error) - // QueryUserSpaceSt(puk []byte) (pattern.UserSpaceSt, error) - // // QuerySpacePricePerGib query space price per GiB. - // QuerySpacePricePerGib() (string, error) - // // QueryTotalIdleSpace query total idle space - // QueryTotalIdleSpace() (uint64, error) - // // QueryTotalServiceSpace query total service space - // QueryTotalServiceSpace() (uint64, error) - // // QueryPurchasedSpace query purchased space - // QueryPurchasedSpace() (uint64, error) - - // // StorageHandler-Extrinsics - - // // BuySpace for purchasing space. - // BuySpace(count uint32) (string, error) - // // ExpansionSpace for expansion space. - // ExpansionSpace(count uint32) (string, error) - // // RenewalSpace is used to extend the validity of space. - // RenewalSpace(days uint32) (string, error) - - // // TeeWorker-State - - // // QueryTeeWorker queries the information of the Tee worker. - // QueryTeeWorker(puk pattern.WorkerPublicKey) (pattern.TeeWorkerInfo, error) - // // QueryTeeInfo queries the information of the Tee worker. - // QueryTeeInfo(puk pattern.WorkerPublicKey) (pattern.TeeInfo, error) - // // QueryMasterPublicKey queries the master publicKey. - // QueryMasterPublicKey() ([]byte, error) - // // QueryAllTeeWorkerMap queries the information of all tee workers. - // QueryAllTeeWorkerMap() ([]pattern.TeeWorkerInfo, error) - // // QueryAllTeeInfo queries the information of all tee workers. - // QueryAllTeeInfo() ([]pattern.TeeInfo, error) - // // QueryTeeWorkEndpoint queries the endpoint of tee workers. - // QueryTeeWorkEndpoint(workPuk pattern.WorkerPublicKey) (string, error) - // // - // QueryWorkerAddedAt(workPuk pattern.WorkerPublicKey) (types.U32, error) - - // // System - - // // QueryBlockHeight queries the block height corresponding to the block hash, - // // If the blockhash is empty, query the latest block height. - // QueryBlockHeight(blockhash string) (uint32, error) - // // QueryNodeSynchronizationSt returns the synchronization status of the current node. - // QueryNodeSynchronizationSt() (bool, error) - // // QueryAccountInfoByAccount query account information by account. - // QueryAccountInfoByAccount(acc string) (types.AccountInfo, error) - // // QueryAccountInfo query account information by public key. - // QueryAccountInfo(puk []byte) (types.AccountInfo, error) - // // GetTokenSymbol returns the token symbol - // GetTokenSymbol() string - // // GetNetworkEnv returns the network environment - // GetNetworkEnv() string - // // SysProperties returns the system properties. - // SysProperties() (pattern.SysProperties, error) - // // SyncState returns the system sync state. - // SyncState() (pattern.SysSyncState, error) - // // SysVersion returns the system version. - // ChainVersion() (string, error) - // // NetListening returns whether the current node is listening. - // NetListening() (bool, error) - // // - // DecodeEventNameFromBlock(block uint64) ([]string, error) - // // - // DecodeEventNameFromBlockhash(blockhash types.Hash) ([]string, error) - // // - // QueryAllAccountInfoFromBlock(block int) ([]types.AccountInfo, error) - // // - // QueryTotalIssuance(block int) (string, error) - - // // CessTreasury - // QueryRoundReward(era uint32, block int32) (string, error) - - // // TransferToken to dest. - // // - // // Receive parameter: - // // - dest: target account. - // // - amount: transfer amount. - // // Return parameter: - // // - string: transaction hash. - // // - string: target account. - // // - error: error message. - // TransferToken(dest string, amount uint64) (string, string, error) - - // // Other - - // // GetSignatureAcc returns the signature account. - // GetSignatureAcc() string - // // GetSignatureAccPulickey returns the signature account public key. - // GetSignatureAccPulickey() []byte - // // GetSubstrateAPI returns Substrate API. - // GetSubstrateAPI() *gsrpc.SubstrateAPI - // // GetChainState returns chain node state. - // GetChainState() bool - // // SetChainState sets the state of the chain node. - // SetChainState(state bool) - // // GetSdkName return sdk name - // GetSDKName() string - // // GetCurrentRpcAddr return current rpc address - // GetCurrentRpcAddr() string - // // SetSdkName set sdk name - // SetSDKName(name string) - // // ReconnectRPC for reconnecting chains. - // ReconnectRPC() error - // // GetMetadata returns the metadata of the chain. - // GetMetadata() *types.Metadata - // // GetURI returns URI. - // GetURI() string - // // GetTreasuryAccount returns the treasury account - // GetTreasuryAccount() string - // // Sign returns the signature of the msg with the private key of the signing account. - // Sign(msg []byte) ([]byte, error) - // // Verify the signature of the msg with the public key of the signing account. - // Verify(msg []byte, sig []byte) (bool, error) - // // Verify Polka Signature With JavaScript - // VerifyPolkaSignatureWithJS(account, msg, signature string) (bool, error) - // // Verify Polka Signature With Base58 - // VerifyPolkaSignatureWithBase58(account, msg, signature string) (bool, error) - // // - // Close() - - // // Upload file to CESS gateway. - // // - // // Receive parameter: - // // - url: the address of the gateway. - // // - file: uploaded file. - // // - bucket: the bucket for storing files, it will be created automatically. - // // Return parameter: - // // - string: [fid] unique identifier for the file. - // // - error: error message. - // // Preconditions: - // // 1. Account requires purchasing space, refer to [BuySpace] interface. - // // 2. Authorize the space usage rights of the account to the gateway account, - // // refer to the [AuthorizeSpace] interface. - // // 3. Make sure the name of the bucket is legal, use the [CheckBucketName] method to check. - // // Explanation: - // // - 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] - // StoreFile(url, file, bucket string) (string, error) - - // // Upload object to CESS gateway. - // // - // // Receive parameter: - // // - url: the address of the gateway. - // // - reader: strings, byte data, file streams, network streams, etc. - // // - bucket: the bucket for storing object, it will be created automatically. - // // Return parameter: - // // - string: [fid] unique identifier for the file. - // // - error: error message. - // // Preconditions: - // // 1. Account requires purchasing space, refer to [BuySpace] interface. - // // 2. Authorize the space usage rights of the account to the gateway account, - // // refer to the [AuthorizeSpace] interface. - // // 3. Make sure the name of the bucket is legal, use the [CheckBucketName] method to check. - // // Explanation: - // // - 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] - // StoreObject(url string, reader io.Reader, bucket string) (string, error) - - // // Download file from CESS public gateway. - // // - // // Receive parameter: - // // - url: the address of the gateway. - // // - fid: unique identifier for the file. - // // - savepath: file save location. - // // Return parameter: - // // - error: error message. - // RetrieveFile(url, fid, savepath string) error - - // // Download object from CESS gateway. - // // - // // Receive parameter: - // // - url: the address of the gateway. - // // - fid: unique identifier for the file. - // // Return parameter: - // // - Reader: object stream. - // // - error: error message. - // RetrieveObject(url, fid string) (io.ReadCloser, error) - - // // Split File into Chunks. - // // - // // Receive parameter: - // // - fpath: the path of the file to be split. - // // - chunksDir: directory path to store file chunks, please do not mix it elsewhere. - // // - chunkSize: the size of each chunk, it does not exceed the file size - // // Return parameter: - // // - int64: chunks total size (byte). - // // - int: number of file chunks. - // // - error: error message. - // SplitFile(fpath, chunksDir string, chunkSize int64, filling bool) (int64, int, error) - - // // Split File into Chunks with standard size. - // // It split file into chunks of the default size and fills the last chunk that does not meet the size. - // // - // // Receive parameter: - // // - fpath: the path of the file to be split. - // // - chunksDir: directory path to store file chunks, please do not mix it elsewhere. - // // Return parameter: - // // - int64: chunks total size (byte). - // // - int: number of file chunks. - // // - error: error message. - // SplitFileWithstandardSize(fpath, chunksDir string) (int64, int, error) - - // // Upload chunk of file to the gateway - // // - // // Receive parameter: - // // - url: the address of the gateway. - // // - chunksDir: directory path to store file chunks, please do not mix it elsewhere. - // // - bucket: the bucket name to store user data. - // // - fname: the name of the file. - // // - chunksNum: total number of file chunks. - // // - chunksId: index of the current chunk to be uploaded ([0,chunksNum)). - // // - totalSize: chunks total size (byte), can be obtained from the first return value of SplitFile - // // Return parameter: - // // - Reader: number of file chunks. - // // - error: error message. - // UploadFileChunk(url, chunksDir, bucket, fname string, chunksNum, chunksId int, totalSize int64) (string, error) - - // // Upload file chunks in the directory to the gateway as much as possible, chunks will be removed after being uploaded, if the chunks are not transferred successfuly, jus - // // - // // Receive parameter: - // // - url: the address of the gateway. - // // - chunksDir: directory path to store file chunks, please do not mix it elsewhere. - // // - bucket: the bucket name to store user data. - // // - fname: the name of the file. - // // - chunksNum: total number of file chunks. - // // - totalSize: chunks total size (byte), can be obtained from the first return value of SplitFile - // // Return parameter: - // // - Reader: number of file chunks. - // // - error: error message. - // UploadFileChunks(url, chunksDir, bucket, fname string, chunksNum int, totalSize int64) (string, error) - - // // retrieve event - - // // - // RetrieveEvent_Audit_SubmitIdleProof(blockhash types.Hash) (event.Event_SubmitIdleProof, error) - // // - // RetrieveEvent_Audit_SubmitServiceProof(blockhash types.Hash) (event.Event_SubmitServiceProof, error) - // // - // RetrieveEvent_Audit_SubmitIdleVerifyResult(blockhash types.Hash) (event.Event_SubmitIdleVerifyResult, error) - // // - // RetrieveEvent_Audit_SubmitServiceVerifyResult(blockhash types.Hash) (event.Event_SubmitServiceVerifyResult, error) - // // - // RetrieveEvent_Oss_OssUpdate(blockhash types.Hash) (event.Event_OssUpdate, error) - // // - // RetrieveEvent_Oss_OssRegister(blockhash types.Hash) (event.Event_OssRegister, error) - // // - // RetrieveEvent_Oss_OssDestroy(blockhash types.Hash) (event.Event_OssDestroy, error) - // // - // RetrieveEvent_Oss_Authorize(blockhash types.Hash) (event.Event_Authorize, error) - // // - // RetrieveEvent_Oss_CancelAuthorize(blockhash types.Hash) (event.Event_CancelAuthorize, error) - // // - // RetrieveEvent_FileBank_UploadDeclaration(blockhash types.Hash) (event.Event_UploadDeclaration, error) - // // - // RetrieveEvent_FileBank_CreateBucket(blockhash types.Hash) (event.Event_CreateBucket, error) - // // - // RetrieveEvent_FileBank_DeleteFile(blockhash types.Hash) (event.Event_DeleteFile, error) - // // - // RetrieveEvent_FileBank_TransferReport(blockhash types.Hash) (event.Event_TransferReport, error) - // // - // RetrieveEvent_FileBank_ClaimRestoralOrder(blockhash types.Hash) (event.Event_ClaimRestoralOrder, error) - // // - // RetrieveEvent_FileBank_RecoveryCompleted(blockhash types.Hash) (event.Event_RecoveryCompleted, error) - // // - // RetrieveEvent_FileBank_IdleSpaceCert(blockhash types.Hash) (event.Event_IdleSpaceCert, error) - // // - // RetrieveEvent_FileBank_ReplaceIdleSpace(blockhash types.Hash) (event.Event_ReplaceIdleSpace, error) - // // - // RetrieveEvent_FileBank_CalculateReport(blockhash types.Hash) (event.Event_CalculateReport, error) - // // - // RetrieveEvent_Sminer_Registered(blockhash types.Hash) (event.Event_Registered, error) - // // - // RetrieveEvent_Sminer_RegisterPoisKey(blockhash types.Hash) (event.Event_RegisterPoisKey, error) - // // - // RetrieveEvent_Sminer_UpdataIp(blockhash types.Hash) (event.Event_UpdatePeerId, error) - // // - // RetrieveEvent_Sminer_UpdataBeneficiary(blockhash types.Hash) (event.Event_UpdateBeneficiary, error) - // // - // RetrieveEvent_Sminer_MinerExitPrep(blockhash types.Hash) (event.Event_MinerExitPrep, error) - // // - // RetrieveEvent_Sminer_IncreaseCollateral(blockhash types.Hash) (event.Event_IncreaseCollateral, error) - // // - // RetrieveEvent_Sminer_Receive(blockhash types.Hash) (event.Event_Receive, error) - // // - // RetrieveEvent_Sminer_Withdraw(blockhash types.Hash) (event.Event_Withdraw, error) - // // - // RetrieveEvent_Sminer_IncreaseDeclarationSpace(blockhash types.Hash) (event.Event_IncreaseDeclarationSpace, error) - // // - // RetrieveEvent_StorageHandler_BuySpace(blockhash types.Hash) (event.Event_BuySpace, error) - // // - // RetrieveEvent_StorageHandler_ExpansionSpace(blockhash types.Hash) (event.Event_ExpansionSpace, error) - // // - // RetrieveEvent_StorageHandler_RenewalSpace(blockhash types.Hash) (event.Event_RenewalSpace, error) - // // - // RetrieveEvent_Balances_Transfer(blockhash types.Hash) (types.EventBalancesTransfer, error) - // // - // RetrieveEvent_FileBank_GenRestoralOrder(blockhash types.Hash) (event.Event_GenerateRestoralOrder, error) - // // - // RetrieveAllEvent_FileBank_UploadDeclaration(blockhash types.Hash) ([]event.AllUploadDeclarationEvent, error) - // // - // RetrieveAllEvent_FileBank_StorageCompleted(blockhash types.Hash) ([]string, error) - // // - // RetrieveAllEvent_FileBank_DeleteFile(blockhash types.Hash) ([]event.AllDeleteFileEvent, error) - // // - // //RetrieveAllEvent(blockhash types.Hash) ([]string, []string, error) - // // - // RetrieveAllEventFromBlock(blockhash types.Hash) ([]string, map[string][]string, error) - // // - // RetrieveBlock(blocknumber uint64) ([]string, []event.ExtrinsicsInfo, []event.TransferInfo, string, string, string, string, int64, error) - // // - // RetrieveBlockAndAll(blocknumber uint64) ([]string, []event.ExtrinsicsInfo, []event.TransferInfo, []string, []string, string, string, string, string, string, int64, error) - // // - // ParseBlockData(blocknumber uint64) (event.BlockData, error) - // // - // InitExtrinsicsName() error + // Audit + QueryChallengeSnapShot(accountID []byte, block int32) (bool, chain.ChallengeInfo, error) + QueryCountedClear(accountID []byte, block int32) (uint8, error) + QueryCountedServiceFailed(accountID []byte, block int32) (uint32, error) + SubmitIdleProof(idleProof []types.U8) (string, error) + SubmitServiceProof(serviceProof []types.U8) (string, error) + + // Balances + QueryTotalIssuance(block int) (string, error) + QueryInactiveIssuance(block int) (string, error) + TransferToken(dest string, amount uint64) (string, string, error) + + // Oss + QueryOss(accountID []byte, block int32) (chain.OssInfo, error) + QueryAllOss(block int32) ([]chain.OssInfo, error) + QueryAllOssPeerId(block int32) ([]string, error) + QueryAuthorityList(accountID []byte, block int32) ([]types.AccountID, error) + Authorize(accountID []byte) (string, error) + CancelAuthorize(accountID []byte) (string, error) + RegisterOss(peerId []byte, domain string) (string, error) + UpdateOss(peerId string, domain string) (string, error) + DestroyOss() (string, error) + + // EVM + SendEvmCall(source types.H160, target types.H160, input types.Bytes, value types.U256, gasLimit types.U64, maxFeePerGas types.U256, accessList []chain.AccessInfo) (string, error) + + // FileBank + QueryBucket(accountID []byte, bucketName string, block int32) (chain.BucketInfo, error) + QueryDealMap(fid string, block int32) (chain.StorageOrder, error) + QueryFile(fid string, block int32) (chain.FileMetadata, error) + QueryRestoralOrder(fragmentHash string, block int32) (chain.RestoralOrderInfo, error) + QueryAllRestoralOrder(block int32) ([]chain.RestoralOrderInfo, error) + QueryAllBucketName(accountID []byte, block int32) ([]string, error) + QueryAllUserFiles(accountID []byte, block int32) ([]string, error) + GenerateStorageOrder(fid string, segment []chain.SegmentDataInfo, owner []byte, filename string, buckname string, filesize uint64) (string, error) + UploadDeclaration(fid string, segment []chain.SegmentList, user chain.UserBrief, filesize uint64) (string, error) + CreateBucket(owner []byte, bucketName string) (string, error) + DeleteBucket(owner []byte, bucketName string) (string, error) + DeleteFile(owner []byte, fid string) (string, error) + TransferReport(index uint8, fid string) (string, error) + GenerateRestoralOrder(fid, fragmentHash string) (string, error) + ClaimRestoralOrder(fragmentHash string) (string, error) + ClaimRestoralNoExistOrder(puk []byte, fid, fragmentHash string) (string, error) + RestoralOrderComplete(fragmentHash string) (string, error) + CertIdleSpace(spaceProofInfo chain.SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk chain.WorkerPublicKey) (string, error) + ReplaceIdleSpace(spaceProofInfo chain.SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk chain.WorkerPublicKey) (string, error) + CalculateReport(teeSig types.Bytes, tagSigInfo chain.TagSigInfo) (string, error) + + // Sminer + QueryExpenders(block int32) (chain.ExpendersInfo, error) + QueryMinerItems(accountID []byte, block int32) (chain.MinerInfo, error) + QueryStakingStartBlock(accountID []byte, block int32) (uint32, error) + QueryAllMiner(block int32) ([]types.AccountID, error) + QueryCounterForMinerItems(block int32) (uint32, error) + QueryRewardMap(accountID []byte, block int32) (chain.MinerReward, error) + QueryRestoralTarget(accountID []byte, block int32) (chain.RestoralTargetInfo, error) + QueryAllRestoralTarget(block int32) ([]chain.RestoralTargetInfo, error) + QueryPendingReplacements(accountID []byte, block int32) (types.U128, error) + QueryCompleteSnapShot(era uint32, block int32) (uint32, uint64, error) + IncreaseCollateral(accountID []byte, token string) (string, error) + IncreaseDeclarationSpace(tibCount uint32) (string, error) + MinerExitPrep() (string, error) + MinerWithdraw() (string, error) + ReceiveReward() (string, error) + RegisterPoisKey(poisKey chain.PoISKeyInfo, teeSignWithAcc, teeSign types.Bytes, teePuk chain.WorkerPublicKey) (string, error) + RegnstkSminer(earnings string, peerId []byte, staking uint64, tibCount uint32) (string, error) + RegnstkAssignStaking(earnings string, peerId []byte, stakingAcc string, tibCount uint32) (string, error) + UpdateBeneficiary(earnings string) (string, error) + UpdateSminerPeerId(peerid chain.PeerId) (string, error) + + // Staking + QueryCounterForValidators(block int) (uint32, error) + QueryValidatorsCount(block int) (uint32, error) + QueryNominatorCount(block int) (uint32, error) + QueryErasTotalStake(era uint32, block int) (string, error) + QueryCurrentEra(block int) (uint32, error) + QueryErasRewardPoints(era uint32, block int32) (chain.StakingEraRewardPoints, error) + QueryAllNominators(block int32) ([]chain.StakingNominations, error) + QueryAllBonded(block int32) ([]types.AccountID, error) + QueryValidatorCommission(accountID []byte, block int) (uint8, error) + QueryEraValidatorReward(era uint32, block int) (string, error) + + // StorageHandler + QueryUnitPrice(block int32) (string, error) + QueryUserOwnedSpace(accountID []byte, block int32) (chain.UserSpaceInfo, error) + QueryTotalIdleSpace(block int32) (uint64, error) + QueryTotalServiceSpace(block int32) (uint64, error) + QueryPurchasedSpace(block int32) (uint64, error) + BuySpace(count uint32) (string, error) + ExpansionSpace(count uint32) (string, error) + RenewalSpace(days uint32) (string, error) + + // System + QueryBlockNumber(blockhash string) (uint32, error) + QueryAccountInfo(account string, block int32) (types.AccountInfo, error) + QueryAccountInfoByAccountID(accountID []byte, block int32) (types.AccountInfo, error) + + // TeeWorker + QueryMasterPubKey(block int32) ([]byte, error) + QueryWorkers(puk chain.WorkerPublicKey, block int32) (chain.WorkerInfo, error) + QueryAllWorkers(block int32) ([]chain.WorkerInfo, error) + QueryEndpoints(puk chain.WorkerPublicKey, block int32) (string, error) + QueryWorkerAddedAt(puk chain.WorkerPublicKey, block int32) (uint32, error) + + // CessTreasury + QueryCurrencyReward(block int32) (string, error) + QueryEraReward(block int32) (string, error) + QueryReserveReward(block int32) (string, error) + QueryRoundReward(era uint32, block int32) (string, error) + + // rpc_call + SystemProperties() (chain.SysProperties, error) + SystemChain() (string, error) + SystemSyncState() (chain.SysSyncState, error) + SystemVersion() (string, error) + NetListening() (bool, error) + + // chain_client + GetSDKName() string + GetCurrentRpcAddr() string + GetRpcState() bool + SetRpcState(state bool) + GetSignatureAcc() string + GetSignatureAccPulickey() []byte + GetSubstrateAPI() *gsrpc.SubstrateAPI + GetMetadata() *types.Metadata + GetTokenSymbol() string + GetNetworkEnv() string + GetURI() string + Sign(msg []byte) ([]byte, error) + Verify(msg []byte, sig []byte) (bool, error) + ReconnectRpc() error + Close() + + // Extrinsics + InitExtrinsicsName() error + + // event + DecodeEventNameFromBlock(block uint64) ([]string, error) + DecodeEventNameFromBlockhash(blockhash types.Hash) ([]string, error) + QueryAllAccountInfoFromBlock(block int) ([]types.AccountInfo, error) + // retrieve event + RetrieveEvent_Audit_SubmitIdleProof(blockhash types.Hash) (chain.Event_SubmitIdleProof, error) + RetrieveEvent_Audit_SubmitServiceProof(blockhash types.Hash) (chain.Event_SubmitServiceProof, error) + RetrieveEvent_Audit_SubmitIdleVerifyResult(blockhash types.Hash) (chain.Event_SubmitIdleVerifyResult, error) + RetrieveEvent_Audit_SubmitServiceVerifyResult(blockhash types.Hash) (chain.Event_SubmitServiceVerifyResult, error) + RetrieveEvent_Oss_OssUpdate(blockhash types.Hash) (chain.Event_OssUpdate, error) + RetrieveEvent_Oss_OssRegister(blockhash types.Hash) (chain.Event_OssRegister, error) + RetrieveEvent_Oss_OssDestroy(blockhash types.Hash) (chain.Event_OssDestroy, error) + RetrieveEvent_Oss_Authorize(blockhash types.Hash) (chain.Event_Authorize, error) + RetrieveEvent_Oss_CancelAuthorize(blockhash types.Hash) (chain.Event_CancelAuthorize, error) + RetrieveEvent_FileBank_UploadDeclaration(blockhash types.Hash) (chain.Event_UploadDeclaration, error) + RetrieveEvent_FileBank_CreateBucket(blockhash types.Hash) (chain.Event_CreateBucket, error) + RetrieveEvent_FileBank_DeleteFile(blockhash types.Hash) (chain.Event_DeleteFile, error) + RetrieveEvent_FileBank_TransferReport(blockhash types.Hash) (chain.Event_TransferReport, error) + RetrieveEvent_FileBank_ClaimRestoralOrder(blockhash types.Hash) (chain.Event_ClaimRestoralOrder, error) + RetrieveEvent_FileBank_RecoveryCompleted(blockhash types.Hash) (chain.Event_RecoveryCompleted, error) + RetrieveEvent_FileBank_IdleSpaceCert(blockhash types.Hash) (chain.Event_IdleSpaceCert, error) + RetrieveEvent_FileBank_ReplaceIdleSpace(blockhash types.Hash) (chain.Event_ReplaceIdleSpace, error) + RetrieveEvent_FileBank_CalculateReport(blockhash types.Hash) (chain.Event_CalculateReport, error) + RetrieveEvent_Sminer_Registered(blockhash types.Hash) (chain.Event_Registered, error) + RetrieveEvent_Sminer_RegisterPoisKey(blockhash types.Hash) (chain.Event_RegisterPoisKey, error) + RetrieveEvent_Sminer_UpdataIp(blockhash types.Hash) (chain.Event_UpdatePeerId, error) + RetrieveEvent_Sminer_UpdataBeneficiary(blockhash types.Hash) (chain.Event_UpdateBeneficiary, error) + RetrieveEvent_Sminer_MinerExitPrep(blockhash types.Hash) (chain.Event_MinerExitPrep, error) + RetrieveEvent_Sminer_IncreaseCollateral(blockhash types.Hash) (chain.Event_IncreaseCollateral, error) + RetrieveEvent_Sminer_Receive(blockhash types.Hash) (chain.Event_Receive, error) + RetrieveEvent_Sminer_Withdraw(blockhash types.Hash) (chain.Event_Withdraw, error) + RetrieveEvent_Sminer_IncreaseDeclarationSpace(blockhash types.Hash) (chain.Event_IncreaseDeclarationSpace, error) + RetrieveEvent_StorageHandler_BuySpace(blockhash types.Hash) (chain.Event_BuySpace, error) + RetrieveEvent_StorageHandler_ExpansionSpace(blockhash types.Hash) (chain.Event_ExpansionSpace, error) + RetrieveEvent_StorageHandler_RenewalSpace(blockhash types.Hash) (chain.Event_RenewalSpace, error) + RetrieveEvent_Balances_Transfer(blockhash types.Hash) (types.EventBalancesTransfer, error) + RetrieveEvent_FileBank_GenRestoralOrder(blockhash types.Hash) (chain.Event_GenerateRestoralOrder, error) + RetrieveAllEvent_FileBank_UploadDeclaration(blockhash types.Hash) ([]chain.AllUploadDeclarationEvent, error) + RetrieveAllEvent_FileBank_StorageCompleted(blockhash types.Hash) ([]string, error) + RetrieveAllEvent_FileBank_DeleteFile(blockhash types.Hash) ([]chain.AllDeleteFileEvent, error) + RetrieveAllEventFromBlock(blockhash types.Hash) ([]string, map[string][]string, error) + RetrieveBlock(blocknumber uint64) ([]string, []chain.ExtrinsicsInfo, []chain.TransferInfo, string, string, string, string, int64, error) + RetrieveBlockAndAll(blocknumber uint64) ([]string, []chain.ExtrinsicsInfo, []chain.TransferInfo, []string, []string, string, string, string, string, string, int64, error) + ParseBlockData(blocknumber uint64) (chain.BlockData, error) } diff --git a/example/file/file.go b/example/file/file.go index b14235d..36b8362 100755 --- a/example/file/file.go +++ b/example/file/file.go @@ -9,15 +9,12 @@ package main import ( "bytes" - "context" "fmt" "io" "log" "time" - cess "github.com/CESSProject/cess-go-sdk" - "github.com/CESSProject/cess-go-sdk/chain" - "github.com/CESSProject/cess-go-sdk/utils" + "github.com/CESSProject/cess-go-sdk/core/process" ) // Substrate well-known mnemonic: @@ -40,50 +37,28 @@ const UploadFile = "file.go" const BucketName = "myBucket" func main() { - // 1. new sdk - sdk, err := NewSDK() - if err != nil { - panic(err) - } - - // 2. buy space - _, err = sdk.BuySpace(1) - if err != nil { - panic(err) - } - - // 3. authorize space to deoss - pub, err := utils.ParsingPublickey(PublicGatewayAccount) - if err != nil { - panic(err) - } - _, err = sdk.Authorize(pub) - if err != nil { - panic(err) - } - - // 4. upload file to deoss - fid, err := sdk.StoreFile(PublicGateway, UploadFile, BucketName) + // upload file to gateway + fid, err := process.StoreFile(PublicGateway, UploadFile, BucketName, MY_MNEMONIC) if err != nil { panic(err) } log.Println("fid:", fid) - // 5. downloag file from deoss - err = sdk.RetrieveFile(PublicGateway, fid, fmt.Sprintf("download_%d", time.Now().UnixNano())) + // downloag file from gateway + err = process.RetrieveFile(PublicGateway, fid, MY_MNEMONIC, fmt.Sprintf("download_%d", time.Now().UnixNano())) if err != nil { panic(err) } - // 6. upload object to deoss - fid, err = sdk.StoreObject(PublicGateway, bytes.NewReader([]byte("test date")), BucketName) + // upload object to gateway + fid, err = process.StoreObject(PublicGateway, BucketName, MY_MNEMONIC, bytes.NewReader([]byte("test date"))) if err != nil { panic(err) } log.Println("fid:", fid) - // 7. download object from deoss - body, err := sdk.RetrieveObject(PublicGateway, fid) + // download object from gateway + body, err := process.RetrieveObject(PublicGateway, fid, MY_MNEMONIC) if err != nil { panic(err) } @@ -94,12 +69,3 @@ func main() { } fmt.Println(string(data)) } - -func NewSDK() (*chain.ChainClient, error) { - return cess.New( - context.Background(), - cess.ConnectRpcAddrs(RPC_ADDRS), - cess.Mnemonic(MY_MNEMONIC), - cess.TransactionTimeout(time.Second*10), - ) -} diff --git a/example/other/main.go b/example/other/main.go deleted file mode 100644 index d9f5ca3..0000000 --- a/example/other/main.go +++ /dev/null @@ -1,148 +0,0 @@ -/* - Copyright (C) CESS. All rights reserved. - Copyright (C) Cumulus Encrypted Storage System. All rights reserved. - - SPDX-License-Identifier: Apache-2.0 -*/ - -package main - -import ( - "errors" - "fmt" - "log" - - gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4" - "github.com/centrifuge/go-substrate-rpc-client/v4/types" - "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" - "github.com/centrifuge/go-substrate-rpc-client/v4/xxhash" - "github.com/decred/base58" - "golang.org/x/crypto/blake2b" -) - -var RPC_ADDRS = []string{ - //testnet - "wss://testnet-rpc0.cess.cloud/ws/", - "wss://testnet-rpc1.cess.cloud/ws/", - "wss://testnet-rpc2.cess.cloud/ws/", -} - -func main() { - accounts, err := GetAllAccountInfoFromBlock(1) - if err != nil { - log.Fatal(err) - } - fmt.Println(len(accounts), " : ", accounts) -} - -func GetAllAccountInfoFromBlock(block int) ([]types.AccountInfo, error) { - var ( - err error - c *gsrpc.SubstrateAPI - ) - for i := 0; i < len(RPC_ADDRS); i++ { - c, err = gsrpc.NewSubstrateAPI(RPC_ADDRS[i]) - if err == nil { - break - } - } - if c == nil { - log.Fatal("all rpc addresses are unavailable") - } - - defer c.Client.Close() - - var data []types.AccountInfo - - key := createPrefixedKey("System", "Account") - - // get all account information from the latest block - if block < 0 { - keys, err := c.RPC.State.GetKeysLatest(key) - if err != nil { - return nil, err - } - set, err := c.RPC.State.QueryStorageAtLatest(keys) - if err != nil { - return nil, err - } - for _, elem := range set { - for _, change := range elem.Changes { - var storageData types.AccountInfo - if err := codec.Decode(change.StorageData, &storageData); err != nil { - fmt.Println("Decode StorageData:", err) - continue - } - var storageKey types.AccountID - if err := codec.Decode(change.StorageKey, &storageKey); err != nil { - fmt.Println("Decode StorageKey:", err) - continue - } - fmt.Println(encodePublicKeyAsAccount(storageKey[:])) - data = append(data, storageData) - } - } - return data, nil - } - - // get all account information from the block - - blockhash, err := c.RPC.Chain.GetBlockHash(uint64(block)) - if err != nil { - return data, err - } - - keys, err := c.RPC.State.GetKeys(key, blockhash) - if err != nil { - return nil, err - } - set, err := c.RPC.State.QueryStorageAt(keys, blockhash) - if err != nil { - return nil, err - } - for _, elem := range set { - for _, change := range elem.Changes { - if change.HasStorageData { - var storageData types.AccountInfo - if err := codec.Decode(change.StorageData, &storageData); err != nil { - fmt.Println("Decode StorageData err:", err) - continue - } - var storageKey types.AccountID - if err := codec.Decode(change.StorageKey, &storageKey); err != nil { - fmt.Println("Decode StorageKey err:", err) - continue - } - fmt.Println(encodePublicKeyAsAccount(storageKey[:])) - data = append(data, storageData) - } - } - } - return data, nil -} - -func createPrefixedKey(pallet, method string) []byte { - return append(xxhash.New128([]byte(pallet)).Sum(nil), xxhash.New128([]byte(method)).Sum(nil)...) -} - -func encodePublicKeyAsAccount(publicKey []byte) (string, error) { - if len(publicKey) != 32 { - return "", errors.New("invalid public key") - } - payload := appendBytes([]byte{0x50, 0xac}, publicKey) - input := appendBytes([]byte{0x53, 0x53, 0x35, 0x38, 0x50, 0x52, 0x45}, payload) - ck := blake2b.Sum512(input) - checkum := ck[:2] - address := base58.Encode(appendBytes(payload, checkum)) - if address == "" { - return address, errors.New("public key encoding failed") - } - return address, nil -} - -func appendBytes(data1, data2 []byte) []byte { - if data2 == nil { - return data1 - } - return append(data1, data2...) -} diff --git a/go.mod b/go.mod index 88d22b9..70c82d3 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,9 @@ module github.com/CESSProject/cess-go-sdk go 1.20 require ( - github.com/CESSProject/go-keyring v0.0.0-20220614131247-ee3a8da30fde 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.2.1 - github.com/decred/base58 v1.0.4 github.com/ethereum/go-ethereum v1.10.20 github.com/go-ping/ping v1.1.0 github.com/google/uuid v1.5.0 @@ -31,6 +29,7 @@ require ( github.com/cosmos/go-bip39 v1.0.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/deckarep/golang-set v1.8.0 // indirect + github.com/decred/base58 v1.0.4 // indirect github.com/decred/dcrd/crypto/blake256 v1.0.1 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect github.com/go-ole/go-ole v1.2.6 // indirect diff --git a/go.sum b/go.sum index aa69c1c..9968d4f 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -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/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/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= From e7b8022dcb88cf4f4e4f2d0331752ad8ccbc9d3d Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Sat, 11 May 2024 14:22:11 +0800 Subject: [PATCH 17/18] update extrinsic_name --- README.md | 14 ++++++-------- chain/extrinsic_name.go | 40 ++++++++++++++++++++++++++++++++++++++++ example/new/sdk.go | 1 - version.go | 2 +- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 363c8f8..58e54a4 100755 --- a/README.md +++ b/README.md @@ -22,10 +22,11 @@ If you find any system errors or you have better suggestions, please submit an i wss://testnet-rpc0.cess.cloud/ws/ wss://testnet-rpc1.cess.cloud/ws/ wss://testnet-rpc2.cess.cloud/ws/ +wss://testnet-rpc3.cess.cloud/ws/ ``` **CESS test network bootstrap node** ``` -_dnsaddr.boot-bucket-testnet.cess.cloud +_dnsaddr.boot-miner-testnet.cess.cloud ``` ## 🚰 CESS test network faucet @@ -38,19 +39,16 @@ https://testnet-faucet.cess.cloud/ To get the package use the standard: ```sh -go get -u "github.com/CESSProject/cess-go-sdk" +go get "github.com/CESSProject/cess-go-sdk" ``` ## ✅ Testing To run test: -1. Run a [CESS node](https://github.com/CESSProject/cess) locally. -2. Run the command - - ```sh - go test -v - ``` +```sh +make check +``` ## 📖 Document diff --git a/chain/extrinsic_name.go b/chain/extrinsic_name.go index bfd2503..299d0a9 100644 --- a/chain/extrinsic_name.go +++ b/chain/extrinsic_name.go @@ -219,6 +219,14 @@ const ( ExtName_Proxy_remove_proxies = "Proxy.remove_proxies" ExtName_Proxy_remove_proxy = "Proxy.remove_proxy" + // Reservoir + ExtName_Reservoir_attend_evnet = "Reservoir.attend_evnet" + ExtName_Reservoir_create_event = "Reservoir.create_event" + ExtName_Reservoir_event_withdraw = "Reservoir.event_withdraw" + ExtName_Reservoir_filling = "Reservoir.filling" + ExtName_Reservoir_store = "Reservoir.store" + ExtName_Reservoir_withdraw = "Reservoir.withdraw" + // Scheduler ExtName_Scheduler_cancel = "Scheduler.cancel" ExtName_Scheduler_cancel_named = "Scheduler.cancel_named" @@ -1198,6 +1206,38 @@ func (c *ChainClient) InitExtrinsicsName() error { return err } + // Reservoir + if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_Reservoir_attend_evnet); err == nil { + ExtrinsicsName[callIndex] = ExtName_Reservoir_attend_evnet + } else { + return err + } + if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_Reservoir_create_event); err == nil { + ExtrinsicsName[callIndex] = ExtName_Reservoir_create_event + } else { + return err + } + if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_Reservoir_event_withdraw); err == nil { + ExtrinsicsName[callIndex] = ExtName_Reservoir_event_withdraw + } else { + return err + } + if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_Reservoir_filling); err == nil { + ExtrinsicsName[callIndex] = ExtName_Reservoir_filling + } else { + return err + } + if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_Reservoir_store); err == nil { + ExtrinsicsName[callIndex] = ExtName_Reservoir_store + } else { + return err + } + if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_Reservoir_withdraw); err == nil { + ExtrinsicsName[callIndex] = ExtName_Reservoir_withdraw + } else { + return err + } + // Scheduler if callIndex, err := c.GetMetadata().FindCallIndex(ExtName_Scheduler_cancel); err == nil { ExtrinsicsName[callIndex] = ExtName_Scheduler_cancel diff --git a/example/new/sdk.go b/example/new/sdk.go index 3c8224e..11094aa 100755 --- a/example/new/sdk.go +++ b/example/new/sdk.go @@ -51,7 +51,6 @@ func main() { fmt.Println(sdk.InitExtrinsicsName()) fmt.Println(sdk.GetCurrentRpcAddr()) - //fmt.Println(sdk.QueryRoundReward(0, -1)) return blockhash, err := sdk.GetSubstrateAPI().RPC.Chain.GetBlockHash(180) diff --git a/version.go b/version.go index 3144802..139d097 100755 --- a/version.go +++ b/version.go @@ -8,4 +8,4 @@ package sdkgo // SDK Version -const Version = "0.5.0.25" +const Version = "0.5.2" From 0962272b87ade71b0f1ac438c2e7c6095527b9ad Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Sat, 11 May 2024 15:42:36 +0800 Subject: [PATCH 18/18] update sdk --- chain/chain.go | 2 +- core/sdk/sdk.go => chain/chainer.go | 140 ++++++++++++++-------------- chain/pattern.go | 3 +- 3 files changed, 71 insertions(+), 74 deletions(-) rename core/sdk/sdk.go => chain/chainer.go (52%) diff --git a/chain/chain.go b/chain/chain.go index 6b78416..fb82a7e 100755 --- a/chain/chain.go +++ b/chain/chain.go @@ -44,7 +44,7 @@ type ChainClient struct { rpcState bool } -// var _ sdk.SDK = (*ChainClient)(nil) +var _ Chainer = (*ChainClient)(nil) // NewChainClient creates a chainclient // - ctx: context diff --git a/core/sdk/sdk.go b/chain/chainer.go similarity index 52% rename from core/sdk/sdk.go rename to chain/chainer.go index 5d7314e..f813248 100755 --- a/core/sdk/sdk.go +++ b/chain/chainer.go @@ -5,18 +5,17 @@ SPDX-License-Identifier: Apache-2.0 */ -package sdk +package chain import ( - "github.com/CESSProject/cess-go-sdk/chain" gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4" "github.com/centrifuge/go-substrate-rpc-client/v4/types" ) -// CESS Go SDK Interface Description -type SDK interface { +// Chain client interface +type Chainer interface { // Audit - QueryChallengeSnapShot(accountID []byte, block int32) (bool, chain.ChallengeInfo, error) + QueryChallengeSnapShot(accountID []byte, block int32) (bool, ChallengeInfo, error) QueryCountedClear(accountID []byte, block int32) (uint8, error) QueryCountedServiceFailed(accountID []byte, block int32) (uint32, error) SubmitIdleProof(idleProof []types.U8) (string, error) @@ -28,8 +27,8 @@ type SDK interface { TransferToken(dest string, amount uint64) (string, string, error) // Oss - QueryOss(accountID []byte, block int32) (chain.OssInfo, error) - QueryAllOss(block int32) ([]chain.OssInfo, error) + QueryOss(accountID []byte, block int32) (OssInfo, error) + QueryAllOss(block int32) ([]OssInfo, error) QueryAllOssPeerId(block int32) ([]string, error) QueryAuthorityList(accountID []byte, block int32) ([]types.AccountID, error) Authorize(accountID []byte) (string, error) @@ -39,18 +38,18 @@ type SDK interface { DestroyOss() (string, error) // EVM - SendEvmCall(source types.H160, target types.H160, input types.Bytes, value types.U256, gasLimit types.U64, maxFeePerGas types.U256, accessList []chain.AccessInfo) (string, error) + SendEvmCall(source types.H160, target types.H160, input types.Bytes, value types.U256, gasLimit types.U64, maxFeePerGas types.U256, accessList []AccessInfo) (string, error) // FileBank - QueryBucket(accountID []byte, bucketName string, block int32) (chain.BucketInfo, error) - QueryDealMap(fid string, block int32) (chain.StorageOrder, error) - QueryFile(fid string, block int32) (chain.FileMetadata, error) - QueryRestoralOrder(fragmentHash string, block int32) (chain.RestoralOrderInfo, error) - QueryAllRestoralOrder(block int32) ([]chain.RestoralOrderInfo, error) + QueryBucket(accountID []byte, bucketName string, block int32) (BucketInfo, error) + QueryDealMap(fid string, block int32) (StorageOrder, error) + QueryFile(fid string, block int32) (FileMetadata, error) + QueryRestoralOrder(fragmentHash string, block int32) (RestoralOrderInfo, error) + QueryAllRestoralOrder(block int32) ([]RestoralOrderInfo, error) QueryAllBucketName(accountID []byte, block int32) ([]string, error) QueryAllUserFiles(accountID []byte, block int32) ([]string, error) - GenerateStorageOrder(fid string, segment []chain.SegmentDataInfo, owner []byte, filename string, buckname string, filesize uint64) (string, error) - UploadDeclaration(fid string, segment []chain.SegmentList, user chain.UserBrief, filesize uint64) (string, error) + GenerateStorageOrder(fid string, segment []SegmentDataInfo, owner []byte, filename string, buckname string, filesize 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) DeleteFile(owner []byte, fid string) (string, error) @@ -59,19 +58,19 @@ type SDK interface { ClaimRestoralOrder(fragmentHash string) (string, error) ClaimRestoralNoExistOrder(puk []byte, fid, fragmentHash string) (string, error) RestoralOrderComplete(fragmentHash string) (string, error) - CertIdleSpace(spaceProofInfo chain.SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk chain.WorkerPublicKey) (string, error) - ReplaceIdleSpace(spaceProofInfo chain.SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk chain.WorkerPublicKey) (string, error) - CalculateReport(teeSig types.Bytes, tagSigInfo chain.TagSigInfo) (string, error) + CertIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk WorkerPublicKey) (string, error) + ReplaceIdleSpace(spaceProofInfo SpaceProofInfo, teeSignWithAcc, teeSign types.Bytes, teePuk WorkerPublicKey) (string, error) + CalculateReport(teeSig types.Bytes, tagSigInfo TagSigInfo) (string, error) // Sminer - QueryExpenders(block int32) (chain.ExpendersInfo, error) - QueryMinerItems(accountID []byte, block int32) (chain.MinerInfo, error) + QueryExpenders(block int32) (ExpendersInfo, error) + QueryMinerItems(accountID []byte, block int32) (MinerInfo, error) QueryStakingStartBlock(accountID []byte, block int32) (uint32, error) QueryAllMiner(block int32) ([]types.AccountID, error) QueryCounterForMinerItems(block int32) (uint32, error) - QueryRewardMap(accountID []byte, block int32) (chain.MinerReward, error) - QueryRestoralTarget(accountID []byte, block int32) (chain.RestoralTargetInfo, error) - QueryAllRestoralTarget(block int32) ([]chain.RestoralTargetInfo, error) + QueryRewardMap(accountID []byte, block int32) (MinerReward, error) + QueryRestoralTarget(accountID []byte, block int32) (RestoralTargetInfo, error) + QueryAllRestoralTarget(block int32) ([]RestoralTargetInfo, error) QueryPendingReplacements(accountID []byte, block int32) (types.U128, error) QueryCompleteSnapShot(era uint32, block int32) (uint32, uint64, error) IncreaseCollateral(accountID []byte, token string) (string, error) @@ -79,11 +78,11 @@ type SDK interface { MinerExitPrep() (string, error) MinerWithdraw() (string, error) ReceiveReward() (string, error) - RegisterPoisKey(poisKey chain.PoISKeyInfo, teeSignWithAcc, teeSign types.Bytes, teePuk chain.WorkerPublicKey) (string, error) + RegisterPoisKey(poisKey PoISKeyInfo, teeSignWithAcc, teeSign types.Bytes, teePuk WorkerPublicKey) (string, error) RegnstkSminer(earnings string, peerId []byte, staking uint64, tibCount uint32) (string, error) RegnstkAssignStaking(earnings string, peerId []byte, stakingAcc string, tibCount uint32) (string, error) UpdateBeneficiary(earnings string) (string, error) - UpdateSminerPeerId(peerid chain.PeerId) (string, error) + UpdateSminerPeerId(peerid PeerId) (string, error) // Staking QueryCounterForValidators(block int) (uint32, error) @@ -91,15 +90,15 @@ type SDK interface { QueryNominatorCount(block int) (uint32, error) QueryErasTotalStake(era uint32, block int) (string, error) QueryCurrentEra(block int) (uint32, error) - QueryErasRewardPoints(era uint32, block int32) (chain.StakingEraRewardPoints, error) - QueryAllNominators(block int32) ([]chain.StakingNominations, error) + QueryErasRewardPoints(era uint32, block int32) (StakingEraRewardPoints, error) + QueryAllNominators(block int32) ([]StakingNominations, error) QueryAllBonded(block int32) ([]types.AccountID, error) QueryValidatorCommission(accountID []byte, block int) (uint8, error) QueryEraValidatorReward(era uint32, block int) (string, error) // StorageHandler QueryUnitPrice(block int32) (string, error) - QueryUserOwnedSpace(accountID []byte, block int32) (chain.UserSpaceInfo, error) + QueryUserOwnedSpace(accountID []byte, block int32) (UserSpaceInfo, error) QueryTotalIdleSpace(block int32) (uint64, error) QueryTotalServiceSpace(block int32) (uint64, error) QueryPurchasedSpace(block int32) (uint64, error) @@ -114,10 +113,10 @@ type SDK interface { // TeeWorker QueryMasterPubKey(block int32) ([]byte, error) - QueryWorkers(puk chain.WorkerPublicKey, block int32) (chain.WorkerInfo, error) - QueryAllWorkers(block int32) ([]chain.WorkerInfo, error) - QueryEndpoints(puk chain.WorkerPublicKey, block int32) (string, error) - QueryWorkerAddedAt(puk chain.WorkerPublicKey, block int32) (uint32, error) + QueryWorkers(puk WorkerPublicKey, block int32) (WorkerInfo, error) + QueryAllWorkers(block int32) ([]WorkerInfo, error) + QueryEndpoints(puk WorkerPublicKey, block int32) (string, error) + QueryWorkerAddedAt(puk WorkerPublicKey, block int32) (uint32, error) // CessTreasury QueryCurrencyReward(block int32) (string, error) @@ -126,9 +125,9 @@ type SDK interface { QueryRoundReward(era uint32, block int32) (string, error) // rpc_call - SystemProperties() (chain.SysProperties, error) + SystemProperties() (SysProperties, error) SystemChain() (string, error) - SystemSyncState() (chain.SysSyncState, error) + SystemSyncState() (SysSyncState, error) SystemVersion() (string, error) NetListening() (bool, error) @@ -155,45 +154,44 @@ type SDK interface { // event DecodeEventNameFromBlock(block uint64) ([]string, error) DecodeEventNameFromBlockhash(blockhash types.Hash) ([]string, error) - QueryAllAccountInfoFromBlock(block int) ([]types.AccountInfo, error) // retrieve event - RetrieveEvent_Audit_SubmitIdleProof(blockhash types.Hash) (chain.Event_SubmitIdleProof, error) - RetrieveEvent_Audit_SubmitServiceProof(blockhash types.Hash) (chain.Event_SubmitServiceProof, error) - RetrieveEvent_Audit_SubmitIdleVerifyResult(blockhash types.Hash) (chain.Event_SubmitIdleVerifyResult, error) - RetrieveEvent_Audit_SubmitServiceVerifyResult(blockhash types.Hash) (chain.Event_SubmitServiceVerifyResult, error) - RetrieveEvent_Oss_OssUpdate(blockhash types.Hash) (chain.Event_OssUpdate, error) - RetrieveEvent_Oss_OssRegister(blockhash types.Hash) (chain.Event_OssRegister, error) - RetrieveEvent_Oss_OssDestroy(blockhash types.Hash) (chain.Event_OssDestroy, error) - RetrieveEvent_Oss_Authorize(blockhash types.Hash) (chain.Event_Authorize, error) - RetrieveEvent_Oss_CancelAuthorize(blockhash types.Hash) (chain.Event_CancelAuthorize, error) - RetrieveEvent_FileBank_UploadDeclaration(blockhash types.Hash) (chain.Event_UploadDeclaration, error) - RetrieveEvent_FileBank_CreateBucket(blockhash types.Hash) (chain.Event_CreateBucket, error) - RetrieveEvent_FileBank_DeleteFile(blockhash types.Hash) (chain.Event_DeleteFile, error) - RetrieveEvent_FileBank_TransferReport(blockhash types.Hash) (chain.Event_TransferReport, error) - RetrieveEvent_FileBank_ClaimRestoralOrder(blockhash types.Hash) (chain.Event_ClaimRestoralOrder, error) - RetrieveEvent_FileBank_RecoveryCompleted(blockhash types.Hash) (chain.Event_RecoveryCompleted, error) - RetrieveEvent_FileBank_IdleSpaceCert(blockhash types.Hash) (chain.Event_IdleSpaceCert, error) - RetrieveEvent_FileBank_ReplaceIdleSpace(blockhash types.Hash) (chain.Event_ReplaceIdleSpace, error) - RetrieveEvent_FileBank_CalculateReport(blockhash types.Hash) (chain.Event_CalculateReport, error) - RetrieveEvent_Sminer_Registered(blockhash types.Hash) (chain.Event_Registered, error) - RetrieveEvent_Sminer_RegisterPoisKey(blockhash types.Hash) (chain.Event_RegisterPoisKey, error) - RetrieveEvent_Sminer_UpdataIp(blockhash types.Hash) (chain.Event_UpdatePeerId, error) - RetrieveEvent_Sminer_UpdataBeneficiary(blockhash types.Hash) (chain.Event_UpdateBeneficiary, error) - RetrieveEvent_Sminer_MinerExitPrep(blockhash types.Hash) (chain.Event_MinerExitPrep, error) - RetrieveEvent_Sminer_IncreaseCollateral(blockhash types.Hash) (chain.Event_IncreaseCollateral, error) - RetrieveEvent_Sminer_Receive(blockhash types.Hash) (chain.Event_Receive, error) - RetrieveEvent_Sminer_Withdraw(blockhash types.Hash) (chain.Event_Withdraw, error) - RetrieveEvent_Sminer_IncreaseDeclarationSpace(blockhash types.Hash) (chain.Event_IncreaseDeclarationSpace, error) - RetrieveEvent_StorageHandler_BuySpace(blockhash types.Hash) (chain.Event_BuySpace, error) - RetrieveEvent_StorageHandler_ExpansionSpace(blockhash types.Hash) (chain.Event_ExpansionSpace, error) - RetrieveEvent_StorageHandler_RenewalSpace(blockhash types.Hash) (chain.Event_RenewalSpace, error) + RetrieveEvent_Audit_SubmitIdleProof(blockhash types.Hash) (Event_SubmitIdleProof, error) + RetrieveEvent_Audit_SubmitServiceProof(blockhash types.Hash) (Event_SubmitServiceProof, error) + RetrieveEvent_Audit_SubmitIdleVerifyResult(blockhash types.Hash) (Event_SubmitIdleVerifyResult, error) + RetrieveEvent_Audit_SubmitServiceVerifyResult(blockhash types.Hash) (Event_SubmitServiceVerifyResult, error) + RetrieveEvent_Oss_OssUpdate(blockhash types.Hash) (Event_OssUpdate, error) + RetrieveEvent_Oss_OssRegister(blockhash types.Hash) (Event_OssRegister, error) + RetrieveEvent_Oss_OssDestroy(blockhash types.Hash) (Event_OssDestroy, error) + RetrieveEvent_Oss_Authorize(blockhash types.Hash) (Event_Authorize, error) + RetrieveEvent_Oss_CancelAuthorize(blockhash types.Hash) (Event_CancelAuthorize, error) + RetrieveEvent_FileBank_UploadDeclaration(blockhash types.Hash) (Event_UploadDeclaration, error) + RetrieveEvent_FileBank_CreateBucket(blockhash types.Hash) (Event_CreateBucket, error) + RetrieveEvent_FileBank_DeleteFile(blockhash types.Hash) (Event_DeleteFile, error) + RetrieveEvent_FileBank_TransferReport(blockhash types.Hash) (Event_TransferReport, error) + RetrieveEvent_FileBank_ClaimRestoralOrder(blockhash types.Hash) (Event_ClaimRestoralOrder, error) + RetrieveEvent_FileBank_RecoveryCompleted(blockhash types.Hash) (Event_RecoveryCompleted, error) + RetrieveEvent_FileBank_IdleSpaceCert(blockhash types.Hash) (Event_IdleSpaceCert, error) + RetrieveEvent_FileBank_ReplaceIdleSpace(blockhash types.Hash) (Event_ReplaceIdleSpace, error) + RetrieveEvent_FileBank_CalculateReport(blockhash types.Hash) (Event_CalculateReport, error) + RetrieveEvent_Sminer_Registered(blockhash types.Hash) (Event_Registered, error) + RetrieveEvent_Sminer_RegisterPoisKey(blockhash types.Hash) (Event_RegisterPoisKey, error) + RetrieveEvent_Sminer_UpdataIp(blockhash types.Hash) (Event_UpdatePeerId, error) + RetrieveEvent_Sminer_UpdataBeneficiary(blockhash types.Hash) (Event_UpdateBeneficiary, error) + RetrieveEvent_Sminer_MinerExitPrep(blockhash types.Hash) (Event_MinerExitPrep, error) + RetrieveEvent_Sminer_IncreaseCollateral(blockhash types.Hash) (Event_IncreaseCollateral, error) + RetrieveEvent_Sminer_Receive(blockhash types.Hash) (Event_Receive, error) + RetrieveEvent_Sminer_Withdraw(blockhash types.Hash) (Event_Withdraw, error) + RetrieveEvent_Sminer_IncreaseDeclarationSpace(blockhash types.Hash) (Event_IncreaseDeclarationSpace, error) + RetrieveEvent_StorageHandler_BuySpace(blockhash types.Hash) (Event_BuySpace, error) + RetrieveEvent_StorageHandler_ExpansionSpace(blockhash types.Hash) (Event_ExpansionSpace, error) + RetrieveEvent_StorageHandler_RenewalSpace(blockhash types.Hash) (Event_RenewalSpace, error) RetrieveEvent_Balances_Transfer(blockhash types.Hash) (types.EventBalancesTransfer, error) - RetrieveEvent_FileBank_GenRestoralOrder(blockhash types.Hash) (chain.Event_GenerateRestoralOrder, error) - RetrieveAllEvent_FileBank_UploadDeclaration(blockhash types.Hash) ([]chain.AllUploadDeclarationEvent, error) + RetrieveEvent_FileBank_GenRestoralOrder(blockhash types.Hash) (Event_GenerateRestoralOrder, error) + RetrieveAllEvent_FileBank_UploadDeclaration(blockhash types.Hash) ([]AllUploadDeclarationEvent, error) RetrieveAllEvent_FileBank_StorageCompleted(blockhash types.Hash) ([]string, error) - RetrieveAllEvent_FileBank_DeleteFile(blockhash types.Hash) ([]chain.AllDeleteFileEvent, error) + RetrieveAllEvent_FileBank_DeleteFile(blockhash types.Hash) ([]AllDeleteFileEvent, error) RetrieveAllEventFromBlock(blockhash types.Hash) ([]string, map[string][]string, error) - RetrieveBlock(blocknumber uint64) ([]string, []chain.ExtrinsicsInfo, []chain.TransferInfo, string, string, string, string, int64, error) - RetrieveBlockAndAll(blocknumber uint64) ([]string, []chain.ExtrinsicsInfo, []chain.TransferInfo, []string, []string, string, string, string, string, string, int64, error) - ParseBlockData(blocknumber uint64) (chain.BlockData, error) + RetrieveBlock(blocknumber uint64) ([]string, []ExtrinsicsInfo, []TransferInfo, string, string, string, string, int64, error) + RetrieveBlockAndAll(blocknumber uint64) ([]string, []ExtrinsicsInfo, []TransferInfo, []string, []string, string, string, string, string, string, int64, error) + ParseBlockData(blocknumber uint64) (BlockData, error) } diff --git a/chain/pattern.go b/chain/pattern.go index 0bddb9d..1a7e9eb 100644 --- a/chain/pattern.go +++ b/chain/pattern.go @@ -233,8 +233,7 @@ var ( ERR_RPC_TIMEOUT = errors.New("timeout") ERR_RPC_EMPTY_VALUE = errors.New("empty") ERR_RPC_PRIORITYTOOLOW = "Priority is too low" - - ERR_IdleProofIsEmpty = errors.New("idle data proof is empty") + ERR_IdleProofIsEmpty = errors.New("idle data proof is empty") ) const (