From ecbecfef3e25b1a9e46479fef18163a7b7e3e2e9 Mon Sep 17 00:00:00 2001 From: uaanaa Date: Tue, 21 Jun 2022 17:00:36 +0800 Subject: [PATCH 1/2] add tx confirm --- .gitignore | 1 + xuper/options.go | 24 +++++++++++++++++++++ xuper/query.go | 51 ++++++++++++++++++++++++++++++++++++++++++++ xuper/xuperclient.go | 8 +++++++ 4 files changed, 84 insertions(+) diff --git a/.gitignore b/.gitignore index 81fe9d5..a28b3a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # IDE related .vscode/ +.idea/ # output files output/ diff --git a/xuper/options.go b/xuper/options.go index fba9599..410300a 100644 --- a/xuper/options.go +++ b/xuper/options.go @@ -27,6 +27,8 @@ type requestOptions struct { type queryOption struct { bcname string + blockNums int64 + waitTime int64 } // RequestOption tx opt. @@ -46,6 +48,26 @@ func WithQueryBcname(bcname string) QueryOption { } } +// WithBlockNums query tx status +func WithBlockNums(blockNums int64) QueryOption { + return func(opts *queryOption) error { + if 0 < blockNums && blockNums < 100 { + opts.blockNums = blockNums + return nil + } + return errors.New("block nums must be between 0 and 100") + } +} + +// WithMaxWaitTime query tx status max wait time +func WithMaxWaitTime(waitTime int64) QueryOption { + return func(opts *queryOption) error { + opts.waitTime = waitTime + return nil + } +} + + // WithConfigFile set xuperclient config file. func WithConfigFile(configFile string) ClientOption { return func(opts *clientOptions) error { @@ -134,3 +156,5 @@ func WithOtherAuthRequires(authRequires []string) RequestOption { return nil } } + + diff --git a/xuper/query.go b/xuper/query.go index 509d917..809caf1 100644 --- a/xuper/query.go +++ b/xuper/query.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "math/big" + "time" "github.com/xuperchain/xuper-sdk-go/v2/common" "github.com/xuperchain/xuperchain/service/pb" @@ -31,6 +32,13 @@ func getBCname(opt *queryOption) string { return chainName } +func getWaitTime(opt *queryOption) time.Duration { + if opt.waitTime == 0 { + opt.waitTime = 1 + } + return time.Second * time.Duration(opt.waitTime) +} + func (x *XClient) queryTxByID(txID string, opts ...QueryOption) (*pb.Transaction, error) { rawTx, err := hex.DecodeString(txID) if err != nil { @@ -400,3 +408,46 @@ func (x *XClient) queryAccountByAK(address string, opts ...QueryOption) ([]strin } return resp.GetAccount(), nil } + + +func (x *XClient) queryTxStatus(txID string, opts ...QueryOption) (bool, error) { + opt, err := initQueryOpts(opts...) + if err != nil { + return false, err + } + waitTime := getWaitTime(opt) + blockNums := opt.blockNums + if blockNums ==0 { + blockNums = 3 + } + // 交易所在区块高度 + tx, err := x.queryTxByID(txID,opts...) + if err != nil { + return false, err + } + blockID := tx.GetBlockid() + + block, err := x.queryBlockByID(fmt.Sprintf("%x", blockID), opts...) + blockHeight := block.GetBlock().Height + ch := make(chan bool) + go x.watchBlockHeight(ch, blockHeight, blockNums, opts...) + select { + case <-time.After(waitTime): + fmt.Println("wait timeout") + return false, nil + case v := <-ch: + return v, nil + } +} + +func (x *XClient) watchBlockHeight(ch chan bool, height int64, blockNums int64, opts ...QueryOption) { + for { + sys, _ := x.querySystemStatus(opts...) + sysHeight := sys.GetSystemsStatus().BcsStatus[0].Block.Height + if sysHeight-height > blockNums { + ch<- true + return + } + time.Sleep(time.Second * 3) + } +} diff --git a/xuper/xuperclient.go b/xuper/xuperclient.go index 6e8fe44..cac8f38 100644 --- a/xuper/xuperclient.go +++ b/xuper/xuperclient.go @@ -667,3 +667,11 @@ func (x *XClient) QueryNetURL(opts ...QueryOption) (string, error) { func (x *XClient) QueryAccountByAK(address string, opts ...QueryOption) ([]string, error) { return x.queryAccountByAK(address, opts...) } + +// QueryTxStatus Check whether the tx is on the chain +// +// Parameters: +// - `txID` : the tx id +func (x *XClient) QueryTxStatus(txID string, opts ...QueryOption) (bool, error) { + return x.queryTxStatus(txID, opts...) +} \ No newline at end of file From 8ee3fcdd5e7d442191faedde1ab900be3c278185 Mon Sep 17 00:00:00 2001 From: uaanaa Date: Tue, 21 Jun 2022 17:03:15 +0800 Subject: [PATCH 2/2] Update .gitignore --- .gitignore | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a28b3a2..7665cae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ # IDE related .vscode/ -.idea/ # output files output/ @@ -10,4 +9,4 @@ output/ # test coverage.* -.DS_Store \ No newline at end of file +.DS_Store