From 0f967addfb70246a2f79a41bef087f590775f64e Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Sun, 28 Apr 2024 18:04:23 +0800 Subject: [PATCH] Substats chall (#203) * add Punishment * update QueryChallengeInfo * add QueryCompleteSnapShot and QueryRoundReward --- chain/audit.go | 19 +++++++++-- chain/chain.go | 13 +++++++ chain/event.go | 72 +++++++++++++++++++++++++++++++++----- chain/sminer.go | 58 +++++++++++++++++++++++++++++++ chain/treasury.go | 76 +++++++++++++++++++++++++++++++++++++++++ core/event/events.go | 8 +++++ core/pattern/pattern.go | 16 +++++++++ core/sdk/sdk.go | 9 ++++- example/new/sdk.go | 8 ++--- 9 files changed, 262 insertions(+), 17 deletions(-) create mode 100644 chain/treasury.go diff --git a/chain/audit.go b/chain/audit.go index cf077b6..3454ca1 100755 --- a/chain/audit.go +++ b/chain/audit.go @@ -50,7 +50,7 @@ func (c *ChainClient) QueryChallengeVerifyExpiration() (uint32, error) { return uint32(data), nil } -func (c *ChainClient) QueryChallengeInfo(accountID []byte) (bool, pattern.ChallengeInfo, error) { +func (c *ChainClient) QueryChallengeInfo(accountID []byte, block int32) (bool, pattern.ChallengeInfo, error) { defer func() { if err := recover(); err != nil { log.Println(utils.RecoverError(err)) @@ -70,13 +70,26 @@ func (c *ChainClient) QueryChallengeInfo(accountID []byte) (bool, pattern.Challe return false, 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(), pattern.AUDIT, pattern.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(), pattern.AUDIT, pattern.CHALLENGESNAPSHOT, err) c.SetChainState(false) return false, data, err } - return ok, data, nil } diff --git a/chain/chain.go b/chain/chain.go index f322643..ba11e27 100755 --- a/chain/chain.go +++ b/chain/chain.go @@ -50,6 +50,7 @@ type ChainClient struct { tokenSymbol string networkEnv string signatureAcc string + treasuryAcc string name string chainState bool } @@ -143,6 +144,14 @@ func NewChainClient( 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 } @@ -231,6 +240,10 @@ func (c *ChainClient) GetURI() string { return c.keyring.URI } +func (c *ChainClient) GetTreasuryAccount() string { + return c.treasuryAcc +} + func (c *ChainClient) Sign(msg []byte) ([]byte, error) { return signature.Sign(msg, c.keyring.URI) } diff --git a/chain/event.go b/chain/event.go index 8e5424c..188dd68 100755 --- a/chain/event.go +++ b/chain/event.go @@ -2170,6 +2170,14 @@ func (c *ChainClient) ParseBlockData(blocknumber uint64) (event.BlockData, error if err != nil { return blockdata, err } + if to == c.treasuryAcc { + blockdata.Punishment = append(blockdata.Punishment, event.Punishment{ + ExtrinsicHash: blockdata.Extrinsics[extrinsicIndex].Hash, + From: from, + To: to, + Amount: amount, + }) + } blockdata.TransferInfo = append(blockdata.TransferInfo, event.TransferInfo{ ExtrinsicName: name, ExtrinsicHash: blockdata.Extrinsics[extrinsicIndex].Hash, @@ -2301,55 +2309,101 @@ func (c *ChainClient) ParseBlockData(blocknumber uint64) (event.BlockData, error } else if e.Name == event.SystemExtrinsicFailed { for m := 0; m < len(blockdata.UploadDecInfo); m++ { if blockdata.UploadDecInfo[m].ExtrinsicHash == blockdata.Extrinsics[extrinsicIndex].Hash { - blockdata.UploadDecInfo = append(blockdata.UploadDecInfo[:extrinsicIndex], blockdata.UploadDecInfo[extrinsicIndex+1:]...) + if len(blockdata.UploadDecInfo) == 1 { + blockdata.UploadDecInfo = nil + } else { + blockdata.UploadDecInfo = append(blockdata.UploadDecInfo[:m], blockdata.UploadDecInfo[m+1:]...) + } break } } for m := 0; m < len(blockdata.DeleteFileInfo); m++ { if blockdata.DeleteFileInfo[m].ExtrinsicHash == blockdata.Extrinsics[extrinsicIndex].Hash { - blockdata.DeleteFileInfo = append(blockdata.DeleteFileInfo[:extrinsicIndex], blockdata.DeleteFileInfo[extrinsicIndex+1:]...) + if len(blockdata.DeleteFileInfo) == 1 { + blockdata.DeleteFileInfo = nil + } else { + blockdata.DeleteFileInfo = append(blockdata.DeleteFileInfo[:m], blockdata.DeleteFileInfo[m+1:]...) + } break } } for m := 0; m < len(blockdata.MinerReg); m++ { if blockdata.MinerReg[m].ExtrinsicHash == blockdata.Extrinsics[extrinsicIndex].Hash { - blockdata.MinerReg = append(blockdata.MinerReg[:extrinsicIndex], blockdata.MinerReg[extrinsicIndex+1:]...) + if len(blockdata.MinerReg) == 1 { + blockdata.MinerReg = nil + } else { + blockdata.MinerReg = append(blockdata.MinerReg[:m], blockdata.MinerReg[m+1:]...) + } break } } for m := 0; m < len(blockdata.CreateBucketInfo); m++ { if blockdata.CreateBucketInfo[m].ExtrinsicHash == blockdata.Extrinsics[extrinsicIndex].Hash { - blockdata.CreateBucketInfo = append(blockdata.CreateBucketInfo[:extrinsicIndex], blockdata.CreateBucketInfo[extrinsicIndex+1:]...) + if len(blockdata.CreateBucketInfo) == 1 { + blockdata.CreateBucketInfo = nil + } else { + blockdata.CreateBucketInfo = append(blockdata.CreateBucketInfo[:m], blockdata.CreateBucketInfo[m+1:]...) + } break } } for m := 0; m < len(blockdata.DeleteBucketInfo); m++ { if blockdata.DeleteBucketInfo[m].ExtrinsicHash == blockdata.Extrinsics[extrinsicIndex].Hash { - blockdata.DeleteBucketInfo = append(blockdata.DeleteBucketInfo[:extrinsicIndex], blockdata.DeleteBucketInfo[extrinsicIndex+1:]...) + if len(blockdata.DeleteBucketInfo) == 1 { + blockdata.DeleteBucketInfo = nil + } else { + blockdata.DeleteBucketInfo = append(blockdata.DeleteBucketInfo[:m], blockdata.DeleteBucketInfo[m+1:]...) + } break } } for m := 0; m < len(blockdata.SubmitIdleProve); m++ { if blockdata.SubmitIdleProve[m].ExtrinsicHash == blockdata.Extrinsics[extrinsicIndex].Hash { - blockdata.SubmitIdleProve = append(blockdata.SubmitIdleProve[:extrinsicIndex], blockdata.SubmitIdleProve[extrinsicIndex+1:]...) + if len(blockdata.SubmitIdleProve) == 1 { + blockdata.SubmitIdleProve = nil + } else { + blockdata.SubmitIdleProve = append(blockdata.SubmitIdleProve[:m], blockdata.SubmitIdleProve[m+1:]...) + } break } } for m := 0; m < len(blockdata.SubmitServiceProve); m++ { if blockdata.SubmitServiceProve[m].ExtrinsicHash == blockdata.Extrinsics[extrinsicIndex].Hash { - blockdata.SubmitServiceProve = append(blockdata.SubmitServiceProve[:extrinsicIndex], blockdata.SubmitServiceProve[extrinsicIndex+1:]...) + if len(blockdata.SubmitServiceProve) == 1 { + blockdata.SubmitServiceProve = nil + } else { + blockdata.SubmitServiceProve = append(blockdata.SubmitServiceProve[:m], blockdata.SubmitServiceProve[m+1:]...) + } break } } for m := 0; m < len(blockdata.SubmitIdleResult); m++ { if blockdata.SubmitIdleResult[m].ExtrinsicHash == blockdata.Extrinsics[extrinsicIndex].Hash { - blockdata.SubmitIdleResult = append(blockdata.SubmitIdleResult[:extrinsicIndex], blockdata.SubmitIdleResult[extrinsicIndex+1:]...) + if len(blockdata.SubmitIdleResult) == 1 { + blockdata.SubmitIdleResult = nil + } else { + blockdata.SubmitIdleResult = append(blockdata.SubmitIdleResult[:m], blockdata.SubmitIdleResult[m+1:]...) + } break } } for m := 0; m < len(blockdata.SubmitServiceResult); m++ { if blockdata.SubmitServiceResult[m].ExtrinsicHash == blockdata.Extrinsics[extrinsicIndex].Hash { - blockdata.SubmitServiceResult = append(blockdata.SubmitServiceResult[:extrinsicIndex], blockdata.SubmitServiceResult[extrinsicIndex+1:]...) + if len(blockdata.SubmitServiceResult) == 1 { + blockdata.SubmitServiceResult = nil + } else { + blockdata.SubmitServiceResult = append(blockdata.SubmitServiceResult[:m], blockdata.SubmitServiceResult[m+1:]...) + } + break + } + } + for m := 0; m < len(blockdata.Punishment); m++ { + if blockdata.Punishment[m].ExtrinsicHash == blockdata.Extrinsics[extrinsicIndex].Hash { + if len(blockdata.Punishment) == 1 { + blockdata.Punishment = nil + } else { + blockdata.Punishment = append(blockdata.Punishment[:m], blockdata.Punishment[m+1:]...) + } break } } diff --git a/chain/sminer.go b/chain/sminer.go index 5391a01..e2dd214 100755 --- a/chain/sminer.go +++ b/chain/sminer.go @@ -430,6 +430,64 @@ func (c *ChainClient) QueryPendingReplacements(puk []byte) (types.U128, error) { return data, nil } +func (c *ChainClient) QueryCompleteSnapShot(era uint32, block int32) (uint32, uint64, error) { + defer func() { + if err := recover(); err != nil { + log.Println(utils.RecoverError(err)) + } + }() + + var data pattern.CompleteSnapShotType + + if !c.GetChainState() { + return 0, 0, pattern.ERR_RPC_CONNECTION + } + + param, err := codec.Encode(era) + if err != nil { + return 0, 0, err + } + + key, err := types.CreateStorageKey(c.metadata, pattern.SMINER, pattern.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) + 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) + c.SetChainState(false) + return 0, 0, err + } + if !ok { + return 0, 0, pattern.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) + 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) + c.SetChainState(false) + return 0, 0, err + } + if !ok { + return 0, 0, pattern.ERR_RPC_EMPTY_VALUE + } + + return uint32(data.MinerCount), data.TotalPower.Uint64(), nil +} + func (c *ChainClient) UpdateSminerPeerId(peerid pattern.PeerId) (string, error) { c.lock.Lock() defer func() { diff --git a/chain/treasury.go b/chain/treasury.go new file mode 100644 index 0000000..1391614 --- /dev/null +++ b/chain/treasury.go @@ -0,0 +1,76 @@ +/* + 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/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" +) + +func (c *ChainClient) QueryRoundReward(era uint32, block int32) (string, error) { + defer func() { + if err := recover(); err != nil { + log.Println(utils.RecoverError(err)) + } + }() + + var data pattern.RoundRewardType + + if !c.GetChainState() { + return "", pattern.ERR_RPC_CONNECTION + } + + param, err := codec.Encode(era) + if err != nil { + return "", err + } + + key, err := types.CreateStorageKey(c.metadata, pattern.CessTreasury, pattern.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) + 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) + c.SetChainState(false) + return "", err + } + if !ok { + return "", pattern.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) + 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) + c.SetChainState(false) + return "", err + } + if !ok { + return "", pattern.ERR_RPC_EMPTY_VALUE + } + + return data.TotalReward.String(), nil +} diff --git a/core/event/events.go b/core/event/events.go index 4260f10..01edcc9 100755 --- a/core/event/events.go +++ b/core/event/events.go @@ -654,6 +654,7 @@ type BlockData struct { SubmitServiceProve []SubmitServiceProve SubmitIdleResult []SubmitIdleResult SubmitServiceResult []SubmitServiceResult + Punishment []Punishment } type ExtrinsicsInfo struct { @@ -724,3 +725,10 @@ type SubmitServiceResult struct { Miner string Result bool } + +type Punishment struct { + ExtrinsicHash string + From string + To string + Amount string +} diff --git a/core/pattern/pattern.go b/core/pattern/pattern.go index 7a7439b..9df86cc 100755 --- a/core/pattern/pattern.go +++ b/core/pattern/pattern.go @@ -52,6 +52,8 @@ const ( BALANCES = "Balances" // SYSTEM is a module about the system SYSTEM = "System" + // + CessTreasury = "CessTreasury" ) // Chain state @@ -75,6 +77,7 @@ const ( EXPENDERS = "Expenders" RESTORALTARGETINFO = "RestoralTarget" STAKINGSTARTBLOCK = "StakingStartBlock" + CompleteSnapShot = "CompleteSnapShot" // TEEWORKER TEEWorkers = "Workers" @@ -117,6 +120,9 @@ const ( // SYSTEM ACCOUNT = "Account" EVENTS = "Events" + + // CessTreasury + RoundReward = "RoundReward" ) // Extrinsics @@ -521,6 +527,16 @@ type StakingValidatorPrefs struct { 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 diff --git a/core/sdk/sdk.go b/core/sdk/sdk.go index 0a6bb26..83effa8 100755 --- a/core/sdk/sdk.go +++ b/core/sdk/sdk.go @@ -25,7 +25,7 @@ type SDK interface { QueryChallengeVerifyExpiration() (uint32, error) // QueryChallengeInfo queries accountID's challenge information // Tip: accountID can only be a storage node account - QueryChallengeInfo(accountID []byte) (bool, pattern.ChallengeInfo, error) + QueryChallengeInfo(accountID []byte, block int32) (bool, pattern.ChallengeInfo, error) // Audit-Extrinsics @@ -155,6 +155,8 @@ type SDK interface { // 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 @@ -278,6 +280,9 @@ type SDK interface { // QueryTotalIssuance(block int) (string, error) + // CessTreasury + QueryRoundReward(era uint32, block int32) (string, error) + // TransferToken to dest. // // Receive parameter: @@ -315,6 +320,8 @@ type SDK interface { 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. diff --git a/example/new/sdk.go b/example/new/sdk.go index e85820d..487e30e 100755 --- a/example/new/sdk.go +++ b/example/new/sdk.go @@ -27,11 +27,11 @@ var MY_MNEMONIC = "bottom drive obey lake curtain smoke basket hold race lonely var RPC_ADDRS = []string{ //devnet - //"wss://devnet-rpc.cess.cloud/ws/", + "wss://devnet-rpc.cess.cloud/ws/", //testnet //"wss://testnet-rpc0.cess.cloud/ws/", - "wss://testnet-rpc1.cess.cloud/ws/", - "wss://testnet-rpc2.cess.cloud/ws/", + //"wss://testnet-rpc1.cess.cloud/ws/", + //"wss://testnet-rpc2.cess.cloud/ws/", } func main() { @@ -50,7 +50,7 @@ func main() { fmt.Println(sdk.InitExtrinsicsName()) fmt.Println(sdk.GetCurrentRpcAddr()) // - fmt.Println(sdk.QueryEraValidatorReward(0)) + fmt.Println(sdk.QueryRoundReward(0, -1)) return blockhash, err := sdk.GetSubstrateAPI().RPC.Chain.GetBlockHash(180)