Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tx confirm #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ output/
# test
coverage.*

.DS_Store
.DS_Store
24 changes: 24 additions & 0 deletions xuper/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type requestOptions struct {

type queryOption struct {
bcname string
blockNums int64
waitTime int64
}

// RequestOption tx opt.
Expand All @@ -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 {
Expand Down Expand Up @@ -134,3 +156,5 @@ func WithOtherAuthRequires(authRequires []string) RequestOption {
return nil
}
}


51 changes: 51 additions & 0 deletions xuper/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"math/big"
"time"

"github.com/xuperchain/xuper-sdk-go/v2/common"
"github.com/xuperchain/xuperchain/service/pb"
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
}
8 changes: 8 additions & 0 deletions xuper/xuperclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}