diff --git a/chain/event.go b/chain/event.go index 0d55332..58efe93 100644 --- a/chain/event.go +++ b/chain/event.go @@ -95,7 +95,7 @@ func (c *chainClient) RetrieveEvent_FileBank_ClaimRestoralOrder(blockhash types. } } } - return result, errors.New("failed: no FileBank_ClaimRestoralOrder event found") + return result, errors.Errorf("failed: no %s event found", event.FileBankClaimRestoralOrder) } func (c *chainClient) RetrieveEvent_Audit_SubmitIdleProof(blockhash types.Hash) (event.Event_SubmitIdleProof, error) { @@ -143,7 +143,7 @@ func (c *chainClient) RetrieveEvent_Audit_SubmitIdleProof(blockhash types.Hash) } } } - return result, errors.New("failed: no Audit_SubmitIdleProof event found") + return result, errors.Errorf("failed: no %s event found", event.AuditSubmitIdleProof) } func (c *chainClient) RetrieveEvent_Audit_SubmitServiceProof(blockhash types.Hash) (event.Event_SubmitServiceProof, error) { @@ -191,7 +191,7 @@ func (c *chainClient) RetrieveEvent_Audit_SubmitServiceProof(blockhash types.Has } } } - return result, errors.New("failed: no Audit_SubmitServiceProof event found") + return result, errors.Errorf("failed: no %s event found", event.AuditSubmitServiceProof) } func (c *chainClient) RetrieveEvent_Audit_SubmitIdleVerifyResult(blockhash types.Hash) (event.Event_SubmitIdleVerifyResult, error) { @@ -238,7 +238,7 @@ func (c *chainClient) RetrieveEvent_Audit_SubmitIdleVerifyResult(blockhash types } } } - return result, errors.New("failed: no Audit_SubmitIdleVerifyResult event found") + return result, errors.Errorf("failed: no %s event found", event.AuditSubmitIdleVerifyResult) } func (c *chainClient) RetrieveEvent_Audit_SubmitServiceVerifyResult(blockhash types.Hash) (event.Event_SubmitServiceVerifyResult, error) { @@ -285,7 +285,7 @@ func (c *chainClient) RetrieveEvent_Audit_SubmitServiceVerifyResult(blockhash ty } } } - return result, errors.New("failed: no Audit_SubmitServiceVerifyResult event found") + return result, errors.Errorf("failed: no %s event found", event.AuditSubmitServiceVerifyResult) } func (c *chainClient) RetrieveEvent_Oss_OssUpdate(blockhash types.Hash) (event.Event_OssUpdate, error) { @@ -332,7 +332,7 @@ func (c *chainClient) RetrieveEvent_Oss_OssUpdate(blockhash types.Hash) (event.E } } } - return result, errors.New("failed: no Oss_OssUpdate event found") + return result, errors.Errorf("failed: no %s event found", event.OssOssUpdate) } func (c *chainClient) RetrieveEvent_Oss_OssRegister(blockhash types.Hash) (event.Event_OssRegister, error) { @@ -379,7 +379,7 @@ func (c *chainClient) RetrieveEvent_Oss_OssRegister(blockhash types.Hash) (event } } } - return result, errors.New("failed: no Oss_OssRegister event found") + return result, errors.Errorf("failed: no %s event found", event.OssOssRegister) } func (c *chainClient) RetrieveEvent_Oss_OssDestroy(blockhash types.Hash) (event.Event_OssDestroy, error) { @@ -426,7 +426,7 @@ func (c *chainClient) RetrieveEvent_Oss_OssDestroy(blockhash types.Hash) (event. } } } - return result, errors.New("failed: no Oss_OssDestroy event found") + return result, errors.Errorf("failed: no %s event found", event.OssOssDestroy) } func (c *chainClient) RetrieveEvent_Oss_Authorize(blockhash types.Hash) (event.Event_Authorize, error) { @@ -473,7 +473,7 @@ func (c *chainClient) RetrieveEvent_Oss_Authorize(blockhash types.Hash) (event.E } } } - return result, errors.New("failed: no Oss_Authorize event found") + return result, errors.Errorf("failed: no %s event found", event.OssAuthorize) } func (c *chainClient) RetrieveEvent_Oss_CancelAuthorize(blockhash types.Hash) (event.Event_CancelAuthorize, error) { @@ -520,7 +520,7 @@ func (c *chainClient) RetrieveEvent_Oss_CancelAuthorize(blockhash types.Hash) (e } } } - return result, errors.New("failed: no Oss_CancelAuthorize event found") + return result, errors.Errorf("failed: no %s event found", event.OssCancelAuthorize) } func (c *chainClient) RetrieveEvent_FileBank_UploadDeclaration(blockhash types.Hash) (event.Event_UploadDeclaration, error) { @@ -567,7 +567,7 @@ func (c *chainClient) RetrieveEvent_FileBank_UploadDeclaration(blockhash types.H } } } - return result, errors.New("failed: no FileBank_UploadDeclaration event found") + return result, errors.Errorf("failed: no %s event found", event.FileBankUploadDeclaration) } func (c *chainClient) RetrieveEvent_FileBank_CreateBucket(blockhash types.Hash) (event.Event_CreateBucket, error) { @@ -614,7 +614,7 @@ func (c *chainClient) RetrieveEvent_FileBank_CreateBucket(blockhash types.Hash) } } } - return result, errors.New("failed: no FileBank_CreateBucket event found") + return result, errors.Errorf("failed: no %s event found", event.FileBankCreateBucket) } func (c *chainClient) RetrieveEvent_FileBank_DeleteBucket(blockhash types.Hash) (event.Event_DeleteBucket, error) { @@ -661,7 +661,7 @@ func (c *chainClient) RetrieveEvent_FileBank_DeleteBucket(blockhash types.Hash) } } } - return result, errors.New("failed: no FileBank_DeleteBucket event found") + return result, errors.Errorf("failed: no %s event found", event.FileBankDeleteBucket) } func (c *chainClient) RetrieveEvent_FileBank_DeleteFile(blockhash types.Hash) (event.Event_DeleteFile, error) { @@ -708,7 +708,7 @@ func (c *chainClient) RetrieveEvent_FileBank_DeleteFile(blockhash types.Hash) (e } } } - return result, errors.New("failed: no FileBank_DeleteFile event found") + return result, errors.Errorf("failed: no %s event found", event.FileBankDeleteFile) } func (c *chainClient) RetrieveEvent_FileBank_TransferReport(blockhash types.Hash) (event.Event_TransferReport, error) { @@ -755,7 +755,7 @@ func (c *chainClient) RetrieveEvent_FileBank_TransferReport(blockhash types.Hash } } } - return result, errors.New("failed: no FileBank_TransferReport event found") + return result, errors.Errorf("failed: no %s event found", event.FileBankTransferReport) } func (c *chainClient) RetrieveEvent_FileBank_RecoveryCompleted(blockhash types.Hash) (event.Event_RecoveryCompleted, error) { @@ -802,7 +802,7 @@ func (c *chainClient) RetrieveEvent_FileBank_RecoveryCompleted(blockhash types.H } } } - return result, errors.New("failed: no FileBank_RecoveryCompleted event found") + return result, errors.Errorf("failed: no %s event found", event.FileBankRecoveryCompleted) } func (c *chainClient) RetrieveEvent_FileBank_IdleSpaceCert(blockhash types.Hash) (event.Event_IdleSpaceCert, error) { @@ -849,7 +849,7 @@ func (c *chainClient) RetrieveEvent_FileBank_IdleSpaceCert(blockhash types.Hash) } } } - return result, errors.New("failed: no FileBank_IdleSpaceCert event found") + return result, errors.Errorf("failed: no %s event found", event.FileBankIdleSpaceCert) } func (c *chainClient) RetrieveEvent_FileBank_ReplaceIdleSpace(blockhash types.Hash) (event.Event_ReplaceIdleSpace, error) { @@ -896,7 +896,7 @@ func (c *chainClient) RetrieveEvent_FileBank_ReplaceIdleSpace(blockhash types.Ha } } } - return result, errors.New("failed: no FileBank_ReplaceIdleSpace event found") + return result, errors.Errorf("failed: no %s event found", event.FileBankReplaceIdleSpace) } func (c *chainClient) RetrieveEvent_Sminer_UpdataIp(blockhash types.Hash) (event.Event_UpdataIp, error) { @@ -943,7 +943,7 @@ func (c *chainClient) RetrieveEvent_Sminer_UpdataIp(blockhash types.Hash) (event } } } - return result, errors.New("failed: no Sminer_UpdataIp event found") + return result, errors.Errorf("failed: no %s event found", event.SminerUpdatePeerId) } func (c *chainClient) RetrieveEvent_Sminer_UpdataBeneficiary(blockhash types.Hash) (event.Event_UpdataBeneficiary, error) { @@ -990,7 +990,7 @@ func (c *chainClient) RetrieveEvent_Sminer_UpdataBeneficiary(blockhash types.Has } } } - return result, errors.New("failed: no Sminer_UpdataBeneficiary event found") + return result, errors.Errorf("failed: no %s event found", event.SminerUpdateBeneficiary) } func (c *chainClient) RetrieveEvent_Sminer_Registered(blockhash types.Hash) (event.Event_Registered, error) { @@ -1037,7 +1037,54 @@ func (c *chainClient) RetrieveEvent_Sminer_Registered(blockhash types.Hash) (eve } } } - return result, errors.New("failed: no Sminer_Registered event found") + return result, errors.Errorf("failed: no %s event found", event.SminerRegistered) +} + +func (c *chainClient) RetrieveEvent_Sminer_RegisterPoisKey(blockhash types.Hash) (event.Event_RegisterPoisKey, error) { + var result event.Event_RegisterPoisKey + events, err := c.eventRetriever.GetEvents(blockhash) + if err != nil { + return result, err + } + for _, e := range events { + if e.Name == event.SminerRegisterPoisKey { + for _, v := range e.Fields { + if reflect.TypeOf(v.Value).Kind() == reflect.Slice { + vf := reflect.ValueOf(v.Value) + if vf.Len() > 0 { + allValue := fmt.Sprintf("%v", vf.Index(0)) + if strings.Contains(v.Name, "AccountId32") { + temp := strings.Split(allValue, "] ") + puk := make([]byte, types.AccountIDLen) + for _, v := range temp { + if strings.Count(v, " ") == (types.AccountIDLen - 1) { + subValue := strings.TrimPrefix(v, "[") + ids := strings.Split(subValue, " ") + if len(ids) != types.AccountIDLen { + continue + } + for kk, vv := range ids { + intv, _ := strconv.Atoi(vv) + puk[kk] = byte(intv) + } + } + } + if !utils.CompareSlice(puk, c.GetSignatureAccPulickey()) { + continue + } + accid, err := types.NewAccountID(puk) + if err != nil { + continue + } + result.Miner = *accid + return result, nil + } + } + } + } + } + } + return result, errors.Errorf("failed: no %s event found", event.SminerRegisterPoisKey) } func (c *chainClient) RetrieveEvent_Sminer_MinerExitPrep(blockhash types.Hash) (event.Event_MinerExitPrep, error) { @@ -1084,7 +1131,7 @@ func (c *chainClient) RetrieveEvent_Sminer_MinerExitPrep(blockhash types.Hash) ( } } } - return result, errors.New("failed: no Sminer_MinerExitPrep event found") + return result, errors.Errorf("failed: no %s event found", event.SminerMinerExitPrep) } func (c *chainClient) RetrieveEvent_Sminer_IncreaseCollateral(blockhash types.Hash) (event.Event_IncreaseCollateral, error) { @@ -1131,7 +1178,7 @@ func (c *chainClient) RetrieveEvent_Sminer_IncreaseCollateral(blockhash types.Ha } } } - return result, errors.New("failed: no Sminer_IncreaseCollateral event found") + return result, errors.Errorf("failed: no %s event found", event.SminerIncreaseCollateral) } func (c *chainClient) RetrieveEvent_Sminer_Receive(blockhash types.Hash) (event.Event_Receive, error) { @@ -1178,7 +1225,7 @@ func (c *chainClient) RetrieveEvent_Sminer_Receive(blockhash types.Hash) (event. } } } - return result, errors.New("failed: no Sminer_Receive event found") + return result, errors.Errorf("failed: no %s event found", event.SminerReceive) } func (c *chainClient) RetrieveEvent_Sminer_Withdraw(blockhash types.Hash) (event.Event_Withdraw, error) { @@ -1225,7 +1272,7 @@ func (c *chainClient) RetrieveEvent_Sminer_Withdraw(blockhash types.Hash) (event } } } - return result, errors.New("failed: no Sminer_Withdraw event found") + return result, errors.Errorf("failed: no %s event found", event.SminerWithdraw) } func (c *chainClient) RetrieveEvent_StorageHandler_BuySpace(blockhash types.Hash) (event.Event_BuySpace, error) { @@ -1272,7 +1319,7 @@ func (c *chainClient) RetrieveEvent_StorageHandler_BuySpace(blockhash types.Hash } } } - return result, errors.New("failed: no StorageHandler_BuySpace event found") + return result, errors.Errorf("failed: no %s event found", event.StorageHandlerBuySpace) } func (c *chainClient) RetrieveEvent_StorageHandler_ExpansionSpace(blockhash types.Hash) (event.Event_ExpansionSpace, error) { @@ -1319,7 +1366,7 @@ func (c *chainClient) RetrieveEvent_StorageHandler_ExpansionSpace(blockhash type } } } - return result, errors.New("failed: no StorageHandler_ExpansionSpace event found") + return result, errors.Errorf("failed: no %s event found", event.StorageHandlerExpansionSpace) } func (c *chainClient) RetrieveEvent_StorageHandler_RenewalSpace(blockhash types.Hash) (event.Event_RenewalSpace, error) { @@ -1366,7 +1413,7 @@ func (c *chainClient) RetrieveEvent_StorageHandler_RenewalSpace(blockhash types. } } } - return result, errors.New("failed: no StorageHandler_RenewalSpace event found") + return result, errors.Errorf("failed: no %s event found", event.StorageHandlerRenewalSpace) } func (c *chainClient) RetrieveEvent_Balances_Transfer(blockhash types.Hash) (types.EventBalancesTransfer, error) { @@ -1376,7 +1423,7 @@ func (c *chainClient) RetrieveEvent_Balances_Transfer(blockhash types.Hash) (typ return result, err } for _, e := range events { - if e.Name == "Balances.Transfer" { + if e.Name == event.BalanceTransfer { for _, v := range e.Fields { if reflect.TypeOf(v.Value).Kind() == reflect.Slice { vf := reflect.ValueOf(v.Value) @@ -1413,7 +1460,7 @@ func (c *chainClient) RetrieveEvent_Balances_Transfer(blockhash types.Hash) (typ } } } - return result, errors.New("failed: no Balances_Transfer event found") + return result, errors.Errorf("failed: no %s event found", event.BalanceTransfer) } func (c *chainClient) RetrieveEvent_FileBank_GenRestoralOrder(blockhash types.Hash) (event.Event_GenerateRestoralOrder, error) { @@ -1460,7 +1507,7 @@ func (c *chainClient) RetrieveEvent_FileBank_GenRestoralOrder(blockhash types.Ha } } } - return result, errors.New("failed: no FilaBank_GenerateRestoralOrder event found") + return result, errors.Errorf("failed: no %s event found", event.FileBankGenerateRestoralOrder) } func (c *chainClient) RetrieveAllEvent_FileBank_UploadDeclaration(blockhash types.Hash) ([]event.AllUploadDeclarationEvent, error) { diff --git a/chain/sminer.go b/chain/sminer.go index cb55e6c..345045b 100644 --- a/chain/sminer.go +++ b/chain/sminer.go @@ -122,237 +122,6 @@ func (c *chainClient) QuaryStorageNodeRewardInfo(puk []byte) (pattern.RewardsTyp return reward, nil } -func (c *chainClient) RegisterOrUpdateSminer(peerId []byte, earnings string, pledge uint64) (string, string, error) { - c.lock.Lock() - defer func() { - c.lock.Unlock() - if err := recover(); err != nil { - log.Println(utils.RecoverError(err)) - } - }() - - var ( - err error - txhash string - pubkey []byte - minerinfo pattern.MinerInfo - acc *types.AccountID - call types.Call - accountInfo types.AccountInfo - ) - - if !c.GetChainState() { - return txhash, earnings, pattern.ERR_RPC_CONNECTION - } - - c.SetSdkName(pattern.Name_Sminer) - - var peerid pattern.PeerId - if len(peerid) != len(peerId) { - return txhash, earnings, fmt.Errorf("invalid peerid: %v", peerId) - } - - for i := 0; i < len(peerid); i++ { - peerid[i] = types.U8(peerId[i]) - } - - key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) - if err != nil { - return txhash, earnings, errors.Wrap(err, "[CreateStorageKey]") - } - - minerinfo, err = c.QueryStorageMiner(c.keyring.PublicKey) - if err != nil { - if err.Error() != pattern.ERR_Empty { - return txhash, earnings, err - } - } else { - if !utils.CompareSlice([]byte(string(minerinfo.PeerId[:])), peerId) { - txhash, err = c.updateSminerPeerId(key, peerid) - if err != nil { - return txhash, earnings, err - } - } - acc, _ := utils.EncodePublicKeyAsCessAccount(minerinfo.BeneficiaryAcc[:]) - if earnings != "" { - if acc != earnings { - puk, err := utils.ParsingPublickey(earnings) - if err != nil { - return txhash, acc, err - } - txhash, err = c.updateEarningsAcc(key, puk) - return txhash, earnings, err - } - } - return "", acc, nil - } - - pubkey, err = utils.ParsingPublickey(earnings) - if err != nil { - return txhash, earnings, errors.Wrap(err, "[DecodeToPub]") - } - acc, err = types.NewAccountID(pubkey) - if err != nil { - return txhash, earnings, errors.Wrap(err, "[NewAccountID]") - } - realTokens, ok := new(big.Int).SetString(strconv.FormatUint(pledge, 10)+pattern.TokenPrecision_CESS, 10) - if !ok { - return txhash, earnings, errors.New("[big.Int.SetString]") - } - call, err = types.NewCall(c.metadata, pattern.TX_SMINER_REGISTER, *acc, peerid, types.NewU128(*realTokens)) - if err != nil { - return txhash, earnings, errors.Wrap(err, "[NewCall]") - } - - ok, err = c.api.RPC.State.GetStorageLatest(key, &accountInfo) - if err != nil { - return txhash, earnings, errors.Wrap(err, "[GetStorageLatest]") - } - - if !ok { - keyStr, _ := utils.NumsToByteStr(key, map[string]bool{}) - return txhash, earnings, fmt.Errorf( - "chain rpc.state.GetStorageLatest[%v]: %v", - keyStr, - 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, earnings, errors.Wrap(err, "[Sign]") - } - - // 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) { - o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) - err = ext.Sign(c.keyring, o) - if err != nil { - return txhash, earnings, errors.Wrap(err, "[Sign]") - } - sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) - if err != nil { - c.SetChainState(false) - return txhash, earnings, errors.Wrap(err, "[SubmitAndWatchExtrinsic]") - } - } else { - c.SetChainState(false) - return txhash, earnings, 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_Sminer_Registered(status.AsInBlock) - return txhash, earnings, err - } - case err = <-sub.Err(): - return txhash, earnings, errors.Wrap(err, "[sub]") - case <-timeout.C: - return txhash, earnings, pattern.ERR_RPC_TIMEOUT - } - } -} - -func (c *chainClient) updateSminerPeerId(key types.StorageKey, peerid pattern.PeerId) (string, error) { - var ( - err error - txhash string - call types.Call - accountInfo types.AccountInfo - ) - - call, err = types.NewCall(c.metadata, pattern.TX_SMINER_UPDATEPEERID, peerid) - if err != nil { - return txhash, errors.Wrap(err, "[NewCall]") - } - - 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 { - if strings.Contains(err.Error(), pattern.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]") - } - sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) - if err != nil { - c.SetChainState(false) - return txhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]") - } - } else { - 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_Sminer_UpdataIp(status.AsInBlock) - return txhash, err - } - case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") - case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT - } - } -} - func (c *chainClient) UpdateSminerPeerId(peerid pattern.PeerId) (string, error) { c.lock.Lock() defer func() { @@ -641,88 +410,6 @@ func (c *chainClient) UpdateEarningsAcc(puk []byte) (string, error) { } } -func (c *chainClient) updateEarningsAcc(key types.StorageKey, puk []byte) (string, error) { - var ( - txhash string - accountInfo types.AccountInfo - ) - - acc, err := types.NewAccountID(puk) - if err != nil { - return txhash, errors.Wrap(err, "[NewAccountID]") - } - - call, err := types.NewCall(c.metadata, pattern.TX_SMINER_UPDATEINCOME, *acc) - if err != nil { - return txhash, errors.Wrap(err, "[NewCall]") - } - - 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 { - if strings.Contains(err.Error(), pattern.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]") - } - sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) - if err != nil { - c.SetChainState(false) - return txhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]") - } - } else { - 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_Sminer_UpdataBeneficiary(status.AsInBlock) - return txhash, err - } - case err = <-sub.Err(): - return txhash, errors.Wrap(err, "[sub]") - case <-timeout.C: - return txhash, pattern.ERR_RPC_TIMEOUT - } - } -} - func (c *chainClient) UpdateEarningsAccount(earnings string) (string, error) { puk, err := utils.ParsingPublickey(earnings) if err != nil { @@ -1052,7 +739,7 @@ func (s *chainClient) Expenders() (pattern.ExpendersInfo, error) { return data, nil } -func (c *chainClient) RegisterOrUpdateSminer_V2(peerId []byte, earnings string, pledge uint64, poisKey pattern.PoISKeyInfo, sign pattern.TeeSignature) (string, string, error) { +func (c *chainClient) RegisterSminer(peerId []byte, earnings string, pledge uint64) (string, error) { c.lock.Lock() defer func() { c.lock.Unlock() @@ -1065,21 +752,20 @@ func (c *chainClient) RegisterOrUpdateSminer_V2(peerId []byte, earnings string, err error txhash string pubkey []byte - minerinfo pattern.MinerInfo acc *types.AccountID call types.Call accountInfo types.AccountInfo ) if !c.GetChainState() { - return txhash, earnings, pattern.ERR_RPC_CONNECTION + return txhash, pattern.ERR_RPC_CONNECTION } c.SetSdkName(pattern.Name_Sminer) var peerid pattern.PeerId if len(peerid) != len(peerId) { - return txhash, earnings, fmt.Errorf("invalid peerid: %v", peerId) + return txhash, fmt.Errorf("invalid peerid: %v", peerId) } for i := 0; i < len(peerid); i++ { @@ -1088,60 +774,34 @@ func (c *chainClient) RegisterOrUpdateSminer_V2(peerId []byte, earnings string, key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) if err != nil { - return txhash, earnings, errors.Wrap(err, "[CreateStorageKey]") - } - - minerinfo, err = c.QueryStorageMiner(c.keyring.PublicKey) - if err != nil { - if err.Error() != pattern.ERR_Empty { - return txhash, earnings, err - } - } else { - if !utils.CompareSlice([]byte(string(minerinfo.PeerId[:])), peerId) { - txhash, err = c.updateSminerPeerId(key, peerid) - if err != nil { - return txhash, earnings, err - } - } - acc, _ := utils.EncodePublicKeyAsCessAccount(minerinfo.BeneficiaryAcc[:]) - if earnings != "" { - if acc != earnings { - puk, err := utils.ParsingPublickey(earnings) - if err != nil { - return txhash, acc, err - } - txhash, err = c.updateEarningsAcc(key, puk) - return txhash, earnings, err - } - } - return "", acc, nil + return txhash, errors.Wrap(err, "[CreateStorageKey]") } pubkey, err = utils.ParsingPublickey(earnings) if err != nil { - return txhash, earnings, errors.Wrap(err, "[DecodeToPub]") + return txhash, errors.Wrap(err, "[DecodeToPub]") } acc, err = types.NewAccountID(pubkey) if err != nil { - return txhash, earnings, errors.Wrap(err, "[NewAccountID]") + return txhash, errors.Wrap(err, "[NewAccountID]") } realTokens, ok := new(big.Int).SetString(strconv.FormatUint(pledge, 10)+pattern.TokenPrecision_CESS, 10) if !ok { - return txhash, earnings, errors.New("[big.Int.SetString]") + return txhash, errors.New("[big.Int.SetString]") } - call, err = types.NewCall(c.metadata, pattern.TX_SMINER_REGISTER, *acc, peerid, types.NewU128(*realTokens), poisKey, sign) + call, err = types.NewCall(c.metadata, pattern.TX_SMINER_REGISTER, *acc, peerid, types.NewU128(*realTokens)) if err != nil { - return txhash, earnings, errors.Wrap(err, "[NewCall]") + return txhash, errors.Wrap(err, "[NewCall]") } ok, err = c.api.RPC.State.GetStorageLatest(key, &accountInfo) if err != nil { - return txhash, earnings, errors.Wrap(err, "[GetStorageLatest]") + return txhash, errors.Wrap(err, "[GetStorageLatest]") } if !ok { keyStr, _ := utils.NumsToByteStr(key, map[string]bool{}) - return txhash, earnings, fmt.Errorf( + return txhash, fmt.Errorf( "chain rpc.state.GetStorageLatest[%v]: %v", keyStr, pattern.ERR_RPC_EMPTY_VALUE, @@ -1163,7 +823,7 @@ func (c *chainClient) RegisterOrUpdateSminer_V2(peerId []byte, earnings string, // Sign the transaction err = ext.Sign(c.keyring, o) if err != nil { - return txhash, earnings, errors.Wrap(err, "[Sign]") + return txhash, errors.Wrap(err, "[Sign]") } // Do the transfer and track the actual status @@ -1173,16 +833,16 @@ func (c *chainClient) RegisterOrUpdateSminer_V2(peerId []byte, earnings string, o.Nonce = types.NewUCompactFromUInt(uint64(accountInfo.Nonce + 1)) err = ext.Sign(c.keyring, o) if err != nil { - return txhash, earnings, errors.Wrap(err, "[Sign]") + return txhash, errors.Wrap(err, "[Sign]") } sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) if err != nil { c.SetChainState(false) - return txhash, earnings, errors.Wrap(err, "[SubmitAndWatchExtrinsic]") + return txhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]") } } else { c.SetChainState(false) - return txhash, earnings, errors.Wrap(err, "[SubmitAndWatchExtrinsic]") + return txhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]") } } defer sub.Unsubscribe() @@ -1196,12 +856,116 @@ func (c *chainClient) RegisterOrUpdateSminer_V2(peerId []byte, earnings string, if status.IsInBlock { txhash = status.AsInBlock.Hex() _, err = c.RetrieveEvent_Sminer_Registered(status.AsInBlock) - return txhash, earnings, err + return txhash, err + } + case err = <-sub.Err(): + return txhash, errors.Wrap(err, "[sub]") + case <-timeout.C: + return txhash, pattern.ERR_RPC_TIMEOUT + } + } +} + +func (c *chainClient) RegisterSminerPOISKey(poisKey pattern.PoISKeyInfo, sign pattern.TeeSignature) (string, error) { + c.lock.Lock() + defer func() { + c.lock.Unlock() + if err := recover(); err != nil { + log.Println(utils.RecoverError(err)) + } + }() + + var ( + err error + txhash string + call types.Call + accountInfo types.AccountInfo + ) + + if !c.GetChainState() { + return txhash, pattern.ERR_RPC_CONNECTION + } + + c.SetSdkName(pattern.Name_Sminer) + + key, err := types.CreateStorageKey(c.metadata, pattern.SYSTEM, pattern.ACCOUNT, c.keyring.PublicKey) + if err != nil { + return txhash, errors.Wrap(err, "[CreateStorageKey]") + } + + call, err = types.NewCall(c.metadata, pattern.TX_SMINER_REGISTERPOISKEY, poisKey, sign) + if err != nil { + return txhash, errors.Wrap(err, "[NewCall]") + } + + ok, err := c.api.RPC.State.GetStorageLatest(key, &accountInfo) + if err != nil { + return txhash, errors.Wrap(err, "[GetStorageLatest]") + } + + 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, + ) + } + + 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 { + if strings.Contains(err.Error(), pattern.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]") + } + sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext) + if err != nil { + c.SetChainState(false) + return txhash, errors.Wrap(err, "[SubmitAndWatchExtrinsic]") + } + } else { + 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_Sminer_RegisterPoisKey(status.AsInBlock) + return txhash, err } case err = <-sub.Err(): - return txhash, earnings, errors.Wrap(err, "[sub]") + return txhash, errors.Wrap(err, "[sub]") case <-timeout.C: - return txhash, earnings, pattern.ERR_RPC_TIMEOUT + return txhash, pattern.ERR_RPC_TIMEOUT } } } diff --git a/core/event/events.go b/core/event/events.go index fdf36a3..7577c52 100644 --- a/core/event/events.go +++ b/core/event/events.go @@ -71,6 +71,12 @@ type Event_Registered struct { Topics []types.Hash } +type Event_RegisterPoisKey struct { + Phase types.Phase + Miner types.AccountID + Topics []types.Hash +} + type Event_DrawFaucetMoney struct { Phase types.Phase Topics []types.Hash @@ -431,6 +437,7 @@ type EventRecords struct { // SMINER Sminer_Registered []Event_Registered + Sminer_RegisterPoisKey []Event_RegisterPoisKey Sminer_DrawFaucetMoney []Event_DrawFaucetMoney Sminer_FaucetTopUpMoney []Event_FaucetTopUpMoney Sminer_LessThan24Hours []Event_LessThan24Hours diff --git a/core/event/eventsName.go b/core/event/eventsName.go index 205e24c..f2a0c80 100644 --- a/core/event/eventsName.go +++ b/core/event/eventsName.go @@ -17,6 +17,9 @@ const ( AuditSubmitIdleVerifyResult = "Audit.SubmitIdleVerifyResult" AuditSubmitServiceVerifyResult = "Audit.SubmitServiceVerifyResult" + // BALANCE + BalanceTransfer = "Balances.Transfer" + // FILEBANK FileBankDeleteFile = "FileBank.DeleteFile" FileBankFillerDelete = "FileBank.FillerDelete" @@ -42,6 +45,7 @@ const ( // SMINER SminerRegistered = "Sminer.Registered" + SminerRegisterPoisKey = "Sminer.RegisterPoisKey" SminerDrawFaucetMoney = "Sminer.DrawFaucetMoney" SminerFaucetTopUpMoney = "Sminer.FaucetTopUpMoney" SminerIncreaseCollateral = "Sminer.IncreaseCollateral" diff --git a/core/pattern/pattern.go b/core/pattern/pattern.go index 64d07df..b068080 100644 --- a/core/pattern/pattern.go +++ b/core/pattern/pattern.go @@ -116,13 +116,14 @@ const ( TX_OSS_UNAUTHORIZE = OSS + DOT + "cancel_authorize" // SMINER - TX_SMINER_REGISTER = SMINER + DOT + "regnstk" - 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_REGISTER = SMINER + DOT + "regnstk" + 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" // FILEBANK TX_FILEBANK_PUTBUCKET = FILEBANK + DOT + "create_bucket" @@ -267,7 +268,7 @@ type MinerInfo struct { IdleSpace types.U128 ServiceSpace types.U128 LockSpace types.U128 - SpaceProofInfo SpaceProofInfo + SpaceProofInfo types.Option[SpaceProofInfo] ServiceBloomFilter BloomFilter TeeSignature TeeSignature } @@ -462,8 +463,9 @@ type ExpendersInfo struct { } type PoISKeyInfo struct { - G PoISKey_G - N PoISKey_N + Acc types.AccountID + G PoISKey_G + N PoISKey_N } type IdleSignInfo struct { diff --git a/core/sdk/sdk.go b/core/sdk/sdk.go index 264b6e8..88fba2c 100644 --- a/core/sdk/sdk.go +++ b/core/sdk/sdk.go @@ -148,10 +148,10 @@ type SDK interface { // Sminer-Extrinsics - // RegisterOrUpdateSminer register or update sminer information - RegisterOrUpdateSminer(peerId []byte, earnings string, pledge uint64) (string, string, error) - // - RegisterOrUpdateSminer_V2(peerId []byte, earnings string, pledge uint64, poisKey pattern.PoISKeyInfo, sign pattern.TeeSignature) (string, string, error) + // RegisterSminer register sminer information + RegisterSminer(peerId []byte, earnings string, pledge uint64) (string, error) + // RegisterSminerPOISKey register the pois key of sminer + RegisterSminerPOISKey(poisKey pattern.PoISKeyInfo, sign pattern.TeeSignature) (string, error) // ExitSminer exit mining ExitSminer() (string, error) // UpdateEarningsAcc update earnings account. @@ -393,6 +393,8 @@ type SDK interface { // 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_UpdataIp, error) // RetrieveEvent_Sminer_UpdataBeneficiary(blockhash types.Hash) (event.Event_UpdataBeneficiary, error) diff --git a/example_test.go b/example_test.go index 716d72d..a7299bd 100644 --- a/example_test.go +++ b/example_test.go @@ -96,7 +96,7 @@ func Example_register_storage_node() { log.Fatalf("err: %v", err.Error()) } - _, _, err = cli.RegisterOrUpdateSminer(p2p.GetPeerPublickey(), os.Getenv("MY_ADDR"), 0) + _, err = cli.RegisterSminer(p2p.GetPeerPublickey(), os.Getenv("MY_ADDR"), 0) if err != nil { log.Fatalf("err: %v", err.Error()) }